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