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