e09bdefd8832544710856c9be521784d80075922
[fa-stable.git] / reporting / rep102.php
1 <?php
2
3 $page_security = 2;
4 // ----------------------------------------------------------------
5 // $ Revision:  2.0 $
6 // Creator:     Joe Hunt
7 // date_:       2005-05-19
8 // Title:       Aged Customer Balances
9 // ----------------------------------------------------------------
10 $path_to_root="../";
11
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");
16
17 //----------------------------------------------------------------------------------------------------
18
19 // trial_inquiry_controls();
20 print_aged_customer_analysis();
21
22 function get_invoices($costomer_id, $to)
23 {
24         $todate = date2sql($to);
25         $PastDueDays1 = get_company_pref('past_due_days');
26         $PastDueDays2 = 2 * $PastDueDays1;
27
28         // Revomed allocated from sql
29         $sql = "SELECT ".TB_PREF."sys_types.type_name, ".TB_PREF."debtor_trans.type, ".TB_PREF."debtor_trans.reference,
30                 ".TB_PREF."debtor_trans.tran_date,
31                 (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_discount) as Balance,
32                 IF (".TB_PREF."payment_terms.days_before_due > 0,
33                         CASE WHEN TO_DAYS('$todate') - TO_DAYS(".TB_PREF."debtor_trans.tran_date) >= ".TB_PREF."payment_terms.days_before_due THEN
34                                 ".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_discount
35                         ELSE
36                                 0
37                         END,
38
39                         CASE WHEN TO_DAYS('$todate') - TO_DAYS(DATE_ADD(DATE_ADD(".TB_PREF."debtor_trans.tran_date,
40                                 INTERVAL 1 MONTH), INTERVAL (".TB_PREF."payment_terms.day_in_following_month -
41                                 DAYOFMONTH(".TB_PREF."debtor_trans.tran_date)) DAY)) >= 0 THEN
42                                         ".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_discount
43                         ELSE
44                                 0
45                         END
46                 ) AS Due,
47                 IF (".TB_PREF."payment_terms.days_before_due > 0,
48                         CASE WHEN TO_DAYS('$todate') - TO_DAYS(".TB_PREF."debtor_trans.tran_date) > ".TB_PREF."payment_terms.days_before_due
49                                 AND TO_DAYS('$todate') - TO_DAYS(".TB_PREF."debtor_trans.tran_date) >= (".TB_PREF."payment_terms.days_before_due + $PastDueDays1) THEN
50                                         ".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_discount
51                         ELSE
52                                 0
53                         END,
54
55                         CASE WHEN TO_DAYS('$todate') - TO_DAYS(DATE_ADD(DATE_ADD(".TB_PREF."debtor_trans.tran_date,
56                                 INTERVAL 1 MONTH), INTERVAL (".TB_PREF."payment_terms.day_in_following_month -
57                                 DAYOFMONTH(".TB_PREF."debtor_trans.tran_date)) DAY)) >= $PastDueDays1 THEN
58                                         ".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_discount
59                         ELSE
60                                 0
61                         END
62                 ) AS Overdue1,
63                 IF (".TB_PREF."payment_terms.days_before_due > 0,
64                         CASE WHEN TO_DAYS('$todate') - TO_DAYS(".TB_PREF."debtor_trans.tran_date) > ".TB_PREF."payment_terms.days_before_due
65                                 AND TO_DAYS('$todate') - TO_DAYS(".TB_PREF."debtor_trans.tran_date) >= (".TB_PREF."payment_terms.days_before_due + $PastDueDays2) THEN
66                                         ".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_discount
67                         ELSE
68                                 0
69                         END,
70
71                         CASE WHEN TO_DAYS('$todate') - TO_DAYS(DATE_ADD(DATE_ADD(".TB_PREF."debtor_trans.tran_date,
72                                 INTERVAL 1 MONTH), INTERVAL (".TB_PREF."payment_terms.day_in_following_month -
73                                 DAYOFMONTH(".TB_PREF."debtor_trans.tran_date)) DAY)) >= $PastDueDays2 THEN
74                                         ".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_discount
75                         ELSE
76                                 0
77                         END
78                 ) AS Overdue2
79
80                 FROM ".TB_PREF."debtors_master,
81                         ".TB_PREF."payment_terms,
82                         ".TB_PREF."debtor_trans,
83                         ".TB_PREF."sys_types
84
85                 WHERE ".TB_PREF."sys_types.type_id = ".TB_PREF."debtor_trans.type
86                     AND ".TB_PREF."debtor_trans.type <> 13
87                         AND ".TB_PREF."debtors_master.payment_terms = ".TB_PREF."payment_terms.terms_indicator
88                         AND ".TB_PREF."debtors_master.debtor_no = ".TB_PREF."debtor_trans.debtor_no
89                         AND ".TB_PREF."debtor_trans.debtor_no = $costomer_id
90                         AND ".TB_PREF."debtor_trans.tran_date <= '$todate'
91                         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_discount) > 0.004
92                         ORDER BY ".TB_PREF."debtor_trans.tran_date";
93
94
95         return db_query($sql, "The customer details could not be retrieved");
96 }
97
98 //----------------------------------------------------------------------------------------------------
99
100 function print_aged_customer_analysis()
101 {
102     global $comp_path, $path_to_root;
103
104     include_once($path_to_root . "reporting/includes/pdf_report.inc");
105
106     $to = $_POST['PARAM_0'];
107     $fromcust = $_POST['PARAM_1'];
108     $currency = $_POST['PARAM_2'];
109         $summaryOnly = $_POST['PARAM_3'];
110     $graphics = $_POST['PARAM_4'];
111     $comments = $_POST['PARAM_5'];
112         if ($graphics)
113         {
114                 include_once($path_to_root . "reporting/includes/class.graphic.inc");
115                 $pg = new graph();
116         }
117
118         if ($fromcust == reserved_words::get_all_numeric())
119                 $from = _('All');
120         else
121                 $from = get_customer_name($fromcust);
122     $dec = user_price_dec();
123
124         if ($summaryOnly == 1)
125                 $summary = _('Summary Only');
126         else
127                 $summary = _('Detailed Report');
128         if ($currency == reserved_words::get_all())
129         {
130                 $convert = true;
131                 $currency = _('Balances in Home Currency');
132         }
133         else
134                 $convert = false;
135
136         $PastDueDays1 = get_company_pref('past_due_days');
137         $PastDueDays2 = 2 * $PastDueDays1;
138         $nowdue = "1-" . $PastDueDays1 . " " . _('Days');
139         $pastdue1 = $PastDueDays1 + 1 . "-" . $PastDueDays2 . " " . _('Days');
140         $pastdue2 = _('Over') . " " . $PastDueDays2 . " " . _('Days');
141
142         $cols = array(0, 100, 130, 190, 250, 320, 385, 450,     515);
143         $headers = array(_('Customer'), '',     '',     _('Current'), $nowdue, $pastdue1, $pastdue2,
144                 _('Total Balance'));
145
146         $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right',     'right');
147
148     $params =   array(  0 => $comments,
149                                         1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
150                                     2 => array('text' => _('Customer'), 'from' => $from, 'to' => ''),
151                                     3 => array('text' => _('Currency'), 'from' => $currency, 'to' => ''),
152                         4 => array('text' => _('Type'),         'from' => $summary,'to' => ''));
153
154         if ($convert)
155                 $headers[2] = _('Currency');
156     $rep = new FrontReport(_('Aged Customer Analysis'), "AgedCustomerAnalysis.pdf", user_pagesize());
157
158     $rep->Font();
159     $rep->Info($params, $cols, $headers, $aligns);
160     $rep->Header();
161
162         $total = array(0,0,0,0);
163
164         $sql = "SELECT debtor_no, name, curr_code FROM ".TB_PREF."debtors_master ";
165         if ($fromcust != reserved_words::get_all_numeric())
166                 $sql .= "WHERE debtor_no=$fromcust ";
167         $sql .= "ORDER BY name";
168         $result = db_query($sql, "The customers could not be retrieved");
169
170         while ($myrow=db_fetch($result))
171         {
172                 if (!$convert && $currency != $myrow['curr_code'])
173                         continue;
174                 $rep->fontSize += 2;
175                 $rep->TextCol(0, 3, $myrow['name']);
176                 if ($convert)
177                 {
178                         $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $to);
179                         $rep->TextCol(2, 4,     $myrow['curr_code']);
180                 }
181                 else
182                         $rate = 1.0;
183                 $rep->fontSize -= 2;
184                 $custrec = get_customer_details($myrow['debtor_no'], $to);
185                 foreach ($custrec as $i => $value)
186                         $custrec[$i] *= $rate;
187                 $total[0] += ($custrec["Balance"] - $custrec["Due"]);
188                 $total[1] += ($custrec["Due"]-$custrec["Overdue1"]);
189                 $total[2] += ($custrec["Overdue1"]-$custrec["Overdue2"]);
190                 $total[3] += $custrec["Overdue2"];
191                 $total[4] += $custrec["Balance"];
192                 $str = array(number_format2(($custrec["Balance"] - $custrec["Due"]),$dec),
193                         number_format2(($custrec["Due"]-$custrec["Overdue1"]),$dec),
194                         number_format2(($custrec["Overdue1"]-$custrec["Overdue2"]) ,$dec),
195                         number_format2($custrec["Overdue2"],$dec),
196                         number_format2($custrec["Balance"],$dec));
197                 for ($i = 0; $i < count($str); $i++)
198                         $rep->TextCol($i + 3, $i + 4, $str[$i]);
199                 $rep->NewLine(1, 2);
200                 if (!$summaryOnly)
201                 {
202                         $res = get_invoices($myrow['debtor_no'], $to);
203                 if (db_num_rows($res)==0)
204                                 continue;
205                 $rep->Line($rep->row + 4);
206                         while ($trans=db_fetch($res))
207                         {
208                                 $rep->NewLine(1, 2);
209                         $rep->TextCol(0, 1,     $trans['type_name'], -2);
210                                 $rep->TextCol(1, 2,     $trans['reference'], -2);
211                                 $rep->TextCol(2, 3, sql2date($trans['tran_date']), -2);
212                                 if ($trans['type'] == 11 || $trans['type'] == 12 || $trans['type'] == 2)
213                                 {
214                                         $trans['Balance'] *= -1;
215                                         $trans['Due'] *= -1;
216                                         $trans['Overdue1'] *= -1;
217                                         $trans['Overdue2'] *= -1;
218                                 }
219                                 foreach ($trans as $i => $value)
220                                         $trans[$i] *= $rate;
221                                 $str = array(number_format2(($trans["Balance"] - $trans["Due"]),$dec),
222                                         number_format2(($trans["Due"]-$trans["Overdue1"]),$dec),
223                                         number_format2(($trans["Overdue1"]-$trans["Overdue2"]) ,$dec),
224                                         number_format2($trans["Overdue2"],$dec),
225                                         number_format2($trans["Balance"],$dec));
226                                 for ($i = 0; $i < count($str); $i++)
227                                         $rep->TextCol($i + 3, $i + 4, $str[$i]);
228                         }
229                         $rep->Line($rep->row - 8);
230                         $rep->NewLine(2);
231                 }
232         }
233         if ($summaryOnly)
234         {
235         $rep->Line($rep->row  + 4);
236         $rep->NewLine();
237         }
238         $rep->fontSize += 2;
239         $rep->TextCol(0, 3, _('Grand Total'));
240         $rep->fontSize -= 2;
241         for ($i = 0; $i < count($total); $i++)
242         {
243                 $rep->TextCol($i + 3, $i + 4, number_format2($total[$i], $dec));
244                 if ($graphics && $i < count($total) - 1)
245                 {
246                         $pg->y[$i] = abs($total[$i]);
247                 }
248         }
249         $rep->Line($rep->row - 8);
250         if ($graphics)
251         {
252                 global $decseps, $graph_skin;
253                 $pg->x = array(_('Current'), $nowdue, $pastdue1, $pastdue2);
254                 $pg->title     = $rep->title;
255                 $pg->axis_x    = _("Days");
256                 $pg->axis_y    = _("Amount");
257                 $pg->graphic_1 = $to;
258                 $pg->type      = $graphics;
259                 $pg->skin      = $graph_skin;
260                 $pg->built_in  = false;
261                 $pg->fontfile  = $path_to_root . "reporting/fonts/Vera.ttf";
262                 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
263                 $filename = $comp_path .'/'. user_company(). "/images/test.png";
264                 $pg->display($filename, true);
265                 $w = $pg->width / 1.5;
266                 $h = $pg->height / 1.5;
267                 $x = ($rep->pageWidth - $w) / 2;
268                 $rep->NewLine(2);
269                 if ($rep->row - $h < $rep->bottomMargin)
270                         $rep->Header();
271                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
272         }
273     $rep->End();
274 }
275
276 ?>