Bugfix in new inventory purchasing report.
[fa-stable.git] / reporting / rep306.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_SALESANALYTIC';
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Inventory Sales 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 . "/includes/banking.inc");
25 include_once($path_to_root . "/gl/includes/gl_db.inc");
26 include_once($path_to_root . "/inventory/includes/db/items_category_db.inc");
27
28 //----------------------------------------------------------------------------------------------------
29
30 print_inventory_purchase();
31
32 function getTransactions($category, $location, $fromsupp, $item, $from, $to)
33 {
34         $from = date2sql($from);
35         $to = date2sql($to);
36         $sql = "SELECT ".TB_PREF."stock_master.category_id,
37                         ".TB_PREF."stock_category.description AS cat_description,
38                         ".TB_PREF."stock_master.stock_id,
39                         ".TB_PREF."stock_master.description, ".TB_PREF."stock_master.inactive,
40                         ".TB_PREF."stock_moves.loc_code,
41                         ".TB_PREF."supp_trans.supplier_id,
42                         ".TB_PREF."supp_trans.supp_reference,
43                         ".TB_PREF."suppliers.supp_name AS supplier_name,
44                         ".TB_PREF."stock_moves.tran_date,
45                         ".TB_PREF."stock_moves.qty AS qty,
46                         ".TB_PREF."stock_moves.price*(1-".TB_PREF."stock_moves.discount_percent) AS price
47                 FROM ".TB_PREF."stock_master,
48                         ".TB_PREF."stock_category,
49                         ".TB_PREF."supp_trans,
50                         ".TB_PREF."suppliers,
51                         ".TB_PREF."stock_moves
52                 WHERE ".TB_PREF."stock_master.stock_id=".TB_PREF."stock_moves.stock_id
53                 AND ".TB_PREF."stock_master.category_id=".TB_PREF."stock_category.category_id
54                 AND ".TB_PREF."supp_trans.supplier_id=".TB_PREF."suppliers.supplier_id
55                 AND (".TB_PREF."stock_moves.type=".TB_PREF."supp_trans.type OR ".TB_PREF."stock_moves.type=".ST_SUPPRECEIVE.")
56                 AND ".TB_PREF."stock_moves.trans_no=".TB_PREF."supp_trans.trans_no
57                 AND ".TB_PREF."stock_moves.tran_date>='$from'
58                 AND ".TB_PREF."stock_moves.tran_date<='$to'
59                 AND (".TB_PREF."supp_trans.type=".ST_SUPPINVOICE." OR ".TB_PREF."stock_moves.type=".ST_SUPPCREDIT.")
60                 AND (".TB_PREF."stock_master.mb_flag='B' OR ".TB_PREF."stock_master.mb_flag='M')";
61                 if ($category != 0)
62                         $sql .= " AND ".TB_PREF."stock_master.category_id = ".db_escape($category);
63                 if ($location != '')
64                         $sql .= " AND ".TB_PREF."stock_moves.loc_code = ".db_escape($location);
65                 if ($fromsupp != '')
66                         $sql .= " AND ".TB_PREF."suppliers.supplier_id = ".db_escape($fromsupp);
67                 if ($item != '')
68                         $sql .= " AND ".TB_PREF."stock_master.stock_id = ".db_escape($item);
69                 $sql .= " ORDER BY ".TB_PREF."stock_master.category_id,
70                         ".TB_PREF."suppliers.supp_name, ".TB_PREF."stock_master.stock_id, ".TB_PREF."stock_moves.tran_date";
71     return db_query($sql,"No transactions were returned");
72
73 }
74
75 //----------------------------------------------------------------------------------------------------
76
77 function print_inventory_purchase()
78 {
79     global $path_to_root;
80
81         $from = $_POST['PARAM_0'];
82         $to = $_POST['PARAM_1'];
83     $category = $_POST['PARAM_2'];
84     $location = $_POST['PARAM_3'];
85     $fromsupp = $_POST['PARAM_4'];
86     $item = $_POST['PARAM_5'];
87         $comments = $_POST['PARAM_6'];
88         $destination = $_POST['PARAM_7'];
89         if ($destination)
90                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
91         else
92                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
93
94     $dec = user_price_dec();
95
96         if ($category == ALL_NUMERIC)
97                 $category = 0;
98         if ($category == 0)
99                 $cat = _('All');
100         else
101                 $cat = get_category_name($category);
102
103         if ($location == '')
104                 $loc = _('All');
105         else
106                 $loc = get_location_name($location);
107
108         if ($fromsupp == '')
109                 $froms = _('All');
110         else
111                 $froms = get_supplier_name($fromsupp);
112
113         if ($item == '')
114                 $itm = _('All');
115         else
116                 $itm = $item;
117
118         $cols = array(0, 75, 175, 220, 270, 370, 390, 470,      535);
119
120         $headers = array(_('Category'), _('Description'), _('Date'), _('#'), _('Supplier'), _('Qty'), _('Unit Price'), _('Total'));
121         if ($fromsupp != '')
122                 $headers[4] = '';
123
124         $aligns = array('left', 'left', 'left', 'left', 'left', 'left', 'right', 'right');
125
126     $params =   array(  0 => $comments,
127                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
128                                     2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
129                                     3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''),
130                                     4 => array('text' => _('Supplier'), 'from' => $froms, 'to' => ''),
131                                     5 => array('text' => _('Item'), 'from' => $itm, 'to' => ''));
132
133     $rep = new FrontReport(_('Inventory Purchasing Report'), "InventoryPurchasingReport", user_pagesize());
134
135     $rep->Font();
136     $rep->Info($params, $cols, $headers, $aligns);
137     $rep->NewPage();
138
139         $res = getTransactions($category, $location, $fromsupp, $item, $from, $to);
140         $total = $total_supp = $grandtotal = 0.0;
141         $total_qty = 0.0;
142         $catt = $stock_description = $supplier_name = '';
143         while ($trans=db_fetch($res))
144         {
145                 if ($stock_description != $trans['description'])
146                 {
147                         if ($stock_description != '')
148                         {
149                                 if ($supplier_name != '')
150                                 {
151                                         $rep->NewLine(2, 3);
152                                         $rep->TextCol(0, 1, _('Total'));
153                                         $rep->TextCol(1, 4, $stock_description);
154                                         $rep->TextCol(4, 5, $supplier_name);
155                                         $rep->TextCol(5, 7, $total_qty);
156                                         $rep->AmountCol(7, 8, $total_supp, $dec);
157                                         $rep->Line($rep->row - 2);
158                                         $rep->NewLine();
159                                         $total_supp = $total_qty = 0.0;
160                                         $supplier_name = $trans['supplier_name'];
161                                 }       
162                         }
163                         $stock_description = $trans['description'];
164                 }
165
166                 if ($supplier_name != $trans['supplier_name'])
167                 {
168                         if ($supplier_name != '')
169                         {
170                                 $rep->NewLine(2, 3);
171                                 $rep->TextCol(0, 1, _('Total'));
172                                 $rep->TextCol(1, 4, $stock_description);
173                                 $rep->TextCol(4, 5, $supplier_name);
174                                 $rep->TextCol(5, 7, $total_qty);
175                                 $rep->AmountCol(7, 8, $total_supp, $dec);
176                                 $rep->Line($rep->row - 2);
177                                 $rep->NewLine();
178                                 $total_supp = $total_qty = 0.0;
179                         }
180                         $supplier_name = $trans['supplier_name'];
181                 }
182                 if ($catt != $trans['cat_description'])
183                 {
184                         if ($catt != '')
185                         {
186                                 $rep->NewLine(2, 3);
187                                 $rep->TextCol(0, 1, _('Total'));
188                                 $rep->TextCol(1, 7, $catt);
189                                 $rep->AmountCol(7, 8, $total, $dec);
190                                 $rep->Line($rep->row - 2);
191                                 $rep->NewLine();
192                                 $rep->NewLine();
193                                 $total = 0.0;
194                         }
195                         $rep->TextCol(0, 1, $trans['category_id']);
196                         $rep->TextCol(1, 6, $trans['cat_description']);
197                         $catt = $trans['cat_description'];
198                         $rep->NewLine();
199                 }
200                 
201                 $curr = get_supplier_currency($trans['supplier_id']);
202                 $rate = get_exchange_rate_from_home_currency($curr, sql2date($trans['tran_date']));
203                 $trans['price'] *= $rate;
204                 $rep->NewLine();
205                 $rep->fontSize -= 2;
206                 $rep->TextCol(0, 1, $trans['stock_id']);
207                 if ($fromsupp == ALL_TEXT)
208                 {
209                         $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
210                         $rep->TextCol(2, 3, $trans['tran_date']);
211                         $rep->TextCol(3, 4, $trans['supp_reference']);
212                         $rep->TextCol(4, 5, $trans['supplier_name']);
213                 }
214                 else
215                 {
216                         $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
217                         $rep->TextCol(2, 3, $trans['tran_date']);
218                         $rep->TextCol(3, 4, $trans['supp_reference']);
219                 }       
220                 $rep->AmountCol(5, 6, $trans['qty'], get_qty_dec($trans['stock_id']));
221                 $rep->AmountCol(6, 7, $trans['price'], $dec);
222                 $amt = $trans['qty'] * $trans['price'];
223                 $rep->AmountCol(7, 8, $amt, $dec);
224                 $rep->fontSize += 2;
225                 $total += $amt;
226                 $total_supp += $amt;
227                 $grandtotal += $amt;
228                 $total_qty += $trans['qty'];
229         }
230         if ($stock_description != '')
231         {
232                 if ($supplier_name != '')
233                 {
234                         $rep->NewLine(2, 3);
235                         $rep->TextCol(0, 1, _('Total'));
236                         $rep->TextCol(1, 4, $stock_description);
237                         $rep->TextCol(4, 5, $supplier_name);
238                         $rep->TextCol(5, 7, $total_qty);
239                         $rep->AmountCol(7, 8, $total_supp, $dec);
240                         $rep->Line($rep->row - 2);
241                         $rep->NewLine();
242                         $rep->NewLine();
243                         $total_supp = $total_qty = 0.0;
244                         $supplier_name = $trans['supplier_name'];
245                 }       
246         }
247         if ($supplier_name != '')
248         {
249                 $rep->NewLine(2, 3);
250                 $rep->TextCol(0, 1, _('Total'));
251                 $rep->TextCol(1, 4, $stock_description);
252                 $rep->TextCol(4, 5, $supplier_name);
253                 $rep->TextCol(5, 7, $total_qty);
254                 $rep->AmountCol(7, 8, $total_supp, $dec);
255                 $rep->Line($rep->row - 2);
256                 $rep->NewLine();
257                 $rep->NewLine();
258         }
259
260         $rep->NewLine(2, 3);
261         $rep->TextCol(0, 1, _('Total'));
262         $rep->TextCol(1, 7, $catt);
263         $rep->AmountCol(7, 8, $total, $dec);
264         $rep->Line($rep->row - 2);
265         $rep->NewLine();
266         $rep->NewLine(2, 1);
267         $rep->TextCol(0, 7, _('Grand Total'));
268         $rep->AmountCol(7, 8, $grandtotal, $dec);
269
270         $rep->Line($rep->row  - 4);
271         $rep->NewLine();
272     $rep->End();
273 }
274
275 ?>