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