[0000095] Bad behaviour of routine number_format and round in Windows.
authorJoe Hunt <joe.hunt.consulting@gmail.com>
Fri, 5 Dec 2008 22:56:57 +0000 (22:56 +0000)
committerJoe Hunt <joe.hunt.consulting@gmail.com>
Fri, 5 Dec 2008 22:56:57 +0000 (22:56 +0000)
13 files changed:
CHANGELOG.txt
gl/includes/db/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

index e0d9c2f6250dc8959f70b614a70ec32f3afaf2bb..5561b425dcf4acf08d8d74711215ba770cbd3cf3 100644 (file)
@@ -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.
index c681a0fbbc09de9ca08dda7a5f36c892ce5e64a1..60b173d6902b80e5d2e9cf47a4d63117ce0f717b 100644 (file)
@@ -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");
index d26bbbbd7c4c5097ad8970b4914662c1690a4434..a86551df7484067b5fe03ab06137670d3b898d21 100644 (file)
@@ -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;
index bd4b36ee3bb246263d11d179b287cb952c120524..397347a026d50d0951fb107f4528455d29ab1528 100644 (file)
@@ -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) {
index d72b0dcb95ec4d6697b325dab191bab0dc7a6ebf..88fcfd59bbee596a76618bb28e059acc86643a45 100644 (file)
@@ -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++;
index 1288e5173e6a0fa90192d049ee7b7c606978bd90..05b28c3fdaf68f85636d1127c7edbbbcd311e328 100644 (file)
@@ -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);
index e9be5d7669f1d94dbe449de250b67a8ead918352..da7643bc82da9d8dec2de5919c21d32da936043b 100644 (file)
@@ -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);
index 659a4a490e3774b7f09c1f0bfc38bf7c2ea7b2d5..3ec500381a8cbcacda99bafca85888d4ee3f0810 100644 (file)
@@ -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);
index c6e0240f2289033aa1be1a934e4c01e6eb6adaef..47ed425845f03e0a26e8cd522b482984eada3cc9 100644 (file)
@@ -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);
index f220fca140512116bcd29f3f7af4c1d8fe935855..05e73eb0f1f7549810301279d357b577ada190b5 100644 (file)
@@ -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;
 
index 3d7d2e75253d70b0febbd844408e92b6a797f229..9bd01beb5927e08d8b0f74c465609bb14fe81d55 100644 (file)
@@ -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;
 
index f14e0ced9f75925bdc797da922478fe0e044d906..1d90396bf42de7ff9d96e651ce19a4891f818b2f 100644 (file)
@@ -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;
 
index 333a11301284140d63818efc27cebc402988c9fd..782c951d6db165f13f16971521ea81c25b176172 100644 (file)
@@ -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);