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