New tax system - sales side
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Tue, 16 Jul 2019 19:37:13 +0000 (21:37 +0200)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Mon, 19 Aug 2019 10:41:58 +0000 (12:41 +0200)
15 files changed:
includes/types.inc
purchasing/includes/db/grn_db.inc
purchasing/includes/po_class.inc
purchasing/includes/supp_trans_class.inc
reporting/rep109.php
reporting/rep111.php
reporting/rep209.php
sales/includes/cart_class.inc
sales/includes/db/sales_credit_db.inc
sales/includes/db/sales_delivery_db.inc
sales/includes/db/sales_invoice_db.inc
sales/includes/sales_db.inc
taxes/db/tax_groups_db.inc
taxes/tax_calc.inc
taxes/tax_rules.inc

index 212de03a2792c8ded33689791b3e4bf9292dbfe3..f83f20228d65931fcdf9fe0ef4e3d3a10b07dd7a 100644 (file)
@@ -261,7 +261,7 @@ $vat_categories = array(
        VC_ASSETS => _('Fixed assets'),
        VC_NONDEDUCT => _('No VAT deductible'),
        VC_SERVICES => _('Other services'),
-//     VC_PARTIAL => _('VAT partially deductible'),
+       VC_PARTIAL => _('VAT partially deductible'),
        VC_REVERSE => _('Reverse charge'),
 );
 
index c303933fc95f5ef79ab2408ccf870558b8b8ecdc..fbf231f47eac6352aa6aa3eb42aedfa6383ca5a9 100644 (file)
@@ -126,7 +126,7 @@ function write_grn(&$po)
                        {
                                /* This must be the first receipt of goods against this PO line. */
                                /* Store provisional cost used in GL posting - inventory is updated to actual cost later when invoice is received */
-                               $order_line->unit_cost = round2($order_line->taxfree_charge_value($po) / $po->ex_rate / $order_line->quantity, user_price_dec());
+                               $order_line->unit_cost = round2($order_line->taxfree_charge_value() / $po->ex_rate / $order_line->quantity, user_price_dec());
                        }
 
                        //------------------- update average material cost and clearing account --------------------------------
@@ -152,7 +152,7 @@ function write_grn(&$po)
                        $po->line_items[$line_no]->grn_item_id = $grn_item;
                        /* Update location stock records - NB  a po cannot be entered for a service/kit parts done automatically */
                        add_stock_move(ST_SUPPRECEIVE, $order_line->stock_id, $grn, $po->Location, $date_, "",
-                               $order_line->quantity, $order_line->unit_cost, $order_line->taxfree_charge_value($po)/$order_line->quantity);
+                               $order_line->quantity, $order_line->unit_cost, $order_line->taxfree_charge_value()/$order_line->quantity);
 
                } /* quantity received is != 0 */
        }
index 22a9274bf4278c9621d89e6ee5a07283b14d82f5..df485f00cb543e0a880afe6b5007bfc32fe60bf6 100644 (file)
@@ -71,8 +71,11 @@ class purch_order
        
        function add_to_order($stock_id, $qty, $item_descr, $price, $req_del_date, $qty_inv, $qty_recd, $qty_ordered=0, $grn_item_id=0, $po_item_id=0, $unit_cost=0)
        {
-               $this->line_items[] = new po_line_details($stock_id, $item_descr, $qty, $price,
+               $line = new po_line_details($stock_id, $item_descr, $qty, $price,
                        $req_del_date, $qty_inv, $qty_recd, $qty_ordered, $grn_item_id, $po_item_id, $unit_cost);
+
+               $this->line_items[] = $line;
+               $line->cart = $this;
        }
 
        function update_order_item($line_no, $qty, $price, $req_del_date, $description="")
@@ -193,7 +196,7 @@ class purch_order
        {
                // split nominal line values
                foreach($this->line_items as $line)
-                       $line->split_item_value($this);
+                       $line->split_item_value();
 
                // Exact tax values are currently entered as tax totals, so we need to move the differences back on line level.
                // currently first item with given tax type will be fixed with the calculated difference
@@ -242,6 +245,7 @@ class po_line_details
        var $quantity;          // this document line quantity
        //---
 
+       var $cart; // line context
        var $descr_editable;
        var $vat_category;
        var $gl_amounts;        // splited line value (after call to split_line_value method)
@@ -275,20 +279,20 @@ class po_line_details
        //
        // GRN line tax free value.
        //
-       function taxfree_charge_value($po)
+       function taxfree_charge_value()
        {
-               return get_tax_free_price_for_item($po->trans_type, $this->stock_id, $this->quantity*$this->price,
-                 $po->tax_group_id, $po->tax_included);
+               $this->split_item_value();
+               return $this->gl_amounts['Net'];
        }
 
        /*
                Splits item value to parts posted to GL.
        */
-       function split_item_value($cart)
+       function split_item_value()
        {
         $vat_factor = 1;
 
-               return $this->gl_amounts = split_item_price($this->stock_id, $this->price*$this->quantity, $cart->tax_group_id, $cart->tax_included, 
+               return $this->gl_amounts = split_item_price($this->stock_id, $this->price*$this->quantity, $this->cart->tax_group_id, $this->cart->tax_included, 
                        ST_SUPPINVOICE, $vat_factor);
        }
   
index 59d0e5c298ad59be0e60e375b894e1a8ca6d1eed..ddb55681e038a515dbe36a0353b840c2da6bc678 100644 (file)
@@ -78,10 +78,12 @@ class supp_trans
                $qty_recd, $prev_quantity_inv, $this_quantity_inv, $order_price, $chg_price, 
                $std_cost_unit=null, $gl_code='')
        {
-               $this->grn_items[$grn_item_id] = new grn_item($grn_item_id, $po_detail_item, 
+               $line = new grn_item($grn_item_id, $po_detail_item, 
                        $item_code, $item_description, $qty_recd, $prev_quantity_inv, $this_quantity_inv, 
                        $order_price, $chg_price, $std_cost_unit, $gl_code, $this->tax_included);
 
+               $line->cart = $this;
+               $this->grn_items[$grn_item_id] = $line;
                $this->src_docs = find_src_invoices($this);
 
                unset($this->tax_overrides); // cancel tax overrides after cart change
@@ -161,12 +163,12 @@ class supp_trans
        //
        //      Returns total invoice amount without taxes.
        //
-    function get_total_taxfree($tax_group_id=null)
+    function get_total_taxfree()
     {
        $total = 0;
 
                foreach ($this->grn_items as $ln_itm)
-               $total += round(($ln_itm->this_quantity_inv * $ln_itm->taxfree_charge_price($tax_group_id, $this->trans_type)),
+               $total += round(($ln_itm->this_quantity_inv * $ln_itm->taxfree_charge_price()),
                         user_price_dec());
 
                foreach ($this->gl_codes as $gl_line)
@@ -203,7 +205,7 @@ class supp_trans
        {
                // split nominal line values
                foreach($this->grn_items as $line)
-                       $line->split_item_value($this);
+                       $line->split_item_value();
 
                // Exact tax values are currently entered as tax totals, so we need to move the differences back on line level.
                // currently first item with given tax type will be fixed with the calculated difference
@@ -257,6 +259,8 @@ all the info to do the necessary entries without looking up ie additional querie
        var $gl_amounts;        // splited line value (after call to split_line_value method
        var $vat_category;
 
+       var $cart; // line context
+
        function __construct($id, $po_detail_item, $item_code, $item_description, $qty_recd, 
                $prev_quantity_inv, $this_quantity_inv, $order_price, $chg_price,
                $std_cost_unit, $gl_code, $tax_included)
@@ -285,22 +289,22 @@ all the info to do the necessary entries without looking up ie additional querie
 //               $this->chg_price, $tax_group_id, $this->tax_included);
 //     }
 
-       function taxfree_charge_price($tax_group_id, $trans_type=ST_PURCHORDER)
+       function taxfree_charge_price()
        {
-               return get_tax_free_price_for_item($trans_type, $this->item_code, $this->chg_price, 
-                 $tax_group_id, $this->tax_included);
+               $this->split_item_value();
+               return $this->gl_amounts['Net'];
        }
 
        /*
                Splits item value to parts posted to GL.
        */
-       function split_item_value($cart)
+       function split_item_value()
        {
 
         $vat_factor = 1;
 
-               return $this->gl_amounts = split_item_price($this->item_code, $this->chg_price*$this->this_quantity_inv, $cart->tax_group_id, $cart->tax_included, 
-                       ST_SUPPINVOICE, $vat_factor);
+               return $this->gl_amounts = split_item_price($this->item_code, $this->chg_price*$this->this_quantity_inv, $this->cart->tax_group_id, $this->cart->tax_included, 
+                       $this->cart->trans_type, $vat_factor);
        }
 }
 
index e96af1d4c4022f2202e06d3bda2ccb28df1e1b0f..06aecf085289e1d0294af0d06d379eb4ff9d91d1 100644 (file)
@@ -169,7 +169,7 @@ function print_sales_orders()
                }
 
                $tax_items = get_tax_for_items(ST_SALESINVOICE, $items, $prices, $myrow["freight_cost"],
-                 $myrow['tax_group_id'], $myrow['tax_included'],  null);
+                 $myrow['tax_group_id'], $myrow['tax_included']);
                $first = true;
                foreach($tax_items as $tax_item)
                {
index 23ccc7f1c0c5f8eed1a9186b3f019bca201529e6..a8c779271d0277903a0ccd0955ec15790e2b415b 100644 (file)
@@ -166,7 +166,7 @@ function print_sales_quotations()
                }
 
                $tax_items = get_tax_for_items(ST_SALESORDER, $items, $prices, $myrow["freight_cost"],
-                 $myrow['tax_group_id'], $myrow['tax_included'],  null);
+                 $myrow['tax_group_id'], $myrow['tax_included']);
                $first = true;
                foreach($tax_items as $tax_item)
                {
index 8b4decbdba3f7528f30f3b1ed6fad285ef1d109b..7e990baea1496f5f9086cc8bda6ad188ee93a7b5 100644 (file)
@@ -168,7 +168,7 @@ function print_po()
                $rep->NewLine();
 
                $tax_items = get_tax_for_items(ST_PURCHORDER, $items, $prices, 0,
-                 $myrow['tax_group_id'], $myrow['tax_included'],  null, TCA_LINES);
+                 $myrow['tax_group_id'], $myrow['tax_included'], TCA_LINES);
                $first = true;
                foreach($tax_items as $tax_item)
                {
index 6a7b38533358be9f20c1b2e0e8ba412403232448..e1f63d5309fec9920e0df08cf716e0a2c70df1de 100644 (file)
@@ -61,7 +61,6 @@ class Cart
 
        var $tax_group_id;
        var $tax_group_name;
-       var $tax_group_array = null; // saves db queries
        var $price_factor;       // ditto for price calculations
 
        var     $pos;                   // user assigned POS
@@ -368,7 +367,6 @@ class Cart
                $this->phone = $phone;
                $this->email = $email;
                $this->tax_group_id = $tax_group_id;
-               $this->tax_group_array = get_tax_group_items_as_array($tax_group_id);
        }
 
        function set_sales_type($sales_type, $sales_name, $tax_included=0, $factor=0)
@@ -401,6 +399,7 @@ class Cart
                        $qty_done,  $unit_cost, $description, $id, $src_no, $src_id);
 
                if ($line->valid) {
+                       $line->cart = $this;
                        $this->line_items[$line_no] = $line;
                        return 1;
                } else
@@ -503,11 +502,22 @@ class Cart
                return 0;
        }
 
+       /*
+               Split line value to cost and taxes.
+               Stores calculated amounts in $line->gl_amounts arrays.
+       */
+       function split_line_values()
+       {
+               // split nominal line values
+               foreach ($this->line_items as $line)
+                       $line->split_item_value();
+       }
+
        function get_taxes($shipping_cost=null)
        {
                $items = array();
                $prices = array();
-               if($shipping_cost==null)
+               if ($shipping_cost == null)
                        $shipping_cost = $this->freight_cost;
 
                foreach ($this->line_items as $ln_itm) {
@@ -518,7 +528,7 @@ class Cart
                }
 
                $taxes = get_tax_for_items($this->trans_type, $items, $prices, $shipping_cost,
-                       $this->tax_group_id, $this->tax_included,  $this->tax_group_array);
+                       $this->tax_group_id, $this->tax_included);
 
     // Adjustment for swiss franken, we always have 5 rappen = 1/20 franken
     if ($this->customer_currency == 'CHF') {
@@ -541,21 +551,9 @@ class Cart
 
        function get_shipping_tax()
        {
+               $freight = split_item_price(NULL, $this->freight_cost, $this->tax_group_id, $this->tax_included, $this->trans_type);
 
-               $tax_items = get_shipping_tax_as_array($this->tax_group_id);
-               $tax_rate = 0;
-               if ($tax_items != null) {
-                       foreach ($tax_items as $item_tax) {
-                               $index = $item_tax['tax_type_id'];
-                               if (isset($this->tax_group_array[$index]['rate'])) {
-                                       $tax_rate += $item_tax['rate'];
-                               }
-                       }
-               }
-               if($this->tax_included)
-                       return round($this->freight_cost*$tax_rate/($tax_rate+100),  user_price_dec());
-               else
-                       return round($this->freight_cost*$tax_rate/100,  user_price_dec());
+               return $freight['Tax'];
        }
        /*
                Returns transaction value including all taxes
@@ -672,6 +670,7 @@ class line_details
        var $unit_cost;
        var $descr_editable;
 
+       var $cart; // line context
        var $valid; // validation in constructor
        /*
                Line quantity properties in various cart create modes:
@@ -737,5 +736,20 @@ class line_details
        {
                return $this->price;
        }
+
+       function taxfree_charge_price()
+       {
+               $this->split_item_value();
+               return $this->gl_amounts['Net'];
+       }
+
+       /*
+               Splits item value to parts posted to GL.
+       */
+       function split_item_value()
+       {
+               return $this->gl_amounts = split_item_price($this->stock_id, $this->price*$this->quantity, $this->cart->tax_group_id, $this->cart->tax_included, 
+                       $this->cart->trans_type);
+       }
 }
 
index 43b6e3081c466128c46d60979d76db342e24c761..37e0157711bf425f835a233ea8dd2898c9ade65d 100644 (file)
@@ -101,31 +101,30 @@ function write_credit_note(&$credit_note, $write_off_acc)
        }
 
        $total = 0;
-       foreach ($credit_note->line_items as $credit_line) {
+       $credit_note->split_line_values();
+       foreach ($credit_note->line_items as $line) {
 
-               if ($credit_invoice && $credit_line->qty_dispatched!=$credit_line->qty_old ) {
-                       update_parent_line(ST_CUSTCREDIT, $credit_line->src_id,($credit_line->qty_dispatched
-                                       -$credit_line->qty_old));
+               if ($credit_invoice && $line->qty_dispatched!=$line->qty_old ) {
+                       update_parent_line(ST_CUSTCREDIT, $line->src_id,($line->qty_dispatched
+                                       -$line->qty_old));
                }
 
-               $line_taxfree_price = get_tax_free_price_for_item(ST_CUSTCREDIT, $credit_line->stock_id, $credit_line->price,
-                 0, $credit_note->tax_included, $credit_note->tax_group_array);
+               $line_taxfree_price = $line->gl_amounts['Net'];
 
-               $line_tax = get_full_price_for_item(ST_CUSTCREDIT, $credit_line->stock_id, $credit_line->price,
-                 0, $credit_note->tax_included, $credit_note->tax_group_array) - $line_taxfree_price;
+               $line_tax = $line->gl_amounts['Net'];
 
-               $credit_line->unit_cost = get_unit_cost($credit_line->stock_id); 
+               $line->unit_cost = get_unit_cost($line->stock_id); 
                
-               write_customer_trans_detail_item(ST_CUSTCREDIT, $credit_no, $credit_line->stock_id,
-                       $credit_line->item_description, $credit_line->qty_dispatched,
-                       $credit_line->line_price(), $line_tax, $credit_line->discount_percent,
-                       $credit_line->unit_cost, $credit_line->src_id, $trans_no==0 ? 0:  $credit_line->id);
+               write_customer_trans_detail_item(ST_CUSTCREDIT, $credit_no, $line->stock_id,
+                       $line->item_description, $line->qty_dispatched,
+                       $line->line_price(), $line_tax, $line->discount_percent,
+                       $line->unit_cost, $line->src_id, $trans_no==0 ? 0:  $line->id);
 
                if ($credit_type == 'Return')
-                       add_credit_movements_item($credit_note, $credit_line,
-                               $credit_type, ($line_taxfree_price+$line_tax)*(1-$credit_line->discount_percent), $credit_invoice);
+                       add_credit_movements_item($credit_note, $line,
+                               $credit_type, ($line_taxfree_price+$line_tax)*(1-$line->discount_percent), $credit_invoice);
 
-               $total += add_gl_trans_credit_costs($credit_note, $credit_line, $credit_no,
+               $total += add_gl_trans_credit_costs($credit_note, $line, $credit_no,
                    $credit_date, $credit_type, $write_off_acc, $branch_data);
        } /*end of credit_line loop */
 
@@ -166,8 +165,8 @@ function write_credit_note(&$credit_note, $write_off_acc)
                $Refs->save(ST_CUSTCREDIT, $credit_no, $credit_note->reference);
        }
        hook_db_postwrite($credit_note, ST_CUSTCREDIT);
-       commit_transaction();
 
+       commit_transaction();
        return $credit_no;
 }
 
@@ -178,7 +177,7 @@ function write_credit_note(&$credit_note, $write_off_acc)
 function add_credit_movements_item(&$credit_note, &$credit_line,
        $credit_type, $price, $credited_invoice=0)
 {
-    //Chaitanya : Stamp current cost in stock moves $credit_line does not fetch cost
+    //Chaitanya : Stamp current cost in stock moves $line does not fetch cost
     $curr_std_cost = get_unit_cost($credit_line->stock_id);
 
        $reference = _("Return");
@@ -231,12 +230,9 @@ function add_gl_trans_credit_costs($order, $order_line, $credit_no, $date_,
 
        if ($order_line->line_price() != 0) {
 
-               $line_taxfree_price =
-               get_tax_free_price_for_item(ST_CUSTCREDIT, $order_line->stock_id, $order_line->price,
-                 0, $order->tax_included, $order->tax_group_array);
+               $line_taxfree_price = $order_line->gl_amounts['Net'];
 
-               $line_tax = get_full_price_for_item(ST_CUSTCREDIT, $order_line->stock_id, $order_line->price,
-                 0, $order->tax_included, $order->tax_group_array) - $line_taxfree_price;
+               $line_tax = $order_line->gl_amounts['Tax'];
 
 
                //Post sales transaction to GL credit sales
index 18faa0be27f1e16bf3148cce546e49440c8f3595..3c260a52c142ee7d5bdb9ca34e7aaae4a6713cb7 100644 (file)
@@ -68,46 +68,46 @@ function write_sales_delivery(&$delivery,$bo_policy)
                $branch_data = get_branch_accounts($delivery->Branch);
                $total = 0;
        }
-       foreach ($delivery->line_items as $line_no => $delivery_line) {
 
-               $qty = $delivery_line->qty_dispatched;
-               $line_price = $delivery_line->line_price();
-               $line_taxfree_price = get_tax_free_price_for_item(ST_CUSTDELIVERY, $delivery_line->stock_id,
-                       $delivery_line->price*$qty, $delivery->tax_group_id, $delivery->tax_included,
-                       $delivery->tax_group_array);
+       $delivery->split_line_values();
 
-               $line_tax = get_full_price_for_item(ST_CUSTDELIVERY, $delivery_line->stock_id,
-                               $delivery_line->price * $qty, 0, $delivery->tax_included, $delivery->tax_group_array) - $line_taxfree_price;
+       foreach ($delivery->line_items as $line_no => $line) {
 
-               $delivery_line->unit_cost = get_unit_cost($delivery_line->stock_id);
+               $qty = $line->qty_dispatched;
+               $line_price = $line->line_price();
+               $line_taxfree_price = $line->gl_amounts['Net'];
+
+               $line_tax = $line->gl_amounts['Tax'];
+
+               $line->unit_cost = get_unit_cost($line->stock_id);
 
                /* add delivery details for all lines */
-               write_customer_trans_detail_item(ST_CUSTDELIVERY, $delivery_no, $delivery_line->stock_id,
-                       $delivery_line->item_description, $delivery_line->qty_dispatched,
-                       $delivery_line->line_price(), $qty ? $line_tax/$qty : 0,
-                       $delivery_line->discount_percent, $delivery_line->unit_cost, $delivery_line->src_id,
-                       $trans_no ? $delivery_line->id : 0);
+               write_customer_trans_detail_item(ST_CUSTDELIVERY, $delivery_no, $line->stock_id,
+                       $line->item_description, $line->qty_dispatched,
+                       $line->line_price(), $qty ? $line_tax/$qty : 0,
+                       $line->discount_percent, $line->unit_cost, $line->src_id,
+                       $trans_no ? $line->id : 0);
 
        // Now update sales_order_details for the quantity delivered
-               if ($delivery_line->qty_old != $delivery_line->qty_dispatched)
-                       update_parent_line(ST_CUSTDELIVERY, $delivery_line->src_id,
-                               $delivery_line->qty_dispatched-$delivery_line->qty_old);
+               if ($line->qty_old != $line->qty_dispatched)
+                       update_parent_line(ST_CUSTDELIVERY, $line->src_id,
+                               $line->qty_dispatched-$line->qty_old);
 
-               if ($delivery_line->qty_dispatched != 0) {
-                       add_stock_move(ST_CUSTDELIVERY, $delivery_line->stock_id, $delivery_no,
+               if ($line->qty_dispatched != 0) {
+                       add_stock_move(ST_CUSTDELIVERY, $line->stock_id, $delivery_no,
                                $delivery->Location, $delivery->document_date, $delivery->reference,
-                               -$delivery_line->qty_dispatched, $delivery_line->unit_cost,
-                                $line_price*(1-$delivery_line->discount_percent));
+                               -$line->qty_dispatched, $line->unit_cost,
+                                $line_price*(1-$line->discount_percent));
 
-                       $mb_flag = get_mb_flag($delivery_line->stock_id);
+                       $mb_flag = get_mb_flag($line->stock_id);
 
                        if (is_fixed_asset($mb_flag)) {
                                $sql = "UPDATE ".TB_PREF."stock_master SET inactive=1, material_cost=0
-                                       WHERE stock_id=".db_escape($delivery_line->stock_id);
+                                       WHERE stock_id=".db_escape($line->stock_id);
                                db_query($sql,"The cost details for the fixed asset could not be updated");
                        }
 
-                       $stock_gl_code = get_stock_gl_code($delivery_line->stock_id);
+                       $stock_gl_code = get_stock_gl_code($line->stock_id);
 
                        // If there is a Customer Dimension, then override with this,
                        // else take the Item Dimension (if any)
@@ -124,11 +124,11 @@ function write_sales_delivery(&$delivery,$bo_policy)
                        }
                        /* insert gl_trans to credit stock and debit cost of sales at standard cost*/
                        
-                       if (is_inventory_item($delivery_line->stock_id)) {
+                       if (is_inventory_item($line->stock_id)) {
                                // Fixed Assets
                                if ($delivery->fixed_asset) {
-                                   $fa_purchase_cost = get_purchase_cost($delivery_line->stock_id);
-                                       $fa_depreciation = $fa_purchase_cost - $delivery_line->unit_cost;
+                                   $fa_purchase_cost = get_purchase_cost($line->stock_id);
+                                       $fa_depreciation = $fa_purchase_cost - $line->unit_cost;
                                        /*first remove depreciation*/
                                        add_gl_trans_std_cost(ST_CUSTDELIVERY, $delivery_no,
                                                $delivery->document_date, $stock_gl_code["adjustment_account"], $dim, $dim2, "",
@@ -146,17 +146,17 @@ function write_sales_delivery(&$delivery,$bo_policy)
                                                PT_CUSTOMER, $delivery->customer_id);
                                }
                                // Invetory Items
-                               else if ($delivery_line->unit_cost != 0) {
+                               else if ($line->unit_cost != 0) {
                                        /*first the cost of sales entry*/
                                        add_gl_trans_std_cost(ST_CUSTDELIVERY, $delivery_no,
                                                $delivery->document_date, $stock_gl_code["cogs_account"], $dim, $dim2, "",
-                                               $delivery_line->unit_cost * $delivery_line->qty_dispatched,
+                                               $line->unit_cost * $line->qty_dispatched,
                                                PT_CUSTOMER, $delivery->customer_id);
 
                                        /*now the stock entry*/
                                        add_gl_trans_std_cost(ST_CUSTDELIVERY, $delivery_no, $delivery->document_date,
                                                $stock_gl_code["inventory_account"], 0, 0, "",
-                                               (-$delivery_line->unit_cost * $delivery_line->qty_dispatched),
+                                               (-$line->unit_cost * $line->qty_dispatched),
                                                PT_CUSTOMER, $delivery->customer_id);
                                }                               
 
index 96ba23a6adb0d5bcbb41c09dc19536fe3c916147..1f79aa09db3780410457b0c71af0a167d1172298 100644 (file)
@@ -56,7 +56,7 @@ function write_sales_invoice(&$invoice)
        }
 
        if($invoice->tax_included==0) {
-           $items_added_tax = $ov_gst-$freight_tax;
+           $items_added_tax = $ov_gst - $freight_tax;
            $freight_added_tax = $freight_tax;
        } else {
            $items_added_tax = 0;
@@ -102,34 +102,31 @@ function write_sales_invoice(&$invoice)
        // for prepayments use deferred income account if set
        $sales_account = $invoice->is_prepaid() ? get_company_pref('deferred_income_act') : 0;
 
-       foreach ($invoice->line_items as $line_no => $invoice_line) {
-               $qty = $invoice_line->qty_dispatched;
-               $line_taxfree_price = get_tax_free_price_for_item(ST_SALESINVOICE, $invoice_line->stock_id,
-                       $invoice_line->price * $qty, $invoice->tax_group_id, $invoice->tax_included,
-                       $invoice->tax_group_array);
+       $invoice->split_line_values();
+       foreach ($invoice->line_items as $line_no => $line) {
+               $qty = $line->qty_dispatched;
+               $line_taxfree_price = $line->gl_amounts['Net'];
 
-               $line_tax = get_full_price_for_item(ST_SALESINVOICE, $invoice_line->stock_id,
-                       $invoice_line->price * $qty, 0, $invoice->tax_included,
-                       $invoice->tax_group_array) - $line_taxfree_price;
+               $line_tax = $line->gl_amounts['Tax'];
 
-               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(), $qty ? $line_tax/$qty : 0, $invoice_line->discount_percent,
-                       $invoice_line->unit_cost, $invoice_line->src_id,
-                       $trans_no ? $invoice_line->id : 0);
+               write_customer_trans_detail_item(ST_SALESINVOICE, $invoice_no, $line->stock_id,
+                       $line->item_description, $line->qty_dispatched,
+                       $line->line_price(), $qty ? $line_tax/$qty : 0, $line->discount_percent,
+                       $line->unit_cost, $line->src_id,
+                       $trans_no ? $line->id : 0);
 
                // Update delivery items for the quantity invoiced
-               if ($invoice_line->qty_old != $invoice_line->qty_dispatched)
+               if ($line->qty_old != $line->qty_dispatched)
                {
                        if ($invoice->is_prepaid())
-                               update_prepaid_so_line($invoice_line->src_id, $invoice_line->qty_dispatched-$invoice_line->qty_old);
+                               update_prepaid_so_line($line->src_id, $line->qty_dispatched-$line->qty_old);
                        else
-                               update_parent_line(ST_SALESINVOICE, $invoice_line->src_id, ($invoice_line->qty_dispatched-$invoice_line->qty_old));
+                               update_parent_line(ST_SALESINVOICE, $line->src_id, ($line->qty_dispatched-$line->qty_old));
                }
-               if ($invoice_line->qty_dispatched != 0) {
-                       $stock_gl_code = get_stock_gl_code($invoice_line->stock_id);
+               if ($line->qty_dispatched != 0) {
+                       $stock_gl_code = get_stock_gl_code($line->stock_id);
 
-                       if ($invoice_line->line_price() != 0) {
+                       if ($line->line_price() != 0) {
                                //Post sales transaction to GL credit sales
 
                                // If there is a Branch Sales Account, then override with this,
@@ -145,11 +142,11 @@ function write_sales_invoice(&$invoice)
                                $total += add_gl_trans_customer(ST_SALESINVOICE, $invoice_no, $date_, $sales_account, $dim, $dim2,
                                        -$line_taxfree_price*$prepaid_factor, $invoice->customer_id);
 
-                               if ($invoice_line->discount_percent != 0) {
+                               if ($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->discount_percent)*$prepaid_factor, $invoice->customer_id);
+                                               ($line_taxfree_price * $line->discount_percent)*$prepaid_factor, $invoice->customer_id);
                                } /*end of if discount !=0 */
                        }
                } /*quantity dispatched is more than 0 */
index 7f0a752f942a7cfc2e18d4565efde7b7eea432f0..26e92955d29ead94ebee3d7ee5c07bba35cc78b5 100644 (file)
@@ -268,8 +268,7 @@ function read_sales_trans($doc_type, $trans_no, &$cart)
                $result = get_customer_trans_details($doc_type,$trans_no);
                if (db_num_rows($result) > 0) {
                        for($line_no=0; $myrow = db_fetch($result); $line_no++) {
-                               $cart->line_items[$line_no] = new line_details(
-                                       $myrow["stock_id"],$myrow["quantity"],
+                               $cart->add_to_cart($line_no, $myrow["stock_id"],$myrow["quantity"],
                                        $myrow["unit_price"], $myrow["discount_percent"],
                                        $myrow["qty_done"], $myrow["unit_cost"],
                                        $myrow["StockDescription"],$myrow["id"], $myrow["debtor_trans_no"],
index dc269fdc8018c4cc5ab012ec47ba8ebd891b9724..5e152855cbaf5cb419f7be3a40cbf17c79b33dcb 100644 (file)
@@ -121,7 +121,6 @@ function get_tax_group_items_as_array($id)
                $ret_tax_array[$tax_group_item['tax_type_id']] = $tax_group_item;
 
        }
-       
        return $ret_tax_array;
 }
 
index 84aa9ce6372595ca380371e985178753dea4dace..004fcd33fcb49ae2bc64a497bd21692b5969b760 100644 (file)
@@ -91,7 +91,7 @@ function get_base_taxdata($stock_id, $group_id)
 
 /*
        Main tax procedure splitting transaction item value according to item tax rules applicable:
-               $stock_id - stock item code; special case is '' for shipping
+               $stock_id - stock item code; NULL for shipping
                $amount - price/value to be splitted
                $tax_group - entity tax group
                $tax_included - whether value includes all taxes
@@ -230,7 +230,7 @@ function get_tax_for_items($trans_type, $items, $prices, $shipping_cost, $tax_gr
        // if shipping cost is passed, just add to the prices/items tables
        if ($shipping_cost != 0)
        {
-               $items[] = null;
+               $items[] = NULL;
                $prices[] = $shipping_cost;
                if ($vat_factors)
                        $vat_factors[] = 1;
index 9dd3519fd1a54b85262f21b9600cbcd3446add6d..fa13419b5beb3da4589911cbf42e7c3e469308a2 100644 (file)
     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
 ***********************************************************************/
 /*
-       FA tax rules basic reimplementation. Final version would involve changes in database scheme,
-       and/or tax system class selection according to site requirements.
-
-       TODO:
-       . changes in Sales module
-       . change all tax related methods in supp_trans to use split_item_price instead of other functions like get_taxes_for_item
+       FA tax basic rules.
 */
 define('TAX_NONE', 0); // none option
 define('TQ_NONE', 0); // none option