The aging reports / inquiries had a 1 day error in presentation. Fixed.
[fa-stable.git] / reporting / rep102.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 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Aged Customer 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_aged_customer_analysis();
29
30 function get_invoices($customer_id, $to, $all=true)
31 {
32         $todate = date2sql($to);
33         $current = 1;
34         $PastDueDays1 = get_company_pref('past_due_days') + $current;
35         $PastDueDays2 = 2 * $PastDueDays1 + $current;
36
37         // Revomed allocated from sql
38         if ($all)
39         $value = "(".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + "
40                         .TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_freight_tax + "
41                         .TB_PREF."debtor_trans.ov_discount)";
42         else            
43         $value = "(".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + "
44                         .TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_freight_tax + "
45                         .TB_PREF."debtor_trans.ov_discount - ".TB_PREF."debtor_trans.alloc)";
46         $due = "IF (".TB_PREF."debtor_trans.type=".ST_SALESINVOICE.",".TB_PREF."debtor_trans.due_date,".TB_PREF."debtor_trans.tran_date)";
47         $sql = "SELECT ".TB_PREF."debtor_trans.type, ".TB_PREF."debtor_trans.reference,
48                 ".TB_PREF."debtor_trans.tran_date,
49                 $value as Balance,
50                 IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $current,$value,0) AS Due,
51                 IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $PastDueDays1,$value,0) AS Overdue1,
52                 IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $PastDueDays2,$value,0) AS Overdue2
53
54                 FROM ".TB_PREF."debtors_master,
55                         ".TB_PREF."debtor_trans
56
57                 WHERE ".TB_PREF."debtor_trans.type <> ".ST_CUSTDELIVERY."
58                         AND ".TB_PREF."debtors_master.debtor_no = ".TB_PREF."debtor_trans.debtor_no
59                         AND ".TB_PREF."debtor_trans.debtor_no = $customer_id 
60                         AND ".TB_PREF."debtor_trans.tran_date <= '$todate'
61                         AND ABS(".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_freight_tax + ".TB_PREF."debtor_trans.ov_discount) > ".FLOAT_COMP_DELTA." ";
62         if (!$all)
63                 $sql .= "AND ABS(".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_freight_tax + ".TB_PREF."debtor_trans.ov_discount - ".TB_PREF."debtor_trans.alloc) > ".FLOAT_COMP_DELTA." ";  
64         $sql .= "ORDER BY ".TB_PREF."debtor_trans.tran_date";
65
66         return db_query($sql, "The customer details could not be retrieved");
67 }
68
69 //----------------------------------------------------------------------------------------------------
70
71 function print_aged_customer_analysis()
72 {
73     global $path_to_root, $systypes_array;
74
75         $to = $_POST['PARAM_0'];
76         $fromcust = $_POST['PARAM_1'];
77         $currency = $_POST['PARAM_2'];
78         $show_all = $_POST['PARAM_3'];
79         $summaryOnly = $_POST['PARAM_4'];
80         $no_zeros = $_POST['PARAM_5'];
81         $graphics = $_POST['PARAM_6'];
82         $comments = $_POST['PARAM_7'];
83         $orientation = $_POST['PARAM_8'];
84         $destination = $_POST['PARAM_9'];
85         if ($destination)
86                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
87         else
88                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
89         $orientation = ($orientation ? 'L' : 'P');
90         if ($graphics)
91         {
92                 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
93                 $pg = new graph();
94         }
95
96         if ($fromcust == ALL_TEXT)
97                 $from = _('All');
98         else
99                 $from = get_customer_name($fromcust);
100         $dec = user_price_dec();
101
102         if ($summaryOnly == 1)
103                 $summary = _('Summary Only');
104         else
105                 $summary = _('Detailed Report');
106         if ($currency == ALL_TEXT)
107         {
108                 $convert = true;
109                 $currency = _('Balances in Home Currency');
110         }
111         else
112                 $convert = false;
113
114         if ($no_zeros) $nozeros = _('Yes');
115         else $nozeros = _('No');
116         if ($show_all) $show = _('Yes');
117         else $show = _('No');
118
119         $PastDueDays1 = get_company_pref('past_due_days');
120         $PastDueDays2 = 2 * $PastDueDays1;
121         $nowdue = "1-" . $PastDueDays1 . " " . _('Days');
122         $pastdue1 = $PastDueDays1 + 1 . "-" . $PastDueDays2 . " " . _('Days');
123         $pastdue2 = _('Over') . " " . $PastDueDays2 . " " . _('Days');
124
125         $cols = array(0, 100, 130, 190, 250, 320, 385, 450,     515);
126         $headers = array(_('Customer'), '',     '',     _('Current'), $nowdue, $pastdue1, $pastdue2,
127                 _('Total Balance'));
128
129         $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right',     'right');
130
131         $params =   array(      0 => $comments,
132                                 1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
133                                 2 => array('text' => _('Customer'),     'from' => $from, 'to' => ''),
134                                 3 => array('text' => _('Currency'), 'from' => $currency, 'to' => ''),
135                                 4 => array('text' => _('Type'),         'from' => $summary,'to' => ''),
136                     5 => array('text' => _('Show Also Allocated'), 'from' => $show, 'to' => ''),                
137                                 6 => array('text' => _('Suppress Zeros'), 'from' => $nozeros, 'to' => ''));
138
139         if ($convert)
140                 $headers[2] = _('Currency');
141     $rep = new FrontReport(_('Aged Customer Analysis'), "AgedCustomerAnalysis", user_pagesize(), 9, $orientation);
142     if ($orientation == 'L')
143         recalculate_cols($cols);
144  
145     $rep->Font();
146     $rep->Info($params, $cols, $headers, $aligns);
147     $rep->NewPage();
148
149         $total = array(0,0,0,0, 0);
150
151         $sql = "SELECT debtor_no, name, curr_code FROM ".TB_PREF."debtors_master";
152         if ($fromcust != ALL_TEXT)
153                 $sql .= " WHERE debtor_no=".db_escape($fromcust);
154         $sql .= " ORDER BY name";
155         $result = db_query($sql, "The customers could not be retrieved");
156
157         while ($myrow=db_fetch($result))
158         {
159                 if (!$convert && $currency != $myrow['curr_code'])
160                         continue;
161
162                 if ($convert) $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $to);
163                 else $rate = 1.0;
164                 $custrec = get_customer_details($myrow['debtor_no'], $to, $show_all);
165                 if (!$custrec)
166                         continue;
167                 $custrec['Balance'] *= $rate;
168                 $custrec['Due'] *= $rate;
169                 $custrec['Overdue1'] *= $rate;
170                 $custrec['Overdue2'] *= $rate;
171                 $str = array($custrec["Balance"] - $custrec["Due"],
172                         $custrec["Due"]-$custrec["Overdue1"],
173                         $custrec["Overdue1"]-$custrec["Overdue2"],
174                         $custrec["Overdue2"],
175                         $custrec["Balance"]);
176                 if ($no_zeros && floatcmp(array_sum($str), 0) == 0) continue;
177
178                 $rep->fontSize += 2;
179                 $rep->TextCol(0, 2, $myrow['name']);
180                 if ($convert) $rep->TextCol(2, 3,       $myrow['curr_code']);
181                 $rep->fontSize -= 2;
182                 $total[0] += ($custrec["Balance"] - $custrec["Due"]);
183                 $total[1] += ($custrec["Due"]-$custrec["Overdue1"]);
184                 $total[2] += ($custrec["Overdue1"]-$custrec["Overdue2"]);
185                 $total[3] += $custrec["Overdue2"];
186                 $total[4] += $custrec["Balance"];
187                 for ($i = 0; $i < count($str); $i++)
188                         $rep->AmountCol($i + 3, $i + 4, $str[$i], $dec);
189                 $rep->NewLine(1, 2);
190                 if (!$summaryOnly)
191                 {
192                         $res = get_invoices($myrow['debtor_no'], $to, $show_all);
193                 if (db_num_rows($res)==0)
194                                 continue;
195                 $rep->Line($rep->row + 4);
196                         while ($trans=db_fetch($res))
197                         {
198                                 $rep->NewLine(1, 2);
199                         $rep->TextCol(0, 1, $systypes_array[$trans['type']], -2);
200                                 $rep->TextCol(1, 2,     $trans['reference'], -2);
201                                 $rep->DateCol(2, 3, $trans['tran_date'], true, -2);
202                                 if ($trans['type'] == ST_CUSTCREDIT || $trans['type'] == ST_CUSTPAYMENT || $trans['type'] == ST_BANKDEPOSIT)
203                                 {
204                                         $trans['Balance'] *= -1;
205                                         $trans['Due'] *= -1;
206                                         $trans['Overdue1'] *= -1;
207                                         $trans['Overdue2'] *= -1;
208                                 }
209                                 foreach ($trans as $i => $value)
210                                         $trans[$i] *= $rate;
211                                 $str = array($trans["Balance"] - $trans["Due"],
212                                         $trans["Due"]-$trans["Overdue1"],
213                                         $trans["Overdue1"]-$trans["Overdue2"],
214                                         $trans["Overdue2"],
215                                         $trans["Balance"]);
216                                 for ($i = 0; $i < count($str); $i++)
217                                         $rep->AmountCol($i + 3, $i + 4, $str[$i], $dec);
218                         }
219                         $rep->Line($rep->row - 8);
220                         $rep->NewLine(2);
221                 }
222         }
223         if ($summaryOnly)
224         {
225         $rep->Line($rep->row  + 4);
226         $rep->NewLine();
227         }
228         $rep->fontSize += 2;
229         $rep->TextCol(0, 3, _('Grand Total'));
230         $rep->fontSize -= 2;
231         for ($i = 0; $i < count($total); $i++)
232         {
233                 $rep->AmountCol($i + 3, $i + 4, $total[$i], $dec);
234                 if ($graphics && $i < count($total) - 1)
235                 {
236                         $pg->y[$i] = abs($total[$i]);
237                 }
238         }
239         $rep->Line($rep->row - 8);
240         if ($graphics)
241         {
242                 global $decseps, $graph_skin;
243                 $pg->x = array(_('Current'), $nowdue, $pastdue1, $pastdue2);
244                 $pg->title     = $rep->title;
245                 $pg->axis_x    = _("Days");
246                 $pg->axis_y    = _("Amount");
247                 $pg->graphic_1 = $to;
248                 $pg->type      = $graphics;
249                 $pg->skin      = $graph_skin;
250                 $pg->built_in  = false;
251                 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
252                 $filename = company_path(). "/pdf_files/". uniqid("").".png";
253                 $pg->display($filename, true);
254                 $w = $pg->width / 1.5;
255                 $h = $pg->height / 1.5;
256                 $x = ($rep->pageWidth - $w) / 2;
257                 $rep->NewLine(2);
258                 if ($rep->row - $h < $rep->bottomMargin)
259                         $rep->NewPage();
260                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
261         }
262         $rep->NewLine();
263     $rep->End();
264 }
265
266 ?>