- // removed - debtor_trans.alloc from all summations
- $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=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,
+
+ $sign = "IF(trans.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 (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,