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