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