1e0685969480b3eec364126a2b1fc291795ace97
[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[0] = $init[1] = 0.0;
152         $init[0] = round2(abs($bal['charges']*$rate), $dec);
153         $init[1] = round2(Abs($bal['credits']*$rate), $dec);
154         $init[2] = round2($bal['Allocated']*$rate, $dec);
155
156         $init[3] = $init[1] - $init[0];
157         $accumulate += $init[3];
158
159         $res = getTransactions($myrow['supplier_id'], $from, $to);
160
161         $total = array(0,0,0,0);
162         for ($i = 0; $i < 4; $i++)
163         {
164             $total[$i] += $init[$i];
165             $grandtotal[$i] += $init[$i];
166         }
167
168         if (db_num_rows($res) == 0 && !$no_zeros) 
169         {
170             $rep->TextCol(0, 2, $myrow['name']);
171             $rep->AmountCol(3, 4, $init[3], $dec);
172             $rep->AmountCol(7, 8, $init[3], $dec);
173             //$rep->Line($rep->row  - 2);
174                 $rep->NewLine();
175
176             continue;
177         }
178         $curr_db = $curr_cr = 0;
179         while ($trans=db_fetch($res))
180         {
181             //if ($no_zeros && floatcmp(abs($trans['TotalAmount']), $trans['Allocated']) == 0) continue;
182             $item[0] = $item[1] = 0.0;
183             if ($trans['TotalAmount'] > 0.0)
184             {
185                 $item[0] = round2(abs($trans['TotalAmount']) * $rate, $dec);
186                 $curr_cr += $item[0];
187                 $tot_cur_cr += $item[0];
188
189                 $accumulate -= $item[0];
190             }
191             else
192             {
193                 $item[1] = round2(abs($trans['TotalAmount']) * $rate, $dec);
194                 $curr_db += $item[1];
195                 $tot_cur_db += $item[1];
196                 $accumulate += $item[1];
197             }
198             $item[2] = round2($trans['Allocated'] * $rate, $dec);
199             if ($trans['TotalAmount'] > 0.0)
200                 $item[3] = $item[2] - $item[0];
201             else
202                 $item[3] = ($item[2] - $item[1]) * -1;
203
204             for ($i = 0; $i < 4; $i++)
205             {
206                 $total[$i] += $item[$i];
207                 $grandtotal[$i] += $item[$i];
208             }
209             $total[3] = $total[1] - $total[0];
210         }
211                 if ($no_zeros && $total[3] == 0.0 && $curr_db == 0.0 && $curr_cr == 0.0) continue;
212         $rep->TextCol(0, 2, $myrow['name']);
213         $rep->AmountCol(3, 4, $total[3] + $curr_cr - $curr_db, $dec);
214         $rep->AmountCol(4, 5, $curr_db, $dec);
215         $rep->AmountCol(5, 6, $curr_cr, $dec);
216         $rep->AmountCol(7, 8, $total[3], $dec);
217         for ($i = 2; $i < 4; $i++)
218         {
219             $total[$i] = 0.0;
220         }
221         //$rep->Line($rep->row  - 2);
222         $rep->NewLine();
223     }
224     $rep->Line($rep->row + 4); // added line by Joe
225     $rep->NewLine();
226     $rep->fontSize += 2;
227     $rep->TextCol(0, 3,    _('Grand Total'));
228     $rep->fontSize -= 2;
229
230     $grandtotal[3] = $grandtotal[1] - $grandtotal[0];
231
232         $rep->AmountCol(3, 4,$grandtotal[3] - $tot_cur_db + $tot_cur_cr, $dec);
233
234         $rep->AmountCol(4, 5,$tot_cur_db, $dec);
235         $rep->AmountCol(5, 6,$tot_cur_cr, $dec);
236
237     $rep->AmountCol(7, 8,$grandtotal[3], $dec);
238     $rep->Line($rep->row - 6, 1);
239     $rep->End();
240 }