Preparation for Excel Writer continued
[fa-stable.git] / reporting / rep201.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:       Supplier Balances
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_supplier_balances();
30
31 function getTransactions($supplier_id, $date)
32 {
33         $date = date2sql($date);
34
35     $sql = "SELECT ".TB_PREF."supp_trans.*, ".TB_PREF."sys_types.type_name,
36                                 (".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount)
37                                 AS TotalAmount, ".TB_PREF."supp_trans.alloc AS Allocated,
38                                 ((".TB_PREF."supp_trans.type = 20)
39                                         AND ".TB_PREF."supp_trans.due_date < '$date') AS OverDue
40                         FROM ".TB_PREF."supp_trans, ".TB_PREF."sys_types
41                         WHERE ".TB_PREF."supp_trans.tran_date <= '$date' AND ".TB_PREF."supp_trans.supplier_id = '$supplier_id'
42                                 AND ".TB_PREF."supp_trans.type = ".TB_PREF."sys_types.type_id
43                                 ORDER BY ".TB_PREF."supp_trans.tran_date";
44
45     $TransResult = db_query($sql,"No transactions were returned");
46
47     return $TransResult;
48 }
49
50 //----------------------------------------------------------------------------------------------------
51
52 function print_supplier_balances()
53 {
54     global $path_to_root;
55
56     $to = $_POST['PARAM_0'];
57     $fromsupp = $_POST['PARAM_1'];
58     $currency = $_POST['PARAM_2'];
59     $comments = $_POST['PARAM_3'];
60         $destination = $_POST['PARAM_4'];
61         if ($destination)
62         {
63                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
64                 $filename = "SupplierBalances.xml";
65         }       
66         else
67         {
68                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
69                 $filename = "SupplierBalances.pdf";
70         }
71
72         if ($fromsupp == reserved_words::get_all_numeric())
73                 $from = _('All');
74         else
75                 $from = get_supplier_name($fromsupp);
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'), _('#'), _('Date'), _('Due Date'), _('Charges'),
89                 _('Credits'), _('Allocated'), _('Outstanding'));
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(_('Supplier Balances'), $filename, 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,0,0);
106
107         $sql = "SELECT supplier_id, supp_name AS name, curr_code FROM ".TB_PREF."suppliers ";
108         if ($fromsupp != reserved_words::get_all_numeric())
109                 $sql .= "WHERE supplier_id=$fromsupp ";
110         $sql .= "ORDER BY supp_name";
111         $result = db_query($sql, "The customers could not be retrieved");
112
113         while ($myrow=db_fetch($result))
114         {
115                 if (!$convert && $currency != $myrow['curr_code'])
116                         continue;
117                 $rep->fontSize += 2;
118                 $rep->TextCol(0, 3, $myrow['name']);
119                 if ($convert)
120                         $rep->TextCol(3, 4,     $myrow['curr_code']);
121                 $rep->fontSize -= 2;
122                 $rep->NewLine(1, 2);
123                 $res = getTransactions($myrow['supplier_id'], $to);
124                 if (db_num_rows($res)==0)
125                         continue;
126                 $rep->Line($rep->row + 4);
127                 $total[0] = $total[1] = $total[2] = $total[3] = 0.0;
128                 while ($trans=db_fetch($res))
129                 {
130                         $rep->NewLine(1, 2);
131                         $rep->TextCol(0, 1,     $trans['type_name']);
132                         $rep->TextCol(1, 2,     $trans['reference']);
133                         $rep->DateCol(2, 3,     $trans['tran_date'], true);
134                         if ($trans['type'] == 20)
135                                 $rep->DateCol(3, 4,     $trans['due_date'], true);
136                         $item[0] = $item[1] = 0.0;
137                         if ($convert)
138                                 $rate = $trans['rate'];
139                         else
140                                 $rate = 1.0;
141                         if ($trans['TotalAmount'] > 0.0)
142                         {
143                                 $item[0] = round2(abs($trans['TotalAmount']) * $rate, $dec);
144                                 $rep->AmountCol(4, 5, $item[0], $dec);
145                         }
146                         else
147                         {
148                                 $item[1] = round2(abs($trans['TotalAmount']) * $rate, $dec);
149                                 $rep->AmountCol(5, 6, $item[1], $dec);
150                         }
151                         $item[2] = round2($trans['Allocated'] * $rate, $dec);
152                         $rep->AmountCol(6, 7, $item[2], $dec);
153                         /*
154                         if ($trans['type'] == 20)
155                                 $item[3] = ($trans['TotalAmount'] - $trans['Allocated']) * $rate;
156                         else
157                                 $item[3] = ($trans['TotalAmount'] + $trans['Allocated']) * $rate;
158                         */      
159                         if ($trans['type'] == 20)
160                                 $item[3] = $item[0] + $item[1] - $item[2];
161                         else    
162                                 $item[3] = $item[0] - $item[1] + $item[2];
163                         $rep->AmountCol(7, 8, $item[3], $dec);
164                         for ($i = 0; $i < 4; $i++)
165                         {
166                                 $total[$i] += $item[$i];
167                                 $grandtotal[$i] += $item[$i];
168                         }
169                 }
170                 $rep->Line($rep->row - 8);
171                 $rep->NewLine(2);
172                 $rep->TextCol(0, 3,     _('Total'));
173                 for ($i = 0; $i < 4; $i++)
174                 {
175                         $rep->AmountCol($i + 4, $i + 5, $total[$i], $dec);
176                         $total[$i] = 0.0;
177                 }
178         $rep->Line($rep->row  - 4);
179         $rep->NewLine(2);
180         }
181         $rep->fontSize += 2;
182         $rep->TextCol(0, 3,     _('Grand Total'));
183         $rep->fontSize -= 2;
184         for ($i = 0; $i < 4; $i++)
185                 $rep->AmountCol($i + 4, $i + 5,$grandtotal[$i], $dec);
186         $rep->Line($rep->row  - 4);
187         $rep->NewLine();
188     $rep->End();
189 }
190
191 ?>