4 // ----------------------------------------------------------------
8 // Title: Ages Supplier Analysis
9 // ----------------------------------------------------------------
12 include_once($path_to_root . "includes/session.inc");
13 include_once($path_to_root . "includes/date_functions.inc");
14 include_once($path_to_root . "includes/data_checks.inc");
15 include_once($path_to_root . "gl/includes/gl_db.inc");
17 //----------------------------------------------------------------------------------------------------
19 // trial_inquiry_controls();
20 print_aged_supplier_analysis();
22 //----------------------------------------------------------------------------------------------------
24 function get_invoices($supplier_id, $to)
26 $todate = date2sql($to);
27 $PastDueDays1 = get_company_pref('past_due_days');
28 $PastDueDays2 = 2 * $PastDueDays1;
30 // Revomed allocated from sql
31 $sql = "SELECT ".TB_PREF."sys_types.type_name,
32 ".TB_PREF."supp_trans.reference,
33 ".TB_PREF."supp_trans.tran_date,
34 (".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount) as Balance,
35 IF (".TB_PREF."payment_terms.days_before_due > 0,
36 CASE WHEN TO_DAYS('$todate') - TO_DAYS(".TB_PREF."supp_trans.tran_date) >= ".TB_PREF."payment_terms.days_before_due
38 ".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount
43 CASE WHEN TO_DAYS('$todate') - TO_DAYS(DATE_ADD(DATE_ADD(".TB_PREF."supp_trans.tran_date,
44 INTERVAL 1 MONTH), INTERVAL (".TB_PREF."payment_terms.day_in_following_month -
45 DAYOFMONTH(".TB_PREF."supp_trans.tran_date)) DAY)) >= 0
47 ".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount
52 IF (".TB_PREF."payment_terms.days_before_due > 0,
53 CASE WHEN TO_DAYS('$todate') - TO_DAYS(".TB_PREF."supp_trans.tran_date) > ".TB_PREF."payment_terms.days_before_due
54 AND TO_DAYS('$todate') - TO_DAYS(".TB_PREF."supp_trans.tran_date) >= (".TB_PREF."payment_terms.days_before_due + $PastDueDays1)
56 ".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount
61 CASE WHEN TO_DAYS('$todate') - TO_DAYS(DATE_ADD(DATE_ADD(".TB_PREF."supp_trans.tran_date,
62 INTERVAL 1 MONTH), INTERVAL (".TB_PREF."payment_terms.day_in_following_month -
63 DAYOFMONTH(".TB_PREF."supp_trans.tran_date)) DAY)) >= $PastDueDays1
65 ".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount
70 IF (".TB_PREF."payment_terms.days_before_due > 0,
71 CASE WHEN TO_DAYS('$todate') - TO_DAYS(".TB_PREF."supp_trans.tran_date) > ".TB_PREF."payment_terms.days_before_due
72 AND TO_DAYS('$todate') - TO_DAYS(".TB_PREF."supp_trans.tran_date) >= (".TB_PREF."payment_terms.days_before_due + $PastDueDays2)
74 ".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount
79 CASE WHEN TO_DAYS('$todate') - TO_DAYS(DATE_ADD(DATE_ADD(".TB_PREF."supp_trans.tran_date,
80 INTERVAL 1 MONTH), INTERVAL (".TB_PREF."payment_terms.day_in_following_month -
81 DAYOFMONTH(".TB_PREF."supp_trans.tran_date)) DAY)) >= $PastDueDays2
83 ".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount
89 FROM ".TB_PREF."suppliers,
90 ".TB_PREF."payment_terms,
91 ".TB_PREF."supp_trans,
94 WHERE ".TB_PREF."sys_types.type_id = ".TB_PREF."supp_trans.type
95 AND ".TB_PREF."suppliers.payment_terms = ".TB_PREF."payment_terms.terms_indicator
96 AND ".TB_PREF."suppliers.supplier_id = ".TB_PREF."supp_trans.supplier_id
97 AND ".TB_PREF."supp_trans.supplier_id = $supplier_id
98 AND ".TB_PREF."supp_trans.tran_date <= '$todate'
99 AND ABS(".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount) > 0.004
100 ORDER BY ".TB_PREF."supp_trans.tran_date";
103 return db_query($sql, "The supplier details could not be retrieved");
106 //----------------------------------------------------------------------------------------------------
108 function print_aged_supplier_analysis()
110 global $path_to_root;
112 include_once($path_to_root . "reporting/includes/pdf_report.inc");
114 $to = $_POST['PARAM_0'];
115 $fromsupp = $_POST['PARAM_1'];
116 $tosupp = $_POST['PARAM_2'];
117 $currency = $_POST['PARAM_3'];
118 $summaryOnly = $_POST['PARAM_4'];
119 $graphics = $_POST['PARAM_5'];
120 $comments = $_POST['PARAM_6'];
123 include_once($path_to_root . "reporting/includes/class.graphic.inc");
127 if ($fromsupp == null)
131 $dec = user_price_dec();
133 if ($summaryOnly == 1)
134 $summary = _('Summary Only');
136 $summary = _('Detailed Report');
137 if ($currency == reserved_words::get_all())
140 $currency = _('Balances in Home Currency');
144 $PastDueDays1 = get_company_pref('past_due_days');
145 $PastDueDays2 = 2 * $PastDueDays1;
146 $nowdue = "1-" . $PastDueDays1 . " " . _('Days');
147 $pastdue1 = $PastDueDays1 + 1 . "-" . $PastDueDays2 . " " . _('Days');
148 $pastdue2 = _('Over') . " " . $PastDueDays2 . " " . _('Days');
150 $cols = array(0, 100, 130, 190, 250, 320, 385, 450, 515);
152 $headers = array(_('Supplier'), '', '', _('Current'), $nowdue, $pastdue1,$pastdue2,
155 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right', 'right');
157 $params = array( 0 => $comments,
158 1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
159 2 => array('text' => _('Supplier'), 'from' => get_supplier_name($fromsupp),
160 'to' => get_supplier_name($tosupp)),
161 3 => array('text' => _('Currency'),'from' => $currency,'to' => ''),
162 4 => array('text' => _('Type'), 'from' => $summary,'to' => ''));
165 $headers[2] = _('currency');
166 $rep = new FrontReport(_('Aged Supplier Analysis'), "AgedSupplierAnalysis.pdf", user_pagesize());
169 $rep->Info($params, $cols, $headers, $aligns);
173 $total[0] = $total[1] = $total[2] = $total[3] = $total[4] = 0.0;
174 $PastDueDays1 = get_company_pref('past_due_days');
175 $PastDueDays2 = 2 * $PastDueDays1;
177 $nowdue = "1-" . $PastDueDays1 . " " . _('Days');
178 $pastdue1 = $PastDueDays1 + 1 . "-" . $PastDueDays2 . " " . _('Days');
179 $pastdue2 = _('Over') . " " . $PastDueDays2 . " " . _('Days');
181 $sql = "SELECT supplier_id, supp_name AS name, curr_code FROM ".TB_PREF."suppliers
182 WHERE supplier_id>=$fromsupp AND supplier_id<=$tosupp ORDER BY name";
183 $result = db_query($sql, "The suppliers could not be retrieved");
185 while ($myrow=db_fetch($result))
187 if (!$convert && $currency != $myrow['curr_code'])
190 $rep->TextCol(0, 3, $myrow['name']);
193 $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $to);
194 $rep->TextCol(2, 4, $myrow['curr_code']);
199 $supprec = get_supplier_details($myrow['supplier_id'], $to);
200 foreach ($supprec as $i => $value)
201 $supprec[$i] *= $rate;
202 $total[0] += ($supprec["Balance"] - $supprec["Due"]);
203 $total[1] += ($supprec["Due"]-$supprec["Overdue1"]);
204 $total[2] += ($supprec["Overdue1"]-$supprec["Overdue2"]);
205 $total[3] += $supprec["Overdue2"];
206 $total[4] += $supprec["Balance"];
207 $str = array(number_format2(($supprec["Balance"] - $supprec["Due"]),$dec),
208 number_format2(($supprec["Due"]-$supprec["Overdue1"]),$dec),
209 number_format2(($supprec["Overdue1"]-$supprec["Overdue2"]) ,$dec),
210 number_format2($supprec["Overdue2"],$dec),
211 number_format2($supprec["Balance"],$dec));
212 for ($i = 0; $i < count($str); $i++)
213 $rep->TextCol($i + 3, $i + 4, $str[$i]);
217 $res = get_invoices($myrow['supplier_id'], $to);
218 if (db_num_rows($res)==0)
220 $rep->Line($rep->row + 4);
221 while ($trans=db_fetch($res))
224 $rep->TextCol(0, 1, $trans['type_name'], -2);
225 $rep->TextCol(1, 2, $trans['reference'], -2);
226 $rep->TextCol(2, 3, sql2date($trans['tran_date']), -2);
227 foreach ($trans as $i => $value)
229 $str = array(number_format2(($trans["Balance"] - $trans["Due"]),$dec),
230 number_format2(($trans["Due"]-$trans["Overdue1"]),$dec),
231 number_format2(($trans["Overdue1"]-$trans["Overdue2"]) ,$dec),
232 number_format2($trans["Overdue2"],$dec),
233 number_format2($trans["Balance"],$dec));
234 for ($i = 0; $i < count($str); $i++)
235 $rep->TextCol($i + 3, $i + 4, $str[$i]);
237 $rep->Line($rep->row - 8);
243 $rep->Line($rep->row + 4);
247 $rep->TextCol(0, 3, _('Grand Total'));
249 for ($i = 0; $i < count($total); $i++)
251 $rep->TextCol($i + 3, $i + 4, number_format2($total[$i], $dec));
252 if ($graphics && $i < count($total) - 1)
254 $pg->y[$i] = abs($total[$i]);
257 $rep->Line($rep->row - 8);
260 global $decseps, $graph_skin;
261 $pg->x = array(_('Current'), $nowdue, $pastdue1, $pastdue2);
262 $pg->title = $rep->title;
263 $pg->axis_x = _("Days");
264 $pg->axis_y = _("Amount");
265 $pg->graphic_1 = $to;
266 $pg->type = $graphics;
267 $pg->skin = $graph_skin;
268 $pg->built_in = false;
269 $pg->fontfile = $path_to_root . "reporting/fonts/Vera.ttf";
270 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
271 $filename = $path_to_root . "reporting/pdf_files/test.png";
272 $pg->display($filename, true);
273 $w = $pg->width / 1.5;
274 $h = $pg->height / 1.5;
275 $x = ($rep->pageWidth - $w) / 2;
277 if ($rep->row - $h < $rep->bottomMargin)
279 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);