4 // ----------------------------------------------------------------
8 // Title: Aged Customer Balances
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_customer_analysis();
22 function get_invoices($costomer_id, $to)
24 $todate = date2sql($to);
25 $PastDueDays1 = get_company_pref('past_due_days');
26 $PastDueDays2 = 2 * $PastDueDays1;
28 // Revomed allocated from sql
29 $value = "(".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + "
30 .TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_freight_tax + "
31 .TB_PREF."debtor_trans.ov_discount)";
32 $due = "IF (".TB_PREF."debtor_trans.type=10,".TB_PREF."debtor_trans.due_date,".TB_PREF."debtor_trans.tran_date)";
33 $sql = "SELECT ".TB_PREF."sys_types.type_name, ".TB_PREF."debtor_trans.type, ".TB_PREF."debtor_trans.reference,
34 ".TB_PREF."debtor_trans.tran_date,
36 IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= 0,$value,0) AS Due,
37 IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $PastDueDays1,$value,0) AS Overdue1,
38 IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $PastDueDays2,$value,0) AS Overdue2
40 FROM ".TB_PREF."debtors_master,
41 ".TB_PREF."payment_terms,
42 ".TB_PREF."debtor_trans,
45 WHERE ".TB_PREF."sys_types.type_id = ".TB_PREF."debtor_trans.type
46 AND ".TB_PREF."debtor_trans.type <> 13
47 AND ".TB_PREF."debtors_master.payment_terms = ".TB_PREF."payment_terms.terms_indicator
48 AND ".TB_PREF."debtors_master.debtor_no = ".TB_PREF."debtor_trans.debtor_no
49 AND ".TB_PREF."debtor_trans.debtor_no = $costomer_id
50 AND ".TB_PREF."debtor_trans.tran_date <= '$todate'
51 AND ABS(".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_freight_tax + ".TB_PREF."debtor_trans.ov_discount) > 0.004
52 ORDER BY ".TB_PREF."debtor_trans.tran_date";
54 return db_query($sql, "The customer details could not be retrieved");
57 //----------------------------------------------------------------------------------------------------
59 function print_aged_customer_analysis()
61 global $comp_path, $path_to_root;
63 include_once($path_to_root . "reporting/includes/pdf_report.inc");
65 $to = $_POST['PARAM_0'];
66 $fromcust = $_POST['PARAM_1'];
67 $currency = $_POST['PARAM_2'];
68 $summaryOnly = $_POST['PARAM_3'];
69 $graphics = $_POST['PARAM_4'];
70 $comments = $_POST['PARAM_5'];
73 include_once($path_to_root . "reporting/includes/class.graphic.inc");
77 if ($fromcust == reserved_words::get_all_numeric())
80 $from = get_customer_name($fromcust);
81 $dec = user_price_dec();
83 if ($summaryOnly == 1)
84 $summary = _('Summary Only');
86 $summary = _('Detailed Report');
87 if ($currency == reserved_words::get_all())
90 $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);
102 $headers = array(_('Customer'), '', '', _('Current'), $nowdue, $pastdue1, $pastdue2,
105 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right', 'right');
107 $params = array( 0 => $comments,
108 1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
109 2 => array('text' => _('Customer'), 'from' => $from, 'to' => ''),
110 3 => array('text' => _('Currency'), 'from' => $currency, 'to' => ''),
111 4 => array('text' => _('Type'), 'from' => $summary,'to' => ''));
114 $headers[2] = _('Currency');
115 $rep = new FrontReport(_('Aged Customer Analysis'), "AgedCustomerAnalysis.pdf", user_pagesize());
118 $rep->Info($params, $cols, $headers, $aligns);
121 $total = array(0,0,0,0, 0);
123 $sql = "SELECT debtor_no, name, curr_code FROM ".TB_PREF."debtors_master ";
124 if ($fromcust != reserved_words::get_all_numeric())
125 $sql .= "WHERE debtor_no=$fromcust ";
126 $sql .= "ORDER BY name";
127 $result = db_query($sql, "The customers could not be retrieved");
129 while ($myrow=db_fetch($result))
131 if (!$convert && $currency != $myrow['curr_code'])
134 $rep->TextCol(0, 3, $myrow['name']);
137 $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $to);
138 $rep->TextCol(2, 4, $myrow['curr_code']);
143 $custrec = get_customer_details($myrow['debtor_no'], $to);
144 foreach ($custrec as $i => $value)
145 $custrec[$i] *= $rate;
146 $total[0] += ($custrec["Balance"] - $custrec["Due"]);
147 $total[1] += ($custrec["Due"]-$custrec["Overdue1"]);
148 $total[2] += ($custrec["Overdue1"]-$custrec["Overdue2"]);
149 $total[3] += $custrec["Overdue2"];
150 $total[4] += $custrec["Balance"];
151 $str = array(number_format2(($custrec["Balance"] - $custrec["Due"]),$dec),
152 number_format2(($custrec["Due"]-$custrec["Overdue1"]),$dec),
153 number_format2(($custrec["Overdue1"]-$custrec["Overdue2"]) ,$dec),
154 number_format2($custrec["Overdue2"],$dec),
155 number_format2($custrec["Balance"],$dec));
156 for ($i = 0; $i < count($str); $i++)
157 $rep->TextCol($i + 3, $i + 4, $str[$i]);
161 $res = get_invoices($myrow['debtor_no'], $to);
162 if (db_num_rows($res)==0)
164 $rep->Line($rep->row + 4);
165 while ($trans=db_fetch($res))
168 $rep->TextCol(0, 1, $trans['type_name'], -2);
169 $rep->TextCol(1, 2, $trans['reference'], -2);
170 $rep->TextCol(2, 3, sql2date($trans['tran_date']), -2);
171 if ($trans['type'] == 11 || $trans['type'] == 12 || $trans['type'] == 2)
173 $trans['Balance'] *= -1;
175 $trans['Overdue1'] *= -1;
176 $trans['Overdue2'] *= -1;
178 foreach ($trans as $i => $value)
180 $str = array(number_format2(($trans["Balance"] - $trans["Due"]),$dec),
181 number_format2(($trans["Due"]-$trans["Overdue1"]),$dec),
182 number_format2(($trans["Overdue1"]-$trans["Overdue2"]) ,$dec),
183 number_format2($trans["Overdue2"],$dec),
184 number_format2($trans["Balance"],$dec));
185 for ($i = 0; $i < count($str); $i++)
186 $rep->TextCol($i + 3, $i + 4, $str[$i]);
188 $rep->Line($rep->row - 8);
194 $rep->Line($rep->row + 4);
198 $rep->TextCol(0, 3, _('Grand Total'));
200 for ($i = 0; $i < count($total); $i++)
202 $rep->TextCol($i + 3, $i + 4, number_format2($total[$i], $dec));
203 if ($graphics && $i < count($total) - 1)
205 $pg->y[$i] = abs($total[$i]);
208 $rep->Line($rep->row - 8);
211 global $decseps, $graph_skin;
212 $pg->x = array(_('Current'), $nowdue, $pastdue1, $pastdue2);
213 $pg->title = $rep->title;
214 $pg->axis_x = _("Days");
215 $pg->axis_y = _("Amount");
216 $pg->graphic_1 = $to;
217 $pg->type = $graphics;
218 $pg->skin = $graph_skin;
219 $pg->built_in = false;
220 $pg->fontfile = $path_to_root . "reporting/fonts/Vera.ttf";
221 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
222 $filename = $comp_path .'/'. user_company(). "/images/test.png";
223 $pg->display($filename, true);
224 $w = $pg->width / 1.5;
225 $h = $pg->height / 1.5;
226 $x = ($rep->pageWidth - $w) / 2;
228 if ($rep->row - $h < $rep->bottomMargin)
230 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);