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