+// Stores journal/bank transaction tax details if applicable
+//
+function add_gl_tax_details($gl_code, $trans_type, $trans_no, $amount, $ex_rate, $date, $memo, $included=0)
+{
+ $tax_type = is_tax_account($gl_code);
+ if(!$tax_type) return; // $gl_code is not tax account
+
+ $tax = get_tax_type($tax_type);
+ //if ($gl_code == $tax['sales_gl_code'])
+ if ($trans_type == ST_SALESINVOICE || $trans_type == ST_CUSTDELIVERY || $trans_type == ST_CUSTCREDIT)
+ $amount = -$amount;
+ // we have to restore net amount as we cannot know the base amount
+ if ($tax['rate'] == 0) {
+// display_warning(_("You should not post gl transactions
+// to tax account with zero tax rate."));
+ $net_amount = 0;
+ } else {
+ // calculate net amount
+ $net_amount = $amount/$tax['rate']*100;
+ }
+
+ add_trans_tax_details($trans_type, $trans_no, $tax['id'], $tax['rate'], $included,
+ $amount, $net_amount, $ex_rate, $date, $memo);
+
+}
+
+//--------------------------------------------------------------------------------
+//
+// Store transaction tax details for fiscal purposes with 'freezed'
+// actual tax type rate.
+//
+function add_trans_tax_details($trans_type, $trans_no, $tax_id, $rate, $included,
+ $amount, $net_amount, $ex_rate, $tran_date, $memo)
+{
+
+ $sql = "INSERT INTO ".TB_PREF."trans_tax_details
+ (trans_type, trans_no, tran_date, tax_type_id, rate, ex_rate,
+ included_in_price, net_amount, amount, memo)
+ VALUES (".db_escape($trans_type)."," . db_escape($trans_no).",'"
+ .date2sql($tran_date)."',".db_escape($tax_id).","
+ .db_escape($rate).",".db_escape($ex_rate).",".($included ? 1:0).","
+ .db_escape($net_amount).","
+ .db_escape($amount).",".db_escape($memo).")";
+
+ db_query($sql, "Cannot save trans tax details");
+
+}
+//----------------------------------------------------------------------------------------
+
+function get_trans_tax_details($trans_type, $trans_no)
+{
+ $sql = "SELECT ".TB_PREF."trans_tax_details.*, "
+ .TB_PREF."tax_types.name AS tax_type_name
+ FROM ".TB_PREF."trans_tax_details,".TB_PREF."tax_types
+ WHERE trans_type = ".db_escape($trans_type)."
+ AND trans_no = ".db_escape($trans_no)."
+ AND (net_amount != 0 OR amount != 0)
+ AND ".TB_PREF."tax_types.id = ".TB_PREF."trans_tax_details.tax_type_id";
+
+ return db_query($sql, "The transaction tax details could not be retrieved");
+}
+
+//----------------------------------------------------------------------------------------
+
+function void_trans_tax_details($type, $type_no)
+{
+ $sql = "UPDATE ".TB_PREF."trans_tax_details SET amount=0, net_amount=0
+ WHERE trans_no=".db_escape($type_no)
+ ." AND trans_type=".db_escape($type);