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 ***********************************************************************/
13 // ----------------------------------------------------------------
17 // Title: Ages Supplier Analysis
18 // ----------------------------------------------------------------
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");
26 //----------------------------------------------------------------------------------------------------
28 // trial_inquiry_controls();
29 print_aged_supplier_analysis();
31 //----------------------------------------------------------------------------------------------------
33 function get_invoices($supplier_id, $to)
35 $todate = date2sql($to);
36 $PastDueDays1 = get_company_pref('past_due_days');
37 $PastDueDays2 = 2 * $PastDueDays1;
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,
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
50 FROM ".TB_PREF."suppliers,
51 ".TB_PREF."payment_terms,
52 ".TB_PREF."supp_trans,
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";
64 return db_query($sql, "The supplier details could not be retrieved");
67 //----------------------------------------------------------------------------------------------------
69 function print_aged_supplier_analysis()
71 global $comp_path, $path_to_root;
73 $to = $_POST['PARAM_0'];
74 $fromsupp = $_POST['PARAM_1'];
75 $currency = $_POST['PARAM_2'];
76 $summaryOnly = $_POST['PARAM_3'];
77 $graphics = $_POST['PARAM_4'];
78 $comments = $_POST['PARAM_5'];
79 $destination = $_POST['PARAM_6'];
82 include_once($path_to_root . "/reporting/includes/excel_report.inc");
83 $filename = "AgedSupplierAnalysis.xml";
87 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
88 $filename = "AgedSupplierAnalysis.pdf";
92 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
96 if ($fromsupp == reserved_words::get_all_numeric())
99 $from = get_supplier_name($fromsupp);
100 $dec = user_price_dec();
102 if ($summaryOnly == 1)
103 $summary = _('Summary Only');
105 $summary = _('Detailed Report');
106 if ($currency == reserved_words::get_all())
109 $currency = _('Balances in Home Currency');
113 $PastDueDays1 = get_company_pref('past_due_days');
114 $PastDueDays2 = 2 * $PastDueDays1;
115 $nowdue = "1-" . $PastDueDays1 . " " . _('Days');
116 $pastdue1 = $PastDueDays1 + 1 . "-" . $PastDueDays2 . " " . _('Days');
117 $pastdue2 = _('Over') . " " . $PastDueDays2 . " " . _('Days');
119 $cols = array(0, 100, 130, 190, 250, 320, 385, 450, 515);
121 $headers = array(_('Supplier'), '', '', _('Current'), $nowdue, $pastdue1,$pastdue2,
124 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right', 'right');
126 $params = array( 0 => $comments,
127 1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
128 2 => array('text' => _('Supplier'), 'from' => $from, 'to' => ''),
129 3 => array('text' => _('Currency'),'from' => $currency,'to' => ''),
130 4 => array('text' => _('Type'), 'from' => $summary,'to' => ''));
133 $headers[2] = _('currency');
134 $rep = new FrontReport(_('Aged Supplier Analysis'), $filename, user_pagesize());
137 $rep->Info($params, $cols, $headers, $aligns);
141 $total[0] = $total[1] = $total[2] = $total[3] = $total[4] = 0.0;
142 $PastDueDays1 = get_company_pref('past_due_days');
143 $PastDueDays2 = 2 * $PastDueDays1;
145 $nowdue = "1-" . $PastDueDays1 . " " . _('Days');
146 $pastdue1 = $PastDueDays1 + 1 . "-" . $PastDueDays2 . " " . _('Days');
147 $pastdue2 = _('Over') . " " . $PastDueDays2 . " " . _('Days');
149 $sql = "SELECT supplier_id, supp_name AS name, curr_code FROM ".TB_PREF."suppliers ";
150 if ($fromsupp != reserved_words::get_all_numeric())
151 $sql .= "WHERE supplier_id=$fromsupp ";
152 $sql .= "ORDER BY supp_name";
153 $result = db_query($sql, "The suppliers could not be retrieved");
155 while ($myrow=db_fetch($result))
157 if (!$convert && $currency != $myrow['curr_code'])
160 $rep->TextCol(0, 2, $myrow['name']);
163 $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $to);
164 $rep->TextCol(2, 3, $myrow['curr_code']);
169 $supprec = get_supplier_details($myrow['supplier_id'], $to);
170 foreach ($supprec as $i => $value)
171 $supprec[$i] *= $rate;
172 $total[0] += ($supprec["Balance"] - $supprec["Due"]);
173 $total[1] += ($supprec["Due"]-$supprec["Overdue1"]);
174 $total[2] += ($supprec["Overdue1"]-$supprec["Overdue2"]);
175 $total[3] += $supprec["Overdue2"];
176 $total[4] += $supprec["Balance"];
177 $str = array($supprec["Balance"] - $supprec["Due"],
178 $supprec["Due"]-$supprec["Overdue1"],
179 $supprec["Overdue1"]-$supprec["Overdue2"],
180 $supprec["Overdue2"],
181 $supprec["Balance"]);
182 for ($i = 0; $i < count($str); $i++)
183 $rep->AmountCol($i + 3, $i + 4, $str[$i], $dec);
187 $res = get_invoices($myrow['supplier_id'], $to);
188 if (db_num_rows($res)==0)
190 $rep->Line($rep->row + 4);
191 while ($trans=db_fetch($res))
194 $rep->TextCol(0, 1, $trans['type_name'], -2);
195 $rep->TextCol(1, 2, $trans['reference'], -2);
196 $rep->TextCol(2, 3, sql2date($trans['tran_date']), -2);
197 foreach ($trans as $i => $value)
199 $str = array($trans["Balance"] - $trans["Due"],
200 $trans["Due"]-$trans["Overdue1"],
201 $trans["Overdue1"]-$trans["Overdue2"],
204 for ($i = 0; $i < count($str); $i++)
205 $rep->AmountCol($i + 3, $i + 4, $str[$i], $dec);
207 $rep->Line($rep->row - 8);
213 $rep->Line($rep->row + 4);
217 $rep->TextCol(0, 3, _('Grand Total'));
219 for ($i = 0; $i < count($total); $i++)
221 $rep->AmountCol($i + 3, $i + 4, $total[$i], $dec);
222 if ($graphics && $i < count($total) - 1)
224 $pg->y[$i] = abs($total[$i]);
227 $rep->Line($rep->row - 8);
231 global $decseps, $graph_skin;
232 $pg->x = array(_('Current'), $nowdue, $pastdue1, $pastdue2);
233 $pg->title = $rep->title;
234 $pg->axis_x = _("Days");
235 $pg->axis_y = _("Amount");
236 $pg->graphic_1 = $to;
237 $pg->type = $graphics;
238 $pg->skin = $graph_skin;
239 $pg->built_in = false;
240 $pg->fontfile = $path_to_root . "/reporting/fonts/Vera.ttf";
241 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
242 $filename = $comp_path.'/'.user_company(). "/pdf_files/test.png";
243 $pg->display($filename, true);
244 $w = $pg->width / 1.5;
245 $h = $pg->height / 1.5;
246 $x = ($rep->pageWidth - $w) / 2;
248 if ($rep->row - $h < $rep->bottomMargin)
250 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);