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 // ----------------------------------------------------------------
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 print_aged_supplier_analysis();
30 //----------------------------------------------------------------------------------------------------
32 function get_invoices($supplier_id, $to, $all=true)
34 $todate = date2sql($to);
35 $PastDueDays1 = get_company_pref('past_due_days');
36 $PastDueDays2 = 2 * $PastDueDays1;
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,
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
49 FROM ".TB_PREF."suppliers,
50 ".TB_PREF."payment_terms,
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 ";
59 $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) > 0.004 ";
60 $sql .= "ORDER BY ".TB_PREF."supp_trans.tran_date";
63 return db_query($sql, "The supplier details could not be retrieved");
66 //----------------------------------------------------------------------------------------------------
68 function print_aged_supplier_analysis()
70 global $path_to_root, $systypes_array;
72 $to = $_POST['PARAM_0'];
73 $fromsupp = $_POST['PARAM_1'];
74 $currency = $_POST['PARAM_2'];
75 $show_all = $_POST['PARAM_3'];
76 $summaryOnly = $_POST['PARAM_4'];
77 $no_zeros = $_POST['PARAM_5'];
78 $graphics = $_POST['PARAM_6'];
79 $comments = $_POST['PARAM_7'];
80 $destination = $_POST['PARAM_8'];
83 include_once($path_to_root . "/reporting/includes/excel_report.inc");
85 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
88 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
92 if ($fromsupp == ALL_TEXT)
95 $from = get_supplier_name($fromsupp);
96 $dec = user_price_dec();
98 if ($summaryOnly == 1)
99 $summary = _('Summary Only');
101 $summary = _('Detailed Report');
102 if ($currency == ALL_TEXT)
105 $currency = _('Balances in Home Currency');
110 if ($no_zeros) $nozeros = _('Yes');
111 else $nozeros = _('No');
112 if ($show_all) $show = _('Yes');
113 else $show = _('No');
115 $PastDueDays1 = get_company_pref('past_due_days');
116 $PastDueDays2 = 2 * $PastDueDays1;
117 $nowdue = "1-" . $PastDueDays1 . " " . _('Days');
118 $pastdue1 = $PastDueDays1 + 1 . "-" . $PastDueDays2 . " " . _('Days');
119 $pastdue2 = _('Over') . " " . $PastDueDays2 . " " . _('Days');
121 $cols = array(0, 100, 130, 190, 250, 320, 385, 450, 515);
123 $headers = array(_('Supplier'), '', '', _('Current'), $nowdue, $pastdue1,$pastdue2,
126 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right', 'right');
128 $params = array( 0 => $comments,
129 1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
130 2 => array('text' => _('Supplier'), 'from' => $from, 'to' => ''),
131 3 => array('text' => _('Currency'),'from' => $currency,'to' => ''),
132 4 => array('text' => _('Type'), 'from' => $summary,'to' => ''),
133 5 => array('text' => _('Show Also Allocated'), 'from' => $show, 'to' => ''),
134 6 => array('text' => _('Suppress Zeros'), 'from' => $nozeros, 'to' => ''));
137 $headers[2] = _('currency');
138 $rep = new FrontReport(_('Aged Supplier Analysis'), "AgedSupplierAnalysis", user_pagesize());
141 $rep->Info($params, $cols, $headers, $aligns);
145 $total[0] = $total[1] = $total[2] = $total[3] = $total[4] = 0.0;
146 $PastDueDays1 = get_company_pref('past_due_days');
147 $PastDueDays2 = 2 * $PastDueDays1;
149 $nowdue = "1-" . $PastDueDays1 . " " . _('Days');
150 $pastdue1 = $PastDueDays1 + 1 . "-" . $PastDueDays2 . " " . _('Days');
151 $pastdue2 = _('Over') . " " . $PastDueDays2 . " " . _('Days');
153 $sql = "SELECT supplier_id, supp_name AS name, curr_code FROM ".TB_PREF."suppliers";
154 if ($fromsupp != ALL_TEXT)
155 $sql .= " WHERE supplier_id=".db_escape($fromsupp);
156 $sql .= " ORDER BY supp_name";
157 $result = db_query($sql, "The suppliers could not be retrieved");
159 while ($myrow=db_fetch($result))
161 if (!$convert && $currency != $myrow['curr_code']) continue;
163 if ($convert) $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $to);
166 $supprec = get_supplier_details($myrow['supplier_id'], $to, $show_all);
169 $supprec['Balance'] *= $rate;
170 $supprec['Due'] *= $rate;
171 $supprec['Overdue1'] *= $rate;
172 $supprec['Overdue2'] *= $rate;
174 $str = array($supprec["Balance"] - $supprec["Due"],
175 $supprec["Due"]-$supprec["Overdue1"],
176 $supprec["Overdue1"]-$supprec["Overdue2"],
177 $supprec["Overdue2"],
178 $supprec["Balance"]);
180 if ($no_zeros && array_sum($str) == 0) continue;
183 $rep->TextCol(0, 2, $myrow['name']);
184 if ($convert) $rep->TextCol(2, 3, $myrow['curr_code']);
186 $total[0] += ($supprec["Balance"] - $supprec["Due"]);
187 $total[1] += ($supprec["Due"]-$supprec["Overdue1"]);
188 $total[2] += ($supprec["Overdue1"]-$supprec["Overdue2"]);
189 $total[3] += $supprec["Overdue2"];
190 $total[4] += $supprec["Balance"];
191 for ($i = 0; $i < count($str); $i++)
192 $rep->AmountCol($i + 3, $i + 4, $str[$i], $dec);
196 $res = get_invoices($myrow['supplier_id'], $to, $show_all);
197 if (db_num_rows($res)==0)
199 $rep->Line($rep->row + 4);
200 while ($trans=db_fetch($res))
203 $rep->TextCol(0, 1, $systypes_array[$trans['type']], -2);
204 $rep->TextCol(1, 2, $trans['reference'], -2);
205 $rep->TextCol(2, 3, sql2date($trans['tran_date']), -2);
206 foreach ($trans as $i => $value)
208 $str = array($trans["Balance"] - $trans["Due"],
209 $trans["Due"]-$trans["Overdue1"],
210 $trans["Overdue1"]-$trans["Overdue2"],
213 for ($i = 0; $i < count($str); $i++)
214 $rep->AmountCol($i + 3, $i + 4, $str[$i], $dec);
216 $rep->Line($rep->row - 8);
222 $rep->Line($rep->row + 4);
226 $rep->TextCol(0, 3, _('Grand Total'));
228 for ($i = 0; $i < count($total); $i++)
230 $rep->AmountCol($i + 3, $i + 4, $total[$i], $dec);
231 if ($graphics && $i < count($total) - 1)
233 $pg->y[$i] = abs($total[$i]);
236 $rep->Line($rep->row - 8);
240 global $decseps, $graph_skin;
241 $pg->x = array(_('Current'), $nowdue, $pastdue1, $pastdue2);
242 $pg->title = $rep->title;
243 $pg->axis_x = _("Days");
244 $pg->axis_y = _("Amount");
245 $pg->graphic_1 = $to;
246 $pg->type = $graphics;
247 $pg->skin = $graph_skin;
248 $pg->built_in = false;
249 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
250 $filename = company_path(). "/pdf_files/". uniqid("").".png";
251 $pg->display($filename, true);
252 $w = $pg->width / 1.5;
253 $h = $pg->height / 1.5;
254 $x = ($rep->pageWidth - $w) / 2;
256 if ($rep->row - $h < $rep->bottomMargin)
258 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);