2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_CUSTPAYMREP';
14 // ----------------------------------------------------------------
18 // Title: Customer Balances
19 // ----------------------------------------------------------------
22 include_once($path_to_root . "/includes/session.inc");
23 include_once($path_to_root . "/includes/date_functions.inc");
24 include_once($path_to_root . "/includes/data_checks.inc");
25 include_once($path_to_root . "/gl/includes/gl_db.inc");
26 include_once($path_to_root . "/sales/includes/db/customers_db.inc");
28 //----------------------------------------------------------------------------------------------------
30 // trial_inquiry_controls();
31 print_customer_balances();
33 function get_open_balance($debtorno, $to, $convert)
38 $sql = "SELECT SUM(IF(t.type = ".ST_SALESINVOICE." OR t.type = ".ST_BANKPAYMENT.",
39 (t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount)";
42 $sql .= ", 0)) AS charges,
43 SUM(IF(t.type <> ".ST_SALESINVOICE." AND t.type <> ".ST_BANKPAYMENT.",
44 (t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount)";
47 $sql .= " * -1, 0)) AS credits,
51 $sql .= ") AS Allocated,
52 SUM(IF(t.type = ".ST_SALESINVOICE." OR t.type = ".ST_BANKPAYMENT.",
53 (t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount - t.alloc)";
57 ((t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount) * -1 + t.alloc)";
60 $sql .= ")) AS OutStanding
61 FROM ".TB_PREF."debtor_trans t
62 WHERE t.debtor_no = ".db_escape($debtorno)
63 ." AND t.type <> ".ST_CUSTDELIVERY;
65 $sql .= " AND t.tran_date < '$to'";
66 $sql .= " GROUP BY debtor_no";
68 $result = db_query($sql,"No transactions were returned");
69 return db_fetch($result);
72 function get_transactions($debtorno, $from, $to)
74 $from = date2sql($from);
77 $sql = "SELECT ".TB_PREF."debtor_trans.*,
78 (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight +
79 ".TB_PREF."debtor_trans.ov_freight_tax + ".TB_PREF."debtor_trans.ov_discount)
80 AS TotalAmount, ".TB_PREF."debtor_trans.alloc AS Allocated,
81 ((".TB_PREF."debtor_trans.type = ".ST_SALESINVOICE.")
82 AND ".TB_PREF."debtor_trans.due_date < '$to') AS OverDue
83 FROM ".TB_PREF."debtor_trans
84 WHERE ".TB_PREF."debtor_trans.tran_date >= '$from'
85 AND ".TB_PREF."debtor_trans.tran_date <= '$to'
86 AND ".TB_PREF."debtor_trans.debtor_no = ".db_escape($debtorno)."
87 AND ".TB_PREF."debtor_trans.type <> ".ST_CUSTDELIVERY."
88 ORDER BY ".TB_PREF."debtor_trans.tran_date";
90 return db_query($sql,"No transactions were returned");
93 //----------------------------------------------------------------------------------------------------
95 function print_customer_balances()
97 global $path_to_root, $systypes_array;
99 $from = $_POST['PARAM_0'];
100 $to = $_POST['PARAM_1'];
101 $fromcust = $_POST['PARAM_2'];
102 $show_balance = $_POST['PARAM_3'];
103 $currency = $_POST['PARAM_4'];
104 $no_zeros = $_POST['PARAM_5'];
105 $comments = $_POST['PARAM_6'];
106 $orientation = $_POST['PARAM_7'];
107 $destination = $_POST['PARAM_8'];
109 include_once($path_to_root . "/reporting/includes/excel_report.inc");
111 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
113 $orientation = ($orientation ? 'L' : 'P');
114 if ($fromcust == ALL_TEXT)
117 $cust = get_customer_name($fromcust);
118 $dec = user_price_dec();
120 if ($currency == ALL_TEXT)
123 $currency = _('Balances in Home Currency');
128 if ($no_zeros) $nozeros = _('Yes');
129 else $nozeros = _('No');
131 $cols = array(0, 100, 130, 190, 250, 320, 385, 450, 515);
133 $headers = array(_('Trans Type'), _('#'), _('Date'), _('Due Date'), _('Charges'), _('Credits'),
134 _('Allocated'), _('Outstanding'));
137 $headers[7] = _('Balance');
138 $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
140 $params = array( 0 => $comments,
141 1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
142 2 => array('text' => _('Customer'), 'from' => $cust, 'to' => ''),
143 3 => array('text' => _('Currency'), 'from' => $currency, 'to' => ''),
144 4 => array('text' => _('Suppress Zeros'), 'from' => $nozeros, 'to' => ''));
146 $rep = new FrontReport(_('Customer Balances'), "CustomerBalances", user_pagesize(), 9, $orientation);
147 if ($orientation == 'L')
148 recalculate_cols($cols);
150 $rep->Info($params, $cols, $headers, $aligns);
153 $grandtotal = array(0,0,0,0);
155 $sql = "SELECT debtor_no, name, curr_code FROM ".TB_PREF."debtors_master ";
156 if ($fromcust != ALL_TEXT)
157 $sql .= "WHERE debtor_no=".db_escape($fromcust);
158 $sql .= " ORDER BY name";
159 $result = db_query($sql, "The customers could not be retrieved");
162 while ($myrow = db_fetch($result))
164 if (!$convert && $currency != $myrow['curr_code']) continue;
167 $bal = get_open_balance($myrow['debtor_no'], $from, $convert);
168 $init[0] = $init[1] = 0.0;
169 $init[0] = round2(abs($bal['charges']), $dec);
170 $init[1] = round2(Abs($bal['credits']), $dec);
171 $init[2] = round2($bal['Allocated'], $dec);
174 $init[3] = $init[0] - $init[1];
175 $accumulate += $init[3];
178 $init[3] = round2($bal['OutStanding'], $dec);
180 $res = get_transactions($myrow['debtor_no'], $from, $to);
181 if ($no_zeros && db_num_rows($res) == 0) continue;
185 $rep->TextCol(0, 2, $myrow['name']);
187 $rep->TextCol(2, 3, $myrow['curr_code']);
189 $rep->TextCol(3, 4, _("Open Balance"));
190 $rep->AmountCol(4, 5, $init[0], $dec);
191 $rep->AmountCol(5, 6, $init[1], $dec);
192 $rep->AmountCol(6, 7, $init[2], $dec);
193 $rep->AmountCol(7, 8, $init[3], $dec);
194 $total = array(0,0,0,0);
195 for ($i = 0; $i < 4; $i++)
197 $total[$i] += $init[$i];
198 $grandtotal[$i] += $init[$i];
201 if (db_num_rows($res)==0)
203 $rep->Line($rep->row + 4);
204 while ($trans = db_fetch($res))
206 if ($no_zeros && floatcmp($trans['TotalAmount'], $trans['Allocated']) == 0) continue;
208 $rep->TextCol(0, 1, $systypes_array[$trans['type']]);
209 $rep->TextCol(1, 2, $trans['reference']);
210 $rep->DateCol(2, 3, $trans['tran_date'], true);
211 if ($trans['type'] == ST_SALESINVOICE)
212 $rep->DateCol(3, 4, $trans['due_date'], true);
213 $item[0] = $item[1] = 0.0;
215 $rate = $trans['rate'];
218 if ($trans['type'] == ST_CUSTCREDIT || $trans['type'] == ST_CUSTPAYMENT || $trans['type'] == ST_BANKDEPOSIT)
219 $trans['TotalAmount'] *= -1;
220 if ($trans['TotalAmount'] > 0.0)
222 $item[0] = round2(abs($trans['TotalAmount']) * $rate, $dec);
223 $rep->AmountCol(4, 5, $item[0], $dec);
224 $accumulate += $item[0];
228 $item[1] = round2(Abs($trans['TotalAmount']) * $rate, $dec);
229 $rep->AmountCol(5, 6, $item[1], $dec);
230 $accumulate -= $item[1];
232 $item[2] = round2($trans['Allocated'] * $rate, $dec);
233 $rep->AmountCol(6, 7, $item[2], $dec);
234 if ($trans['type'] == ST_SALESINVOICE || $trans['type'] == ST_BANKPAYMENT)
235 $item[3] = $item[0] + $item[1] - $item[2];
237 $item[3] = $item[0] - $item[1] + $item[2];
239 $rep->AmountCol(7, 8, $accumulate, $dec);
241 $rep->AmountCol(7, 8, $item[3], $dec);
242 for ($i = 0; $i < 4; $i++)
244 $total[$i] += $item[$i];
245 $grandtotal[$i] += $item[$i];
248 $total[3] = $total[0] - $total[1];
250 $rep->Line($rep->row - 8);
252 $rep->TextCol(0, 3, _('Total'));
253 for ($i = 0; $i < 4; $i++)
254 $rep->AmountCol($i + 4, $i + 5, $total[$i], $dec);
255 $rep->Line($rep->row - 4);
259 $rep->TextCol(0, 3, _('Grand Total'));
262 $grandtotal[3] = $grandtotal[0] - $grandtotal[1];
263 for ($i = 0; $i < 4; $i++)
264 $rep->AmountCol($i + 4, $i + 5, $grandtotal[$i], $dec);
265 $rep->Line($rep->row - 4);