Customer balances reports: fixed balance sums for prepayment invoices.
[fa-stable.git] / reporting / rep115.php
index 01085565cb7c55d0054755ed2c351c56f09f6e05..484746b61c11ddb6c02acbd026f16efbf2af3eca 100644 (file)
@@ -35,7 +35,7 @@ function get_open_balance($debtorno, $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(prep_amount, prep_amount, ov_amount + ov_gst + ov_freight + ov_freight_tax + 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,";
     $sql .= "SUM(IF(t.type != ".ST_SALESINVOICE." AND NOT(t.type IN (".ST_JOURNAL." , ".ST_BANKPAYMENT.")), 
@@ -216,7 +216,11 @@ function print_customer_balances()
         $accumulate = 0;
         $rate = $convert ? get_exchange_rate_from_home_currency($myrow['curr_code'], Today()) : 1;
         $bal = get_open_balance($myrow['debtor_no'], $from, $convert);
-        $init[0] = $init[1] = 0.0;
+        $init = array();
+               $bal['charges'] = isset($bal['charges']) ? $bal['charges'] : 0;
+               $bal['credits'] = isset($bal['credits']) ? $bal['credits'] : 0;
+               $bal['Allocated'] = isset($bal['Allocated']) ? $bal['Allocated'] : 0;
+               $bal['OutStanding'] = isset($bal['OutStanding']) ? $bal['OutStanding'] : 0;
         $init[0] = round2(abs($bal['charges'] * $rate), $dec);
         $init[1] = round2(Abs($bal['credits'] * $rate), $dec);
         $init[2] = round2($bal['Allocated'] * $rate, $dec);
@@ -232,7 +236,7 @@ function print_customer_balances()
             $grandtotal[$i] += $init[$i];
         }
 
-        if (db_num_rows($res) == 0 && !no_zeros) 
+        if (db_num_rows($res) == 0 && !$no_zeros) 
         {
                    $rep->TextCol(0, 2, $myrow['name']);
             $rep->AmountCol(3, 4, $init[3], $dec);