- $sql = "SELECT ".TB_PREF."sys_types.type_name,
- ".TB_PREF."supp_trans.reference,
- ".TB_PREF."supp_trans.tran_date,
- (".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount) as Balance,
- IF (".TB_PREF."payment_terms.days_before_due > 0,
- CASE WHEN TO_DAYS('$todate') - TO_DAYS(".TB_PREF."supp_trans.tran_date) >= ".TB_PREF."payment_terms.days_before_due
- THEN
- ".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount
- ELSE
- 0
- END,
-
- CASE WHEN TO_DAYS('$todate') - TO_DAYS(DATE_ADD(DATE_ADD(".TB_PREF."supp_trans.tran_date,
- INTERVAL 1 MONTH), INTERVAL (".TB_PREF."payment_terms.day_in_following_month -
- DAYOFMONTH(".TB_PREF."supp_trans.tran_date)) DAY)) >= 0
- THEN
- ".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount
- ELSE
- 0
- END
- ) AS Due,
- IF (".TB_PREF."payment_terms.days_before_due > 0,
- CASE WHEN TO_DAYS('$todate') - TO_DAYS(".TB_PREF."supp_trans.tran_date) > ".TB_PREF."payment_terms.days_before_due
- AND TO_DAYS('$todate') - TO_DAYS(".TB_PREF."supp_trans.tran_date) >= (".TB_PREF."payment_terms.days_before_due + $PastDueDays1)
- THEN
- ".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount
- ELSE
- 0
- END,
-
- CASE WHEN TO_DAYS('$todate') - TO_DAYS(DATE_ADD(DATE_ADD(".TB_PREF."supp_trans.tran_date,
- INTERVAL 1 MONTH), INTERVAL (".TB_PREF."payment_terms.day_in_following_month -
- DAYOFMONTH(".TB_PREF."supp_trans.tran_date)) DAY)) >= $PastDueDays1
- THEN
- ".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount
- ELSE
- 0
- END
- ) AS Overdue1,
- IF (".TB_PREF."payment_terms.days_before_due > 0,
- CASE WHEN TO_DAYS('$todate') - TO_DAYS(".TB_PREF."supp_trans.tran_date) > ".TB_PREF."payment_terms.days_before_due
- AND TO_DAYS('$todate') - TO_DAYS(".TB_PREF."supp_trans.tran_date) >= (".TB_PREF."payment_terms.days_before_due + $PastDueDays2)
- THEN
- ".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount
- ELSE
- 0
- END,
-
- CASE WHEN TO_DAYS('$todate') - TO_DAYS(DATE_ADD(DATE_ADD(".TB_PREF."supp_trans.tran_date,
- INTERVAL 1 MONTH), INTERVAL (".TB_PREF."payment_terms.day_in_following_month -
- DAYOFMONTH(".TB_PREF."supp_trans.tran_date)) DAY)) >= $PastDueDays2
- THEN
- ".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount
- ELSE
- 0
- END
- ) AS Overdue2
-
- FROM ".TB_PREF."suppliers,
- ".TB_PREF."payment_terms,
- ".TB_PREF."supp_trans,
- ".TB_PREF."sys_types
-
- WHERE ".TB_PREF."sys_types.type_id = ".TB_PREF."supp_trans.type
- AND ".TB_PREF."suppliers.payment_terms = ".TB_PREF."payment_terms.terms_indicator
- AND ".TB_PREF."suppliers.supplier_id = ".TB_PREF."supp_trans.supplier_id
- AND ".TB_PREF."supp_trans.supplier_id = $supplier_id
- AND ".TB_PREF."supp_trans.tran_date <= '$todate'
- AND ABS(".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount) > 0.004
- ORDER BY ".TB_PREF."supp_trans.tran_date";
+ if ($all)
+ $value = "(trans.ov_amount + trans.ov_gst + trans.ov_discount)";
+ else
+ $value = "IF (trans.type=".ST_SUPPINVOICE." OR trans.type=".ST_BANKDEPOSIT.",
+ (trans.ov_amount + trans.ov_gst + trans.ov_discount - trans.alloc),
+ (trans.ov_amount + trans.ov_gst + trans.ov_discount + trans.alloc))";
+ $due = "IF (trans.type=".ST_SUPPINVOICE." OR trans.type=".ST_SUPPCREDIT.",trans.due_date,trans.tran_date)";
+ $sql = "SELECT trans.type,
+ trans.reference,
+ trans.tran_date,
+ $value as Balance,
+ IF ((TO_DAYS('$todate') - TO_DAYS($due)) > 0,$value,0) AS Due,
+ IF ((TO_DAYS('$todate') - TO_DAYS($due)) > $PastDueDays1,$value,0) AS Overdue1,
+ IF ((TO_DAYS('$todate') - TO_DAYS($due)) > $PastDueDays2,$value,0) AS Overdue2
+
+ FROM ".TB_PREF."suppliers supplier,
+ ".TB_PREF."supp_trans trans
+
+ WHERE supplier.supplier_id = trans.supplier_id
+ AND trans.supplier_id = $supplier_id
+ AND trans.tran_date <= '$todate'
+ AND ABS(trans.ov_amount + trans.ov_gst + trans.ov_discount) > ".FLOAT_COMP_DELTA;
+ if (!$all)
+ $sql .= " AND ABS(trans.ov_amount + trans.ov_gst + trans.ov_discount) - trans.alloc > ".FLOAT_COMP_DELTA;
+ $sql .= " ORDER BY trans.tran_date";