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