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