PHP 7.4 Bugs in some reports.
[fa-stable.git] / reporting / rep101.php
1 <?php
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';
13
14 // ----------------------------------------------------------------
15 // $ Revision:  2.0 $
16 // Creator:     Joe Hunt
17 // date_:       2005-05-19
18 // Title:       Customer Balances
19 // ----------------------------------------------------------------
20 $path_to_root="..";
21
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");
27
28 //----------------------------------------------------------------------------------------------------
29
30 print_customer_balances();
31
32 function get_open_balance($debtorno, $to)
33 {
34         if($to)
35                 $to = date2sql($to);
36         $sql = "SELECT SUM(IF(t.type = ".ST_SALESINVOICE." OR (t.type IN (".ST_JOURNAL." , ".ST_BANKPAYMENT.") AND t.ov_amount>0),
37              -abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount), 0)) AS charges,";
38
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
42     $sql .= "SUM(IF(t.type != ".ST_SALESINVOICE." AND NOT(t.type IN (".ST_JOURNAL." , ".ST_BANKPAYMENT.")), t.alloc * -1, t.alloc)) AS Allocated,";
43
44         $sql .= "SUM(IF(t.type = ".ST_SALESINVOICE." OR (t.type IN (".ST_JOURNAL." , ".ST_BANKPAYMENT.") AND t.ov_amount>0), 1, -1) *
45                         (abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount) - abs(t.alloc))) AS OutStanding
46                 FROM ".TB_PREF."debtor_trans t
47         WHERE t.debtor_no = ".db_escape($debtorno)
48                 ." AND t.type <> ".ST_CUSTDELIVERY;
49     if ($to)
50         $sql .= " AND t.tran_date < '$to'";
51         $sql .= " GROUP BY debtor_no";
52
53     $result = db_query($sql,"No transactions were returned");
54     return db_fetch($result);
55 }
56
57 function get_transactions($debtorno, $from, $to)
58 {
59         $from = date2sql($from);
60         $to = date2sql($to);
61
62         $allocated_from = 
63                         "(SELECT trans_type_from as trans_type, trans_no_from as trans_no, date_alloc, sum(amt) amount
64                         FROM ".TB_PREF."cust_allocations alloc
65                                 WHERE person_id=".db_escape($debtorno)."
66                                         AND date_alloc <= '$to'
67                                 GROUP BY trans_type_from, trans_no_from) alloc_from";
68         $allocated_to = 
69                         "(SELECT trans_type_to as trans_type, trans_no_to as trans_no, date_alloc, sum(amt) amount
70                         FROM ".TB_PREF."cust_allocations alloc
71                                 WHERE person_id=".db_escape($debtorno)."
72                                         AND date_alloc <= '$to'
73                                 GROUP BY trans_type_to, trans_no_to) alloc_to";
74
75      $sql = "SELECT trans.*,
76                 (trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount) AS TotalAmount,
77                 IFNULL(alloc_from.amount, alloc_to.amount) AS Allocated,
78                 ((trans.type = ".ST_SALESINVOICE.")     AND trans.due_date < '$to') AS OverDue
79         FROM ".TB_PREF."debtor_trans trans
80                         LEFT JOIN ".TB_PREF."voided voided ON trans.type=voided.type AND trans.trans_no=voided.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
84         WHERE trans.tran_date >= '$from'
85                         AND trans.tran_date <= '$to'
86                         AND trans.debtor_no = ".db_escape($debtorno)."
87                         AND trans.type <> ".ST_CUSTDELIVERY."
88                         AND ISNULL(voided.id)
89         ORDER BY trans.tran_date";
90     return db_query($sql,"No transactions were returned");
91 }
92
93 //----------------------------------------------------------------------------------------------------
94
95 function print_customer_balances()
96 {
97         global $path_to_root, $systypes_array;
98
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'];
108         if ($destination)
109                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
110         else
111                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
112
113         $orientation = ($orientation ? 'L' : 'P');
114         if ($fromcust == ALL_TEXT)
115                 $cust = _('All');
116         else
117                 $cust = get_customer_name($fromcust);
118     $dec = user_price_dec();
119
120         if ($show_balance) $sb = _('Yes');
121         else $sb = _('No');
122
123         if ($currency == ALL_TEXT)
124         {
125                 $convert = true;
126                 $currency = _('Balances in Home Currency');
127         }
128         else
129                 $convert = false;
130
131         if ($no_zeros) $nozeros = _('Yes');
132         else $nozeros = _('No');
133
134         $cols = array(0, 95, 140, 200,  250, 320, 385, 450,     515);
135
136         $headers = array(_('Trans Type'), _('#'), _('Date'), _('Due Date'), _('Charges'), _('Credits'),
137                 _('Allocated'),         _('Outstanding'));
138
139         if ($show_balance)
140                 $headers[7] = _('Balance');
141         $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
142
143     $params =   array(  0 => $comments,
144                                     1 => array('text' => _('Period'), 'from' => $from,          'to' => $to),
145                                     2 => array('text' => _('Customer'), 'from' => $cust,        'to' => ''),
146                                     3 => array('text' => _('Show Balance'), 'from' => $sb,      'to' => ''),
147                                     4 => array('text' => _('Currency'), 'from' => $currency, 'to' => ''),
148                                                 5 => array('text' => _('Suppress Zeros'), 'from' => $nozeros, 'to' => ''));
149
150     $rep = new FrontReport(_('Customer Balances'), "CustomerBalances", user_pagesize(), 9, $orientation);
151     if ($orientation == 'L')
152         recalculate_cols($cols);
153     $rep->Font();
154     $rep->Info($params, $cols, $headers, $aligns);
155     $rep->NewPage();
156
157         $grandtotal = array(0,0,0,0);
158
159         $sql = "SELECT debtor_no, name, curr_code FROM ".TB_PREF."debtors_master ";
160         if ($fromcust != ALL_TEXT)
161                 $sql .= "WHERE debtor_no=".db_escape($fromcust);
162         $sql .= " ORDER BY name";
163         $result = db_query($sql, "The customers could not be retrieved");
164
165         while ($myrow = db_fetch($result))
166         {
167                 if (!$convert && $currency != $myrow['curr_code']) continue;
168                 
169                 $accumulate = 0;
170                 $rate = $convert ? get_exchange_rate_from_home_currency($myrow['curr_code'], Today()) : 1;
171                 $bal = get_open_balance($myrow['debtor_no'], $from);
172                 $init = array();
173                 $bal['charges'] = isset($bal['charges']) ? $bal['charges'] : 0;
174                 $bal['credits'] = isset($bal['credits']) ? $bal['credits'] : 0;
175                 $bal['Allocated'] = isset($bal['Allocated']) ? $bal['Allocated'] : 0;
176                 $bal['OutStanding'] = isset($bal['OutStanding']) ? $bal['OutStanding'] : 0;
177                 $init[0] = round2(abs($bal['charges']*$rate), $dec);
178                 $init[1] = round2(Abs($bal['credits']*$rate), $dec);
179                 $init[2] = round2($bal['Allocated']*$rate, $dec);
180                 if ($show_balance)
181                 {
182                         $init[3] = $init[0] - $init[1];
183                         $accumulate += $init[3];
184                 }       
185                 else    
186                         $init[3] = round2($bal['OutStanding']*$rate, $dec);
187
188                 $res = get_transactions($myrow['debtor_no'], $from, $to);
189                 if ($no_zeros && db_num_rows($res) == 0) continue;
190
191                 $rep->fontSize += 2;
192                 $rep->TextCol(0, 2, $myrow['name']);
193                 if ($convert)
194                         $rep->TextCol(2, 3,     $myrow['curr_code']);
195                 $rep->fontSize -= 2;
196                 $rep->TextCol(3, 4,     _("Open Balance"));
197                 $rep->AmountCol(4, 5, $init[0], $dec);
198                 $rep->AmountCol(5, 6, $init[1], $dec);
199                 $rep->AmountCol(6, 7, $init[2], $dec);
200                 $rep->AmountCol(7, 8, $init[3], $dec);
201                 $total = array(0,0,0,0);
202                 for ($i = 0; $i < 4; $i++)
203                 {
204                         $total[$i] += $init[$i];
205                         $grandtotal[$i] += $init[$i];
206                 }
207                 $rep->NewLine(1, 2);
208                 $rep->Line($rep->row + 4);
209                 if (db_num_rows($res)==0) {
210                         $rep->NewLine(1, 2);
211                         continue;
212                 }
213                 while ($trans = db_fetch($res))
214                 {
215             if ($no_zeros) {
216                 if ($show_balance) {
217                     if ($trans['TotalAmount'] == 0) continue;
218                 } else {
219                     if (floatcmp($trans['TotalAmount'], $trans['Allocated']) == 0) continue;
220                 }
221             }
222                         $rep->NewLine(1, 2);
223                         $rep->TextCol(0, 1, $systypes_array[$trans['type']]);
224                         $rep->TextCol(1, 2,     $trans['reference']);
225                         $rep->DateCol(2, 3,     $trans['tran_date'], true);
226                         if ($trans['type'] == ST_SALESINVOICE)
227                                 $rep->DateCol(3, 4,     $trans['due_date'], true);
228                         $item[0] = $item[1] = 0.0;
229                         if ($trans['type'] == ST_CUSTCREDIT || $trans['type'] == ST_CUSTPAYMENT || $trans['type'] == ST_BANKDEPOSIT)
230                                 $trans['TotalAmount'] *= -1;
231                         if ($trans['TotalAmount'] > 0.0)
232                         {
233                                 $item[0] = round2(abs($trans['TotalAmount']) * $rate, $dec);
234                                 $rep->AmountCol(4, 5, $item[0], $dec);
235                                 $accumulate += $item[0];
236                                 $item[2] = round2($trans['Allocated'] * $rate, $dec);
237                         }
238                         else
239                         {
240                                 $item[1] = round2(Abs($trans['TotalAmount']) * $rate, $dec);
241                                 $rep->AmountCol(5, 6, $item[1], $dec);
242                                 $accumulate -= $item[1];
243                                 $item[2] = round2($trans['Allocated'] * $rate, $dec) * -1;
244                         }
245                         $rep->AmountCol(6, 7, $item[2], $dec);
246                         if (($trans['type'] == ST_JOURNAL && $item[0]) || $trans['type'] == ST_SALESINVOICE || $trans['type'] == ST_BANKPAYMENT)
247                                 $item[3] = $item[0] - $item[2];
248                         else    
249                                 $item[3] = -$item[1] - $item[2];
250                         if ($show_balance)      
251                                 $rep->AmountCol(7, 8, $accumulate, $dec);
252                         else    
253                                 $rep->AmountCol(7, 8, $item[3], $dec);
254                         for ($i = 0; $i < 4; $i++)
255                         {
256                                 $total[$i] += $item[$i];
257                                 $grandtotal[$i] += $item[$i];
258                         }
259                         if ($show_balance)
260                                 $total[3] = $total[0] - $total[1];
261                 }
262                 $rep->Line($rep->row - 8);
263                 $rep->NewLine(2);
264                 $rep->TextCol(0, 3, _('Total'));
265                 for ($i = 0; $i < 4; $i++)
266                         $rep->AmountCol($i + 4, $i + 5, $total[$i], $dec);
267                 $rep->Line($rep->row  - 4);
268                 $rep->NewLine(2);
269         }
270         $rep->fontSize += 2;
271         $rep->TextCol(0, 3, _('Grand Total'));
272         $rep->fontSize -= 2;
273         if ($show_balance)
274                 $grandtotal[3] = $grandtotal[0] - $grandtotal[1];
275         for ($i = 0; $i < 4; $i++)
276                 $rep->AmountCol($i + 4, $i + 5, $grandtotal[$i], $dec);
277         $rep->Line($rep->row  - 4);
278         $rep->NewLine();
279         $rep->End();
280 }
281