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