From 21a54e6ceb2149d31ab7738af30f731f6fe10251 Mon Sep 17 00:00:00 2001 From: Joe Hunt Date: Fri, 5 Dec 2008 22:56:57 +0000 Subject: [PATCH] [0000095] Bad behaviour of routine number_format and round in Windows. --- CHANGELOG.txt | 15 +++++++++++++-- gl/includes/db/gl_db_trans.inc | 6 +++--- includes/banking.inc | 6 +++--- includes/current_user.inc | 9 ++++++++- purchasing/includes/ui/invoice_ui.inc | 10 +++++----- reporting/rep107.php | 2 +- reporting/rep109.php | 2 +- reporting/rep110.php | 2 +- reporting/rep209.php | 2 +- sales/view/view_credit.php | 2 +- sales/view/view_dispatch.php | 2 +- sales/view/view_invoice.php | 2 +- sales/view/view_sales_order.php | 2 +- 13 files changed, 40 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index e0d9c2f6..5561b425 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -20,8 +20,19 @@ Legend: $ -> Affected files 05-Dec-2008 Joe Hunt -# [0000095] Bad behaviour of routine number_format in Windows. -$ /includes/current_user.inc +# [0000095] Bad behaviour of routine number_format and round in Windows. +$ /gl/includes/gl_db_trans.inc + /includes/banking.inc + /includes/current_user.inc + /purchasing/includes/ui/invoice_ui.inc + /reporting/rep107.php + /reporting/rep109.php + /reporting/rep110.php + /reporting/rep209.php + /sales/view/view_credit.php + /sales/view/view_dispatch.php + /sales/view/view_invoice.php + /sales/view/view_sales_order.php 04-Dec-2008 Janusz Dobrowolski # Check write permissions and create per company subdirectory structure. diff --git a/gl/includes/db/gl_db_trans.inc b/gl/includes/db/gl_db_trans.inc index c681a0fb..60b173d6 100644 --- a/gl/includes/db/gl_db_trans.inc +++ b/gl/includes/db/gl_db_trans.inc @@ -18,10 +18,10 @@ function add_gl_trans($type, $trans_id, $date_, $account, $dimension, $dimension if ($rate == 0) $amount_in_home_currency = to_home_currency($amount, $currency, $date_); else - $amount_in_home_currency = round($amount * $rate, user_price_dec()); + $amount_in_home_currency = round2($amount * $rate, user_price_dec()); } else - $amount_in_home_currency = round($amount, user_price_dec()); + $amount_in_home_currency = round2($amount, user_price_dec()); if ($dimension == null || $dimension < 0) $dimension = 0; if ($dimension2 == null || $dimension2 < 0) @@ -75,7 +75,7 @@ function add_gl_trans_std_cost($type, $trans_id, $date_, $account, $dimension, $ // Function for even out rounding problems function add_gl_balance($type, $trans_id, $date_, $amount, $person_type_id=null, $person_id=null) { - $amount = round($amount, user_price_dec()); + $amount = round2($amount, user_price_dec()); if ($amount != 0) return add_gl_trans($type, $trans_id, $date_, get_company_pref('exchange_diff_act'), 0, 0, "", $amount, null, $person_type_id, $person_id, "The balanced GL transaction could not be inserted"); diff --git a/includes/banking.inc b/includes/banking.inc index d26bbbbd..a86551df 100644 --- a/includes/banking.inc +++ b/includes/banking.inc @@ -105,7 +105,7 @@ function get_exchange_rate_to_home_currency($currency_code, $date_) function to_home_currency($amount, $currency_code, $date_) { $ex_rate = get_exchange_rate_to_home_currency($currency_code, $date_); - return round($amount / $ex_rate, user_price_dec()); + return round2($amount / $ex_rate, user_price_dec()); } //---------------------------------------------------------------------------------- @@ -163,8 +163,8 @@ function exchange_variation($pyt_type, $pyt_no, $type, $trans_no, $pyt_date, $am } if (is_company_currency($curr)) return; - $inv_amt = round($amount * $trans['rate'], user_price_dec()); - $pay_amt = round($amount * $pyt_trans['rate'], user_price_dec()); + $inv_amt = round2($amount * $trans['rate'], user_price_dec()); + $pay_amt = round2($amount * $pyt_trans['rate'], user_price_dec()); if ($inv_amt != $pay_amt) { $diff = $inv_amt - $pay_amt; diff --git a/includes/current_user.inc b/includes/current_user.inc index bd4b36ee..397347a0 100644 --- a/includes/current_user.inc +++ b/includes/current_user.inc @@ -108,13 +108,20 @@ class current_user //-------------------------------------------------------------------------- +function round2($number, $decimals=0) +{ + $delta = ($number < 0 ? -.000001 : .000001); + return round($number+$delta, $decimals); +} + function number_format2($number, $decimals=0) { global $thoseps, $decseps; $tsep = $thoseps[$_SESSION["wa_current_user"]->prefs->tho_sep()]; $dsep = $decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()]; //return number_format($number, $decimals, $dsep, $tsep); - return number_format($number+.000001, $decimals, $dsep, $tsep); + $delta = ($number < 0 ? -.000001 : .000001); + return number_format($number+$delta, $decimals, $dsep, $tsep); } function price_format($number) { diff --git a/purchasing/includes/ui/invoice_ui.inc b/purchasing/includes/ui/invoice_ui.inc index d72b0dcb..88fcfd59 100644 --- a/purchasing/includes/ui/invoice_ui.inc +++ b/purchasing/includes/ui/invoice_ui.inc @@ -27,7 +27,7 @@ function copy_to_trans(&$supp_trans) { foreach ( $supp_trans->grn_items as $grn) { - $supp_trans->ov_amount += round(($grn->this_quantity_inv * $grn->chg_price), + $supp_trans->ov_amount += round2(($grn->this_quantity_inv * $grn->chg_price), user_price_dec()); } } @@ -344,9 +344,9 @@ function display_grn_items_for_selection(&$supp_trans, $k) null, null, $dec); amount_cells(null, 'ChgPrice'.$n, price_format($myrow["unit_price"])); if ($supp_trans->is_invoice) - amount_cell(round($myrow["unit_price"] * ($myrow["qty_recd"] - $myrow["quantity_inv"]), user_price_dec())); + amount_cell(round2($myrow["unit_price"] * ($myrow["qty_recd"] - $myrow["quantity_inv"]), user_price_dec())); else - amount_cell(round($myrow["unit_price"] * max($myrow['quantity_inv'], 0), user_price_dec())); + amount_cell(round2($myrow["unit_price"] * max($myrow['quantity_inv'], 0), user_price_dec())); if ($supp_trans->is_invoice) submit_cells('grn_item_id'.$n, _("Add"), '', _("Add to Invoice"), true); else @@ -460,7 +460,7 @@ function display_grn_items(&$supp_trans, $mode=0) } qty_cell(abs($entered_grn->this_quantity_inv), true, $dec); amount_cell($entered_grn->chg_price); - amount_cell( round($entered_grn->chg_price * abs($entered_grn->this_quantity_inv), user_price_dec()), true); + amount_cell( round2($entered_grn->chg_price * abs($entered_grn->this_quantity_inv), user_price_dec()), true); if ($mode == 1) { @@ -470,7 +470,7 @@ function display_grn_items(&$supp_trans, $mode=0) } end_row(); - $total_grn_value += round($entered_grn->chg_price * abs($entered_grn->this_quantity_inv), + $total_grn_value += round2($entered_grn->chg_price * abs($entered_grn->this_quantity_inv), user_price_dec()); $i++; diff --git a/reporting/rep107.php b/reporting/rep107.php index 1288e517..05b28c3f 100644 --- a/reporting/rep107.php +++ b/reporting/rep107.php @@ -104,7 +104,7 @@ function print_invoices() $SubTotal = 0; while ($myrow2=db_fetch($result)) { - $Net = round($sign * ((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]), + $Net = round2($sign * ((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]), user_price_dec()); $SubTotal += $Net; $DisplayPrice = number_format2($myrow2["unit_price"],$dec); diff --git a/reporting/rep109.php b/reporting/rep109.php index e9be5d76..da7643bc 100644 --- a/reporting/rep109.php +++ b/reporting/rep109.php @@ -92,7 +92,7 @@ function print_sales_orders() $SubTotal = 0; while ($myrow2=db_fetch($result)) { - $Net = round(((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]), + $Net = round2(((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]), user_price_dec()); $SubTotal += $Net; $DisplayPrice = number_format2($myrow2["unit_price"],$dec); diff --git a/reporting/rep110.php b/reporting/rep110.php index 659a4a49..3ec50038 100644 --- a/reporting/rep110.php +++ b/reporting/rep110.php @@ -83,7 +83,7 @@ function print_deliveries() $SubTotal = 0; while ($myrow2=db_fetch($result)) { - $Net = round(((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]), + $Net = round2(((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]), user_price_dec()); $SubTotal += $Net; $DisplayPrice = number_format2($myrow2["unit_price"],$dec); diff --git a/reporting/rep209.php b/reporting/rep209.php index c6e0240f..47ed4258 100644 --- a/reporting/rep209.php +++ b/reporting/rep209.php @@ -103,7 +103,7 @@ function print_po() $SubTotal = 0; while ($myrow2=db_fetch($result)) { - $Net = round(($myrow2["unit_price"] * $myrow2["quantity_ordered"]), + $Net = round2(($myrow2["unit_price"] * $myrow2["quantity_ordered"]), user_price_dec()); $SubTotal += $Net; $DisplayPrice = number_format2($myrow2["unit_price"],$dec); diff --git a/sales/view/view_credit.php b/sales/view/view_credit.php index f220fca1..05e73eb0 100644 --- a/sales/view/view_credit.php +++ b/sales/view/view_credit.php @@ -90,7 +90,7 @@ if (db_num_rows($result) > 0) alt_table_row_color($k); - $value = round(((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]), + $value = round2(((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]), user_price_dec()); $sub_total += $value; diff --git a/sales/view/view_dispatch.php b/sales/view/view_dispatch.php index 3d7d2e75..9bd01beb 100644 --- a/sales/view/view_dispatch.php +++ b/sales/view/view_dispatch.php @@ -111,7 +111,7 @@ if (db_num_rows($result) > 0) if($myrow2['quantity']==0) continue; alt_table_row_color($k); - $value = round(((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]), + $value = round2(((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]), user_price_dec()); $sub_total += $value; diff --git a/sales/view/view_invoice.php b/sales/view/view_invoice.php index f14e0ced..1d90396b 100644 --- a/sales/view/view_invoice.php +++ b/sales/view/view_invoice.php @@ -111,7 +111,7 @@ if (db_num_rows($result) > 0) if($myrow2["quantity"]==0) continue; alt_table_row_color($k); - $value = round(((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]), + $value = round2(((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]), user_price_dec()); $sub_total += $value; diff --git a/sales/view/view_sales_order.php b/sales/view/view_sales_order.php index 333a1130..782c951d 100644 --- a/sales/view/view_sales_order.php +++ b/sales/view/view_sales_order.php @@ -177,7 +177,7 @@ $k = 0; //row colour counter foreach ($_SESSION['Items']->line_items as $stock_item) { - $line_total = round($stock_item->quantity * $stock_item->price * (1 - $stock_item->discount_percent), + $line_total = round2($stock_item->quantity * $stock_item->price * (1 - $stock_item->discount_percent), user_price_dec()); alt_table_row_color($k); -- 2.30.2