e9412e6a12977657d5b60e98fab0c453de35334a
[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.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."debtors_master.payment_terms = ".TB_PREF."payment_terms.terms_indicator 
87                         AND ".TB_PREF."debtors_master.debtor_no = ".TB_PREF."debtor_trans.debtor_no
88                         AND ".TB_PREF."debtor_trans.debtor_no = $costomer_id 
89                         AND ".TB_PREF."debtor_trans.tran_date <= '$todate' 
90                         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
91                         ORDER BY ".TB_PREF."debtor_trans.tran_date";
92
93
94         return db_query($sql, "The customer details could not be retrieved");
95 }
96
97 //----------------------------------------------------------------------------------------------------
98
99 function print_aged_customer_analysis()
100 {
101     global $path_to_root;
102
103     include_once($path_to_root . "reporting/includes/pdf_report.inc");
104
105     $to = $_POST['PARAM_0'];
106     $fromcust = $_POST['PARAM_1'];
107     $tocust = $_POST['PARAM_2'];
108     $currency = $_POST['PARAM_3'];
109         $summaryOnly = $_POST['PARAM_4'];
110     $graphics = $_POST['PARAM_5'];
111     $comments = $_POST['PARAM_6'];
112         if ($graphics)
113         {
114                 include_once($path_to_root . "reporting/includes/class.graphic.inc");
115                 $pg = new graph();
116         }       
117     
118         if ($fromcust == null)
119                 $fromcust = 0;
120         if ($tocust == null)
121                 $tocust = 0;
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' => get_customer_name($fromcust),
151                             'to' => get_customer_name($tocust)),
152                                     3 => array('text' => _('Currency'), 'from' => $currency, 'to' => ''),
153                         4 => array('text' => _('Type'),         'from' => $summary,'to' => ''));
154
155         if ($convert)
156                 $headers[2] = _('Currency');
157     $rep = new FrontReport(_('Aged Customer Analysis'), "AgedCustomerAnalysis.pdf", user_pagesize());
158
159     $rep->Font();
160     $rep->Info($params, $cols, $headers, $aligns);
161     $rep->Header();
162
163         $total = array();
164         $total[0] = $total[1] = $total[2] = $total[3] = $total[4] = 0.0;
165         
166         $sql = "SELECT debtor_no, name, curr_code FROM ".TB_PREF."debtors_master
167                 WHERE debtor_no>=$fromcust AND debtor_no<=$tocust 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                                 foreach ($trans as $i => $value) 
213                                         $trans[$i] *= $rate;
214                                 $str = array(number_format2(($trans["Balance"] - $trans["Due"]),$dec),
215                                         number_format2(($trans["Due"]-$trans["Overdue1"]),$dec),
216                                         number_format2(($trans["Overdue1"]-$trans["Overdue2"]) ,$dec),
217                                         number_format2($trans["Overdue2"],$dec),
218                                         number_format2($trans["Balance"],$dec));
219                                 for ($i = 0; $i < count($str); $i++)
220                                         $rep->TextCol($i + 3, $i + 4, $str[$i]);
221                         }
222                         $rep->Line($rep->row - 8);
223                         $rep->NewLine(2);
224                 }       
225         }
226         if ($summaryOnly)
227         {
228         $rep->Line($rep->row  + 4);
229         $rep->NewLine();
230         }
231     $rep->fontSize += 2;
232         $rep->TextCol(0, 3, _('Grand Total'));
233         $rep->fontSize -= 2;
234         for ($i = 0; $i < count($total); $i++)
235         {
236                 $rep->TextCol($i + 3, $i + 4, number_format2($total[$i], $dec));
237                 if ($graphics && $i < count($total) - 1)
238                 {
239                         $pg->y[$i] = abs($total[$i]);
240                 }       
241         }       
242         $rep->Line($rep->row - 8);
243         if ($graphics)
244         {
245                 global $decseps, $graph_skin;
246                 $pg->x = array(_('Current'), $nowdue, $pastdue1, $pastdue2);
247                 $pg->title     = $rep->title;
248                 $pg->axis_x    = _("Days");
249                 $pg->axis_y    = _("Amount");
250                 $pg->graphic_1 = $to;
251                 $pg->type      = $graphics;
252                 $pg->skin      = $graph_skin;
253                 $pg->built_in  = false;
254                 $pg->fontfile  = $path_to_root . "reporting/fonts/Vera.ttf";
255                 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
256                 $filename = $path_to_root . "reporting/pdf_files/test.png";
257                 $pg->display($filename, true);
258                 $w = $pg->width / 1.5;
259                 $h = $pg->height / 1.5;
260                 $x = ($rep->pageWidth - $w) / 2;
261                 $rep->NewLine(2);
262                 if ($rep->row - $h < $rep->bottomMargin)
263                         $rep->Header();
264                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
265         }
266     $rep->End();
267 }
268
269 ?>