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