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");
27 //----------------------------------------------------------------------------------------------------
29 // trial_inquiry_controls();
30 print_customer_balances();
32 function get_open_balance($debtorno, $to, $convert)
36 $sql = "SELECT SUM(IF(".TB_PREF."debtor_trans.type = ".ST_SALESINVOICE.", (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst +
37 ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_freight_tax + ".TB_PREF."debtor_trans.ov_discount)";
40 $sql .= ", 0)) AS charges,
41 SUM(IF(".TB_PREF."debtor_trans.type <> ".ST_SALESINVOICE.", (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst +
42 ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_freight_tax + ".TB_PREF."debtor_trans.ov_discount)";
45 $sql .= " * -1, 0)) AS credits,
46 SUM(".TB_PREF."debtor_trans.alloc";
49 $sql .= ") AS Allocated,
50 SUM(IF(".TB_PREF."debtor_trans.type = ".ST_SALESINVOICE.", (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst +
51 ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_freight_tax + ".TB_PREF."debtor_trans.ov_discount - ".TB_PREF."debtor_trans.alloc)";
55 ((".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight +
56 ".TB_PREF."debtor_trans.ov_freight_tax + ".TB_PREF."debtor_trans.ov_discount) * -1 + ".TB_PREF."debtor_trans.alloc)";
59 $sql .= ")) AS OutStanding
60 FROM ".TB_PREF."debtor_trans
61 WHERE ".TB_PREF."debtor_trans.tran_date < '$to'
62 AND ".TB_PREF."debtor_trans.debtor_no = ".db_escape($debtorno)."
63 AND ".TB_PREF."debtor_trans.type <> ".ST_CUSTDELIVERY." GROUP BY debtor_no";
65 $result = db_query($sql,"No transactions were returned");
66 return db_fetch($result);
69 function get_transactions($debtorno, $from, $to)
71 $from = date2sql($from);
74 $sql = "SELECT ".TB_PREF."debtor_trans.*,
75 (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight +
76 ".TB_PREF."debtor_trans.ov_freight_tax + ".TB_PREF."debtor_trans.ov_discount)
77 AS TotalAmount, ".TB_PREF."debtor_trans.alloc AS Allocated,
78 ((".TB_PREF."debtor_trans.type = ".ST_SALESINVOICE.")
79 AND ".TB_PREF."debtor_trans.due_date < '$to') AS OverDue
80 FROM ".TB_PREF."debtor_trans
81 WHERE ".TB_PREF."debtor_trans.tran_date >= '$from'
82 AND ".TB_PREF."debtor_trans.tran_date <= '$to'
83 AND ".TB_PREF."debtor_trans.debtor_no = ".db_escape($debtorno)."
84 AND ".TB_PREF."debtor_trans.type <> ".ST_CUSTDELIVERY."
85 ORDER BY ".TB_PREF."debtor_trans.tran_date";
87 return db_query($sql,"No transactions were returned");
90 //----------------------------------------------------------------------------------------------------
92 function print_customer_balances()
94 global $path_to_root, $systypes_array;
96 $from = $_POST['PARAM_0'];
97 $to = $_POST['PARAM_1'];
98 $fromcust = $_POST['PARAM_2'];
99 $currency = $_POST['PARAM_3'];
100 $no_zeros = $_POST['PARAM_4'];
101 $comments = $_POST['PARAM_5'];
102 $destination = $_POST['PARAM_6'];
104 include_once($path_to_root . "/reporting/includes/excel_report.inc");
106 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
108 if ($fromcust == ALL_NUMERIC)
111 $cust = get_customer_name($fromcust);
112 $dec = user_price_dec();
114 if ($currency == ALL_TEXT)
117 $currency = _('Balances in Home Currency');
122 if ($no_zeros) $nozeros = _('Yes');
123 else $nozeros = _('No');
125 $cols = array(0, 100, 130, 190, 250, 320, 385, 450, 515);
127 $headers = array(_('Trans Type'), _('#'), _('Date'), _('Due Date'), _('Charges'), _('Credits'),
128 _('Allocated'), _('Outstanding'));
130 $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
132 $params = array( 0 => $comments,
133 1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
134 2 => array('text' => _('Customer'), 'from' => $cust, 'to' => ''),
135 3 => array('text' => _('Currency'), 'from' => $currency, 'to' => ''),
136 4 => array('text' => _('Suppress Zeros'), 'from' => $nozeros, 'to' => ''));
138 $rep = new FrontReport(_('Customer Balances'), "CustomerBalances", user_pagesize());
141 $rep->Info($params, $cols, $headers, $aligns);
144 $grandtotal = array(0,0,0,0);
146 $sql = "SELECT debtor_no, name, curr_code FROM ".TB_PREF."debtors_master ";
147 if ($fromcust != ALL_NUMERIC)
148 $sql .= "WHERE debtor_no=".db_escape($fromcust);
149 $sql .= " ORDER BY name";
150 $result = db_query($sql, "The customers could not be retrieved");
153 while ($myrow = db_fetch($result))
155 if (!$convert && $currency != $myrow['curr_code']) continue;
157 $bal = get_open_balance($myrow['debtor_no'], $from, $convert);
158 $init[0] = $init[1] = 0.0;
159 $init[0] = round2(abs($bal['charges']), $dec);
160 $init[1] = round2(Abs($bal['credits']), $dec);
161 $init[2] = round2($bal['Allocated'], $dec);
162 $init[3] = round2($bal['OutStanding'], $dec);;
164 $res = get_transactions($myrow['debtor_no'], $from, $to);
165 if ($no_zeros && db_num_rows($res) == 0) continue;
169 $rep->TextCol(0, 2, $myrow['name']);
171 $rep->TextCol(2, 3, $myrow['curr_code']);
173 $rep->TextCol(3, 4, _("Open Balance"));
174 $rep->AmountCol(4, 5, $init[0], $dec);
175 $rep->AmountCol(5, 6, $init[1], $dec);
176 $rep->AmountCol(6, 7, $init[2], $dec);
177 $rep->AmountCol(7, 8, $init[3], $dec);
178 $total = array(0,0,0,0);
179 for ($i = 0; $i < 4; $i++)
181 $total[$i] += $init[$i];
182 $grandtotal[$i] += $init[$i];
185 if (db_num_rows($res)==0)
187 $rep->Line($rep->row + 4);
188 while ($trans = db_fetch($res))
190 if ($no_zeros && $trans['TotalAmount'] == 0 && $trans['Allocated'] == 0) continue;
192 $rep->TextCol(0, 1, $systypes_array[$trans['type']]);
193 $rep->TextCol(1, 2, $trans['reference']);
194 $rep->DateCol(2, 3, $trans['tran_date'], true);
195 if ($trans['type'] == ST_SALESINVOICE)
196 $rep->DateCol(3, 4, $trans['due_date'], true);
197 $item[0] = $item[1] = 0.0;
199 $rate = $trans['rate'];
202 if ($trans['type'] == ST_CUSTCREDIT || $trans['type'] == ST_CUSTPAYMENT || $trans['type'] == ST_BANKDEPOSIT)
203 $trans['TotalAmount'] *= -1;
204 if ($trans['TotalAmount'] > 0.0)
206 $item[0] = round2(abs($trans['TotalAmount']) * $rate, $dec);
207 $rep->AmountCol(4, 5, $item[0], $dec);
211 $item[1] = round2(Abs($trans['TotalAmount']) * $rate, $dec);
212 $rep->AmountCol(5, 6, $item[1], $dec);
214 $item[2] = round2($trans['Allocated'] * $rate, $dec);
215 $rep->AmountCol(6, 7, $item[2], $dec);
217 if ($trans['type'] == 10)
218 $item[3] = ($trans['TotalAmount'] - $trans['Allocated']) * $rate;
220 $item[3] = ($trans['TotalAmount'] + $trans['Allocated']) * $rate;
222 if ($trans['type'] == ST_SALESINVOICE || $trans['type'] == ST_BANKPAYMENT)
223 $item[3] = $item[0] + $item[1] - $item[2];
225 $item[3] = $item[0] - $item[1] + $item[2];
226 $rep->AmountCol(7, 8, $item[3], $dec);
227 for ($i = 0; $i < 4; $i++)
229 $total[$i] += $item[$i];
230 $grandtotal[$i] += $item[$i];
233 $rep->Line($rep->row - 8);
235 $rep->TextCol(0, 3, _('Total'));
236 for ($i = 0; $i < 4; $i++)
237 $rep->AmountCol($i + 4, $i + 5, $total[$i], $dec);
238 $rep->Line($rep->row - 4);
242 $rep->TextCol(0, 3, _('Grand Total'));
244 for ($i = 0; $i < 4; $i++)
245 $rep->AmountCol($i + 4, $i + 5, $grandtotal[$i], $dec);
246 $rep->Line($rep->row - 4);