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