PHP 7.4 Bugs in some reports.
[fa-stable.git] / reporting / rep201.php
1 <?php
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_SUPPLIERANALYTIC';
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2018-12-21
17 // Title:       Supplier Trial Balances
18 // ----------------------------------------------------------------
19 $path_to_root="..";
20
21 include_once($path_to_root . "/includes/session.inc");
22 include_once($path_to_root . "/includes/date_functions.inc");
23 include_once($path_to_root . "/includes/data_checks.inc");
24 include_once($path_to_root . "/gl/includes/gl_db.inc");
25
26 //----------------------------------------------------------------------------------------------------
27
28 print_supplier_balances();
29
30 function get_open_balance($supplier_id, $to)
31 {
32     if ($to)
33         $to = date2sql($to);
34
35     $sql = "SELECT SUM(IF(t.type = ".ST_SUPPINVOICE." OR (t.type IN (".ST_JOURNAL." , ".ST_BANKDEPOSIT.") AND t.ov_amount>0),
36         -abs(t.ov_amount + t.ov_gst + t.ov_discount), 0)) AS charges,";
37
38     $sql .= "SUM(IF(t.type != ".ST_SUPPINVOICE." AND NOT(t.type IN (".ST_JOURNAL." , ".ST_BANKDEPOSIT.") AND t.ov_amount>0),
39         abs(t.ov_amount + t.ov_gst + t.ov_discount) * -1, 0)) AS credits,";
40
41     $sql .= "SUM(IF(t.type != ".ST_SUPPINVOICE." AND NOT(t.type IN (".ST_JOURNAL." , ".ST_BANKDEPOSIT.")), t.alloc * -1, t.alloc)) 
42         AS Allocated,";
43
44     $sql .= "SUM(IF(t.type = ".ST_SUPPINVOICE.", 1, -1) *
45         (abs(t.ov_amount + t.ov_gst + t.ov_discount) - abs(t.alloc))) AS OutStanding
46         FROM ".TB_PREF."supp_trans t
47         WHERE t.supplier_id = ".db_escape($supplier_id);
48     if ($to)
49         $sql .= " AND t.tran_date < '$to'";
50     $sql .= " GROUP BY supplier_id";
51
52     $result = db_query($sql,"No transactions were returned");
53     return db_fetch($result);
54 }
55
56 function getTransactions($supplier_id, $from, $to)
57 {
58         $from = date2sql($from);
59         $to = date2sql($to);
60
61     $sql = "SELECT *,
62                                 (ov_amount + ov_gst + ov_discount) AS TotalAmount,
63                                 alloc AS Allocated,
64                                 ((type = ".ST_SUPPINVOICE.") AND due_date < '$to') AS OverDue
65                         FROM ".TB_PREF."supp_trans
66                         WHERE tran_date >= '$from' AND tran_date <= '$to' 
67                         AND supplier_id = '$supplier_id' AND ov_amount!=0
68                                 ORDER BY tran_date";
69
70     $TransResult = db_query($sql,"No transactions were returned");
71
72     return $TransResult;
73 }
74
75 //----------------------------------------------------------------------------------------------------
76
77 function print_supplier_balances()
78 {
79         global $path_to_root, $systypes_array;
80
81         $from = $_POST['PARAM_0'];
82         $to = $_POST['PARAM_1'];
83         $fromsupp = $_POST['PARAM_2'];
84         $show_balance = $_POST['PARAM_3'];
85         $currency = $_POST['PARAM_4'];
86         $no_zeros = $_POST['PARAM_5'];
87         $comments = $_POST['PARAM_6'];
88         $orientation = $_POST['PARAM_7'];
89         $destination = $_POST['PARAM_8'];
90         if ($destination)
91                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
92         else
93                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
94
95         $orientation = ($orientation ? 'L' : 'P');
96         if ($fromsupp == ALL_TEXT)
97                 $supp = _('All');
98         else
99                 $supp = get_supplier_name($fromsupp);
100         $dec = user_price_dec();
101
102         if ($currency == ALL_TEXT)
103         {
104                 $convert = true;
105                 $currency = _('Balances in Home currency');
106         }
107         else
108                 $convert = false;
109
110         if ($no_zeros) $nozeros = _('Yes');
111         else $nozeros = _('No');
112
113         $cols = array(0, 95, 140, 200,  250, 320, 385, 450,     515);
114
115         $headers = array(_('Trans Type'), _('#'), _('Date'), _('Due Date'), _('Charges'),
116                 _('Credits'), _('Allocated'), _('Outstanding'));
117
118         if ($show_balance)
119                 $headers[7] = _('Balance');
120         $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
121
122     $params =   array(  0 => $comments,
123                         1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
124                         2 => array('text' => _('Supplier'), 'from' => $supp, 'to' => ''),
125                         3 => array(  'text' => _('Currency'),'from' => $currency, 'to' => ''),
126                                 4 => array('text' => _('Suppress Zeros'), 'from' => $nozeros, 'to' => ''));
127
128     $rep = new FrontReport(_('Supplier Balances'), "SupplierBalances", user_pagesize(), 9, $orientation);
129     if ($orientation == 'L')
130         recalculate_cols($cols);
131
132     $rep->Font();
133     $rep->Info($params, $cols, $headers, $aligns);
134     $rep->NewPage();
135
136         $total = array();
137         $grandtotal = array(0,0,0,0);
138
139         $sql = "SELECT supplier_id, supp_name AS name, curr_code FROM ".TB_PREF."suppliers";
140         if ($fromsupp != ALL_TEXT)
141                 $sql .= " WHERE supplier_id=".db_escape($fromsupp);
142         $sql .= " ORDER BY supp_name";
143         $result = db_query($sql, "The customers could not be retrieved");
144
145         while ($myrow=db_fetch($result))
146         {
147                 if (!$convert && $currency != $myrow['curr_code'])
148                         continue;
149                 $accumulate = 0;
150                 $rate = $convert ? get_exchange_rate_from_home_currency($myrow['curr_code'], Today()) : 1;
151                 $bal = get_open_balance($myrow['supplier_id'], $from);
152                 $init = array();
153                 $bal['charges'] = isset($bal['charges']) ? $bal['charges'] : 0;
154                 $bal['credits'] = isset($bal['credits']) ? $bal['credits'] : 0;
155                 $bal['Allocated'] = isset($bal['Allocated']) ? $bal['Allocated'] : 0;
156                 $bal['OutStanding'] = isset($bal['OutStanding']) ? $bal['OutStanding'] : 0;
157                 $init[0] = round2(abs($bal['charges']*$rate), $dec);
158                 $init[1] = round2(Abs($bal['credits']*$rate), $dec);
159                 $init[2] = round2($bal['Allocated']*$rate, $dec);
160                 if ($show_balance)
161                 {
162                         $init[3] = $init[0] - $init[1];
163                         $accumulate += $init[3];
164                 }       
165                 else    
166                         $init[3] = round2($bal['OutStanding']*$rate, $dec);
167                 $res = getTransactions($myrow['supplier_id'], $from, $to);
168                 if ($no_zeros && db_num_rows($res) == 0) continue;
169
170                 $rep->fontSize += 2;
171                 $rep->TextCol(0, 2, $myrow['name']);
172                 if ($convert) $rep->TextCol(2, 3,       $myrow['curr_code']);
173                 $rep->fontSize -= 2;
174                 $rep->TextCol(3, 4,     _("Open Balance"));
175                 $rep->AmountCol(4, 5, $init[0], $dec);
176                 $rep->AmountCol(5, 6, $init[1], $dec);
177                 $rep->AmountCol(6, 7, $init[2], $dec);
178                 $rep->AmountCol(7, 8, $init[3], $dec);
179                 $total = array(0,0,0,0);
180                 for ($i = 0; $i < 4; $i++)
181                 {
182                         $total[$i] += $init[$i];
183                         $grandtotal[$i] += $init[$i];
184                 }
185                 $rep->NewLine(1, 2);
186                 $rep->Line($rep->row + 4);
187                 if (db_num_rows($res)==0) {
188                         $rep->NewLine(1, 2);
189                         continue;
190                 }       
191                 while ($trans=db_fetch($res))
192                 {
193                         if ($no_zeros && floatcmp(abs($trans['TotalAmount']), $trans['Allocated']) == 0) continue;
194                         $rep->NewLine(1, 2);
195                         $rep->TextCol(0, 1, $systypes_array[$trans['type']]);
196                         $rep->TextCol(1, 2,     $trans['reference']);
197                         $rep->DateCol(2, 3,     $trans['tran_date'], true);
198                         if ($trans['type'] == ST_SUPPINVOICE)
199                                 $rep->DateCol(3, 4,     $trans['due_date'], true);
200                         $item[0] = $item[1] = 0.0;
201                         if ($trans['TotalAmount'] > 0.0)
202                         {
203                                 $item[0] = round2(abs($trans['TotalAmount']) * $rate, $dec);
204                                 $rep->AmountCol(4, 5, $item[0], $dec);
205                                 $accumulate += $item[0];
206                                 $item[2] = round2($trans['Allocated'] * $rate, $dec);
207                         }
208                         else
209                         {
210                                 $item[1] = round2(abs($trans['TotalAmount']) * $rate, $dec);
211                                 $rep->AmountCol(5, 6, $item[1], $dec);
212                                 $accumulate -= $item[1];
213                                 $item[2] = round2($trans['Allocated'] * $rate, $dec) * -1;
214                         }
215                         $rep->AmountCol(6, 7, $item[2], $dec);
216                         if ($trans['TotalAmount'] > 0.0)
217                                 $item[3] = $item[0] - $item[2];
218                         else    
219                                 $item[3] = -$item[1] - $item[2];
220                         if ($show_balance)      
221                                 $rep->AmountCol(7, 8, $accumulate, $dec);
222                         else    
223                                 $rep->AmountCol(7, 8, $item[3], $dec);
224                         for ($i = 0; $i < 4; $i++)
225                         {
226                                 $total[$i] += $item[$i];
227                                 $grandtotal[$i] += $item[$i];
228                         }
229                         if ($show_balance)
230                                 $total[3] = $total[0] - $total[1];
231                 }
232                 $rep->Line($rep->row - 8);
233                 $rep->NewLine(2);
234                 $rep->TextCol(0, 3,     _('Total'));
235                 for ($i = 0; $i < 4; $i++)
236                 {
237                         $rep->AmountCol($i + 4, $i + 5, $total[$i], $dec);
238                         $total[$i] = 0.0;
239                 }
240         $rep->Line($rep->row  - 4);
241         $rep->NewLine(2);
242         }
243         $rep->fontSize += 2;
244         $rep->TextCol(0, 3,     _('Grand Total'));
245         $rep->fontSize -= 2;
246         if ($show_balance)
247                 $grandtotal[3] = $grandtotal[0] - $grandtotal[1];
248         for ($i = 0; $i < 4; $i++)
249                 $rep->AmountCol($i + 4, $i + 5,$grandtotal[$i], $dec);
250         $rep->Line($rep->row  - 4);
251         $rep->NewLine();
252     $rep->End();
253 }
254