- $sql = "SELECT SUM(IF(".TB_PREF."supp_trans.type = ".ST_SUPPINVOICE.", (".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst +
- ".TB_PREF."supp_trans.ov_discount)";
- if ($convert)
- $sql .= " * rate";
- $sql .= ", 0)) AS charges,
- SUM(IF(".TB_PREF."supp_trans.type <> ".ST_SUPPINVOICE.", (".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst +
- ".TB_PREF."supp_trans.ov_discount)";
- if ($convert)
- $sql .= "* rate";
- $sql .= ", 0)) AS credits,
- SUM(".TB_PREF."supp_trans.alloc";
- if ($convert)
- $sql .= " * rate";
- $sql .= ") AS Allocated,
- SUM((".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst +
- ".TB_PREF."supp_trans.ov_discount - ".TB_PREF."supp_trans.alloc)";
- if ($convert)
- $sql .= " * rate";
- $sql .= ") AS OutStanding
- FROM ".TB_PREF."supp_trans
- WHERE ".TB_PREF."supp_trans.tran_date < '$to'
- AND ".TB_PREF."supp_trans.supplier_id = '$supplier_id' GROUP BY supplier_id";
+ $sql .= "SUM(IF(t.type != ".ST_SUPPINVOICE." AND NOT(t.type IN (".ST_JOURNAL." , ".ST_BANKDEPOSIT.")), t.alloc * -1, t.alloc))
+ AS Allocated,";
+
+ $sql .= "SUM(IF(t.type = ".ST_SUPPINVOICE.", 1, -1) *
+ (abs(t.ov_amount + t.ov_gst + t.ov_discount) - abs(t.alloc))) AS OutStanding
+ FROM ".TB_PREF."supp_trans t
+ WHERE t.supplier_id = ".db_escape($supplier_id);
+ if ($to)
+ $sql .= " AND t.tran_date < '$to'";
+ $sql .= " GROUP BY supplier_id";