Additonal fixes to tax on foreign currency transactions.
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Thu, 29 Jan 2009 20:32:04 +0000 (20:32 +0000)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Thu, 29 Jan 2009 20:32:04 +0000 (20:32 +0000)
gl/includes/db/gl_db_banking.inc
gl/includes/db/gl_db_trans.inc
gl/inquiry/tax_inquiry.php
purchasing/includes/db/invoice_db.inc
reporting/rep709.php
sql/alter2.1.php
sql/alter2.1.sql

index 7e465091fe1f81d9ea5c52ee04d7f776c57589cf..57412a23e129e7db65fd5fd973315b9f5ea42a9b 100644 (file)
@@ -147,8 +147,10 @@ function add_bank_transaction($trans_type, $from_account, $items, $date_,
                // store tax details if the gl account is a tax account
 
                $amount = $gl_item->amount;
+               $ex_rate = get_exchange_rate_from_home_currency($currency, $date_);
+                       
                add_gl_tax_details($gl_item->code_id, $trans_type, $trans_no, $amount,
-                       $date_, $memo);
+                       $ex_rate, $date_, $memo_);
        }
 
        // do the source account postings
index ae7ac39791b29308fed838d586d5b1519447c9e1..d8e6d58a269b8fa7e6858aa282a1936ab321b7ad 100644 (file)
@@ -211,7 +211,7 @@ function get_budget_trans_from_to($from_date, $to_date, $account, $dimension=0,
 //--------------------------------------------------------------------------------
 //     Stores journal/bank transaction tax details if applicable
 //
-function add_gl_tax_details($gl_code, $trans_type, $trans_no, $amount, $date, $memo)
+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
@@ -228,8 +228,9 @@ function add_gl_tax_details($gl_code, $trans_type, $trans_no, $amount, $date, $m
                // 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, $date, $memo);
+               $amount, $net_amount, $ex_rate, $date, $memo);
                        
 }
 
@@ -239,14 +240,16 @@ function add_gl_tax_details($gl_code, $trans_type, $trans_no, $amount, $date, $m
 //     actual tax type rate.
 //
 function add_trans_tax_details($trans_type, $trans_no, $tax_id, $rate, $included,
-       $amount, $net_amount, $tran_date, $memo)
+       $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,
+               (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).","
-                               .$rate.",".($included ? 1:0).",".db_escape($net_amount).","
+                               .$rate.",".$ex_rate.",".($included ? 1:0).","
+                               .db_escape($net_amount).","
                                .db_escape($amount).",".db_escape($memo).")";
 
        db_query($sql, "Cannot save trans tax details");
@@ -260,7 +263,7 @@ function get_trans_tax_details($trans_type, $trans_no)
                FROM ".TB_PREF."trans_tax_details,".TB_PREF."tax_types
                WHERE trans_type = $trans_type
                AND trans_no = $trans_no
-               AND amount != 0
+               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");
@@ -277,6 +280,33 @@ function void_trans_tax_details($type, $type_no)
        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=1 || trans_type=11 || trans_type=20,-1,1)*
+                               IF(trans_type=0 || trans_type=2 || trans_type=10 || trans_type=11, net_amount*ex_rate,0)) net_input,
+                               SUM(IF( trans_type=1 || trans_type=11 || trans_type=20,-1,1)*
+                               IF(trans_type=0 || trans_type=2 || trans_type=10 || trans_type=11, amount*ex_rate,0)) payable,
+                               SUM(IF( trans_type=1 || trans_type=11 || trans_type=20,-1,1)*
+                               IF(trans_type=0 || trans_type=2 || trans_type=10 || trans_type=11, 0, net_amount*ex_rate)) net_output,
+                               SUM(IF( trans_type=1 || trans_type=11 || trans_type=20,-1,1)*
+                               IF(trans_type=0 || trans_type=2 || trans_type=10 || trans_type=11, 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 != 13
+                       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");
+}
 //--------------------------------------------------------------------------------
 function add_journal_entries($items, $date_, $ref, $reverse, $memo_=null)
 {
@@ -302,7 +332,7 @@ function add_journal_entries($items, $date_, $ref, $reverse, $memo_=null)
        }
                // store tax details if the gl account is a tax account
                add_gl_tax_details($journal_item->code_id, 
-                       $trans_type, $trans_id, -$journal_item->amount, $date_, $memo_);
+                       $trans_type, $trans_id, -$journal_item->amount, 1, $date_, $memo_);
        }
        
        add_comments($trans_type, $trans_id, $date_, $memo_);
@@ -334,7 +364,7 @@ function add_journal_entries($items, $date_, $ref, $reverse, $memo_=null)
                }
                        // store tax details if the gl account is a tax account
                        add_gl_tax_details($journal_item->code_id, 
-                               $trans_type, $trans_id, $journal_item->amount, $date, $memo_);
+                               $trans_type, $trans_id, $journal_item->amount, 1, $date, $memo_);
        }
 
        add_comments($trans_type, $trans_id_reverse, $reversingDate, $memo_);
index 0dc7d8f224bb81b837af3d5c897157501e603397..b7ca2de3f8b99602232294d9c99174c0efe26174 100644 (file)
@@ -83,8 +83,6 @@ function show_results()
 {
        global $path_to_root, $table_style;
 
-       $taxes = get_tax_types();
-
     /*Now get the transactions  */
        div_start('trans_tbl');
        start_table($table_style);
@@ -95,31 +93,15 @@ function show_results()
        $total = 0;
        $bdate = date2sql($_POST['TransFromDate']);
        $edate  = date2sql($_POST['TransToDate']);
+
+       $taxes = get_tax_summary($_POST['TransFromDate'], $_POST['TransToDate']);
+
        while ($tx = db_fetch($taxes))
        {
-               if ($tx['sales_gl_code'] == $tx['purchasing_gl_code'])
-               {
-                       $sql = "SELECT SUM(IF(amount >= 0, amount, 0)) AS payable, SUM(IF(amount < 0, -amount, 0)) AS collectible
-                               FROM ".TB_PREF."gl_trans WHERE account = '".$tx['sales_gl_code']."' AND tran_date >= '$bdate' AND tran_date <= '$edate'";
-                       $result = db_query($sql, "Error retrieving tax inquiry");
-                       $row = db_fetch($result);
-                       $payable = -$row['payable'];
-                       $collectible.= -$row['collectible'];
-               }
-               else
-               {
-                       $sql = "SELECT SUM(amount) AS collectible
-                               FROM ".TB_PREF."gl_trans WHERE account = '".$tx['sales_gl_code']."' AND tran_date >= '$bdate' AND tran_date <= '$edate'";
-                       $result = db_query($sql, "Error retrieving tax inquiry");
-                       $row = db_fetch($result);
-                       $collectible = -$row['collectible'];
-                       $sql = "SELECT SUM(amount) AS payable
-                               FROM ".TB_PREF."gl_trans WHERE account = '".$tx['purchasing_gl_code']."' AND tran_date >= '$bdate' AND tran_date <= '$edate'";
-                       $result = db_query($sql, "Error retrieving tax inquiry");
-                       $row = db_fetch($result);
-                       $payable = -$row['payable'];
-               }
-               $net = $collectible + $payable;
+
+               $payable = $tx['payable'];
+               $collectible = $tx['collectible'];
+               $net = $collectible - $payable;
                $total += $net;
                alt_table_row_color($k);
                label_cell($tx['name'] . " " . $tx['rate'] . "%");
index 8c0ca4cae41a959a57690d60ccf32dbbf1d9d157..d2331c17174dbc4d37a385e0e7c374f5db050b72 100644 (file)
@@ -144,6 +144,7 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
        }
 
     $date_ = $supp_trans->tran_date;
+       $ex_rate = get_exchange_rate_from_home_currency(get_supplier_currency($supp_trans->supplier_id), $date_);
 
     /*First insert the invoice into the supp_trans table*/
        $invoice_id = add_supp_trans($trans_type, $supp_trans->supplier_id, $date_, $supp_trans->due_date,
@@ -188,7 +189,7 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
                // store tax details if the gl account is a tax account
                add_gl_tax_details($entered_gl_code->gl_code, 
                        $trans_type, $invoice_id, $entered_gl_code->amount,
-                       $date_, $supp_trans->supp_reference);
+                       $ex_rate, $date_, $supp_trans->supp_reference);
     }
     foreach ($supp_trans->grn_items as $entered_grn)
     {
@@ -270,7 +271,7 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
                // here we suppose that tax is never included in price (we are company customer).
                        add_trans_tax_details($trans_type, $invoice_id, 
                                $taxitem['tax_type_id'], $taxitem['rate'], 0, $taxitem['Value'],
-                               $taxitem['Net'], $date_, $supp_trans->supp_reference);
+                               $taxitem['Net'], $ex_rate, $date_, $supp_trans->supp_reference);
 
                $total += add_gl_trans_supplier($trans_type, $invoice_id, $date_,
                        $taxitem['purchasing_gl_code'], 0, 0, $taxitem['Value'],
index 13a0c4c6689cd1329a210b30dfc6ab834a0ede5f..842a7663bba036645499a316720e9d4b2f359732 100644 (file)
@@ -33,12 +33,10 @@ function getTaxTransactions($from, $to)
        $fromdate = date2sql($from);
        $todate = date2sql($to);
 
-       $sql = "SELECT taxrec.*, IF(ISNULL(dtrans.rate),IF(ISNULL(strans.rate), taxrec.amount, taxrec.amount*strans.rate), 
-                               taxrec.amount*dtrans.rate) AS amount,
-                   IF(ISNULL(dtrans.rate),IF(ISNULL(strans.rate), taxrec.net_amount,taxrec.net_amount*strans.rate),
-                   taxrec.net_amount*dtrans.rate) AS net_amount,
+       $sql = "SELECT taxrec.*, taxrec.amount*ex_rate AS amount,
+                   taxrec.net_amount*ex_rate AS net_amount,
                                stype.type_name,
-                               if(supp.supp_name is null, debt.name, supp.supp_name) as name,
+                               IF(ISNULL(supp.supp_name), debt.name, supp.supp_name) as name,
                                branch.br_name
                FROM ".TB_PREF."trans_tax_details taxrec
                LEFT JOIN ".TB_PREF."supp_trans strans
index 155aa4a4c48aa1c1fb09151f655b8db0c9478511..ddfb6fe2a6506fd5414779c37c4db929e00261c8 100644 (file)
@@ -75,7 +75,8 @@ class fa2_1 {
        $move_sql =array( 
        "debtor_trans_tax_details" =>
                "SELECT tr.tran_date, tr.type, tr.trans_no, dt.tax_type_id, 
-                       dt.rate, dt.included_in_price, dt.amount, tr.reference as ref
+                       dt.rate, dt.included_in_price, dt.amount, tr.reference as ref,
+                       tr.rate as ex_rate
                FROM ".$pref."debtor_trans_tax_details dt       
                        LEFT JOIN ".$pref."trans_tax_details tt
                                ON dt.debtor_trans_no=tt.trans_no 
@@ -87,7 +88,8 @@ class fa2_1 {
        
        "supp_invoice_tax_items" =>
                "SELECT tr.tran_date, tr.type, tr.trans_no, st.tax_type_id, 
-                       st.rate, st.included_in_price, st.amount, tr.supp_reference as ref
+                       st.rate, st.included_in_price, st.amount, tr.supp_reference as ref,
+                       tr.rate as ex_rate
                        FROM ".$pref."supp_invoice_tax_items st 
                                LEFT JOIN ".$pref."trans_tax_details tt
                                        ON st.supp_trans_no=tt.trans_no 
@@ -106,12 +108,12 @@ class fa2_1 {
                                                        ($row['amount']/$row['rate']*(100-$row['rate']))
                                                        :($row['amount']/$row['rate']*100));
                                $sql2 = "INSERT INTO ".$pref."trans_tax_details 
-                               (trans_type,trans_no,tran_date,tax_type_id,rate,
+                               (trans_type,trans_no,tran_date,tax_type_id,rate,ex_rate,
                                        included_in_price, net_amount, amount, memo)
                                VALUES ('".$row['type']."','".$row['trans_no']."','"
                                        .$row['tran_date']."','".$row['tax_type_id']."','"
-                                       .$row['rate']."','".$row['included_in_price']."','"
-                                       .$net_amount
+                                       .$row['rate']."','".$row['ex_rate']."','"
+                                       .$row['included_in_price']."','".$net_amount
                                        ."','".$row['amount']."','".$row['ref']."')";
                                db_query($sql2, "Cannot move trans tax details from $tbl");
                        }
index 05d16b357115e0954b37a2f0bd8636ba204f7186..ae4ba4c67ed2ec46c16834024d734cdb13a70d9d 100644 (file)
@@ -301,6 +301,7 @@ CREATE TABLE `0_trans_tax_details` (
   `tran_date` date NOT NULL,
   `tax_type_id` int(11) NOT NULL default '0',
   `rate` double NOT NULL default '0',
+  `ex_rate` double NOT NULL default '1',
   `included_in_price` tinyint(1) NOT NULL default '0',
   `net_amount` double NOT NULL default '0',
   `amount` double NOT NULL default '0',