Fixed tax calculation for tax_included sales invoices to avoid rounding differences.
authorJanusz Dobrowolski <janusz@frontaccouting.eu>
Sat, 19 Mar 2011 09:21:23 +0000 (10:21 +0100)
committerJanusz Dobrowolski <janusz@frontaccouting.eu>
Sat, 19 Mar 2011 09:21:23 +0000 (10:21 +0100)
gl/includes/db/gl_db_trans.inc
purchasing/includes/ui/invoice_ui.inc
sales/includes/db/sales_invoice_db.inc
taxes/tax_calc.inc

index 831ff3d301d23ff00ce082b7b76ca36a6ccf67fb..fc581e088be037aaca8abc86075471ad24dc7b95 100644 (file)
@@ -339,7 +339,7 @@ function get_only_budget_trans_from_to($from_date, $to_date, $account, $dimensio
 //--------------------------------------------------------------------------------
 //     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)
+function add_gl_tax_details($gl_code, $trans_type, $trans_no, $amount, $ex_rate, $date, $memo, $included=0, $net_amount = null)
 {
        $tax_type = is_tax_account($gl_code);
        if(!$tax_type) return;  // $gl_code is not tax account
@@ -349,15 +349,16 @@ function add_gl_tax_details($gl_code, $trans_type, $trans_no, $amount, $ex_rate,
        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; 
+       if ($net_amount===null) {
+               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);
                        
index eefe95f1d9201382348e96decf2bce9166d17d14..7eff2392c8aabfac44e24a82664a79207acdaed4 100644 (file)
@@ -363,6 +363,7 @@ function display_grn_items_for_selection(&$supp_trans, $k)
                hidden('order_price'.$n, $myrow['unit_price'], false).
                hidden('std_cost_unit'.$n, $myrow['std_cost_unit'], false).
                hidden('po_detail_item'.$n, $myrow['po_detail_item'], false));
+               hidden('location'.$n, $myrow["loc_code"], false).
                label_cell(get_trans_view_str(ST_PURCHORDER, $myrow["purch_order_no"]));
             label_cell($myrow["item_code"]);
             label_cell($myrow["description"]);
index e9bb9fb7bd3b71d59688faa487a0bfec3a9d2c4c..558b2159aba0159fd9a1bad9c8e0a000ad275cfd 100644 (file)
@@ -77,18 +77,18 @@ function write_sales_invoice(&$invoice)
        }
        $total = 0;
        foreach ($invoice->line_items as $line_no => $invoice_line) {
-
+               $qty = $invoice_line->qty_dispatched;
                $line_taxfree_price = get_tax_free_price_for_item($invoice_line->stock_id,
-                       $invoice_line->price, 0, $invoice->tax_included,
+                       $invoice_line->price * $qty, 0, $invoice->tax_included,
                        $invoice->tax_group_array);
 
                $line_tax = get_full_price_for_item($invoice_line->stock_id,
-                       $invoice_line->price, 0, $invoice->tax_included,
+                       $invoice_line->price * $qty, 0, $invoice->tax_included,
                        $invoice->tax_group_array) - $line_taxfree_price;
 
                write_customer_trans_detail_item(ST_SALESINVOICE, $invoice_no, $invoice_line->stock_id,
                        $invoice_line->item_description, $invoice_line->qty_dispatched,
-                       $invoice_line->line_price(), $line_tax, $invoice_line->discount_percent,
+                       $invoice_line->line_price(), $qty ? $line_tax/$qty : 0, $invoice_line->discount_percent,
                        $invoice_line->standard_cost, $invoice_line->src_id,
                        $trans_no ? $invoice_line->id : 0);
 
@@ -112,14 +112,14 @@ function write_sales_invoice(&$invoice)
                                $dim2 = ($invoice->dimension2_id != $customer['dimension2_id'] ? $invoice->dimension2_id : 
                                        ($customer['dimension2_id'] != 0 ? $customer["dimension2_id"] : $stock_gl_code["dimension2_id"]));
                                $total += add_gl_trans_customer(ST_SALESINVOICE, $invoice_no, $date_, $sales_account, $dim, $dim2,
-                                       (-$line_taxfree_price * $invoice_line->qty_dispatched),
+                                       -$line_taxfree_price ,
                                        $invoice->customer_id, "The sales price GL posting could not be inserted");
 
                                if ($invoice_line->discount_percent != 0) {
 
                                        $total += add_gl_trans_customer(ST_SALESINVOICE, $invoice_no, $date_,
                                                $branch_data["sales_discount_account"], $dim, $dim2,
-                                               ($line_taxfree_price * $invoice_line->qty_dispatched * $invoice_line->discount_percent),
+                                               ($line_taxfree_price * $invoice_line->discount_percent),
                                                $invoice->customer_id, "The sales discount GL posting could not be inserted");
                                } /*end of if discount !=0 */
                        }
index 3f54bd226aa65ee6871e86e30598f11f57d9ec37..6f6e9b7b153d4e5669b0900fc42d49921d154358 100644 (file)
@@ -49,8 +49,7 @@ function get_tax_free_price_for_item($stock_id, $price, $tax_group, $tax_include
        {
                        $tax_multiplier += $taxitem["rate"];
        }
-       
-       return round($price / (1 + ($tax_multiplier / 100)),  2*user_price_dec());
+       return round($price / (1 + ($tax_multiplier / 100)),  user_price_dec());
 }
 //
 //     Full price (incl. VAT) for item $stock_id with line price $price,
@@ -86,7 +85,7 @@ function get_full_price_for_item($stock_id, $price, $tax_group, $tax_included, $
                        $tax_multiplier += $taxitem["rate"];
        }
        
-       return round($price * (1 + ($tax_multiplier / 100)),  2*user_price_dec());
+       return round($price * (1 + ($tax_multiplier / 100)),  user_price_dec());
 }
 
 //---------------------------------------------------------------------------------
@@ -163,7 +162,7 @@ function get_tax_for_items($items, $prices, $shipping_cost, $tax_group, $tax_inc
                                $index = $item_tax['tax_type_id'];
                                if($tax_included==1) {// 2008-11-26 Joe Hunt Taxes are stored without roundings
                                  $nprice = get_tax_free_price_for_item($items[$i], $prices[$i], $tax_group, $tax_included);
-                                 $ret_tax_array[$index]['Value'] += ($nprice * $item_tax['rate'] / 100);
+                                 $ret_tax_array[$index]['Value'] += $prices[$i]-$nprice;
                                  $ret_tax_array[$index]['Net'] += $nprice;
                                } else {
                                  $ret_tax_array[$index]['Value'] += ($prices[$i] * $item_tax['rate'] / 100);