Report fixed according to changed headers API in FrontReport class.
[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.", (".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + 
35         ".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.", (".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + 
40         ".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((".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + 
49         ".TB_PREF."supp_trans.ov_discount - ".TB_PREF."supp_trans.alloc)";
50     if ($convert)
51         $sql .= " * rate";
52     $sql .= ") AS OutStanding
53                 FROM ".TB_PREF."supp_trans
54         WHERE ".TB_PREF."supp_trans.tran_date < '$to'
55                 AND ".TB_PREF."supp_trans.supplier_id = '$supplier_id' GROUP BY supplier_id";
56
57     $result = db_query($sql,"No transactions were returned");
58     return db_fetch($result);
59 }
60
61 function getTransactions($supplier_id, $from, $to)
62 {
63         $from = date2sql($from);
64         $to = date2sql($to);
65
66     $sql = "SELECT ".TB_PREF."supp_trans.*,
67                                 (".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount)
68                                 AS TotalAmount, ".TB_PREF."supp_trans.alloc AS Allocated,
69                                 ((".TB_PREF."supp_trans.type = ".ST_SUPPINVOICE.")
70                                         AND ".TB_PREF."supp_trans.due_date < '$to') AS OverDue
71                         FROM ".TB_PREF."supp_trans
72                         WHERE ".TB_PREF."supp_trans.tran_date >= '$from' AND ".TB_PREF."supp_trans.tran_date <= '$to' 
73                         AND ".TB_PREF."supp_trans.supplier_id = '$supplier_id'
74                                 ORDER BY ".TB_PREF."supp_trans.tran_date";
75
76     $TransResult = db_query($sql,"No transactions were returned");
77
78     return $TransResult;
79 }
80
81 //----------------------------------------------------------------------------------------------------
82
83 function print_supplier_balances()
84 {
85     global $path_to_root, $systypes_array;
86
87     $from = $_POST['PARAM_0'];
88     $to = $_POST['PARAM_1'];
89     $fromsupp = $_POST['PARAM_2'];
90     $currency = $_POST['PARAM_3'];
91     $comments = $_POST['PARAM_4'];
92         $destination = $_POST['PARAM_5'];
93         if ($destination)
94                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
95         else
96                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
97
98         if ($fromsupp == ALL_NUMERIC)
99                 $supp = _('All');
100         else
101                 $supp = get_supplier_name($fromsupp);
102     $dec = user_price_dec();
103
104         if ($currency == ALL_TEXT)
105         {
106                 $convert = true;
107                 $currency = _('Balances in Home currency');
108         }
109         else
110                 $convert = false;
111
112         $cols = array(0, 100, 130, 190, 250, 320, 385, 450,     515);
113
114         $headers = array(_('Trans Type'), _('#'), _('Date'), _('Due Date'), _('Charges'),
115                 _('Credits'), _('Allocated'), _('Outstanding'));
116
117         $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
118
119     $params =   array(  0 => $comments,
120                                     1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
121                                     2 => array('text' => _('Supplier'), 'from' => $supp, 'to' => ''),
122                                     3 => array(  'text' => _('Currency'),'from' => $currency, 'to' => ''));
123
124     $rep = new FrontReport(_('Supplier Balances'), "SupplierBalances", user_pagesize());
125
126     $rep->Font();
127     $rep->Info($params, $cols, $headers, $aligns);
128     $rep->NewPage();
129
130         $total = array();
131         $grandtotal = array(0,0,0,0);
132
133         $sql = "SELECT supplier_id, supp_name AS name, curr_code FROM ".TB_PREF."suppliers";
134         if ($fromsupp != ALL_NUMERIC)
135                 $sql .= " WHERE supplier_id=".db_escape($fromsupp);
136         $sql .= " ORDER BY supp_name";
137         $result = db_query($sql, "The customers could not be retrieved");
138
139         while ($myrow=db_fetch($result))
140         {
141                 if (!$convert && $currency != $myrow['curr_code'])
142                         continue;
143                 $rep->fontSize += 2;
144                 $rep->TextCol(0, 2, $myrow['name']);
145                 if ($convert)
146                         $rep->TextCol(2, 3,     $myrow['curr_code']);
147                 $rep->fontSize -= 2;
148                 $bal = get_open_balance($myrow['supplier_id'], $from, $convert);
149                 $init[0] = $init[1] = 0.0;
150                 $rep->TextCol(3, 4,     _("Open Balance"));
151                 $init[0] = round2(abs($bal['charges']), $dec);
152                 $rep->AmountCol(4, 5, $init[0], $dec);
153                 $init[1] = round2(Abs($bal['credits']), $dec);
154                 $rep->AmountCol(5, 6, $init[1], $dec);
155                 $init[2] = round2($bal['Allocated'], $dec);
156                 $rep->AmountCol(6, 7, $init[2], $dec);
157                 $init[3] = round2($bal['OutStanding'], $dec);;
158                 $rep->AmountCol(7, 8, $init[3], $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                 $rep->NewLine(1, 2);
166                 $res = getTransactions($myrow['supplier_id'], $from, $to);
167                 if (db_num_rows($res)==0)
168                         continue;
169                 $rep->Line($rep->row + 4);
170                 while ($trans=db_fetch($res))
171                 {
172                         $rep->NewLine(1, 2);
173                         $rep->TextCol(0, 1, $systypes_array[$trans['type']]);
174                         $rep->TextCol(1, 2,     $trans['reference']);
175                         $rep->DateCol(2, 3,     $trans['tran_date'], true);
176                         if ($trans['type'] == ST_SUPPINVOICE)
177                                 $rep->DateCol(3, 4,     $trans['due_date'], true);
178                         $item[0] = $item[1] = 0.0;
179                         if ($convert)
180                                 $rate = $trans['rate'];
181                         else
182                                 $rate = 1.0;
183                         if ($trans['TotalAmount'] > 0.0)
184                         {
185                                 $item[0] = round2(abs($trans['TotalAmount']) * $rate, $dec);
186                                 $rep->AmountCol(4, 5, $item[0], $dec);
187                         }
188                         else
189                         {
190                                 $item[1] = round2(abs($trans['TotalAmount']) * $rate, $dec);
191                                 $rep->AmountCol(5, 6, $item[1], $dec);
192                         }
193                         $item[2] = round2($trans['Allocated'] * $rate, $dec);
194                         $rep->AmountCol(6, 7, $item[2], $dec);
195                         /*
196                         if ($trans['type'] == 20)
197                                 $item[3] = ($trans['TotalAmount'] - $trans['Allocated']) * $rate;
198                         else
199                                 $item[3] = ($trans['TotalAmount'] + $trans['Allocated']) * $rate;
200                         */      
201                         if ($trans['type'] == ST_SUPPINVOICE || $trans['type'] == ST_BANKDEPOSIT)
202                                 $item[3] = $item[0] + $item[1] - $item[2];
203                         else    
204                                 $item[3] = $item[0] - $item[1] + $item[2];
205                         $rep->AmountCol(7, 8, $item[3], $dec);
206                         for ($i = 0; $i < 4; $i++)
207                         {
208                                 $total[$i] += $item[$i];
209                                 $grandtotal[$i] += $item[$i];
210                         }
211                 }
212                 $rep->Line($rep->row - 8);
213                 $rep->NewLine(2);
214                 $rep->TextCol(0, 3,     _('Total'));
215                 for ($i = 0; $i < 4; $i++)
216                 {
217                         $rep->AmountCol($i + 4, $i + 5, $total[$i], $dec);
218                         $total[$i] = 0.0;
219                 }
220         $rep->Line($rep->row  - 4);
221         $rep->NewLine(2);
222         }
223         $rep->fontSize += 2;
224         $rep->TextCol(0, 3,     _('Grand Total'));
225         $rep->fontSize -= 2;
226         for ($i = 0; $i < 4; $i++)
227                 $rep->AmountCol($i + 4, $i + 5,$grandtotal[$i], $dec);
228         $rep->Line($rep->row  - 4);
229         $rep->NewLine();
230     $rep->End();
231 }
232
233 ?>