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