Added tax calcualtion algorithm selection in sales and purchasing, driven by company...
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Thu, 6 Jan 2011 20:16:17 +0000 (20:16 +0000)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Thu, 6 Jan 2011 20:16:17 +0000 (20:16 +0000)
17 files changed:
admin/gl_setup.php
includes/sysnames.inc
includes/types.inc
includes/ui/ui_lists.inc
purchasing/includes/db/invoice_db.inc
purchasing/includes/db/po_db.inc
purchasing/includes/db/supp_trans_db.inc
purchasing/includes/db/suppliers_db.inc
purchasing/includes/po_class.inc
purchasing/includes/supp_trans_class.inc
purchasing/includes/ui/invoice_ui.inc
purchasing/manage/suppliers.php
purchasing/supplier_invoice.php
reporting/rep209.php
sql/en_US-demo.sql
sql/en_US-new.sql
taxes/tax_calc.inc

index 79b84ddf49f44af17e675c6d7e8753f837fde3dd..210274746cb0baabcf380851cf6308687ea14d11 100644 (file)
@@ -67,7 +67,7 @@ if (isset($_POST['submit']) && can_process())
                'default_prompt_payment_act', 'default_inventory_act', 'default_cogs_act',
                'default_adj_act', 'default_inv_sales_act', 'default_assembly_act', 'legal_text',
                'past_due_days', 'default_workorder_required', 'default_dim_required',
-               'default_delivery_required', 'grn_clearing_act',
+               'default_delivery_required', 'grn_clearing_act', 'tax_algorithm',
                'allow_negative_stock'=> 0, 'accumulate_shipping'=> 0,
                'po_over_receive' => 0.0, 'po_over_charge' => 0.0, 'default_credit_limit'=>0.0
 )));
@@ -100,6 +100,7 @@ $_POST['pyt_discount_act']  = $myrow["pyt_discount_act"];
 
 $_POST['exchange_diff_act'] = $myrow["exchange_diff_act"];
 $_POST['bank_charge_act'] = $myrow["bank_charge_act"];
+$_POST['tax_algorithm'] = $myrow["tax_algorithm"];
 $_POST['default_sales_act'] = $myrow["default_sales_act"];
 $_POST['default_sales_discount_act']  = $myrow["default_sales_discount_act"];
 $_POST['default_prompt_payment_act']  = $myrow["default_prompt_payment_act"];
@@ -141,6 +142,8 @@ gl_all_accounts_list_row(_("Exchange Variances Account:"), 'exchange_diff_act',
 
 gl_all_accounts_list_row(_("Bank Charges Account:"), 'bank_charge_act', $_POST['bank_charge_act']);
 
+tax_algorithm_list_row(_("Tax Algorithm:"), 'tax_algorithm', $_POST['tax_algorithm']);
+
 //---------------
 
 table_section_title(_("Customers and Sales"));
index 1b9f675487d2febb8b50d2172b62d6ceb10ab5ee..670075770bb3c40a03e0ebaf7fcc41e552b5d3c9 100644 (file)
@@ -181,6 +181,11 @@ $pterm_types = array(
        PTT_DAYS => _("After No. of Days"),
        PTT_FOLLOWING => _("Day In Following Month")
 );
+//----------------------------------------------------------------------------------
 
+$tax_algorithms = array( 
+       TCA_LINES => _("Sum per line taxes"), 
+       TCA_TOTALS => _("Taxes from totals")
+);
 
 ?>
\ No newline at end of file
index 79ef8be71c01699d87a8daf242fee733ad718fe3..9a437f1ba9dad7d78f6624e526344872d4510351 100644 (file)
@@ -226,6 +226,12 @@ define('PTT_CASH', 2);
 define('PTT_DAYS', 3);
 define('PTT_FOLLOWING', 4);
 
+//----------------------------------------------------------------------------------
+// Tax calculation algorithms used in als and purchase (depends on supplier's invoicing software)
+
+define('TCA_TOTALS', 1); // taxes are calculated from respective net totals for all lines
+define('TCA_LINES', 2); // taxes calculated for every line, then summed
+
 include_once($path_to_root . '/includes/sysnames.inc');
 
 ?>
\ No newline at end of file
index 14eb54436083283ba3f40cbaa5d4355ea2a499bc..0fde17de1c1a5ec20e6bdbc4bb3c43dce8890254 100644 (file)
@@ -1407,8 +1407,10 @@ function bank_accounts_list_row($label, $name, $selected_id=null, $submit_on_cha
 }
 //-----------------------------------------------------------------------------------------------
 
-function cash_accounts_list_row($label, $name, $selected_id=null, $submit_on_change=false)
+function cash_accounts_list_row($label, $name, $selected_id=null, $submit_on_change=false, $all_option=false)
 {
+       global $all_items;
+
        $sql = "SELECT ".TB_PREF."bank_accounts.id, bank_account_name, bank_curr_code, inactive
                FROM ".TB_PREF."bank_accounts
                WHERE ".TB_PREF."bank_accounts.account_type=".BT_CASH;
@@ -1418,6 +1420,8 @@ function cash_accounts_list_row($label, $name, $selected_id=null, $submit_on_cha
        echo "<td>";
        echo combo_input($name, $selected_id, $sql, 'id', 'bank_account_name',
        array(
+               'spec_option' => $all_option,
+               'spec_id' => $all_items,
                'format' => '_format_add_curr',
                'select_submit'=> $submit_on_change,
                'async' => true
@@ -2327,4 +2331,32 @@ function coa_list_row($label, $name, $value=null)
        echo array_selector( $name, $value, $coas );
        echo "</td></tr>\n";
 }
+
+function tax_algorithm_list($name, $value=null, $submit_on_change = false)
+{
+       global $tax_algorithms;
+       
+       return array_selector($name, $value, $tax_algorithms, 
+               array( 
+                       'select_submit'=> $submit_on_change,
+                       'async' => true,
+                       )
+       );
+}
+
+function tax_algorithm_list_cells($label, $name, $value=null, $submit_on_change=false)
+{
+       if ($label != null)
+               echo "<td>$label</td>\n";
+       echo "<td>";
+       echo tax_algorithm_list($name, $value, $submit_on_change);
+       echo "</td>\n";
+}
+
+function tax_algorithm_list_row($label, $name, $value=null, $submit_on_change=false)
+{
+       echo "<tr><td class='label'>$label</td>";
+       tax_algorithm_list_cells(null, $name, $value, $submit_on_change);
+       echo "</tr>\n";
+}
 ?>
\ No newline at end of file
index 06d27cde04265d5093cd79c1647c729b1a85678e..344e2b68f7c2ec7cf6f81a18fbe8b70b3a0c8659 100644 (file)
@@ -16,7 +16,7 @@ include_once($path_to_root . "/purchasing/includes/db/invoice_items_db.inc");
 function read_supplier_details_to_trans(&$supp_trans, $supplier_id)
 {
        $sql = "SELECT supp.supp_name, terms.terms, terms.days_before_due,
-               terms.day_in_following_month, supp.tax_included,
+               terms.day_in_following_month, supp.tax_included, supp.tax_algorithm,
                supp.tax_group_id, tax_group.name AS tax_group_name,
                supp.credit_limit - Sum(IFNULL(IF(trans.type=".ST_SUPPCREDIT.", -1, 1) 
                        * (ov_amount + ov_gst + ov_discount),0)) as cur_credit
@@ -31,6 +31,7 @@ function read_supplier_details_to_trans(&$supp_trans, $supplier_id)
 
     $supp_trans->supplier_id = $supplier_id;
     $supp_trans->tax_included = $myrow['tax_included'];
+    $supp_trans->tax_algorithm = $supp_trans->stored_algorithm = $myrow['tax_algorithm'];
     $supp_trans->supplier_name = $myrow['supp_name'];
        $supp_trans->terms = array( 
                'description' => $myrow['terms'],
@@ -129,7 +130,7 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
        $tax_total += $taxes[$n]['Value'];
     }
 
-       $invoice_items_total = $supp_trans->get_items_total();
+       $invoice_items_total = $supp_trans->get_items_total(); // get total with/without tax dep. on tax_included
 //     $invoice_items_total = $supp_trans->get_total_taxfree($supp_trans->tax_group_id);
 
        if($supp_trans->tax_included==0) {
@@ -156,7 +157,8 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
     /*First insert the invoice into the supp_trans table*/
        $invoice_id = write_supp_trans($trans_type, 0, $supp_trans->supplier_id, $date_, $supp_trans->due_date,
                $supp_trans->reference, $supp_trans->supp_reference,
-               $invoice_items_total, $item_added_tax, $supp_trans->ov_discount, "", 0, $supp_trans->tax_included);
+               $invoice_items_total, $item_added_tax, $supp_trans->ov_discount, "", 0, $supp_trans->tax_included,
+               $supp_trans->tax_algorithm);
 
        $total = 0;
     /* Now the control account */
@@ -411,6 +413,7 @@ function read_supp_invoice($trans_no, $trans_type, &$supp_trans)
                $supp_trans->ov_discount = $trans_row["ov_discount"];
                $supp_trans->ov_gst = $trans_row["ov_gst"];
                $supp_trans->tax_included = $trans_row["tax_included"];
+               $supp_trans->stored_algorithm = $supp_trans->tax_algorithm = $trans_row["tax_algorithm"];
 
                $id = $trans_row["trans_no"];
 
index ebd2c7241e177f0dfe0f576bb7728a60e31520e6..2a88e0cb15bb995e0bedb9ca6da855027bf29b79 100644 (file)
@@ -141,7 +141,7 @@ function read_po_header($order_no, &$order)
        $order->order_no = $order_no;
        
        $order->set_supplier($myrow["supplier_id"], $myrow["supp_name"], $myrow["curr_code"],
-               $myrow['tax_group_id'], $myrow["tax_included"]);
+               $myrow['tax_group_id'], $myrow["tax_included"], $myrow["tax_algorithm"]);
 
                $order->credit = get_current_supp_credit($order->supplier_id);
 
index 5ed21c3a3f407416491a1fec3535ef169061ec86..0574ee51d5885a3cae327383f2aaef144036bd05 100644 (file)
@@ -14,7 +14,7 @@
 //     FIXME - this should be revised for transaction update case.
 //
 function write_supp_trans($type, $trans_no, $supplier_id, $date_, $due_date, $reference, $supp_reference,
-       $amount, $amount_tax, $discount, $err_msg="", $rate=0, $included=0)
+       $amount, $amount_tax, $discount, $err_msg="", $rate=0, $included=0, $tax_algorithm = null)
 {
        $new = $trans_no==0;
        $date = date2sql($date_);
@@ -33,11 +33,12 @@ function write_supp_trans($type, $trans_no, $supplier_id, $date_, $due_date, $re
 
 
        $sql = "INSERT INTO ".TB_PREF."supp_trans (trans_no, type, supplier_id, tran_date, due_date,
-               reference, supp_reference, ov_amount, ov_gst, rate, ov_discount, tax_included) ";
+               reference, supp_reference, ov_amount, ov_gst, rate, ov_discount, tax_included, tax_algorithm) ";
        $sql .= "VALUES (".db_escape($trans_no).", ".db_escape($type)
        .", ".db_escape($supplier_id).", '$date', '$due_date',
                ".db_escape($reference).", ".db_escape($supp_reference).", ".db_escape($amount)
-               .", ".db_escape($amount_tax).", ".db_escape($rate).", ".db_escape($discount).", ".db_escape($included).")";
+               .", ".db_escape($amount_tax).", ".db_escape($rate).", ".db_escape($discount).", ".db_escape($included).
+               ", ".db_escape($tax_algorithm, true).")";
 
        if ($err_msg == "")
                $err_msg = "Cannot insert a supplier transaction record";
index 8a0fdca84f85ba4bbfec1535aba0c7fef52d1874..04228e9d5a721dd5fc5a12fc22421d0662fb6cee 100644 (file)
 function add_supplier($supp_name, $supp_ref, $address, $supp_address, $gst_no,
        $website, $supp_account_no, $bank_account, $credit_limit, $dimension_id, $dimension2_id, 
        $curr_code, $payment_terms, $payable_account, $purchase_account, $payment_discount_account, 
-       $notes, $tax_group_id, $tax_included)
+       $notes, $tax_group_id, $tax_included, $tax_algorithm)
 {
        $sql = "INSERT INTO ".TB_PREF."suppliers (supp_name, supp_ref, address, supp_address, gst_no, website,
                supp_account_no, bank_account, credit_limit, dimension_id, dimension2_id, curr_code,
                payment_terms, payable_account, purchase_account, payment_discount_account, notes, 
-               tax_group_id, tax_included)
+               tax_group_id, tax_included, tax_algorithm)
                VALUES (".db_escape($_POST['supp_name']). ", "
                .db_escape($supp_ref). ", "
                .db_escape($address) . ", "
@@ -37,7 +37,8 @@ function add_supplier($supp_name, $supp_ref, $address, $supp_address, $gst_no,
                .db_escape($payment_discount_account). ", "
                .db_escape($notes). ", "
                .db_escape($tax_group_id). ", "
-               .db_escape($tax_included). ")";
+               .db_escape($tax_included). ", "
+               .db_escape($tax_algorithm). ")";
 
        db_query($sql,"The supplier could not be added");
 }
@@ -45,7 +46,7 @@ function add_supplier($supp_name, $supp_ref, $address, $supp_address, $gst_no,
 function update_supplier($supplier_id, $supp_name, $supp_ref, $address, $supp_address, $gst_no, 
        $website, $supp_account_no, $bank_account, $credit_limit, $dimension_id, $dimension2_id, 
        $curr_code, $payment_terms, $payable_account, $purchase_account, $payment_discount_account, 
-       $notes, $tax_group_id, $tax_included)
+       $notes, $tax_group_id, $tax_included, $tax_algorithm)
 {
        $sql = "UPDATE ".TB_PREF."suppliers SET supp_name=".db_escape($supp_name) . ",
                supp_ref=".db_escape($supp_ref) . ",
@@ -65,7 +66,8 @@ function update_supplier($supplier_id, $supp_name, $supp_ref, $address, $supp_ad
                payment_discount_account=".db_escape($payment_discount_account) . ",
                notes=".db_escape($notes) . ",
                tax_group_id=".db_escape($tax_group_id). ",
-               tax_included=".db_escape($tax_included)
+               tax_included=".db_escape($tax_included).",
+               tax_algorithm=".db_escape($tax_algorithm)
                ." WHERE supplier_id = ".db_escape($supplier_id);
 
        db_query($sql,"The supplier could not be updated");
@@ -115,7 +117,7 @@ function get_supplier_details($supplier_id, $to=null)
                          ".TB_PREF."payment_terms.days_before_due,
                          ".TB_PREF."payment_terms.day_in_following_month";
 
-    $result = db_query($sql,"The customer details could not be retrieved");
+    $result = db_query($sql,"The supplier details could not be retrieved");
     $supp = db_fetch($result);
 
     return $supp;
@@ -173,4 +175,12 @@ function is_new_supplier($id)
 
        return !key_in_foreign_table($id, $tables, 'supplier_id');
 }
+
+function update_supp_tax_algorithm($supplier_id, $tax_algorithm)
+{
+       $sql = "UPDATE ".TB_PREF."suppliers SET tax_algorithm=".db_escape($tax_algorithm)
+               . " WHERE supplier_id=".db_escape($supplier_id);
+       return db_query($sql, "cannot update supplier's tax calculation algorithm");
+}
+
 ?>
\ No newline at end of file
index b767e9ea9261f66e9ee64245c4f9067bae4bd475..840569271349a309d4a6184fcedf351417bd8a98 100644 (file)
@@ -32,6 +32,8 @@ class purch_order
        var $tax_group_id;
        var $tax_group_array = null; // saves db queries
        var $tax_included; // type of prices
+       var $tax_algorithm;
+       var $stored_algorithm; // copy to find the change after invoice in processed
        var $terms;
        
        var $reference;
@@ -43,13 +45,14 @@ class purch_order
                $this->lines_on_order = $this->order_no = $this->supplier_id = 0;
        }
        
-       function set_supplier($supplier_id, $supplier_name, $curr_code, $tax_group_id, $tax_included)
+       function set_supplier($supplier_id, $supplier_name, $curr_code, $tax_group_id, $tax_included, $tax_algorithm)
        {
                $this->supplier_id = $supplier_id;
                $this->supplier_name = $supplier_name;
                $this->curr_code = $curr_code;
                $this->tax_group_id = $tax_group_id;
                $this->tax_included = $tax_included;
+               $this->stored_algorithm = $this->tax_algorithm = $tax_algorithm;
                $this->tax_group_array = get_tax_group_items_as_array($tax_group_id);
        }
        
@@ -141,13 +144,11 @@ class purch_order
                        $prices[] = round($ln_itm->price * ($receival ? $ln_itm->receive_qty : $ln_itm->quantity),  user_price_dec());
                }
                $taxes = get_tax_for_items($items, $prices, $shipping_cost,
-                 $this->tax_group_id, $this->tax_included,  $this->tax_group_array);
-
+                 $this->tax_group_id, $this->tax_included,  $this->tax_group_array, $this->tax_algorithm);
        // Adjustment for swiss franken, we always have 5 rappen = 1/20 franken
            if ($this->curr_code == 'CHF') {
                        $val = $taxes['1']['Value'];
-                       $val1 = (floatval((intval(round(($val*20),0)))/20));
-                       $taxes['1']['Value'] = $val1;
+                       $taxes['1']['Value'] = (floatval((intval(round(($val*20),0)))/20));
                } 
                return $taxes;
        }
@@ -169,7 +170,7 @@ class purch_order
 
                if (!$this->tax_included ) {
                        $taxes = get_tax_for_items($items, $prices, 0, $this->tax_group_id,
-                       $this->tax_included,  $this->tax_group_array);
+                       $this->tax_included,  $this->tax_group_array, $this->tax_algorithm);
 
                        foreach($taxes as $tax)
                                $total += round($tax['Value'], $dec);
index 130354d7142516a28303246d7f4e9cf7fd2da9da..f95a78258e4fc8f4222fbd2c1e3fb9c4e4c27914 100644 (file)
@@ -40,6 +40,8 @@ class supp_trans
        var $ov_gst;
        var $gl_codes_counter=0;
        var $credit = 0;
+       var $tax_algorithm;
+       var $stored_algorithm;
 
        function supp_trans($trans_type)
        {
@@ -114,7 +116,7 @@ class supp_trans
         if ($tax_group_id == null)
                $tax_group_id = $this->tax_group_id;
         $taxes = get_tax_for_items($items, $prices, $shipping_cost, $tax_group_id, 
-               $this->tax_included);
+               $this->tax_included, null, $this->tax_algorithm);
 
 ///////////////// Joe Hunt 2009.08.18
 
@@ -156,7 +158,9 @@ class supp_trans
                }       
                return $total;
     }
-
+       //
+       //      Returns transaction total 
+       //
        function get_items_total()
        {
                $total = 0;
index dd2d8ed6dded0be050ac56c1c16db95cd6901714..bbd9300a5957954ae7c5e03798fb52690316406c 100644 (file)
@@ -19,6 +19,7 @@ function copy_from_trans(&$supp_trans)
        $_POST['supp_reference'] = $supp_trans->supp_reference;
        $_POST['reference'] = $supp_trans->reference;
        $_POST['supplier_id'] = $supp_trans->supplier_id;
+       $_POST['tax_algorithm'] = $supp_trans->tax_algorithm;
 }
 
 //--------------------------------------------------------------------------------------------------
@@ -30,6 +31,8 @@ function copy_to_trans(&$supp_trans)
        $supp_trans->due_date = $_POST['due_date'];
        $supp_trans->supp_reference = $_POST['supp_reference'];
        $supp_trans->reference = $_POST['reference'];
+       if (isset($_POST['tax_algorithm']))
+           $supp_trans->tax_algorithm = $_POST['tax_algorithm'];
 
        $supp_trans->ov_amount = 0; /* for starters */
        if (count($supp_trans->grn_items) > 0)
@@ -137,10 +140,13 @@ function invoice_header(&$supp_trans)
 
 function invoice_totals(&$supp_trans)
 {
+       global $Ajax;
+
        copy_to_trans($supp_trans);
 
        $dim = get_company_pref('use_dimension');
        $colspan = ($dim == 2 ? 7 : ($dim == 1 ? 6 : 5));
+       div_start('tax_table');
        start_table(TABLESTYLE, "width=95%");
        label_row(_("Sub-total:"), price_format( $supp_trans->ov_amount), "colspan=$colspan align=right", "align=right");
 
@@ -158,8 +164,14 @@ function invoice_totals(&$supp_trans)
 
     end_table(1);
     start_table(TABLESTYLE2);
+    tax_algorithm_list_row(_("Tax algorithm:"), 'tax_algorithm', null, true);
+       if (list_updated('tax_algorithm')) {
+               $supp_trans->tax_algorithm = $_POST['tax_algorithm'];
+           $Ajax->activate('tax_table');
+       }
     textarea_row(_("Memo:"), "Comments", null, 50, 3);
     end_table(1);
+       div_end();
 }
 
 //--------------------------------------------------------------------------------------------------
index a2c2b450b15a1e0dd001a9156cbc3eb4ab76c7ac..ca34975ea5a96765756e3fc7d5b23441a8c1e690 100644 (file)
@@ -29,7 +29,7 @@ $supplier_id = get_post('supplier_id');
 //--------------------------------------------------------------------------------------------
 function supplier_settings(&$supplier_id)
 {
-
+       
        start_outer_table(TABLESTYLE2);
 
        table_section(1);
@@ -55,6 +55,7 @@ function supplier_settings(&$supplier_id)
                $_POST['credit_limit']  = price_format($myrow["credit_limit"]);
                $_POST['tax_group_id'] = $myrow["tax_group_id"];
                $_POST['tax_included'] = $myrow["tax_included"];
+               $_POST['tax_algorithm'] = $myrow["tax_algorithm"];
                $_POST['payable_account']  = $myrow["payable_account"];
                $_POST['purchase_account']  = $myrow["purchase_account"];
                $_POST['payment_discount_account'] = $myrow["payment_discount_account"];
@@ -68,6 +69,7 @@ function supplier_settings(&$supplier_id)
                $_POST['dimension_id'] = 0;
                $_POST['dimension2_id'] = 0;
                $_POST['tax_included'] = 0;
+               $_POST['tax_algorithm'] = 1;
                $_POST['sales_type'] = -1;
                $_POST['gst_no'] = $_POST['bank_account'] = '';
                $_POST['payment_terms']  = '';
@@ -114,7 +116,7 @@ function supplier_settings(&$supplier_id)
        payment_terms_list_row(_("Payment Terms:"), 'payment_terms', null);
        //
        // tax_included option from supplier record is used directly in update_average_cost() function,
-       // therefore we can't edit the option after any transaction waas done for the supplier.
+       // therefore we can't edit the option after any transaction was done for the supplier.
        //
        if (is_new_supplier($supplier_id))
                check_row(_("Prices contain tax included:"), 'tax_included');
@@ -122,13 +124,17 @@ function supplier_settings(&$supplier_id)
                hidden('tax_included');
                label_row(_("Prices contain tax included:"), $_POST['tax_included'] ? _('Yes') : _('No'));
        }
+       tax_algorithm_list_row(_("Tax Calculation Algorithm:"), 'tax_algorithm', null);
+
+       if (!$supplier_id) table_section(2);
+
        table_section_title(_("Accounts"));
        gl_all_accounts_list_row(_("Accounts Payable Account:"), 'payable_account', $_POST['payable_account']);
        gl_all_accounts_list_row(_("Purchase Account:"), 'purchase_account', $_POST['purchase_account'],
                false, false, _("Use Item Inventory/COGS Account"));
        gl_all_accounts_list_row(_("Purchase Discount Account:"), 'payment_discount_account', $_POST['payment_discount_account']);
 
-       table_section(2);
+       if ($supplier_id) table_section(2);
        $dim = get_company_pref('use_dimension');
        if ($dim >= 1)
        {
@@ -206,7 +212,7 @@ if (isset($_POST['submit']))
                                $_POST['website'], $_POST['supp_account_no'], $_POST['bank_account'], 
                                input_num('credit_limit', 0), $_POST['dimension_id'], $_POST['dimension2_id'], $_POST['curr_code'],
                                $_POST['payment_terms'], $_POST['payable_account'], $_POST['purchase_account'], $_POST['payment_discount_account'],
-                               $_POST['notes'], $_POST['tax_group_id'], @$_POST['tax_included']);
+                               $_POST['notes'], $_POST['tax_group_id'], @$_POST['tax_included'], get_post('tax_algorithm'));
                        update_record_status($_POST['supplier_id'], $_POST['inactive'],
                                'suppliers', 'supplier_id');
 
@@ -219,7 +225,7 @@ if (isset($_POST['submit']))
                                $_POST['gst_no'], $_POST['website'], $_POST['supp_account_no'], $_POST['bank_account'], 
                                input_num('credit_limit',0), $_POST['dimension_id'], $_POST['dimension2_id'],
                                $_POST['curr_code'], $_POST['payment_terms'], $_POST['payable_account'], $_POST['purchase_account'],
-                               $_POST['payment_discount_account'], $_POST['notes'], $_POST['tax_group_id'], check_value('tax_included'));
+                               $_POST['payment_discount_account'], $_POST['notes'], $_POST['tax_group_id'], check_value('tax_included'), get_post('tax_algorithm'));
 
                        $supplier_id = $_POST['supplier_id'] = db_insert_id();
 
index b0434e68d3670ebd64b78dfc4125d84007d1d106..28fb7215d5d8be8ef9fbed31207b23242e278625 100644 (file)
@@ -199,8 +199,12 @@ function handle_commit_invoice()
 
        if (!check_data())
                return;
+       $inv = $_SESSION['supp_trans'];
+       $invoice_no = add_supp_invoice($inv);
 
-       $invoice_no = add_supp_invoice($_SESSION['supp_trans']);
+       // presume supplier data need correction
+       if ($inv->stored_algorithm != $inv->tax_algorithm)
+               update_supp_tax_algorithm($inv->supplier_id, $inv->tax_algorithm);
 
     $_SESSION['supp_trans']->clear_items();
     unset($_SESSION['supp_trans']);
@@ -219,6 +223,7 @@ function check_item_data($n)
 {
        global $check_price_charged_vs_order_price,
                $check_qty_charged_vs_del_qty, $SysPrefs;
+
        if (!check_num('this_quantity_inv'.$n, 0) || input_num('this_quantity_inv'.$n)==0)
        {
                display_error( _("The quantity to invoice must be numeric and greater than zero."));
index 1d297de54d503f21c47c780e9e1cbeb6a853fef9..500263c835665b2990a739b30dacaa806cce7e93 100644 (file)
@@ -34,7 +34,7 @@ print_po();
 function get_po($order_no)
 {
        $sql = "SELECT ".TB_PREF."purch_orders.*, ".TB_PREF."suppliers.supp_name,  "
-               .TB_PREF."suppliers.supp_account_no,".TB_PREF."suppliers.tax_included,
+               .TB_PREF."suppliers.supp_account_no,".TB_PREF."suppliers.tax_included,".TB_PREF."suppliers.tax_algorithm,
                ".TB_PREF."suppliers.curr_code, ".TB_PREF."suppliers.payment_terms, ".TB_PREF."locations.location_name,
                ".TB_PREF."suppliers.address, ".TB_PREF."suppliers.contact, ".TB_PREF."suppliers.tax_group_id
                FROM ".TB_PREF."purch_orders, ".TB_PREF."suppliers, ".TB_PREF."locations
@@ -168,7 +168,7 @@ function print_po()
                $rep->NewLine();
 
                $tax_items = get_tax_for_items($items, $prices, 0,
-                 $myrow['tax_group_id'], $myrow['tax_included'],  null);
+                 $myrow['tax_group_id'], $myrow['tax_included'],  null, $myrow['tax_algorithm']);
                $first = true;
                foreach($tax_items as $tax_item)
                {
index c822b4b128f00f571a1d83e016244993fc19d405..5231bad925b6706fd1b9a130d46b6d377159f86a 100644 (file)
@@ -1877,6 +1877,7 @@ CREATE TABLE IF NOT EXISTS `0_suppliers` (
   `curr_code` char(3) default NULL,
   `payment_terms` int(11) default NULL,
   `tax_included` tinyint(1) NOT NULL default '0',
+  `tax_algorithm` tinyint(1) NOT NULL default '1',
   `dimension_id` int(11) default '0',
   `dimension2_id` int(11) default '0',
   `tax_group_id` int(11) default NULL,
@@ -1894,9 +1895,9 @@ CREATE TABLE IF NOT EXISTS `0_suppliers` (
 -- Dumping data for table `0_suppliers`
 --
 
-INSERT INTO `0_suppliers` VALUES(1, 'Junk Beer ApS', 'Junk Beer', 'Mailing 1\nMailing 2\nMailing 3', 'Address 1\nAddress 2\nAddress 3', '123456', 'Contact', '111', '', '', 'DKK', 3, 0, 1, 0, 2, 1000, '', '2100', '5060', 'A supplier with junk beers.', 0);
-INSERT INTO `0_suppliers` VALUES(2, 'Lucky Luke Inc.', 'Lucky Luke', 'Mailing 1\nMailing 2\nMailing 3', 'Address 1\nAddress 2\nAddress 3', '654321', 'Luke', '333', '', '', 'USD', 3, 0, 0, 0, 1, 500, '', '2100', '5060', '', 0);
-INSERT INTO `0_suppliers` VALUES(3, 'Money Makers Ltd.', 'Money Makers', 'Mailing 1\nMailing 2\nMailing 3', 'Address 1\nAddress 2\nAddress 3', '987654', 'Makers', '222', '', '', 'GBP', 3, 0, 0, 0, 2, 300, '', '2100', '5060', '', 0);
+INSERT INTO `0_suppliers` VALUES(1, 'Junk Beer ApS', 'Junk Beer', 'Mailing 1\nMailing 2\nMailing 3', 'Address 1\nAddress 2\nAddress 3', '123456', 'Contact', '111', '', '', 'DKK', 3, 0, 1, 1, 0, 2, 1000, '', '2100', '5060', 'A supplier with junk beers.', 0);
+INSERT INTO `0_suppliers` VALUES(2, 'Lucky Luke Inc.', 'Lucky Luke', 'Mailing 1\nMailing 2\nMailing 3', 'Address 1\nAddress 2\nAddress 3', '654321', 'Luke', '333', '', '', 'USD', 3, 0, 1, 0, 0, 1, 500, '', '2100', '5060', '', 0);
+INSERT INTO `0_suppliers` VALUES(3, 'Money Makers Ltd.', 'Money Makers', 'Mailing 1\nMailing 2\nMailing 3', 'Address 1\nAddress 2\nAddress 3', '987654', 'Makers', '222', '', '', 'GBP', 3, 0, 1, 0, 0, 2, 300, '', '2100', '5060', '', 0);
 
 -- --------------------------------------------------------
 
@@ -1979,6 +1980,7 @@ CREATE TABLE IF NOT EXISTS `0_supp_trans` (
   `rate` double NOT NULL default '1',
   `alloc` double NOT NULL default '0',
   `tax_included` tinyint(1) NOT NULL default '0',
+  `tax_algorihm` tinyint(1) NOT NULL default '1',
   PRIMARY KEY  (`type`,`trans_no`),
   KEY `supplier_id` (`supplier_id`),
   KEY `SupplierID_2` (`supplier_id`,`supp_reference`),
@@ -1990,9 +1992,9 @@ CREATE TABLE IF NOT EXISTS `0_supp_trans` (
 -- Dumping data for table `0_supp_trans`
 --
 
-INSERT INTO `0_supp_trans` VALUES(7, 20, 2, '1', '5t', '2009-06-21', '2009-07-01', 3300, 0, 165, 1, 3465, 0);
-INSERT INTO `0_supp_trans` VALUES(8, 20, 2, '2', 'cc', '2009-06-21', '2009-07-01', 20, 0, 0, 1, 0, 0);
-INSERT INTO `0_supp_trans` VALUES(4, 22, 2, '1', '', '2009-06-21', '2009-06-21', -3465, 0, 0, 1, 3465, 0);
+INSERT INTO `0_supp_trans` VALUES(7, 20, 2, '1', '5t', '2009-06-21', '2009-07-01', 3300, 0, 165, 1, 3465, 0, 1);
+INSERT INTO `0_supp_trans` VALUES(8, 20, 2, '2', 'cc', '2009-06-21', '2009-07-01', 20, 0, 0, 1, 0, 0, 1);
+INSERT INTO `0_supp_trans` VALUES(4, 22, 2, '1', '', '2009-06-21', '2009-06-21', -3465, 0, 0, 1, 3465, 0, 1);
 
 -- --------------------------------------------------------
 
@@ -2042,6 +2044,7 @@ INSERT INTO `0_sys_prefs` VALUES('profit_loss_year_act', 'glsetup.general', 'var
 INSERT INTO `0_sys_prefs` VALUES('retained_earnings_act', 'glsetup.general', 'varchar', 15, '3590');
 INSERT INTO `0_sys_prefs` VALUES('bank_charge_act', 'glsetup.general', 'varchar', 15, '5690');
 INSERT INTO `0_sys_prefs` VALUES('exchange_diff_act', 'glsetup.general', 'varchar', 15, '4450');
+INSERT INTO `0_sys_prefs` VALUES('tax_algorithm', 'glsetup.general', 'tinyint', 1, '1');
 INSERT INTO `0_sys_prefs` VALUES('default_credit_limit', 'glsetup.customer', 'int', 11, '1000');
 INSERT INTO `0_sys_prefs` VALUES('accumulate_shipping', 'glsetup.customer', 'tinyint', 1, '0');
 INSERT INTO `0_sys_prefs` VALUES('legal_text', 'glsetup.customer', 'tinytext', 0, '');
index e904c2bae20b06e6683e4283ea1cac4ab2758e98..bc1f4ce1552d6ab19c28bb251de4696e45816d07 100644 (file)
@@ -1648,6 +1648,7 @@ CREATE TABLE IF NOT EXISTS `0_suppliers` (
   `curr_code` char(3) default NULL,
   `payment_terms` int(11) default NULL,
   `tax_included` tinyint(1) NOT NULL default '0',
+  `tax_algorithm` tinyint(1) NOT NULL default '1',
   `dimension_id` int(11) default '0',
   `dimension2_id` int(11) default '0',
   `tax_group_id` int(11) default NULL,
@@ -1741,6 +1742,7 @@ CREATE TABLE IF NOT EXISTS `0_supp_trans` (
   `rate` double NOT NULL default '1',
   `alloc` double NOT NULL default '0',
   `tax_included` tinyint(1) NOT NULL default '0',
+  `tax_algorihm` tinyint(1) NOT NULL default '1',
   PRIMARY KEY  (`type`,`trans_no`),
   KEY `supplier_id` (`supplier_id`),
   KEY `SupplierID_2` (`supplier_id`,`supp_reference`),
@@ -1801,6 +1803,7 @@ INSERT INTO `0_sys_prefs` VALUES('profit_loss_year_act', 'glsetup.general', 'var
 INSERT INTO `0_sys_prefs` VALUES('retained_earnings_act', 'glsetup.general', 'varchar', 15, '3590');
 INSERT INTO `0_sys_prefs` VALUES('bank_charge_act', 'glsetup.general', 'varchar', 15, '5690');
 INSERT INTO `0_sys_prefs` VALUES('exchange_diff_act', 'glsetup.general', 'varchar', 15, '4450');
+INSERT INTO `0_sys_prefs` VALUES('tax_algorithm', 'glsetup.general', 'tinyint', 1, '1');
 INSERT INTO `0_sys_prefs` VALUES('default_credit_limit', 'glsetup.customer', 'int', 11, '1000');
 INSERT INTO `0_sys_prefs` VALUES('accumulate_shipping', 'glsetup.customer', 'tinyint', 1, '0');
 INSERT INTO `0_sys_prefs` VALUES('legal_text', 'glsetup.customer', 'tinytext', 0, '');
index 3f54bd226aa65ee6871e86e30598f11f57d9ec37..6e721da7e53f13ca839626d08676beaeefe773b4 100644 (file)
@@ -140,14 +140,17 @@ function get_taxes_for_item($stock_id, $tax_group_items_array)
 //-----------------------------------------------------------------------------------
 // return an array of (tax_type_id, tax_type_name, sales_gl_code, purchasing_gl_code, rate, included_in_price, Value) 
 
-function get_tax_for_items($items, $prices, $shipping_cost, $tax_group, $tax_included=null, $tax_items_array=null)
+function get_tax_for_items($items, $prices, $shipping_cost, $tax_group, $tax_included=null, $tax_items_array=null, $tax_algorithm = null)
 {
+       if (!$tax_algorithm)
+               $tax_algorithm = get_company_pref('tax_algorithm');
        // first create and set an array with all the tax types of the tax group
        if($tax_items_array!=null)
          $ret_tax_array = $tax_items_array;
        else
          $ret_tax_array = get_tax_group_items_as_array($tax_group);
 
+       $dec = user_price_dec();
        foreach($ret_tax_array as $k=>$t)
                $ret_tax_array[$k]['Net'] = 0;
 
@@ -163,10 +166,10 @@ 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'] += round2($nprice * $item_tax['rate'] / 100, $dec);
                                  $ret_tax_array[$index]['Net'] += $nprice;
                                } else {
-                                 $ret_tax_array[$index]['Value'] += ($prices[$i] * $item_tax['rate'] / 100);
+                                 $ret_tax_array[$index]['Value'] += round2($prices[$i] * $item_tax['rate'] / 100, $dec);
                                  $ret_tax_array[$index]['Net'] += $prices[$i];
                                }
                        }
@@ -189,25 +192,30 @@ function get_tax_for_items($items, $prices, $shipping_cost, $tax_group, $tax_inc
                                                $tax_rate += $item_tax['rate'];
                                        }
                                }
-                               $shipping_net = round2($shipping_cost / (1 + ($tax_rate / 100)), user_price_dec());
+                               $shipping_net = round2($shipping_cost / (1 + ($tax_rate / 100)), $dec);
                        }       
                        foreach ($item_taxes as $item_tax) 
                        {
                                $index = $item_tax['tax_type_id'];
                                if(isset($ret_tax_array[$index])) {
                                  if($tax_included==1) {// 2008-11-26 Joe Hunt Taxes are stored without roundings
-                                       $ret_tax_array[$index]['Value'] += ($shipping_net * $item_tax['rate'] / 100);
+                                       $ret_tax_array[$index]['Value'] += round2($shipping_net * $item_tax['rate'] / 100, $dec);
                                    $ret_tax_array[$index]['Net'] += $shipping_net;
                                  } else {
-                                       $ret_tax_array[$index]['Value'] += ($shipping_cost * $item_tax['rate'] / 100);
+                                       $ret_tax_array[$index]['Value'] += round2($shipping_cost * $item_tax['rate'] / 100, $dec);
                                    $ret_tax_array[$index]['Net'] += $shipping_cost;
-                                }
+                                 }
                                }
                        }
                }
        }
        
-       //print_r($ret_tax_array);
+       if ($tax_algorithm == TCA_TOTALS ) {
+               // update taxes with 
+               foreach($ret_tax_array as $index => $item_tax) {
+                       $ret_tax_array[$index]['Value'] = round2($item_tax['Net'] * $item_tax['rate'] / 100, $dec);
+               }
+       }
 
        return $ret_tax_array;
 }