+function get_open_balance($debtorno, $to)
+{
+ if($to)
+ $to = date2sql($to);
+
+ $sql = "SELECT SUM(IF(t.type = ".ST_SALESINVOICE." OR t.type = ".ST_BANKPAYMENT.",
+ (t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount), 0)) AS charges,
+ SUM(IF(t.type <> ".ST_SALESINVOICE." AND t.type <> ".ST_BANKPAYMENT.",
+ (t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount) * -1, 0)) AS credits,
+ SUM(t.alloc) AS Allocated,
+ SUM(IF(t.type = ".ST_SALESINVOICE." OR t.type = ".ST_BANKPAYMENT.",
+ (t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount - t.alloc),
+ ((t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount) * -1 + t.alloc))) AS OutStanding
+ FROM ".TB_PREF."debtor_trans t
+ WHERE t.debtor_no = ".db_escape($debtorno)
+ ." AND t.type <> ".ST_CUSTDELIVERY;
+ if ($to)
+ $sql .= " AND t.tran_date < '$to'";
+ $sql .= " GROUP BY debtor_no";
+
+ $result = db_query($sql,"No transactions were returned");
+ return db_fetch($result);
+}
+
+function get_transactions($debtorno, $from, $to)