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 $value = "(".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount)";
32 $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)";
33 $sql = "SELECT ".TB_PREF."sys_types.type_name,
34 ".TB_PREF."supp_trans.reference,
35 ".TB_PREF."supp_trans.tran_date,
37 IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= 0,$value,0) AS Due,
38 IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $PastDueDays1,$value,0) AS Overdue1,
39 IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $PastDueDays2,$value,0) AS Overdue2
41 FROM ".TB_PREF."suppliers,
42 ".TB_PREF."payment_terms,
43 ".TB_PREF."supp_trans,
46 WHERE ".TB_PREF."sys_types.type_id = ".TB_PREF."supp_trans.type
47 AND ".TB_PREF."suppliers.payment_terms = ".TB_PREF."payment_terms.terms_indicator
48 AND ".TB_PREF."suppliers.supplier_id = ".TB_PREF."supp_trans.supplier_id
49 AND ".TB_PREF."supp_trans.supplier_id = $supplier_id
50 AND ".TB_PREF."supp_trans.tran_date <= '$todate'
51 AND ABS(".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount) > 0.004
52 ORDER BY ".TB_PREF."supp_trans.tran_date";
55 return db_query($sql, "The supplier details could not be retrieved");
58 //----------------------------------------------------------------------------------------------------
60 function print_aged_supplier_analysis()
62 global $comp_path, $path_to_root;
64 include_once($path_to_root . "reporting/includes/pdf_report.inc");
66 $to = $_POST['PARAM_0'];
67 $fromsupp = $_POST['PARAM_1'];
68 $currency = $_POST['PARAM_2'];
69 $summaryOnly = $_POST['PARAM_3'];
70 $graphics = $_POST['PARAM_4'];
71 $comments = $_POST['PARAM_5'];
74 include_once($path_to_root . "reporting/includes/class.graphic.inc");
78 if ($fromsupp == reserved_words::get_all_numeric())
81 $from = get_supplier_name($fromsupp);
82 $dec = user_price_dec();
84 if ($summaryOnly == 1)
85 $summary = _('Summary Only');
87 $summary = _('Detailed Report');
88 if ($currency == reserved_words::get_all())
91 $currency = _('Balances in Home Currency');
95 $PastDueDays1 = get_company_pref('past_due_days');
96 $PastDueDays2 = 2 * $PastDueDays1;
97 $nowdue = "1-" . $PastDueDays1 . " " . _('Days');
98 $pastdue1 = $PastDueDays1 + 1 . "-" . $PastDueDays2 . " " . _('Days');
99 $pastdue2 = _('Over') . " " . $PastDueDays2 . " " . _('Days');
101 $cols = array(0, 100, 130, 190, 250, 320, 385, 450, 515);
103 $headers = array(_('Supplier'), '', '', _('Current'), $nowdue, $pastdue1,$pastdue2,
106 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right', 'right');
108 $params = array( 0 => $comments,
109 1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
110 2 => array('text' => _('Supplier'), 'from' => $from, 'to' => ''),
111 3 => array('text' => _('Currency'),'from' => $currency,'to' => ''),
112 4 => array('text' => _('Type'), 'from' => $summary,'to' => ''));
115 $headers[2] = _('currency');
116 $rep = new FrontReport(_('Aged Supplier Analysis'), "AgedSupplierAnalysis.pdf", user_pagesize());
119 $rep->Info($params, $cols, $headers, $aligns);
123 $total[0] = $total[1] = $total[2] = $total[3] = $total[4] = 0.0;
124 $PastDueDays1 = get_company_pref('past_due_days');
125 $PastDueDays2 = 2 * $PastDueDays1;
127 $nowdue = "1-" . $PastDueDays1 . " " . _('Days');
128 $pastdue1 = $PastDueDays1 + 1 . "-" . $PastDueDays2 . " " . _('Days');
129 $pastdue2 = _('Over') . " " . $PastDueDays2 . " " . _('Days');
131 $sql = "SELECT supplier_id, supp_name AS name, curr_code FROM ".TB_PREF."suppliers ";
132 if ($fromsupp != reserved_words::get_all_numeric())
133 $sql .= "WHERE supplier_id=$fromsupp ";
134 $sql .= "ORDER BY supp_name";
135 $result = db_query($sql, "The suppliers could not be retrieved");
137 while ($myrow=db_fetch($result))
139 if (!$convert && $currency != $myrow['curr_code'])
142 $rep->TextCol(0, 3, $myrow['name']);
145 $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $to);
146 $rep->TextCol(2, 4, $myrow['curr_code']);
151 $supprec = get_supplier_details($myrow['supplier_id'], $to);
152 foreach ($supprec as $i => $value)
153 $supprec[$i] *= $rate;
154 $total[0] += ($supprec["Balance"] - $supprec["Due"]);
155 $total[1] += ($supprec["Due"]-$supprec["Overdue1"]);
156 $total[2] += ($supprec["Overdue1"]-$supprec["Overdue2"]);
157 $total[3] += $supprec["Overdue2"];
158 $total[4] += $supprec["Balance"];
159 $str = array(number_format2(($supprec["Balance"] - $supprec["Due"]),$dec),
160 number_format2(($supprec["Due"]-$supprec["Overdue1"]),$dec),
161 number_format2(($supprec["Overdue1"]-$supprec["Overdue2"]) ,$dec),
162 number_format2($supprec["Overdue2"],$dec),
163 number_format2($supprec["Balance"],$dec));
164 for ($i = 0; $i < count($str); $i++)
165 $rep->TextCol($i + 3, $i + 4, $str[$i]);
169 $res = get_invoices($myrow['supplier_id'], $to);
170 if (db_num_rows($res)==0)
172 $rep->Line($rep->row + 4);
173 while ($trans=db_fetch($res))
176 $rep->TextCol(0, 1, $trans['type_name'], -2);
177 $rep->TextCol(1, 2, $trans['reference'], -2);
178 $rep->TextCol(2, 3, sql2date($trans['tran_date']), -2);
179 foreach ($trans as $i => $value)
181 $str = array(number_format2(($trans["Balance"] - $trans["Due"]),$dec),
182 number_format2(($trans["Due"]-$trans["Overdue1"]),$dec),
183 number_format2(($trans["Overdue1"]-$trans["Overdue2"]) ,$dec),
184 number_format2($trans["Overdue2"],$dec),
185 number_format2($trans["Balance"],$dec));
186 for ($i = 0; $i < count($str); $i++)
187 $rep->TextCol($i + 3, $i + 4, $str[$i]);
189 $rep->Line($rep->row - 8);
195 $rep->Line($rep->row + 4);
199 $rep->TextCol(0, 3, _('Grand Total'));
201 for ($i = 0; $i < count($total); $i++)
203 $rep->TextCol($i + 3, $i + 4, number_format2($total[$i], $dec));
204 if ($graphics && $i < count($total) - 1)
206 $pg->y[$i] = abs($total[$i]);
209 $rep->Line($rep->row - 8);
212 global $decseps, $graph_skin;
213 $pg->x = array(_('Current'), $nowdue, $pastdue1, $pastdue2);
214 $pg->title = $rep->title;
215 $pg->axis_x = _("Days");
216 $pg->axis_y = _("Amount");
217 $pg->graphic_1 = $to;
218 $pg->type = $graphics;
219 $pg->skin = $graph_skin;
220 $pg->built_in = false;
221 $pg->fontfile = $path_to_root . "reporting/fonts/Vera.ttf";
222 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
223 $filename = $comp_path.'/'.user_company(). "/pdf_files/test.png";
224 $pg->display($filename, true);
225 $w = $pg->width / 1.5;
226 $h = $pg->height / 1.5;
227 $x = ($rep->pageWidth - $w) / 2;
229 if ($rep->row - $h < $rep->bottomMargin)
231 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);