Clean up after Bugs 5510 and 5512.
[fa-stable.git] / sales / includes / db / custalloc_db.inc
index 1fa6995acad11cb81def1701502f8518501f5adb..a202d313980beb2d4bb21574b6e2756cdd77dcbc 100644 (file)
@@ -112,7 +112,7 @@ function get_allocatable_from_cust_sql($customer_id, $settled)
                trans.due_date,
                debtor.address,
                trans.version,
-               round(abs(ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount-alloc),6) <= 0 AS settled,
+               round(abs(ov_amount)+ov_gst+ov_freight+ov_freight_tax+ov_discount-alloc,6) <= 0 AS settled,
                trans.debtor_no
 
         FROM "
@@ -121,10 +121,10 @@ function get_allocatable_from_cust_sql($customer_id, $settled)
        ." WHERE trans.debtor_no=debtor.debtor_no
                AND (((type=".ST_CUSTPAYMENT." OR type=".ST_BANKDEPOSIT.") AND (trans.ov_amount > 0))
                 OR (type=".ST_CUSTCREDIT. " AND (ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount)>0)
-                OR (type=".ST_JOURNAL. " AND (trans.ov_amount > 0)))";
+                OR (type=".ST_JOURNAL. " AND (trans.ov_amount < 0)))";
 
        if (!$settled)
-               $sql .= " AND (round(abs(ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount-alloc),6) > 0)";
+               $sql .= " AND round(abs(ov_amount+ov_gst)+ov_freight+ov_freight_tax+ov_discount-alloc, 6) > 0";
 
        if ($customer_id != null)
                $sql .= " AND trans.debtor_no = ".db_escape($customer_id);
@@ -265,7 +265,7 @@ function get_allocatable_from_cust_transactions($customer_id, $trans_no=null, $t
        }
        else
        {
-               $sql .= " AND round(ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount-alloc,6) > 0
+               $sql .= " AND round(abs(ov_amount)+ov_gst+ov_freight+ov_freight_tax+ov_discount-alloc,6) > 0
                        AND trans.type NOT IN (".implode(',',array(ST_CUSTPAYMENT,ST_BANKDEPOSIT,ST_CUSTCREDIT,ST_CUSTDELIVERY)).")";
                $sql .= " GROUP BY type, trans_no";
        }
@@ -327,9 +327,9 @@ function get_sql_for_customer_allocation_inquiry($from, $to, $customer, $filterT
        {
                $today =  date2sql(Today());
                $sql .= " AND trans.due_date < '$today'
-                               AND (round(abs(trans.ov_amount + "
+                               AND (round(abs(trans.ov_amount) + "
                                ."trans.ov_gst + trans.ov_freight + "
-                               ."trans.ov_freight_tax + trans.ov_discount) - trans.alloc,6) > 0) ";
+                               ."trans.ov_freight_tax + trans.ov_discount - trans.alloc,6) > 0) ";
        }
        }
        else
@@ -340,9 +340,9 @@ function get_sql_for_customer_allocation_inquiry($from, $to, $customer, $filterT
 
        if (!$settled)
        {
-               $sql .= " AND (round(IF(trans.prep_amount,trans.prep_amount, abs(trans.ov_amount + trans.ov_gst + "
+               $sql .= " AND (ROUND(IF(trans.prep_amount,trans.prep_amount, ABS(trans.ov_amount) + trans.ov_gst + "
                ."trans.ov_freight + trans.ov_freight_tax + "
-               ."trans.ov_discount)) - trans.alloc,6) != 0) ";
+               ."trans.ov_discount) - trans.alloc,6) != 0) ";
        }
        return $sql;
 }