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