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