0034d786654176b6add8c467cce7fffb7cf761ca
[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 = 'SA_CUSTPAYMREP';
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($customer_id, $to, $all=true)
31 {
32         $todate = date2sql($to);
33         $PastDueDays1 = get_company_pref('past_due_days');
34         $PastDueDays2 = 2 * $PastDueDays1;
35
36         // Revomed allocated from sql
37         if ($all)
38         $value = "(".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + "
39                         .TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_freight_tax + "
40                         .TB_PREF."debtor_trans.ov_discount)";
41         else            
42         $value = "(".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + "
43                         .TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_freight_tax + "
44                         .TB_PREF."debtor_trans.ov_discount - ".TB_PREF."debtor_trans.alloc)";
45         $due = "IF (".TB_PREF."debtor_trans.type=".ST_SALESINVOICE.",".TB_PREF."debtor_trans.due_date,".TB_PREF."debtor_trans.tran_date)";
46         $sql = "SELECT ".TB_PREF."debtor_trans.type, ".TB_PREF."debtor_trans.reference,
47                 ".TB_PREF."debtor_trans.tran_date,
48                 $value as Balance,
49                 IF ((TO_DAYS('$todate') - TO_DAYS($due)) > 0,$value,0) AS Due,
50                 IF ((TO_DAYS('$todate') - TO_DAYS($due)) > $PastDueDays1,$value,0) AS Overdue1,
51                 IF ((TO_DAYS('$todate') - TO_DAYS($due)) > $PastDueDays2,$value,0) AS Overdue2
52
53                 FROM ".TB_PREF."debtors_master,
54                         ".TB_PREF."debtor_trans
55
56                 WHERE ".TB_PREF."debtor_trans.type <> ".ST_CUSTDELIVERY."
57                         AND ".TB_PREF."debtors_master.debtor_no = ".TB_PREF."debtor_trans.debtor_no
58                         AND ".TB_PREF."debtor_trans.debtor_no = $customer_id 
59                         AND ".TB_PREF."debtor_trans.tran_date <= '$todate'
60                         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) > ".FLOAT_COMP_DELTA." ";
61         if (!$all)
62                 $sql .= "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 - ".TB_PREF."debtor_trans.alloc) > ".FLOAT_COMP_DELTA." ";  
63         $sql .= "ORDER BY ".TB_PREF."debtor_trans.tran_date";
64
65         return db_query($sql, "The customer details could not be retrieved");
66 }
67
68 //----------------------------------------------------------------------------------------------------
69
70 function print_aged_customer_analysis()
71 {
72     global $path_to_root, $systypes_array;
73
74         $to = $_POST['PARAM_0'];
75         $fromcust = $_POST['PARAM_1'];
76         $currency = $_POST['PARAM_2'];
77         $show_all = $_POST['PARAM_3'];
78         $summaryOnly = $_POST['PARAM_4'];
79         $no_zeros = $_POST['PARAM_5'];
80         $graphics = $_POST['PARAM_6'];
81         $comments = $_POST['PARAM_7'];
82         $orientation = $_POST['PARAM_8'];
83         $destination = $_POST['PARAM_9'];
84         if ($destination)
85                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
86         else
87                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
88         $orientation = ($orientation ? 'L' : 'P');
89         if ($graphics)
90         {
91                 include_once($path_to_root . "/reporting/includes/class.graphic.inc");
92                 $pg = new graph();
93         }
94
95         if ($fromcust == ALL_TEXT)
96                 $from = _('All');
97         else
98                 $from = get_customer_name($fromcust);
99         $dec = user_price_dec();
100
101         if ($summaryOnly == 1)
102                 $summary = _('Summary Only');
103         else
104                 $summary = _('Detailed Report');
105         if ($currency == ALL_TEXT)
106         {
107                 $convert = true;
108                 $currency = _('Balances in Home Currency');
109         }
110         else
111                 $convert = false;
112
113         if ($no_zeros) $nozeros = _('Yes');
114         else $nozeros = _('No');
115         if ($show_all) $show = _('Yes');
116         else $show = _('No');
117
118         $PastDueDays1 = get_company_pref('past_due_days');
119         $PastDueDays2 = 2 * $PastDueDays1;
120         $nowdue = "1-" . $PastDueDays1 . " " . _('Days');
121         $pastdue1 = $PastDueDays1 + 1 . "-" . $PastDueDays2 . " " . _('Days');
122         $pastdue2 = _('Over') . " " . $PastDueDays2 . " " . _('Days');
123
124         $cols = array(0, 100, 130, 190, 250, 320, 385, 450,     515);
125         $headers = array(_('Customer'), '',     '',     _('Current'), $nowdue, $pastdue1, $pastdue2,
126                 _('Total Balance'));
127
128         $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right',     'right');
129
130         $params =   array(      0 => $comments,
131                                 1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
132                                 2 => array('text' => _('Customer'),     'from' => $from, 'to' => ''),
133                                 3 => array('text' => _('Currency'), 'from' => $currency, 'to' => ''),
134                                 4 => array('text' => _('Type'),         'from' => $summary,'to' => ''),
135                     5 => array('text' => _('Show Also Allocated'), 'from' => $show, 'to' => ''),                
136                                 6 => array('text' => _('Suppress Zeros'), 'from' => $nozeros, 'to' => ''));
137
138         if ($convert)
139                 $headers[2] = _('Currency');
140     $rep = new FrontReport(_('Aged Customer Analysis'), "AgedCustomerAnalysis", user_pagesize(), 9, $orientation);
141     if ($orientation == 'L')
142         recalculate_cols($cols);
143  
144     $rep->Font();
145     $rep->Info($params, $cols, $headers, $aligns);
146     $rep->NewPage();
147
148         $total = array(0,0,0,0, 0);
149
150         $sql = "SELECT debtor_no, name, curr_code FROM ".TB_PREF."debtors_master";
151         if ($fromcust != ALL_TEXT)
152                 $sql .= " WHERE debtor_no=".db_escape($fromcust);
153         $sql .= " ORDER BY name";
154         $result = db_query($sql, "The customers could not be retrieved");
155
156         while ($myrow=db_fetch($result))
157         {
158                 if (!$convert && $currency != $myrow['curr_code'])
159                         continue;
160
161                 if ($convert) $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $to);
162                 else $rate = 1.0;
163                 $custrec = get_customer_details($myrow['debtor_no'], $to, $show_all);
164                 if (!$custrec)
165                         continue;
166                 $custrec['Balance'] *= $rate;
167                 $custrec['Due'] *= $rate;
168                 $custrec['Overdue1'] *= $rate;
169                 $custrec['Overdue2'] *= $rate;
170                 $str = array($custrec["Balance"] - $custrec["Due"],
171                         $custrec["Due"]-$custrec["Overdue1"],
172                         $custrec["Overdue1"]-$custrec["Overdue2"],
173                         $custrec["Overdue2"],
174                         $custrec["Balance"]);
175                 if ($no_zeros && floatcmp(array_sum($str), 0) == 0) continue;
176
177                 $rep->fontSize += 2;
178                 $rep->TextCol(0, 2, $myrow['name']);
179                 if ($convert) $rep->TextCol(2, 3,       $myrow['curr_code']);
180                 $rep->fontSize -= 2;
181                 $total[0] += ($custrec["Balance"] - $custrec["Due"]);
182                 $total[1] += ($custrec["Due"]-$custrec["Overdue1"]);
183                 $total[2] += ($custrec["Overdue1"]-$custrec["Overdue2"]);
184                 $total[3] += $custrec["Overdue2"];
185                 $total[4] += $custrec["Balance"];
186                 for ($i = 0; $i < count($str); $i++)
187                         $rep->AmountCol($i + 3, $i + 4, $str[$i], $dec);
188                 $rep->NewLine(1, 2);
189                 if (!$summaryOnly)
190                 {
191                         $res = get_invoices($myrow['debtor_no'], $to, $show_all);
192                 if (db_num_rows($res)==0)
193                                 continue;
194                 $rep->Line($rep->row + 4);
195                         while ($trans=db_fetch($res))
196                         {
197                                 $rep->NewLine(1, 2);
198                         $rep->TextCol(0, 1, $systypes_array[$trans['type']], -2);
199                                 $rep->TextCol(1, 2,     $trans['reference'], -2);
200                                 $rep->DateCol(2, 3, $trans['tran_date'], true, -2);
201                                 if ($trans['type'] == ST_CUSTCREDIT || $trans['type'] == ST_CUSTPAYMENT || $trans['type'] == ST_BANKDEPOSIT)
202                                 {
203                                         $trans['Balance'] *= -1;
204                                         $trans['Due'] *= -1;
205                                         $trans['Overdue1'] *= -1;
206                                         $trans['Overdue2'] *= -1;
207                                 }
208                                 foreach ($trans as $i => $value)
209                                         $trans[$i] *= $rate;
210                                 $str = array($trans["Balance"] - $trans["Due"],
211                                         $trans["Due"]-$trans["Overdue1"],
212                                         $trans["Overdue1"]-$trans["Overdue2"],
213                                         $trans["Overdue2"],
214                                         $trans["Balance"]);
215                                 for ($i = 0; $i < count($str); $i++)
216                                         $rep->AmountCol($i + 3, $i + 4, $str[$i], $dec);
217                         }
218                         $rep->Line($rep->row - 8);
219                         $rep->NewLine(2);
220                 }
221         }
222         if ($summaryOnly)
223         {
224         $rep->Line($rep->row  + 4);
225         $rep->NewLine();
226         }
227         $rep->fontSize += 2;
228         $rep->TextCol(0, 3, _('Grand Total'));
229         $rep->fontSize -= 2;
230         for ($i = 0; $i < count($total); $i++)
231         {
232                 $rep->AmountCol($i + 3, $i + 4, $total[$i], $dec);
233                 if ($graphics && $i < count($total) - 1)
234                 {
235                         $pg->y[$i] = abs($total[$i]);
236                 }
237         }
238         $rep->Line($rep->row - 8);
239         if ($graphics)
240         {
241                 global $decseps, $graph_skin;
242                 $pg->x = array(_('Current'), $nowdue, $pastdue1, $pastdue2);
243                 $pg->title     = $rep->title;
244                 $pg->axis_x    = _("Days");
245                 $pg->axis_y    = _("Amount");
246                 $pg->graphic_1 = $to;
247                 $pg->type      = $graphics;
248                 $pg->skin      = $graph_skin;
249                 $pg->built_in  = false;
250                 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
251                 $filename = company_path(). "/pdf_files/". uniqid("").".png";
252                 $pg->display($filename, true);
253                 $w = $pg->width / 1.5;
254                 $h = $pg->height / 1.5;
255                 $x = ($rep->pageWidth - $w) / 2;
256                 $rep->NewLine(2);
257                 if ($rep->row - $h < $rep->bottomMargin)
258                         $rep->NewPage();
259                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
260         }
261         $rep->NewLine();
262     $rep->End();
263 }
264
265 ?>