Replaced the Excel Writer engine with XLS driver (the XML driver can later be downloaded)
[fa-stable.git] / reporting / rep203.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:       Payment Report
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_payment_report();
29
30 function getTransactions($supplier, $date)
31 {
32         $date = date2sql($date);
33
34         $sql = "SELECT ".TB_PREF."sys_types.type_name,
35                         ".TB_PREF."supp_trans.supp_reference,
36                         ".TB_PREF."supp_trans.tran_date,
37                         ".TB_PREF."supp_trans.due_date,
38                         ".TB_PREF."supp_trans.trans_no,
39                         ".TB_PREF."supp_trans.type,
40                         ".TB_PREF."supp_trans.rate,
41                         (ABS(".TB_PREF."supp_trans.ov_amount) + ABS(".TB_PREF."supp_trans.ov_gst) - ".TB_PREF."supp_trans.alloc) AS Balance,
42                         (ABS(".TB_PREF."supp_trans.ov_amount) + ABS(".TB_PREF."supp_trans.ov_gst) ) AS TranTotal
43                 FROM ".TB_PREF."supp_trans,
44                         ".TB_PREF."sys_types
45                 WHERE ".TB_PREF."sys_types.type_id = ".TB_PREF."supp_trans.type
46                 AND ".TB_PREF."supp_trans.supplier_id = '" . $supplier . "'
47                 AND ABS(".TB_PREF."supp_trans.ov_amount) + ABS(".TB_PREF."supp_trans.ov_gst) - ".TB_PREF."supp_trans.alloc != 0
48                 AND ".TB_PREF."supp_trans.tran_date <='" . $date . "'
49                 ORDER BY ".TB_PREF."supp_trans.type,
50                         ".TB_PREF."supp_trans.trans_no";
51
52     return db_query($sql, "No transactions were returned");
53 }
54
55 //----------------------------------------------------------------------------------------------------
56
57 function print_payment_report()
58 {
59     global $path_to_root;
60
61     $to = $_POST['PARAM_0'];
62     $fromsupp = $_POST['PARAM_1'];
63     $currency = $_POST['PARAM_2'];
64     $comments = $_POST['PARAM_3'];
65         $destination = $_POST['PARAM_4'];
66         if ($destination)
67                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
68         else
69                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
70
71         if ($fromsupp == reserved_words::get_all_numeric())
72                 $from = _('All');
73         else
74                 $from = get_supplier_name($fromsupp);
75
76     $dec = user_price_dec();
77
78         if ($currency == reserved_words::get_all())
79         {
80                 $convert = true;
81                 $currency = _('Balances in Home Currency');
82         }
83         else
84                 $convert = false;
85
86         $cols = array(0, 100, 130, 190, 250, 320, 385, 450,     515);
87
88         $headers = array(_('Trans Type'), _('#'), _('Due Date'), '', '',
89                 '', _('Total'), _('Balance'));
90
91         $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
92
93     $params =   array(  0 => $comments,
94                                     1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
95                                     2 => array('text' => _('Supplier'), 'from' => $from, 'to' => ''),
96                                     3 => array(  'text' => _('Currency'),'from' => $currency, 'to' => ''));
97
98     $rep = new FrontReport(_('Payment Report'), "PaymentReport", user_pagesize());
99
100     $rep->Font();
101     $rep->Info($params, $cols, $headers, $aligns);
102     $rep->Header();
103
104         $total = array();
105         $grandtotal = array(0,0);
106
107         $sql = "SELECT supplier_id, supp_name AS name, curr_code, ".TB_PREF."payment_terms.terms FROM ".TB_PREF."suppliers, ".TB_PREF."payment_terms
108                 WHERE ";
109         if ($fromsupp != reserved_words::get_all_numeric())
110                 $sql .= "supplier_id=$fromsupp AND ";
111         $sql .= "".TB_PREF."suppliers.payment_terms = ".TB_PREF."payment_terms.terms_indicator
112                 ORDER BY supp_name";
113         $result = db_query($sql, "The customers could not be retrieved");
114
115         while ($myrow=db_fetch($result))
116         {
117                 if (!$convert && $currency != $myrow['curr_code'])
118                         continue;
119                 $rep->fontSize += 2;
120                 $rep->TextCol(0, 6, $myrow['name'] . " - " . $myrow['terms']);
121                 if ($convert)
122                         $rep->TextCol(6, 7,     $myrow['curr_code']);
123                 $rep->fontSize -= 2;
124                 $rep->NewLine(1, 2);
125                 $res = getTransactions($myrow['supplier_id'], $to);
126                 if (db_num_rows($res)==0)
127                         continue;
128                 $rep->Line($rep->row + 4);
129                 $total[0] = $total[1] = 0.0;
130                 while ($trans=db_fetch($res))
131                 {
132                         if ($convert)
133                                 $rate = $trans['rate'];
134                         else
135                                 $rate = 1.0;
136                         $rep->NewLine(1, 2);
137                         $rep->TextCol(0, 1,     $trans['type_name']);
138                         $rep->TextCol(1, 2,     $trans['supp_reference']);
139                         if ($trans['type'] == 20)
140                                 $rep->DateCol(2, 3,     $trans['due_date'], true);
141                         else    
142                                 $rep->DateCol(2, 3,     $trans['tran_date'], true);
143                         if ($trans['type'] != 20)
144                         {
145                                 $trans['TranTotal'] = -$trans['TranTotal'];
146                                 $trans['Balance'] = -$trans['Balance'];
147                         }
148                         $item[0] = $trans['TranTotal'] * $rate;
149                         $rep->AmountCol(6, 7, $item[0], $dec);
150                         $item[1] = $trans['Balance'] * $rate;
151                         $rep->AmountCol(7, 8, $item[1], $dec);
152                         for ($i = 0; $i < 2; $i++)
153                         {
154                                 $total[$i] += $item[$i];
155                                 $grandtotal[$i] += $item[$i];
156                         }
157                 }
158                 $rep->Line($rep->row - 8);
159                 $rep->NewLine(2);
160                 $rep->TextCol(0, 3,     _('Total'));
161                 for ($i = 0; $i < 2; $i++)
162                 {
163                         $rep->AmountCol($i + 6, $i + 7, $total[$i], $dec);
164                         $total[$i] = 0.0;
165                 }
166         $rep->Line($rep->row  - 4);
167         $rep->NewLine(2);
168         }
169         $rep->fontSize += 2;
170         $rep->TextCol(0, 3,     _('Grand Total'));
171         $rep->fontSize -= 2;
172         for ($i = 0; $i < 2; $i++)
173                 $rep->AmountCol($i + 6, $i + 7,$grandtotal[$i], $dec);
174         $rep->Line($rep->row  - 4);
175         $rep->NewLine();
176     $rep->End();
177 }
178
179 ?>