Release 1.0.1 established on SourceForge, fixing the bugs and including a Date Picker...
[fa-stable.git] / reporting / rep202.php
1 <?php
2
3 $page_security = 2;
4 // ----------------------------------------------------------------
5 // $ Revision:  2.0 $
6 // Creator:     Joe Hunt
7 // date_:       2005-05-19
8 // Title:       Ages Supplier Analysis
9 // ----------------------------------------------------------------
10 $path_to_root="../";
11
12 include_once($path_to_root . "includes/session.inc");
13 include_once($path_to_root . "includes/date_functions.inc");
14 include_once($path_to_root . "includes/data_checks.inc");
15 include_once($path_to_root . "gl/includes/gl_db.inc");
16
17 //----------------------------------------------------------------------------------------------------
18
19 // trial_inquiry_controls();
20 print_aged_supplier_analysis();
21
22 //----------------------------------------------------------------------------------------------------
23
24 function get_invoices($supplier_id, $to)
25 {
26         $todate = date2sql($to);
27         $PastDueDays1 = get_company_pref('past_due_days');
28         $PastDueDays2 = 2 * $PastDueDays1;
29
30         // Revomed allocated from sql
31         $sql = "SELECT ".TB_PREF."sys_types.type_name, 
32                         ".TB_PREF."supp_trans.reference, 
33                         ".TB_PREF."supp_trans.tran_date, 
34                         (".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount) as Balance,
35                         IF (".TB_PREF."payment_terms.days_before_due > 0,
36                                 CASE WHEN TO_DAYS('$todate') - TO_DAYS(".TB_PREF."supp_trans.tran_date) >= ".TB_PREF."payment_terms.days_before_due 
37                                 THEN 
38                                         ".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount 
39                                 ELSE
40                                         0 
41                                 END,
42                                 
43                                 CASE WHEN TO_DAYS('$todate') - TO_DAYS(DATE_ADD(DATE_ADD(".TB_PREF."supp_trans.tran_date, 
44                                         INTERVAL 1 MONTH), INTERVAL (".TB_PREF."payment_terms.day_in_following_month - 
45                                         DAYOFMONTH(".TB_PREF."supp_trans.tran_date)) DAY)) >= 0 
46                                 THEN 
47                                         ".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount 
48                                 ELSE 
49                                         0 
50                                 END
51                         ) AS Due,
52                         IF (".TB_PREF."payment_terms.days_before_due > 0,
53                                 CASE WHEN TO_DAYS('$todate') - TO_DAYS(".TB_PREF."supp_trans.tran_date) > ".TB_PREF."payment_terms.days_before_due 
54                                         AND TO_DAYS('$todate') - TO_DAYS(".TB_PREF."supp_trans.tran_date) >= (".TB_PREF."payment_terms.days_before_due + $PastDueDays1) 
55                                 THEN 
56                                         ".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount 
57                                 ELSE 
58                                         0 
59                                 END,
60
61                                 CASE WHEN TO_DAYS('$todate') - TO_DAYS(DATE_ADD(DATE_ADD(".TB_PREF."supp_trans.tran_date, 
62                                         INTERVAL 1 MONTH), INTERVAL (".TB_PREF."payment_terms.day_in_following_month - 
63                                         DAYOFMONTH(".TB_PREF."supp_trans.tran_date)) DAY)) >= $PastDueDays1 
64                                 THEN 
65                                         ".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount 
66                                 ELSE 
67                                         0 
68                                 END
69                         ) AS Overdue1,
70                         IF (".TB_PREF."payment_terms.days_before_due > 0,
71                                 CASE WHEN TO_DAYS('$todate') - TO_DAYS(".TB_PREF."supp_trans.tran_date) > ".TB_PREF."payment_terms.days_before_due 
72                                         AND TO_DAYS('$todate') - TO_DAYS(".TB_PREF."supp_trans.tran_date) >= (".TB_PREF."payment_terms.days_before_due + $PastDueDays2) 
73                                 THEN 
74                                         ".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount 
75                                 ELSE 
76                                         0 
77                                 END,
78
79                                 CASE WHEN TO_DAYS('$todate') - TO_DAYS(DATE_ADD(DATE_ADD(".TB_PREF."supp_trans.tran_date,
80                                         INTERVAL 1 MONTH), INTERVAL (".TB_PREF."payment_terms.day_in_following_month - 
81                                         DAYOFMONTH(".TB_PREF."supp_trans.tran_date)) DAY)) >= $PastDueDays2 
82                                 THEN 
83                                         ".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount 
84                                 ELSE 
85                                         0 
86                                 END
87                         ) AS Overdue2
88            
89                         FROM ".TB_PREF."suppliers, 
90                                 ".TB_PREF."payment_terms, 
91                                 ".TB_PREF."supp_trans, 
92                                 ".TB_PREF."sys_types
93            
94                         WHERE ".TB_PREF."sys_types.type_id = ".TB_PREF."supp_trans.type 
95                                 AND ".TB_PREF."suppliers.payment_terms = ".TB_PREF."payment_terms.terms_indicator 
96                                 AND ".TB_PREF."suppliers.supplier_id = ".TB_PREF."supp_trans.supplier_id
97                                 AND ".TB_PREF."supp_trans.supplier_id = $supplier_id 
98                                 AND ".TB_PREF."supp_trans.tran_date <= '$todate' 
99                                 AND ABS(".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount) > 0.004
100                                 ORDER BY ".TB_PREF."supp_trans.tran_date";
101
102
103         return db_query($sql, "The supplier details could not be retrieved");
104 }
105
106 //----------------------------------------------------------------------------------------------------
107
108 function print_aged_supplier_analysis()
109 {
110     global $path_to_root;
111
112     include_once($path_to_root . "reporting/includes/pdf_report.inc");
113
114     $to = $_POST['PARAM_0'];
115     $fromsupp = $_POST['PARAM_1'];
116     $currency = $_POST['PARAM_2'];
117         $summaryOnly = $_POST['PARAM_3'];
118     $graphics = $_POST['PARAM_4'];
119     $comments = $_POST['PARAM_5'];
120         if ($graphics)
121         {
122                 include_once($path_to_root . "reporting/includes/class.graphic.inc");
123                 $pg = new graph();
124         }       
125     
126         if ($fromsupp == reserved_words::get_all_numeric())
127                 $from = _('All');
128         else
129                 $from = get_supplier_name($fromsupp);
130     $dec = user_price_dec();
131
132         if ($summaryOnly == 1)
133                 $summary = _('Summary Only');
134         else
135                 $summary = _('Detailed Report');
136         if ($currency == reserved_words::get_all())
137         {
138                 $convert = true;
139                 $currency = _('Balances in Home Currency');
140         }
141         else
142                 $convert = false;
143         $PastDueDays1 = get_company_pref('past_due_days');
144         $PastDueDays2 = 2 * $PastDueDays1;
145         $nowdue = "1-" . $PastDueDays1 . " " . _('Days');
146         $pastdue1 = $PastDueDays1 + 1 . "-" . $PastDueDays2 . " " . _('Days');
147         $pastdue2 = _('Over') . " " . $PastDueDays2 . " " . _('Days');
148
149         $cols = array(0, 100, 130, 190, 250, 320, 385, 450,     515);
150
151         $headers = array(_('Supplier'), '',     '',     _('Current'), $nowdue, $pastdue1,$pastdue2,
152                 _('Total Balance'));
153         
154         $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right',     'right');
155
156     $params =   array(  0 => $comments,
157                                     1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
158                                     2 => array('text' => _('Supplier'), 'from' => $from, 'to' => ''),
159                                     3 => array('text' => _('Currency'),'from' => $currency,'to' => ''),
160                         4 => array('text' => _('Type'), 'from' => $summary,'to' => ''));
161
162         if ($convert)
163                 $headers[2] = _('currency');
164     $rep = new FrontReport(_('Aged Supplier Analysis'), "AgedSupplierAnalysis.pdf", user_pagesize());
165
166     $rep->Font();
167     $rep->Info($params, $cols, $headers, $aligns);
168     $rep->Header();
169
170         $total = array();
171         $total[0] = $total[1] = $total[2] = $total[3] = $total[4] = 0.0;
172         $PastDueDays1 = get_company_pref('past_due_days');
173         $PastDueDays2 = 2 * $PastDueDays1;
174
175         $nowdue = "1-" . $PastDueDays1 . " " . _('Days');
176         $pastdue1 = $PastDueDays1 + 1 . "-" . $PastDueDays2 . " " . _('Days');
177         $pastdue2 = _('Over') . " " . $PastDueDays2 . " " . _('Days');
178         
179         $sql = "SELECT supplier_id, supp_name AS name, curr_code FROM ".TB_PREF."suppliers ";
180         if ($fromsupp != reserved_words::get_all_numeric())
181                 $sql .= "WHERE supplier_id=$fromsupp ";
182         $sql .= "ORDER BY supp_name";
183         $result = db_query($sql, "The suppliers could not be retrieved");
184         
185         while ($myrow=db_fetch($result)) 
186         {
187                 if (!$convert && $currency != $myrow['curr_code'])
188                         continue;
189                 $rep->fontSize += 2;
190                 $rep->TextCol(0, 3,     $myrow['name']);
191                 if ($convert)
192                 {
193                         $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $to);
194                         $rep->TextCol(2, 4,     $myrow['curr_code']);
195                 }
196                 else
197                         $rate = 1.0;
198                 $rep->fontSize -= 2;
199                 $supprec = get_supplier_details($myrow['supplier_id'], $to);
200                 foreach ($supprec as $i => $value) 
201                         $supprec[$i] *= $rate;
202                 $total[0] += ($supprec["Balance"] - $supprec["Due"]);
203                 $total[1] += ($supprec["Due"]-$supprec["Overdue1"]);
204                 $total[2] += ($supprec["Overdue1"]-$supprec["Overdue2"]);
205                 $total[3] += $supprec["Overdue2"];
206                 $total[4] += $supprec["Balance"];
207                 $str = array(number_format2(($supprec["Balance"] - $supprec["Due"]),$dec),
208                         number_format2(($supprec["Due"]-$supprec["Overdue1"]),$dec),
209                         number_format2(($supprec["Overdue1"]-$supprec["Overdue2"]) ,$dec),
210                         number_format2($supprec["Overdue2"],$dec),
211                         number_format2($supprec["Balance"],$dec));
212                 for ($i = 0; $i < count($str); $i++)
213                         $rep->TextCol($i + 3, $i + 4, $str[$i]);
214                 $rep->NewLine(1, 2);    
215                 if (!$summaryOnly)
216                 {
217                         $res = get_invoices($myrow['supplier_id'], $to);
218                 if (db_num_rows($res)==0)
219                                 continue;
220                 $rep->Line($rep->row + 4);
221                         while ($trans=db_fetch($res))
222                         {
223                                 $rep->NewLine(1, 2);
224                         $rep->TextCol(0, 1,     $trans['type_name'], -2);
225                                 $rep->TextCol(1, 2,     $trans['reference'], -2);
226                                 $rep->TextCol(2, 3,     sql2date($trans['tran_date']), -2);
227                                 foreach ($trans as $i => $value) 
228                                         $trans[$i] *= $rate;
229                                 $str = array(number_format2(($trans["Balance"] - $trans["Due"]),$dec),
230                                         number_format2(($trans["Due"]-$trans["Overdue1"]),$dec),
231                                         number_format2(($trans["Overdue1"]-$trans["Overdue2"]) ,$dec),
232                                         number_format2($trans["Overdue2"],$dec),
233                                         number_format2($trans["Balance"],$dec));
234                                 for ($i = 0; $i < count($str); $i++)
235                                         $rep->TextCol($i + 3, $i + 4, $str[$i]);
236                         }                                       
237                         $rep->Line($rep->row - 8);
238                         $rep->NewLine(2);
239                 }       
240         }
241         if ($summaryOnly)
242         {
243         $rep->Line($rep->row  + 4);
244         $rep->NewLine();
245         }
246         $rep->fontSize += 2;
247         $rep->TextCol(0, 3,     _('Grand Total'));
248         $rep->fontSize -= 2;
249         for ($i = 0; $i < count($total); $i++)
250         {
251                 $rep->TextCol($i + 3, $i + 4, number_format2($total[$i], $dec));
252                 if ($graphics && $i < count($total) - 1)
253                 {
254                         $pg->y[$i] = abs($total[$i]);
255                 }       
256         }       
257         $rep->Line($rep->row  - 8);
258         if ($graphics)
259         {
260                 global $decseps, $graph_skin;
261                 $pg->x = array(_('Current'), $nowdue, $pastdue1, $pastdue2);
262                 $pg->title     = $rep->title;
263                 $pg->axis_x    = _("Days");
264                 $pg->axis_y    = _("Amount");
265                 $pg->graphic_1 = $to;
266                 $pg->type      = $graphics;
267                 $pg->skin      = $graph_skin;
268                 $pg->built_in  = false;
269                 $pg->fontfile  = $path_to_root . "reporting/fonts/Vera.ttf";
270                 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
271                 $filename = $path_to_root . "reporting/pdf_files/test.png";
272                 $pg->display($filename, true);
273                 $w = $pg->width / 1.5;
274                 $h = $pg->height / 1.5;
275                 $x = ($rep->pageWidth - $w) / 2;
276                 $rep->NewLine(2);
277                 if ($rep->row - $h < $rep->bottomMargin)
278                         $rep->Header();
279                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
280         }
281     $rep->End();
282 }
283
284 ?>