Replaced the Excel Writer engine with XLS driver (the XML driver can later be downloaded)
[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 = 2;
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)
33 {
34         $todate = date2sql($to);
35         $PastDueDays1 = get_company_pref('past_due_days');
36         $PastDueDays2 = 2 * $PastDueDays1;
37
38         // Revomed allocated from sql
39     $value = "(".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount)";
40         $due = "IF (".TB_PREF."supp_trans.type=20 OR ".TB_PREF."supp_trans.type=21,".TB_PREF."supp_trans.due_date,".TB_PREF."supp_trans.tran_date)";
41         $sql = "SELECT ".TB_PREF."sys_types.type_name,
42                 ".TB_PREF."supp_trans.reference,
43                 ".TB_PREF."supp_trans.tran_date,
44                 $value as Balance,
45                 IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= 0,$value,0) AS Due,
46                 IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $PastDueDays1,$value,0) AS Overdue1,
47                 IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $PastDueDays2,$value,0) AS Overdue2
48
49                 FROM ".TB_PREF."suppliers,
50                         ".TB_PREF."payment_terms,
51                         ".TB_PREF."supp_trans,
52                         ".TB_PREF."sys_types
53
54                 WHERE ".TB_PREF."sys_types.type_id = ".TB_PREF."supp_trans.type
55                         AND ".TB_PREF."suppliers.payment_terms = ".TB_PREF."payment_terms.terms_indicator
56                         AND ".TB_PREF."suppliers.supplier_id = ".TB_PREF."supp_trans.supplier_id
57                         AND ".TB_PREF."supp_trans.supplier_id = $supplier_id
58                         AND ".TB_PREF."supp_trans.tran_date <= '$todate'
59                         AND ABS(".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount) > 0.004
60                         ORDER BY ".TB_PREF."supp_trans.tran_date";
61
62
63         return db_query($sql, "The supplier details could not be retrieved");
64 }
65
66 //----------------------------------------------------------------------------------------------------
67
68 function print_aged_supplier_analysis()
69 {
70     global $comp_path, $path_to_root;
71
72     $to = $_POST['PARAM_0'];
73     $fromsupp = $_POST['PARAM_1'];
74     $currency = $_POST['PARAM_2'];
75         $summaryOnly = $_POST['PARAM_3'];
76     $graphics = $_POST['PARAM_4'];
77     $comments = $_POST['PARAM_5'];
78         $destination = $_POST['PARAM_6'];
79         if ($destination)
80                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
81         else
82                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
83         if ($graphics)
84         {
85                 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
86                 $pg = new graph();
87         }
88
89         if ($fromsupp == reserved_words::get_all_numeric())
90                 $from = _('All');
91         else
92                 $from = get_supplier_name($fromsupp);
93     $dec = user_price_dec();
94
95         if ($summaryOnly == 1)
96                 $summary = _('Summary Only');
97         else
98                 $summary = _('Detailed Report');
99         if ($currency == reserved_words::get_all())
100         {
101                 $convert = true;
102                 $currency = _('Balances in Home Currency');
103         }
104         else
105                 $convert = false;
106         $PastDueDays1 = get_company_pref('past_due_days');
107         $PastDueDays2 = 2 * $PastDueDays1;
108         $nowdue = "1-" . $PastDueDays1 . " " . _('Days');
109         $pastdue1 = $PastDueDays1 + 1 . "-" . $PastDueDays2 . " " . _('Days');
110         $pastdue2 = _('Over') . " " . $PastDueDays2 . " " . _('Days');
111
112         $cols = array(0, 100, 130, 190, 250, 320, 385, 450,     515);
113
114         $headers = array(_('Supplier'), '',     '',     _('Current'), $nowdue, $pastdue1,$pastdue2,
115                 _('Total Balance'));
116
117         $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right',     'right');
118
119     $params =   array(  0 => $comments,
120                                     1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
121                                     2 => array('text' => _('Supplier'), 'from' => $from, 'to' => ''),
122                                     3 => array('text' => _('Currency'),'from' => $currency,'to' => ''),
123                         4 => array('text' => _('Type'), 'from' => $summary,'to' => ''));
124
125         if ($convert)
126                 $headers[2] = _('currency');
127     $rep = new FrontReport(_('Aged Supplier Analysis'), "AgedSupplierAnalysis", user_pagesize());
128
129     $rep->Font();
130     $rep->Info($params, $cols, $headers, $aligns);
131     $rep->Header();
132
133         $total = array();
134         $total[0] = $total[1] = $total[2] = $total[3] = $total[4] = 0.0;
135         $PastDueDays1 = get_company_pref('past_due_days');
136         $PastDueDays2 = 2 * $PastDueDays1;
137
138         $nowdue = "1-" . $PastDueDays1 . " " . _('Days');
139         $pastdue1 = $PastDueDays1 + 1 . "-" . $PastDueDays2 . " " . _('Days');
140         $pastdue2 = _('Over') . " " . $PastDueDays2 . " " . _('Days');
141
142         $sql = "SELECT supplier_id, supp_name AS name, curr_code FROM ".TB_PREF."suppliers ";
143         if ($fromsupp != reserved_words::get_all_numeric())
144                 $sql .= "WHERE supplier_id=$fromsupp ";
145         $sql .= "ORDER BY supp_name";
146         $result = db_query($sql, "The suppliers 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                 {
156                         $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $to);
157                         $rep->TextCol(2, 3,     $myrow['curr_code']);
158                 }
159                 else
160                         $rate = 1.0;
161                 $rep->fontSize -= 2;
162                 $supprec = get_supplier_details($myrow['supplier_id'], $to);
163                 foreach ($supprec as $i => $value)
164                         $supprec[$i] *= $rate;
165                 $total[0] += ($supprec["Balance"] - $supprec["Due"]);
166                 $total[1] += ($supprec["Due"]-$supprec["Overdue1"]);
167                 $total[2] += ($supprec["Overdue1"]-$supprec["Overdue2"]);
168                 $total[3] += $supprec["Overdue2"];
169                 $total[4] += $supprec["Balance"];
170                 $str = array($supprec["Balance"] - $supprec["Due"],
171                         $supprec["Due"]-$supprec["Overdue1"],
172                         $supprec["Overdue1"]-$supprec["Overdue2"],
173                         $supprec["Overdue2"],
174                         $supprec["Balance"]);
175                 for ($i = 0; $i < count($str); $i++)
176                         $rep->AmountCol($i + 3, $i + 4, $str[$i], $dec);
177                 $rep->NewLine(1, 2);
178                 if (!$summaryOnly)
179                 {
180                         $res = get_invoices($myrow['supplier_id'], $to);
181                 if (db_num_rows($res)==0)
182                                 continue;
183                 $rep->Line($rep->row + 4);
184                         while ($trans=db_fetch($res))
185                         {
186                                 $rep->NewLine(1, 2);
187                         $rep->TextCol(0, 1,     $trans['type_name'], -2);
188                                 $rep->TextCol(1, 2,     $trans['reference'], -2);
189                                 $rep->TextCol(2, 3,     sql2date($trans['tran_date']), -2);
190                                 foreach ($trans as $i => $value)
191                                         $trans[$i] *= $rate;
192                                 $str = array($trans["Balance"] - $trans["Due"],
193                                         $trans["Due"]-$trans["Overdue1"],
194                                         $trans["Overdue1"]-$trans["Overdue2"],
195                                         $trans["Overdue2"],
196                                         $trans["Balance"]);
197                                 for ($i = 0; $i < count($str); $i++)
198                                         $rep->AmountCol($i + 3, $i + 4, $str[$i], $dec);
199                         }
200                         $rep->Line($rep->row - 8);
201                         $rep->NewLine(2);
202                 }
203         }
204         if ($summaryOnly)
205         {
206         $rep->Line($rep->row  + 4);
207         $rep->NewLine();
208         }
209         $rep->fontSize += 2;
210         $rep->TextCol(0, 3,     _('Grand Total'));
211         $rep->fontSize -= 2;
212         for ($i = 0; $i < count($total); $i++)
213         {
214                 $rep->AmountCol($i + 3, $i + 4, $total[$i], $dec);
215                 if ($graphics && $i < count($total) - 1)
216                 {
217                         $pg->y[$i] = abs($total[$i]);
218                 }
219         }
220         $rep->Line($rep->row  - 8);
221         $rep->NewLine();
222         if ($graphics)
223         {
224                 global $decseps, $graph_skin;
225                 $pg->x = array(_('Current'), $nowdue, $pastdue1, $pastdue2);
226                 $pg->title     = $rep->title;
227                 $pg->axis_x    = _("Days");
228                 $pg->axis_y    = _("Amount");
229                 $pg->graphic_1 = $to;
230                 $pg->type      = $graphics;
231                 $pg->skin      = $graph_skin;
232                 $pg->built_in  = false;
233                 $pg->fontfile  = $path_to_root . "/reporting/fonts/Vera.ttf";
234                 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
235                 $filename = $comp_path.'/'.user_company(). "/pdf_files/test.png";
236                 $pg->display($filename, true);
237                 $w = $pg->width / 1.5;
238                 $h = $pg->height / 1.5;
239                 $x = ($rep->pageWidth - $w) / 2;
240                 $rep->NewLine(2);
241                 if ($rep->row - $h < $rep->bottomMargin)
242                         $rep->Header();
243                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
244         }
245     $rep->End();
246 }
247
248 ?>