From c696fcc9ed013beb349faca8a5ea95d0632d66b7 Mon Sep 17 00:00:00 2001 From: Janusz Dobrowolski Date: Thu, 6 Jan 2011 20:16:17 +0000 Subject: [PATCH] Added tax calcualtion algorithm selection in sales and purchasing, driven by company/supplier setting. --- admin/gl_setup.php | 5 +++- includes/sysnames.inc | 5 ++++ includes/types.inc | 6 +++++ includes/ui/ui_lists.inc | 34 +++++++++++++++++++++++- purchasing/includes/db/invoice_db.inc | 9 ++++--- purchasing/includes/db/po_db.inc | 2 +- purchasing/includes/db/supp_trans_db.inc | 7 ++--- purchasing/includes/db/suppliers_db.inc | 22 ++++++++++----- purchasing/includes/po_class.inc | 13 ++++----- purchasing/includes/supp_trans_class.inc | 8 ++++-- purchasing/includes/ui/invoice_ui.inc | 12 +++++++++ purchasing/manage/suppliers.php | 16 +++++++---- purchasing/supplier_invoice.php | 7 ++++- reporting/rep209.php | 4 +-- sql/en_US-demo.sql | 15 ++++++----- sql/en_US-new.sql | 3 +++ taxes/tax_calc.inc | 24 +++++++++++------ 17 files changed, 147 insertions(+), 45 deletions(-) diff --git a/admin/gl_setup.php b/admin/gl_setup.php index 79b84ddf..21027474 100644 --- a/admin/gl_setup.php +++ b/admin/gl_setup.php @@ -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")); diff --git a/includes/sysnames.inc b/includes/sysnames.inc index 1b9f6754..67007577 100644 --- a/includes/sysnames.inc +++ b/includes/sysnames.inc @@ -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 diff --git a/includes/types.inc b/includes/types.inc index 79ef8be7..9a437f1b 100644 --- a/includes/types.inc +++ b/includes/types.inc @@ -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 diff --git a/includes/ui/ui_lists.inc b/includes/ui/ui_lists.inc index 14eb5443..0fde17de 100644 --- a/includes/ui/ui_lists.inc +++ b/includes/ui/ui_lists.inc @@ -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 ""; 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 "\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 "$label\n"; + echo ""; + echo tax_algorithm_list($name, $value, $submit_on_change); + echo "\n"; +} + +function tax_algorithm_list_row($label, $name, $value=null, $submit_on_change=false) +{ + echo "$label"; + tax_algorithm_list_cells(null, $name, $value, $submit_on_change); + echo "\n"; +} ?> \ No newline at end of file diff --git a/purchasing/includes/db/invoice_db.inc b/purchasing/includes/db/invoice_db.inc index 06d27cde..344e2b68 100644 --- a/purchasing/includes/db/invoice_db.inc +++ b/purchasing/includes/db/invoice_db.inc @@ -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"]; diff --git a/purchasing/includes/db/po_db.inc b/purchasing/includes/db/po_db.inc index ebd2c724..2a88e0cb 100644 --- a/purchasing/includes/db/po_db.inc +++ b/purchasing/includes/db/po_db.inc @@ -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); diff --git a/purchasing/includes/db/supp_trans_db.inc b/purchasing/includes/db/supp_trans_db.inc index 5ed21c3a..0574ee51 100644 --- a/purchasing/includes/db/supp_trans_db.inc +++ b/purchasing/includes/db/supp_trans_db.inc @@ -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"; diff --git a/purchasing/includes/db/suppliers_db.inc b/purchasing/includes/db/suppliers_db.inc index 8a0fdca8..04228e9d 100644 --- a/purchasing/includes/db/suppliers_db.inc +++ b/purchasing/includes/db/suppliers_db.inc @@ -13,12 +13,12 @@ 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 diff --git a/purchasing/includes/po_class.inc b/purchasing/includes/po_class.inc index b767e9ea..84056927 100644 --- a/purchasing/includes/po_class.inc +++ b/purchasing/includes/po_class.inc @@ -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); diff --git a/purchasing/includes/supp_trans_class.inc b/purchasing/includes/supp_trans_class.inc index 130354d7..f95a7825 100644 --- a/purchasing/includes/supp_trans_class.inc +++ b/purchasing/includes/supp_trans_class.inc @@ -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; diff --git a/purchasing/includes/ui/invoice_ui.inc b/purchasing/includes/ui/invoice_ui.inc index dd2d8ed6..bbd9300a 100644 --- a/purchasing/includes/ui/invoice_ui.inc +++ b/purchasing/includes/ui/invoice_ui.inc @@ -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(); } //-------------------------------------------------------------------------------------------------- diff --git a/purchasing/manage/suppliers.php b/purchasing/manage/suppliers.php index a2c2b450..ca34975e 100644 --- a/purchasing/manage/suppliers.php +++ b/purchasing/manage/suppliers.php @@ -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(); diff --git a/purchasing/supplier_invoice.php b/purchasing/supplier_invoice.php index b0434e68..28fb7215 100644 --- a/purchasing/supplier_invoice.php +++ b/purchasing/supplier_invoice.php @@ -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.")); diff --git a/reporting/rep209.php b/reporting/rep209.php index 1d297de5..500263c8 100644 --- a/reporting/rep209.php +++ b/reporting/rep209.php @@ -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) { diff --git a/sql/en_US-demo.sql b/sql/en_US-demo.sql index c822b4b1..5231bad9 100644 --- a/sql/en_US-demo.sql +++ b/sql/en_US-demo.sql @@ -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, ''); diff --git a/sql/en_US-new.sql b/sql/en_US-new.sql index e904c2ba..bc1f4ce1 100644 --- a/sql/en_US-new.sql +++ b/sql/en_US-new.sql @@ -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, ''); diff --git a/taxes/tax_calc.inc b/taxes/tax_calc.inc index 3f54bd22..6e721da7 100644 --- a/taxes/tax_calc.inc +++ b/taxes/tax_calc.inc @@ -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; } -- 2.30.2