2 /**********************************************************************
3 Copyright (C) FrontAccounting Team.
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 Trial 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 print_customer_balances();
32 function get_open_balance($debtorno, $to)
37 $sql = "SELECT SUM(IF(t.type = ".ST_SALESINVOICE." OR (t.type IN (".ST_JOURNAL." , ".ST_BANKPAYMENT.") AND t.ov_amount>0),
38 -abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount), 0)) AS charges,";
39 $sql .= "SUM(IF(t.type != ".ST_SALESINVOICE." AND NOT(t.type IN (".ST_JOURNAL." , ".ST_BANKPAYMENT.") AND t.ov_amount>0),
40 abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount) * -1, 0)) AS credits,";
41 $sql .= "SUM(IF(t.type != ".ST_SALESINVOICE." AND NOT(t.type IN (".ST_JOURNAL." , ".ST_BANKPAYMENT.")),
42 t.alloc * -1, t.alloc)) AS Allocated,";
43 $sql .= "SUM(IF(t.type = ".ST_SALESINVOICE.", 1, -1) *
44 (abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount) - abs(t.alloc))) AS OutStanding
45 FROM ".TB_PREF."debtor_trans t
46 WHERE t.debtor_no = ".db_escape($debtorno)
47 ." AND t.type <> ".ST_CUSTDELIVERY;
49 $sql .= " AND t.tran_date < '$to'";
50 $sql .= " GROUP BY debtor_no";
52 $result = db_query($sql,"No transactions were returned");
53 return db_fetch($result);
56 function get_transactions($debtorno, $from, $to, $only_rec)
58 $from = date2sql($from);
62 "(SELECT trans_type_from as trans_type, trans_no_from as trans_no, date_alloc, sum(amt) amount
63 FROM ".TB_PREF."cust_allocations alloc
64 WHERE person_id=".db_escape($debtorno)."
65 AND date_alloc <= '$to'
66 GROUP BY trans_type_from, trans_no_from) alloc_from";
68 "(SELECT trans_type_to as trans_type, trans_no_to as trans_no, date_alloc, sum(amt) amount
69 FROM ".TB_PREF."cust_allocations alloc
70 WHERE person_id=".db_escape($debtorno)."
71 AND date_alloc <= '$to'
72 GROUP BY trans_type_to, trans_no_to) alloc_to";
74 $sql = "SELECT trans.*, comments.memo_,
75 (trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount) AS TotalAmount,
76 IFNULL(alloc_from.amount, alloc_to.amount) AS Allocated,
77 ((trans.type = ".ST_SALESINVOICE.") AND trans.due_date < '$to') AS OverDue
78 FROM ".TB_PREF."debtor_trans trans
79 LEFT JOIN ".TB_PREF."voided voided ON trans.type=voided.type AND trans.trans_no=voided.id
80 LEFT JOIN ".TB_PREF."comments comments ON trans.type=comments.type AND trans.trans_no=comments.id
81 LEFT JOIN $allocated_from ON alloc_from.trans_type = trans.type AND alloc_from.trans_no = trans.trans_no
82 LEFT JOIN $allocated_to ON alloc_to.trans_type = trans.type AND alloc_to.trans_no = trans.trans_no
83 WHERE trans.tran_date >= '$from'
84 AND trans.tran_date <= '$to'
85 AND trans.debtor_no = ".db_escape($debtorno);
86 $sql .= " AND trans.type <> ".ST_CUSTDELIVERY;
87 $sql .= " AND ISNULL(voided.id)
88 ORDER BY trans.tran_date ";
90 return db_query($sql,"No transactions were returned");
93 function get_customer_reference ($order_number)
96 $sql = "SELECT customer_ref FROM ".TB_PREF."sales_orders WHERE order_no ='$order_number' AND trans_type=".ST_SALESORDER."";
98 $result = db_query($sql,"No Transcation were returned");
100 $val = db_fetch($result);
102 return $val['customer_ref'];
104 //----------------------------------------------------------------------------------------------------
106 function print_customer_balances()
108 global $path_to_root, $systypes_array;
110 $from = $_POST['PARAM_0'];
111 $to = $_POST['PARAM_1'];
112 $fromcust = $_POST['PARAM_2'];
113 $area = $_POST['PARAM_3']; //added by Faisal to filter by area
114 $folk = $_POST['PARAM_4']; // added by Faisal to filter by sales person
115 $currency = $_POST['PARAM_5'];
116 $no_zeros = $_POST['PARAM_6'];
117 $comments = $_POST['PARAM_7'];
118 $orientation = $_POST['PARAM_8'];
119 $destination = $_POST['PARAM_9'];
121 include_once($path_to_root . "/reporting/includes/excel_report.inc");
123 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
125 $orientation = ($orientation ? 'L' : 'P');
126 if ($fromcust == ALL_TEXT)
129 $cust = get_customer_name($fromcust);
130 $dec = user_price_dec();
132 if ($area == ALL_NUMERIC)
136 $sarea = _('All Areas');
138 $sarea = get_area_name($area);
140 if ($folk == ALL_NUMERIC)
143 $salesfolk = _('All Sales Man');
145 $salesfolk = get_salesman_name($folk);
147 if ($currency == ALL_TEXT)
150 $currency = _('Balances in Home Currency');
155 if ($no_zeros) $nozeros = _('Yes');
156 else $nozeros = _('No');
158 $cols = array(0, 100, 130, 190, 250, 320, 385, 450, 515);
159 //$cols = array(0, 70, 140, 180, 230, 270, 350, 445, 495, 555);
161 $headers = array(_('Name'), '', '', _('Open Balance'), _('Debit'), _('Credit'), '', _('Balance'));
163 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right', 'right');
165 $params = array( 0 => $comments,
166 1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
167 2 => array('text' => _('Customer'), 'from' => $cust, 'to' => ''),
168 3 => array('text' => _('Sales Areas'), 'from' => $sarea, 'to' => ''),
169 4 => array('text' => _('Sales Folk'), 'from' => $salesfolk, 'to' => ''),
170 5 => array('text' => _('Currency'), 'from' => $currency, 'to' => ''),
171 6 => array('text' => _('Suppress Zeros'), 'from' => $nozeros, 'to' => ''));
173 $rep = new FrontReport(_('Customer Trial Balance'), "CustomerTB", user_pagesize(), 9, $orientation);
174 if ($orientation == 'L')
175 recalculate_cols($cols);
177 $rep->Info($params, $cols, $headers, $aligns);
180 $grandtotal = array(0,0,0,0);
182 $sql = "SELECT ".TB_PREF."debtors_master.debtor_no, name, curr_code FROM ".TB_PREF."debtors_master
183 INNER JOIN ".TB_PREF."cust_branch
184 ON ".TB_PREF."debtors_master.debtor_no=".TB_PREF."cust_branch.debtor_no
185 INNER JOIN ".TB_PREF."areas
186 ON ".TB_PREF."cust_branch.area = ".TB_PREF."areas.area_code
187 INNER JOIN ".TB_PREF."salesman
188 ON ".TB_PREF."cust_branch.salesman=".TB_PREF."salesman.salesman_code";
189 if ($fromcust != ALL_TEXT )
191 // if ($area != 0 || $folk != 0) continue;
192 $sql .= " WHERE ".TB_PREF."debtors_master.debtor_no=".db_escape($fromcust);
197 $sql .= " WHERE ".TB_PREF."salesman.salesman_code=".db_escape($folk)."
198 AND ".TB_PREF."areas.area_code=".db_escape($area);
200 $sql .= " WHERE ".TB_PREF."areas.area_code=".db_escape($area);
204 $sql .= " WHERE ".TB_PREF."salesman.salesman_code=".db_escape($folk);
207 $sql .= " GROUP BY ".TB_PREF."debtors_master.debtor_no ORDER BY name";
209 $result = db_query($sql, "The customers could not be retrieved");
211 $tot_cur_cr = $tot_cur_db = 0;
212 while ($myrow = db_fetch($result))
214 if (!$convert && $currency != $myrow['curr_code']) continue;
217 $rate = $convert ? get_exchange_rate_from_home_currency($myrow['curr_code'], Today()) : 1;
218 $bal = get_open_balance($myrow['debtor_no'], $from, $convert);
219 $init[0] = $init[1] = 0.0;
220 $init[0] = round2(abs($bal['charges'] * $rate), $dec);
221 $init[1] = round2(Abs($bal['credits'] * $rate), $dec);
222 $init[2] = round2($bal['Allocated'] * $rate, $dec);
223 $init[3] = $init[0] - $init[1];
224 $accumulate += $init[3];
226 $res = get_transactions($myrow['debtor_no'], $from, $to, false);
228 $total = array(0,0,0,0);
229 for ($i = 0; $i < 4; $i++)
231 $total[$i] += $init[$i];
232 $grandtotal[$i] += $init[$i];
235 if (db_num_rows($res) == 0 && !no_zeroes)
237 $rep->TextCol(0, 2, $myrow['name']);
238 $rep->AmountCol(3, 4, $init[3], $dec);
239 $rep->AmountCol(7, 8, $init[3], $dec);
240 //$rep->Line($rep->row - 2);
244 $curr_cr = $curr_db = 0;
245 while ($trans = db_fetch($res)) //Detail starts here
247 $item[0] = $item[1] = 0.0;
248 //modified below by faisal
249 if ($trans['type'] == ST_CUSTCREDIT || $trans['type'] == ST_CUSTPAYMENT || $trans['type'] == ST_BANKDEPOSIT)
250 $trans['TotalAmount'] *= -1;
251 if ($trans['TotalAmount'] > 0.0)
253 $item[0] = round2(abs($trans['TotalAmount']) * $rate, $dec);
254 $accumulate += $item[0];
255 $curr_db += $item[0];
256 $tot_cur_db += $item[0];
257 $item[2] = round2($trans['Allocated'] * $rate, $dec);
261 $item[1] = round2(Abs($trans['TotalAmount']) * $rate, $dec);
262 $accumulate -= $item[1];
263 $curr_cr += $item[1];
264 $tot_cur_cr +=$item[1];
265 $item[2] = round2($trans['Allocated'] * $rate, $dec) * -1;
268 if ($trans['type'] == ST_JOURNAL || $trans['type'] == ST_SALESINVOICE || $trans['type'] == ST_BANKPAYMENT)
269 $item[3] = $item[0] - $item[2];
271 $item[3] = -$item[1] - $item[2];
273 for ($i = 0; $i < 4; $i++)
275 $total[$i] += $item[$i];
276 $grandtotal[$i] += $item[$i];
278 $total[3] = $total[0] - $total[1];
280 if ($no_zeros && $total[3] == 0.0 && $curr_db == 0.0 && $curr_cr == 0.0) continue;
281 $rep->TextCol(0, 2, $myrow['name']);
282 $rep->AmountCol(3, 4, $total[3] + $curr_cr - $curr_db, $dec);
283 $rep->AmountCol(4, 5, $curr_db, $dec);
284 $rep->AmountCol(5, 6, $curr_cr, $dec);
285 $rep->AmountCol(7, 8, $total[3], $dec);
286 //$rep->Line($rep->row - 2);
289 $rep->Line($rep->row + 4);
292 $rep->TextCol(0, 3, _('Grand Total'));
294 $grandtotal[3] = $grandtotal[0] - $grandtotal[1];
296 $rep->AmountCol(3, 4, $grandtotal[3] - $tot_cur_db + $tot_cur_cr, $dec);
297 $rep->AmountCol(4, 5, $tot_cur_db, $dec);
298 $rep->AmountCol(5, 6, $tot_cur_cr, $dec);
299 $rep->AmountCol(7, 8, $grandtotal[3], $dec);
300 $rep->Line($rep->row - 6, 1);