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
40 $value = "(trans.ov_amount + trans.ov_gst + trans.ov_discount)";
42 $value = "IF (trans.type=".ST_SUPPINVOICE." OR trans.type=".ST_BANKDEPOSIT." OR (trans.type=".ST_JOURNAL." AND (trans.ov_amount + trans.ov_gst + trans.ov_discount)>0),
43 (trans.ov_amount + trans.ov_gst + trans.ov_discount - trans.alloc),
44 (trans.ov_amount + trans.ov_gst + trans.ov_discount + trans.alloc))";
45 $due = "IF (trans.type=".ST_SUPPINVOICE." OR trans.type=".ST_SUPPCREDIT.",trans.due_date,trans.tran_date)";
46 $sql = "SELECT trans.type,
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
54 FROM ".TB_PREF."suppliers supplier,
55 ".TB_PREF."supp_trans trans
57 WHERE supplier.supplier_id = trans.supplier_id
58 AND trans.supplier_id = $supplier_id
59 AND trans.tran_date <= '$todate'
60 AND ABS(trans.ov_amount + trans.ov_gst + trans.ov_discount) > ".FLOAT_COMP_DELTA;
62 $sql .= "AND $value <> 0 ";
63 $sql .= " ORDER BY trans.tran_date";
66 return db_query($sql, "The supplier details could not be retrieved");
69 //----------------------------------------------------------------------------------------------------
71 function print_aged_supplier_analysis()
73 global $path_to_root, $systypes_array, $SysPrefs;
75 $to = $_POST['PARAM_0'];
76 $fromsupp = $_POST['PARAM_1'];
77 $currency = $_POST['PARAM_2'];
78 $show_all = $_POST['PARAM_3'];
79 $summaryOnly = $_POST['PARAM_4'];
80 $no_zeros = $_POST['PARAM_5'];
81 $graphics = $_POST['PARAM_6'];
82 $comments = $_POST['PARAM_7'];
83 $orientation = $_POST['PARAM_8'];
84 $destination = $_POST['PARAM_9'];
87 include_once($path_to_root . "/reporting/includes/excel_report.inc");
89 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
90 $orientation = ($orientation ? 'L' : 'P');
93 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
94 $pg = new chart($graphics);
98 if ($fromsupp == ALL_TEXT)
101 $from = get_supplier_name($fromsupp);
102 $dec = user_price_dec();
104 if ($summaryOnly == 1)
105 $summary = _('Summary Only');
107 $summary = _('Detailed Report');
108 if ($currency == ALL_TEXT)
111 $currency = _('Balances in Home Currency');
116 if ($no_zeros) $nozeros = _('Yes');
117 else $nozeros = _('No');
118 if ($show_all) $show = _('Yes');
119 else $show = _('No');
121 $PastDueDays1 = get_company_pref('past_due_days');
122 $PastDueDays2 = 2 * $PastDueDays1;
123 $nowdue = "1-" . $PastDueDays1 . " " . _('Days');
124 $pastdue1 = $PastDueDays1 + 1 . "-" . $PastDueDays2 . " " . _('Days');
125 $pastdue2 = _('Over') . " " . $PastDueDays2 . " " . _('Days');
127 $cols = array(0, 100, 130, 190, 250, 320, 385, 450, 515);
129 $headers = array(_('Supplier'), '', '', _('Current'), $nowdue, $pastdue1,$pastdue2,
132 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right', 'right');
134 $params = array( 0 => $comments,
135 1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
136 2 => array('text' => _('Supplier'), 'from' => $from, 'to' => ''),
137 3 => array('text' => _('Currency'),'from' => $currency,'to' => ''),
138 4 => array('text' => _('Type'), 'from' => $summary,'to' => ''),
139 5 => array('text' => _('Show Also Allocated'), 'from' => $show, 'to' => ''),
140 6 => array('text' => _('Suppress Zeros'), 'from' => $nozeros, 'to' => ''));
143 $headers[2] = _('currency');
144 $rep = new FrontReport(_('Aged Supplier Analysis'), "AgedSupplierAnalysis", user_pagesize(), 9, $orientation);
145 if ($orientation == 'L')
146 recalculate_cols($cols);
149 $rep->Info($params, $cols, $headers, $aligns);
153 $total[0] = $total[1] = $total[2] = $total[3] = $total[4] = 0.0;
154 $PastDueDays1 = get_company_pref('past_due_days');
155 $PastDueDays2 = 2 * $PastDueDays1;
157 $nowdue = "1-" . $PastDueDays1 . " " . _('Days');
158 $pastdue1 = $PastDueDays1 + 1 . "-" . $PastDueDays2 . " " . _('Days');
159 $pastdue2 = _('Over') . " " . $PastDueDays2 . " " . _('Days');
161 $sql = "SELECT supplier_id, supp_name AS name, curr_code, inactive FROM ".TB_PREF."suppliers";
162 if ($fromsupp != ALL_TEXT)
163 $sql .= " WHERE supplier_id=".db_escape($fromsupp);
164 $sql .= " ORDER BY supp_name";
165 $result = db_query($sql, "The suppliers could not be retrieved");
167 while ($myrow=db_fetch($result))
169 if (!$convert && $currency != $myrow['curr_code']) continue;
171 if ($convert) $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $to);
174 $supprec = get_supplier_details($myrow['supplier_id'], $to, $show_all);
177 $supprec['Balance'] *= $rate;
178 $supprec['Due'] *= $rate;
179 $supprec['Overdue1'] *= $rate;
180 $supprec['Overdue2'] *= $rate;
182 $str = array($supprec["Balance"] - $supprec["Due"],
183 $supprec["Due"]-$supprec["Overdue1"],
184 $supprec["Overdue1"]-$supprec["Overdue2"],
185 $supprec["Overdue2"],
186 $supprec["Balance"]);
188 if ($no_zeros && floatcmp(array_sum($str), 0) == 0) continue;
191 $rep->TextCol(0, 2, $myrow['name'].($myrow['inactive']==1 ? " ("._("Inactive").")" : ""));
192 if ($convert) $rep->TextCol(2, 3, $myrow['curr_code']);
194 $total[0] += ($supprec["Balance"] - $supprec["Due"]);
195 $total[1] += ($supprec["Due"]-$supprec["Overdue1"]);
196 $total[2] += ($supprec["Overdue1"]-$supprec["Overdue2"]);
197 $total[3] += $supprec["Overdue2"];
198 $total[4] += $supprec["Balance"];
199 for ($i = 0; $i < count($str); $i++)
200 $rep->AmountCol($i + 3, $i + 4, $str[$i], $dec);
204 $res = get_invoices($myrow['supplier_id'], $to, $show_all);
205 if (db_num_rows($res)==0)
207 $rep->Line($rep->row + 4);
208 while ($trans=db_fetch($res))
211 $rep->TextCol(0, 1, $systypes_array[$trans['type']], -2);
212 $rep->TextCol(1, 2, $trans['reference'], -2);
213 $rep->TextCol(2, 3, sql2date($trans['tran_date']), -2);
214 foreach ($trans as $i => $value)
215 $trans[$i] = (float)$trans[$i] * $rate;
216 $str = array($trans["Balance"] - $trans["Due"],
217 $trans["Due"]-$trans["Overdue1"],
218 $trans["Overdue1"]-$trans["Overdue2"],
221 for ($i = 0; $i < count($str); $i++)
222 $rep->AmountCol($i + 3, $i + 4, $str[$i], $dec);
224 $rep->Line($rep->row - 8);
230 $rep->Line($rep->row + 4);
234 $rep->TextCol(0, 3, _('Grand Total'));
236 for ($i = 0; $i < count($total); $i++)
238 $rep->AmountCol($i + 3, $i + 4, $total[$i], $dec);
239 if ($graphics && $i < count($total) - 1)
241 $serie[$i] = abs($total[$i]);
244 $rep->Line($rep->row - 8);
248 $pg->setStream('png');
249 $pg->setLabels(array(_('Current'), $nowdue, $pastdue1, $pastdue2));
250 $pg->addSerie(_("Balances"), $serie);
251 $pg->setTitle($rep->title);
252 $pg->setXTitle(_("Days"));
253 $pg->setYTitle(_("Amount"));
254 $pg->setDTitle(number_format2($total[4]));
255 $pg->setValues(true);
256 $pg->latin_notation = ($SysPrefs->decseps[user_dec_sep()] != ".");
257 $filename = company_path(). "/pdf_files/". random_id().".png";
258 $pg->display($filename, true);
259 $w = $pg->width / 1.5;
260 $h = $pg->height / 1.5;
261 $x = ($rep->pageWidth - $w) / 2;
263 if ($rep->row - $h < $rep->bottomMargin)
265 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);