Improved float compare in certain files (AR/AP ledger)
authorJoe Hunt <joe.hunt.consulting@gmail.com>
Wed, 4 Jan 2012 13:25:12 +0000 (14:25 +0100)
committerJoe Hunt <joe.hunt.consulting@gmail.com>
Wed, 4 Jan 2012 13:25:12 +0000 (14:25 +0100)
includes/current_user.inc
reporting/rep101.php
reporting/rep102.php
reporting/rep201.php
reporting/rep202.php
sales/inquiry/customer_inquiry.php

index 7c99f233a3fbb9a1e464955ba9ae6af2ec0b77cf..51c03ee626f0e29f75fa453e756a541732a31bd8 100644 (file)
@@ -210,6 +210,20 @@ function number_format2($number, $decimals=0)
        return $decimals==='max' ? rtrim($num, '0') : $num;
 
 }
+
+/* price/float comparision helper to be used in any suspicious place for zero values? 
+usage:
+if (!floatcmp($value1, $value2)) 
+       compare value is 0
+*/
+
+define('FLOAT_COMP_DELTA', 0.0000001);
+
+function floatcmp($a, $b)
+{
+    return $a - $b > FLOAT_COMP_DELTA ? 1 : $b - $a > FLOAT_COMP_DELTA ? -1 : 0;
+}
+
 //
 //     Current ui mode.
 //
index 3f1ece787898c3784891b58ce565764c19c7afbe..0ff78c8ba7bb569df8b8be90f7608560ab292cf3 100644 (file)
@@ -189,7 +189,7 @@ function print_customer_balances()
                $rep->Line($rep->row + 4);
                while ($trans = db_fetch($res))
                {
-                       if ($no_zeros && $trans['TotalAmount'] == 0 && $trans['Allocated'] == 0) continue;
+                       if ($no_zeros && floatcmp($trans['TotalAmount'], $trans['Allocated']) == 0) continue;
                        $rep->NewLine(1, 2);
                        $rep->TextCol(0, 1, $systypes_array[$trans['type']]);
                        $rep->TextCol(1, 2,     $trans['reference']);
index 18fab24d052952aa6649e80e8064fde507fec187..07847070452e10dfb4a78cf56434e6627d8676a8 100644 (file)
@@ -168,7 +168,7 @@ function print_aged_customer_analysis()
                        $custrec["Overdue1"]-$custrec["Overdue2"],
                        $custrec["Overdue2"],
                        $custrec["Balance"]);
-               if ($no_zeros && array_sum($str) == 0) continue;
+               if ($no_zeros && floatcmp(array_sum($str), 0) == 0) continue;
 
                $rep->fontSize += 2;
                $rep->TextCol(0, 2, $myrow['name']);
index 79cd401f1dbeab815384eb73017412762447e841..4004c8a219c335bb279198fb0ff014c5e721bd08 100644 (file)
@@ -175,7 +175,7 @@ function print_supplier_balances()
                $rep->Line($rep->row + 4);
                while ($trans=db_fetch($res))
                {
-                       if ($no_zeros && $trans['TotalAmount'] == 0 && $trans['Allocated'] == 0) continue;
+                       if ($no_zeros && floatcmp(abs($trans['TotalAmount']), $trans['Allocated']) == 0) continue;
                        $rep->NewLine(1, 2);
                        $rep->TextCol(0, 1, $systypes_array[$trans['type']]);
                        $rep->TextCol(1, 2,     $trans['reference']);
index f18abaec1b83da9634f048373b3257481bc6d80d..32f7bed3fe4273c2bce2954cf8bfc454b0285962 100644 (file)
@@ -182,7 +182,7 @@ function print_aged_supplier_analysis()
                        $supprec["Overdue2"],
                        $supprec["Balance"]);
 
-               if ($no_zeros && array_sum($str) == 0) continue;
+               if ($no_zeros && floatcmp(array_sum($str), 0) == 0) continue;
 
                $rep->fontSize += 2;
                $rep->TextCol(0, 2,     $myrow['name']);
index 4508b2f2296a355f9296ea88055d904174e31fa0..3ebc84b5c0b624034f7904947c314836bb126cb5 100644 (file)
@@ -212,7 +212,7 @@ function prt_link($row)
 function check_overdue($row)
 {
        return $row['OverDue'] == 1
-               && (abs($row["TotalAmount"]) - $row["Allocated"] != 0);
+               && floatcmp($row["TotalAmount"], $row["Allocated"]) != 0;
 }
 //------------------------------------------------------------------------------------------------
 $sql = get_sql_for_customer_inquiry();