Customer balances reports: further fixes and cleanups.
[fa-stable.git] / reporting / rep102.php
index a63ac9c5cd58f1b4b987a5f021159546d0e46dad..fe7ff84a206eae16c237684c3e39af4691c5855b 100644 (file)
@@ -33,12 +33,9 @@ function get_invoices($customer_id, $to, $all=true)
        $PastDueDays1 = get_company_pref('past_due_days');
        $PastDueDays2 = 2 * $PastDueDays1;
 
-       // Revomed allocated from sql
-       if ($all)
-        $value = "IF(prep_amount, prep_amount, ov_amount + ov_gst + ov_freight + ov_freight_tax + ov_discount)";
-       else
-        $value = "(IF(prep_amount, prep_amount, ov_amount + ov_gst + ov_freight + ov_freight_tax + ov_discount)-alloc)";
-       $sign = "IF(`type` IN(".implode(',',  array(ST_CUSTCREDIT,ST_CUSTPAYMENT,ST_BANKDEPOSIT,ST_JOURNAL))."), -1, 1)";
+       $sign = "IF(`type` IN(".implode(',',  array(ST_CUSTCREDIT,ST_CUSTPAYMENT,ST_BANKDEPOSIT))."), -1, 1)";
+       $value = "$sign*(IF(trans.prep_amount, trans.prep_amount,
+               ABS(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount)) ".($all ? '' : "- trans.alloc").")";
        $due = "IF (type=".ST_SALESINVOICE.", due_date, tran_date)";
 
        $sql = "SELECT type, reference, tran_date,
@@ -52,10 +49,8 @@ function get_invoices($customer_id, $to, $all=true)
                WHERE type <> ".ST_CUSTDELIVERY."
                        AND debtor_no = $customer_id 
                        AND tran_date <= '$todate'
-                       AND ABS(ov_amount + ov_gst + ov_freight + ov_freight_tax + ov_discount) > " . FLOAT_COMP_DELTA;
+                       AND ABS(IF(trans.prep_amount, trans.prep_amount, ov_amount + ov_gst + ov_freight + ov_freight_tax + ov_discount) ".($all ? '' : '-trans.alloc').") > " . FLOAT_COMP_DELTA;
 
-       if (!$all)
-               $sql .= "AND ABS(ov_amount + ov_gst + ov_freight + ov_freight_tax + ov_discount - alloc) > " . FLOAT_COMP_DELTA;
        $sql .= "ORDER BY tran_date";
 
        return db_query($sql, "The customer transactions could not be retrieved");
@@ -195,14 +190,6 @@ function print_aged_customer_analysis()
                                $rep->TextCol(1, 2,     $trans['reference'], -2);
                                $rep->DateCol(2, 3, $trans['tran_date'], true, -2);
 
-                               if ($trans['type'] == ST_CUSTCREDIT || $trans['type'] == ST_CUSTPAYMENT || $trans['type'] == ST_BANKDEPOSIT)
-                               {
-                                       $trans['Balance'] *= -1;
-                                       $trans['Due'] *= -1;
-                                       $trans['Overdue1'] *= -1;
-                                       $trans['Overdue2'] *= -1;
-                               }
-
                                foreach ($trans as $i => $value)
                                        $trans[$i] = (float)$trans[$i] * $rate;
                                $str = array($trans["Balance"] - $trans["Due"],