Fixed inconsistencies in customer and supplier aging
[fa-stable.git] / reporting / rep202.php
1 <?php
2
3 $page_security = 2;
4 // ----------------------------------------------------------------
5 // $ Revision:  2.0 $
6 // Creator:     Joe Hunt
7 // date_:       2005-05-19
8 // Title:       Ages Supplier Analysis
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_supplier_analysis();
21
22 //----------------------------------------------------------------------------------------------------
23
24 function get_invoices($supplier_id, $to)
25 {
26         $todate = date2sql($to);
27         $PastDueDays1 = get_company_pref('past_due_days');
28         $PastDueDays2 = 2 * $PastDueDays1;
29
30         // Revomed allocated from sql
31     $value = "(".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount)";
32         $due = "IF (".TB_PREF."supp_trans.type=20 OR ".TB_PREF."supp_trans.type=21,".TB_PREF."supp_trans.due_date,".TB_PREF."supp_trans.tran_date)";
33         $sql = "SELECT ".TB_PREF."sys_types.type_name,
34                 ".TB_PREF."supp_trans.reference,
35                 ".TB_PREF."supp_trans.tran_date,
36                 $value as Balance,
37                 IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= 0,$value,0) AS Due,
38                 IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $PastDueDays1,$value,0) AS Overdue1,
39                 IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $PastDueDays2,$value,0) AS Overdue2
40
41                 FROM ".TB_PREF."suppliers,
42                         ".TB_PREF."payment_terms,
43                         ".TB_PREF."supp_trans,
44                         ".TB_PREF."sys_types
45
46                 WHERE ".TB_PREF."sys_types.type_id = ".TB_PREF."supp_trans.type
47                         AND ".TB_PREF."suppliers.payment_terms = ".TB_PREF."payment_terms.terms_indicator
48                         AND ".TB_PREF."suppliers.supplier_id = ".TB_PREF."supp_trans.supplier_id
49                         AND ".TB_PREF."supp_trans.supplier_id = $supplier_id
50                         AND ".TB_PREF."supp_trans.tran_date <= '$todate'
51                         AND ABS(".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount) > 0.004
52                         ORDER BY ".TB_PREF."supp_trans.tran_date";
53
54
55         return db_query($sql, "The supplier details could not be retrieved");
56 }
57
58 //----------------------------------------------------------------------------------------------------
59
60 function print_aged_supplier_analysis()
61 {
62     global $comp_path, $path_to_root;
63
64     include_once($path_to_root . "reporting/includes/pdf_report.inc");
65
66     $to = $_POST['PARAM_0'];
67     $fromsupp = $_POST['PARAM_1'];
68     $currency = $_POST['PARAM_2'];
69         $summaryOnly = $_POST['PARAM_3'];
70     $graphics = $_POST['PARAM_4'];
71     $comments = $_POST['PARAM_5'];
72         if ($graphics)
73         {
74                 include_once($path_to_root . "reporting/includes/class.graphic.inc");
75                 $pg = new graph();
76         }
77
78         if ($fromsupp == reserved_words::get_all_numeric())
79                 $from = _('All');
80         else
81                 $from = get_supplier_name($fromsupp);
82     $dec = user_price_dec();
83
84         if ($summaryOnly == 1)
85                 $summary = _('Summary Only');
86         else
87                 $summary = _('Detailed Report');
88         if ($currency == reserved_words::get_all())
89         {
90                 $convert = true;
91                 $currency = _('Balances in Home Currency');
92         }
93         else
94                 $convert = false;
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
103         $headers = array(_('Supplier'), '',     '',     _('Current'), $nowdue, $pastdue1,$pastdue2,
104                 _('Total Balance'));
105
106         $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right',     'right');
107
108     $params =   array(  0 => $comments,
109                                     1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
110                                     2 => array('text' => _('Supplier'), 'from' => $from, 'to' => ''),
111                                     3 => array('text' => _('Currency'),'from' => $currency,'to' => ''),
112                         4 => array('text' => _('Type'), 'from' => $summary,'to' => ''));
113
114         if ($convert)
115                 $headers[2] = _('currency');
116     $rep = new FrontReport(_('Aged Supplier Analysis'), "AgedSupplierAnalysis.pdf", user_pagesize());
117
118     $rep->Font();
119     $rep->Info($params, $cols, $headers, $aligns);
120     $rep->Header();
121
122         $total = array();
123         $total[0] = $total[1] = $total[2] = $total[3] = $total[4] = 0.0;
124         $PastDueDays1 = get_company_pref('past_due_days');
125         $PastDueDays2 = 2 * $PastDueDays1;
126
127         $nowdue = "1-" . $PastDueDays1 . " " . _('Days');
128         $pastdue1 = $PastDueDays1 + 1 . "-" . $PastDueDays2 . " " . _('Days');
129         $pastdue2 = _('Over') . " " . $PastDueDays2 . " " . _('Days');
130
131         $sql = "SELECT supplier_id, supp_name AS name, curr_code FROM ".TB_PREF."suppliers ";
132         if ($fromsupp != reserved_words::get_all_numeric())
133                 $sql .= "WHERE supplier_id=$fromsupp ";
134         $sql .= "ORDER BY supp_name";
135         $result = db_query($sql, "The suppliers could not be retrieved");
136
137         while ($myrow=db_fetch($result))
138         {
139                 if (!$convert && $currency != $myrow['curr_code'])
140                         continue;
141                 $rep->fontSize += 2;
142                 $rep->TextCol(0, 3,     $myrow['name']);
143                 if ($convert)
144                 {
145                         $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $to);
146                         $rep->TextCol(2, 4,     $myrow['curr_code']);
147                 }
148                 else
149                         $rate = 1.0;
150                 $rep->fontSize -= 2;
151                 $supprec = get_supplier_details($myrow['supplier_id'], $to);
152                 foreach ($supprec as $i => $value)
153                         $supprec[$i] *= $rate;
154                 $total[0] += ($supprec["Balance"] - $supprec["Due"]);
155                 $total[1] += ($supprec["Due"]-$supprec["Overdue1"]);
156                 $total[2] += ($supprec["Overdue1"]-$supprec["Overdue2"]);
157                 $total[3] += $supprec["Overdue2"];
158                 $total[4] += $supprec["Balance"];
159                 $str = array(number_format2(($supprec["Balance"] - $supprec["Due"]),$dec),
160                         number_format2(($supprec["Due"]-$supprec["Overdue1"]),$dec),
161                         number_format2(($supprec["Overdue1"]-$supprec["Overdue2"]) ,$dec),
162                         number_format2($supprec["Overdue2"],$dec),
163                         number_format2($supprec["Balance"],$dec));
164                 for ($i = 0; $i < count($str); $i++)
165                         $rep->TextCol($i + 3, $i + 4, $str[$i]);
166                 $rep->NewLine(1, 2);
167                 if (!$summaryOnly)
168                 {
169                         $res = get_invoices($myrow['supplier_id'], $to);
170                 if (db_num_rows($res)==0)
171                                 continue;
172                 $rep->Line($rep->row + 4);
173                         while ($trans=db_fetch($res))
174                         {
175                                 $rep->NewLine(1, 2);
176                         $rep->TextCol(0, 1,     $trans['type_name'], -2);
177                                 $rep->TextCol(1, 2,     $trans['reference'], -2);
178                                 $rep->TextCol(2, 3,     sql2date($trans['tran_date']), -2);
179                                 foreach ($trans as $i => $value)
180                                         $trans[$i] *= $rate;
181                                 $str = array(number_format2(($trans["Balance"] - $trans["Due"]),$dec),
182                                         number_format2(($trans["Due"]-$trans["Overdue1"]),$dec),
183                                         number_format2(($trans["Overdue1"]-$trans["Overdue2"]) ,$dec),
184                                         number_format2($trans["Overdue2"],$dec),
185                                         number_format2($trans["Balance"],$dec));
186                                 for ($i = 0; $i < count($str); $i++)
187                                         $rep->TextCol($i + 3, $i + 4, $str[$i]);
188                         }
189                         $rep->Line($rep->row - 8);
190                         $rep->NewLine(2);
191                 }
192         }
193         if ($summaryOnly)
194         {
195         $rep->Line($rep->row  + 4);
196         $rep->NewLine();
197         }
198         $rep->fontSize += 2;
199         $rep->TextCol(0, 3,     _('Grand Total'));
200         $rep->fontSize -= 2;
201         for ($i = 0; $i < count($total); $i++)
202         {
203                 $rep->TextCol($i + 3, $i + 4, number_format2($total[$i], $dec));
204                 if ($graphics && $i < count($total) - 1)
205                 {
206                         $pg->y[$i] = abs($total[$i]);
207                 }
208         }
209         $rep->Line($rep->row  - 8);
210         if ($graphics)
211         {
212                 global $decseps, $graph_skin;
213                 $pg->x = array(_('Current'), $nowdue, $pastdue1, $pastdue2);
214                 $pg->title     = $rep->title;
215                 $pg->axis_x    = _("Days");
216                 $pg->axis_y    = _("Amount");
217                 $pg->graphic_1 = $to;
218                 $pg->type      = $graphics;
219                 $pg->skin      = $graph_skin;
220                 $pg->built_in  = false;
221                 $pg->fontfile  = $path_to_root . "reporting/fonts/Vera.ttf";
222                 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
223                 $filename = $comp_path.'/'.user_company(). "/pdf_files/test.png";
224                 $pg->display($filename, true);
225                 $w = $pg->width / 1.5;
226                 $h = $pg->height / 1.5;
227                 $x = ($rep->pageWidth - $w) / 2;
228                 $rep->NewLine(2);
229                 if ($rep->row - $h < $rep->bottomMargin)
230                         $rep->Header();
231                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
232         }
233     $rep->End();
234 }
235
236 ?>