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