A couple of small bugs in customer and supplier balances report fixed.
[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         $currency = $_POST['PARAM_3'];
92         $no_zeros = $_POST['PARAM_4'];
93         $comments = $_POST['PARAM_5'];
94         $orientation = $_POST['PARAM_6'];
95         $destination = $_POST['PARAM_7'];
96         if ($destination)
97                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
98         else
99                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
100
101         $orientation = ($orientation ? 'L' : 'P');
102         if ($fromsupp == ALL_TEXT)
103                 $supp = _('All');
104         else
105                 $supp = get_supplier_name($fromsupp);
106         $dec = user_price_dec();
107
108         if ($currency == ALL_TEXT)
109         {
110                 $convert = true;
111                 $currency = _('Balances in Home currency');
112         }
113         else
114                 $convert = false;
115
116         if ($no_zeros) $nozeros = _('Yes');
117         else $nozeros = _('No');
118
119         $cols = array(0, 100, 130, 190, 250, 320, 385, 450,     515);
120
121         $headers = array(_('Trans Type'), _('#'), _('Date'), _('Due Date'), _('Charges'),
122                 _('Credits'), _('Allocated'), _('Outstanding'));
123
124         $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
125
126     $params =   array(  0 => $comments,
127                         1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
128                         2 => array('text' => _('Supplier'), 'from' => $supp, 'to' => ''),
129                         3 => array(  'text' => _('Currency'),'from' => $currency, 'to' => ''),
130                         4 => array('text' => _('Suppress Zeros'), 'from' => $nozeros, 'to' => ''));
131
132     $rep = new FrontReport(_('Supplier Balances'), "SupplierBalances", user_pagesize(), 9, $orientation);
133     if ($orientation == 'L')
134         recalculate_cols($cols);
135
136     $rep->Font();
137     $rep->Info($params, $cols, $headers, $aligns);
138     $rep->NewPage();
139
140         $total = array();
141         $grandtotal = array(0,0,0,0);
142
143         $sql = "SELECT supplier_id, supp_name AS name, curr_code FROM ".TB_PREF."suppliers";
144         if ($fromsupp != ALL_TEXT)
145                 $sql .= " WHERE supplier_id=".db_escape($fromsupp);
146         $sql .= " ORDER BY supp_name";
147         $result = db_query($sql, "The customers could not be retrieved");
148
149         while ($myrow=db_fetch($result))
150         {
151                 if (!$convert && $currency != $myrow['curr_code'])
152                         continue;
153                 $bal = get_open_balance($myrow['supplier_id'], $from, $convert);
154                 $init[0] = $init[1] = 0.0;
155                 $init[0] = round2(abs($bal['charges']), $dec);
156                 $init[1] = round2(Abs($bal['credits']), $dec);
157                 $init[2] = round2($bal['Allocated'], $dec);
158                 $init[3] = round2($bal['OutStanding'], $dec);;
159                 $total = array(0,0,0,0);
160                 for ($i = 0; $i < 4; $i++)
161                 {
162                         $total[$i] += $init[$i];
163                         $grandtotal[$i] += $init[$i];
164                 }
165                 $res = getTransactions($myrow['supplier_id'], $from, $to);
166                 if ($no_zeros && db_num_rows($res) == 0) continue;
167
168                 $rep->fontSize += 2;
169                 $rep->TextCol(0, 2, $myrow['name']);
170                 if ($convert) $rep->TextCol(2, 3,       $myrow['curr_code']);
171                 $rep->fontSize -= 2;
172                 $rep->TextCol(3, 4,     _("Open Balance"));
173                 $rep->AmountCol(4, 5, $init[0], $dec);
174                 $rep->AmountCol(5, 6, $init[1], $dec);
175                 $rep->AmountCol(6, 7, $init[2], $dec);
176                 $rep->AmountCol(7, 8, $init[3], $dec);
177                 $rep->NewLine(1, 2);
178                 if (db_num_rows($res)==0) continue;
179
180                 $rep->Line($rep->row + 4);
181                 while ($trans=db_fetch($res))
182                 {
183                         if ($no_zeros && floatcmp(abs($trans['TotalAmount']), $trans['Allocated']) == 0) continue;
184                         $rep->NewLine(1, 2);
185                         $rep->TextCol(0, 1, $systypes_array[$trans['type']]);
186                         $rep->TextCol(1, 2,     $trans['reference']);
187                         $rep->DateCol(2, 3,     $trans['tran_date'], true);
188                         if ($trans['type'] == ST_SUPPINVOICE)
189                                 $rep->DateCol(3, 4,     $trans['due_date'], true);
190                         $item[0] = $item[1] = 0.0;
191                         if ($convert)
192                                 $rate = $trans['rate'];
193                         else
194                                 $rate = 1.0;
195                         if ($trans['TotalAmount'] > 0.0)
196                         {
197                                 $item[0] = round2(abs($trans['TotalAmount']) * $rate, $dec);
198                                 $rep->AmountCol(4, 5, $item[0], $dec);
199                         }
200                         else
201                         {
202                                 $item[1] = round2(abs($trans['TotalAmount']) * $rate, $dec);
203                                 $rep->AmountCol(5, 6, $item[1], $dec);
204                         }
205                         $item[2] = round2($trans['Allocated'] * $rate, $dec);
206                         $rep->AmountCol(6, 7, $item[2], $dec);
207                         if ($trans['TotalAmount'] > 0.0)
208                                 $item[3] = $item[0] - $item[2];
209                         else    
210                                 $item[3] = ($item[1] - $item[2]) * -1;
211                         $rep->AmountCol(7, 8, $item[3], $dec);
212                         for ($i = 0; $i < 4; $i++)
213                         {
214                                 $total[$i] += $item[$i];
215                                 $grandtotal[$i] += $item[$i];
216                         }
217                 }
218                 $rep->Line($rep->row - 8);
219                 $rep->NewLine(2);
220                 $rep->TextCol(0, 3,     _('Total'));
221                 for ($i = 0; $i < 4; $i++)
222                 {
223                         $rep->AmountCol($i + 4, $i + 5, $total[$i], $dec);
224                         $total[$i] = 0.0;
225                 }
226         $rep->Line($rep->row  - 4);
227         $rep->NewLine(2);
228         }
229         $rep->fontSize += 2;
230         $rep->TextCol(0, 3,     _('Grand Total'));
231         $rep->fontSize -= 2;
232         for ($i = 0; $i < 4; $i++)
233                 $rep->AmountCol($i + 4, $i + 5,$grandtotal[$i], $dec);
234         $rep->Line($rep->row  - 4);
235         $rep->NewLine();
236     $rep->End();
237 }
238
239 ?>