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