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