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