Customer balances reports: fixed balance sums for prepayment invoices.
[fa-stable.git] / reporting / rep101.php
index fd87944f8631231ba43d783a99f3fc12be019b89..a9bd160952a62fd429b483cefb9f41c29a7515c0 100644 (file)
@@ -34,7 +34,7 @@ function get_open_balance($debtorno, $to)
        if($to)
                $to = date2sql($to);
        $sql = "SELECT SUM(IF(t.type = ".ST_SALESINVOICE." OR (t.type IN (".ST_JOURNAL." , ".ST_BANKPAYMENT.") AND t.ov_amount>0),
-             -abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount), 0)) AS charges,";
+             -abs(IF(t.prep_amount, t.prep_amount, t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount)), 0)) AS charges,";
 
        $sql .= "SUM(IF(t.type != ".ST_SALESINVOICE." AND NOT(t.type IN (".ST_JOURNAL." , ".ST_BANKPAYMENT.") AND t.ov_amount>0),
              abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount) * -1, 0)) AS credits,";             
@@ -42,7 +42,7 @@ function get_open_balance($debtorno, $to)
     $sql .= "SUM(IF(t.type != ".ST_SALESINVOICE." AND NOT(t.type IN (".ST_JOURNAL." , ".ST_BANKPAYMENT.")), t.alloc * -1, t.alloc)) AS Allocated,";
 
        $sql .= "SUM(IF(t.type = ".ST_SALESINVOICE." OR (t.type IN (".ST_JOURNAL." , ".ST_BANKPAYMENT.") AND t.ov_amount>0), 1, -1) *
-                       (abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount) - abs(t.alloc))) AS OutStanding
+                       (IF(t.prep_amount, t.prep_amount, abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount)) - abs(t.alloc))) AS OutStanding
                FROM ".TB_PREF."debtor_trans t
        WHERE t.debtor_no = ".db_escape($debtorno)
                ." AND t.type <> ".ST_CUSTDELIVERY;
@@ -73,7 +73,8 @@ function get_transactions($debtorno, $from, $to)
                                GROUP BY trans_type_to, trans_no_to) alloc_to";
 
      $sql = "SELECT trans.*,
-               (trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount) AS TotalAmount,
+               IF(trans.prep_amount, trans.prep_amount, trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount)
+                       AS TotalAmount,
                IFNULL(alloc_from.amount, alloc_to.amount) AS Allocated,
                ((trans.type = ".ST_SALESINVOICE.")     AND trans.due_date < '$to') AS OverDue
        FROM ".TB_PREF."debtor_trans trans
@@ -133,7 +134,7 @@ function print_customer_balances()
 
        $cols = array(0, 95, 140, 200,  250, 320, 385, 450,     515);
 
-       $headers = array(_('Trans Type'), _('#'), _('Date'), _('Due Date'), _('Charges'), _('Credits'),
+       $headers = array(_('Trans Type'), _('#'), _('Date'), _('Due Date'), _('Debits'), _('Credits'),
                _('Allocated'),         _('Outstanding'));
 
        if ($show_balance)