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