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