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