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