Fixed inconsistencies in customer and supplier aging
[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     $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,
35                 $value as Balance,
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
39
40                 FROM ".TB_PREF."debtors_master,
41                         ".TB_PREF."payment_terms,
42                         ".TB_PREF."debtor_trans,
43                         ".TB_PREF."sys_types
44
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";
53
54         return db_query($sql, "The customer details could not be retrieved");
55 }
56
57 //----------------------------------------------------------------------------------------------------
58
59 function print_aged_customer_analysis()
60 {
61     global $comp_path, $path_to_root;
62
63     include_once($path_to_root . "reporting/includes/pdf_report.inc");
64
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'];
71         if ($graphics)
72         {
73                 include_once($path_to_root . "reporting/includes/class.graphic.inc");
74                 $pg = new graph();
75         }
76
77         if ($fromcust == reserved_words::get_all_numeric())
78                 $from = _('All');
79         else
80                 $from = get_customer_name($fromcust);
81     $dec = user_price_dec();
82
83         if ($summaryOnly == 1)
84                 $summary = _('Summary Only');
85         else
86                 $summary = _('Detailed Report');
87         if ($currency == reserved_words::get_all())
88         {
89                 $convert = true;
90                 $currency = _('Balances in Home Currency');
91         }
92         else
93                 $convert = false;
94
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');
100
101         $cols = array(0, 100, 130, 190, 250, 320, 385, 450,     515);
102         $headers = array(_('Customer'), '',     '',     _('Current'), $nowdue, $pastdue1, $pastdue2,
103                 _('Total Balance'));
104
105         $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right',     'right');
106
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' => ''));
112
113         if ($convert)
114                 $headers[2] = _('Currency');
115     $rep = new FrontReport(_('Aged Customer Analysis'), "AgedCustomerAnalysis.pdf", user_pagesize());
116
117     $rep->Font();
118     $rep->Info($params, $cols, $headers, $aligns);
119     $rep->Header();
120
121         $total = array(0,0,0,0, 0);
122
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");
128
129         while ($myrow=db_fetch($result))
130         {
131                 if (!$convert && $currency != $myrow['curr_code'])
132                         continue;
133                 $rep->fontSize += 2;
134                 $rep->TextCol(0, 3, $myrow['name']);
135                 if ($convert)
136                 {
137                         $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $to);
138                         $rep->TextCol(2, 4,     $myrow['curr_code']);
139                 }
140                 else
141                         $rate = 1.0;
142                 $rep->fontSize -= 2;
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]);
158                 $rep->NewLine(1, 2);
159                 if (!$summaryOnly)
160                 {
161                         $res = get_invoices($myrow['debtor_no'], $to);
162                 if (db_num_rows($res)==0)
163                                 continue;
164                 $rep->Line($rep->row + 4);
165                         while ($trans=db_fetch($res))
166                         {
167                                 $rep->NewLine(1, 2);
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)
172                                 {
173                                         $trans['Balance'] *= -1;
174                                         $trans['Due'] *= -1;
175                                         $trans['Overdue1'] *= -1;
176                                         $trans['Overdue2'] *= -1;
177                                 }
178                                 foreach ($trans as $i => $value)
179                                         $trans[$i] *= $rate;
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]);
187                         }
188                         $rep->Line($rep->row - 8);
189                         $rep->NewLine(2);
190                 }
191         }
192         if ($summaryOnly)
193         {
194         $rep->Line($rep->row  + 4);
195         $rep->NewLine();
196         }
197         $rep->fontSize += 2;
198         $rep->TextCol(0, 3, _('Grand Total'));
199         $rep->fontSize -= 2;
200         for ($i = 0; $i < count($total); $i++)
201         {
202                 $rep->TextCol($i + 3, $i + 4, number_format2($total[$i], $dec));
203                 if ($graphics && $i < count($total) - 1)
204                 {
205                         $pg->y[$i] = abs($total[$i]);
206                 }
207         }
208         $rep->Line($rep->row - 8);
209         if ($graphics)
210         {
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;
227                 $rep->NewLine(2);
228                 if ($rep->row - $h < $rep->bottomMargin)
229                         $rep->Header();
230                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
231         }
232     $rep->End();
233 }
234
235 ?>