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