-
- $value = "IFNULL(IF(trans.type=11 OR trans.type=12 OR trans.type=2, -1, 1)
- * (trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount),0)";
-
- $due = "IF (trans.type=10, trans.due_date, trans.tran_date)";
- $sql = "SELECT ".TB_PREF."debtors_master.name, ".TB_PREF."debtors_master.curr_code, ".TB_PREF."payment_terms.terms,
- ".TB_PREF."debtors_master.credit_limit, ".TB_PREF."credit_status.dissallow_invoices, ".TB_PREF."credit_status.reason_description,
-
- Sum(IFNULL($value,0)) AS Balance,
- Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= 0,$value,0)) AS Due,
- Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $past1,$value,0)) AS Overdue1,
- Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $past2,$value,0)) AS Overdue2
-
- FROM ".TB_PREF."debtors_master
- LEFT JOIN ".TB_PREF."debtor_trans trans ON
- trans.tran_date <= '$todate' AND ".TB_PREF."debtors_master.debtor_no = trans.debtor_no AND trans.type <> 13
-,
- ".TB_PREF."payment_terms,
- ".TB_PREF."credit_status
-
- WHERE
- ".TB_PREF."debtors_master.payment_terms = ".TB_PREF."payment_terms.terms_indicator
- AND ".TB_PREF."debtors_master.credit_status = ".TB_PREF."credit_status.id
- AND ".TB_PREF."debtors_master.debtor_no = ".db_escape($customer_id)."
-
- GROUP BY
- ".TB_PREF."debtors_master.name,
- ".TB_PREF."payment_terms.terms,
- ".TB_PREF."payment_terms.days_before_due,
- ".TB_PREF."payment_terms.day_in_following_month,
- ".TB_PREF."debtors_master.credit_limit,
- ".TB_PREF."credit_status.dissallow_invoices,
- ".TB_PREF."credit_status.reason_description";
+ $sign = "IF(`type` IN(".implode(',', array(ST_CUSTCREDIT,ST_CUSTPAYMENT,ST_BANKDEPOSIT,ST_JOURNAL))."), -1, 1)";
+ if ($all)
+ $value = "IFNULL($sign*(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount),0)";
+ else
+ $value = "IFNULL($sign*(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount -
+ trans.alloc),0)";
+ $due = "IF (trans.type=".ST_SALESINVOICE.", trans.due_date, trans.tran_date)";
+ $sql = "SELECT debtor.name, debtor.curr_code, terms.terms, debtor.credit_limit,
+ credit_status.dissallow_invoices, credit_status.reason_description,
+ Sum(IFNULL($value,0)) AS Balance,
+ Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) > 0,$value,0)) AS Due,
+ Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) > $past1,$value,0)) AS Overdue1,
+ Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) > $past2,$value,0)) AS Overdue2
+ FROM ".TB_PREF."debtors_master debtor
+ LEFT JOIN ".TB_PREF."debtor_trans trans ON trans.tran_date <= '$todate' AND debtor.debtor_no = trans.debtor_no AND trans.type <> ".ST_CUSTDELIVERY.","
+ .TB_PREF."payment_terms terms,"
+ .TB_PREF."credit_status credit_status
+ WHERE
+ debtor.payment_terms = terms.terms_indicator
+ AND debtor.credit_status = credit_status.id
+ AND debtor.debtor_no = ".db_escape($customer_id);
+ if (!$all)
+ $sql .= " AND ABS(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount - trans.alloc) > ".FLOAT_COMP_DELTA;
+ $sql .= " GROUP BY
+ debtor.name,
+ terms.terms,
+ terms.days_before_due,
+ terms.day_in_following_month,
+ debtor.credit_limit,
+ credit_status.dissallow_invoices,
+ credit_status.reason_description";