+// Stores journal/bank transaction tax details if applicable
+//
+function add_gl_tax_details($gl_code, $trans_type, $trans_no, $amount, $ex_rate, $date, $memo)
+{
+ $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'])
+ $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'], 0,
+ $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);
+
+ db_query($sql, "The transaction tax details could not be voided");
+}
+
+function get_tax_summary($from, $to)
+{
+ $fromdate = date2sql($from);
+ $todate = date2sql($to);
+
+ $sql = "SELECT
+ SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE
+ ." || trans_type=".ST_JOURNAL.",-1,1)*
+ IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE
+ ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
+ ." || trans_type=".ST_CUSTCREDIT.", net_amount*ex_rate,0)) net_output,
+
+ SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE
+ ." || trans_type=".ST_JOURNAL.",-1,1)*
+ IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE
+ ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
+ ." || trans_type=".ST_CUSTCREDIT.", amount*ex_rate,0)) payable,
+
+ SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE.",-1,1)*
+ IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE
+ ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
+ ." || trans_type=".ST_CUSTCREDIT.", 0, net_amount*ex_rate)) net_input,
+
+ SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE.",-1,1)*
+ IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE
+ ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
+ ." || trans_type=".ST_CUSTCREDIT.", 0, amount*ex_rate)) collectible,
+ taxrec.rate,
+ ttype.id,
+ ttype.name
+ FROM ".TB_PREF."tax_types ttype,
+ ".TB_PREF."trans_tax_details taxrec
+ WHERE taxrec.tax_type_id=ttype.id
+ AND taxrec.trans_type != ".ST_CUSTDELIVERY."
+ AND taxrec.tran_date >= '$fromdate'
+ AND taxrec.tran_date <= '$todate'
+ GROUP BY ttype.id";
+//display_error($sql);
+ return db_query($sql,"Cannot retrieve tax summary");
+}