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 // ----------------------------------------------------------------
17 // Title: Inventory Sales Report
18 // ----------------------------------------------------------------
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");
28 //----------------------------------------------------------------------------------------------------
30 print_inventory_purchase();
32 function getTransactions($category, $location, $fromsupp, $item, $from, $to)
34 $from = date2sql($from);
36 $sql = "SELECT item.category_id,
37 category.description AS cat_description,
39 item.description, item.inactive,
42 supplier.supp_name AS supplier_name,
46 FROM ".TB_PREF."stock_moves move
47 LEFT JOIN ".TB_PREF."supp_trans credit ON credit.trans_no=move.trans_no AND credit.type=move.type
48 LEFT JOIN ".TB_PREF."grn_batch grn ON grn.id=move.trans_no AND 25=move.type
49 LEFT JOIN ".TB_PREF."suppliers supplier ON IFNULL(grn.supplier_id, credit.supplier_id)=supplier.supplier_id,
50 ".TB_PREF."stock_master item,
51 ".TB_PREF."stock_category category
52 WHERE item.stock_id=move.stock_id
53 AND item.category_id=category.category_id
54 AND move.tran_date>='$from'
55 AND move.tran_date<='$to'
56 AND (move.type=".ST_SUPPRECEIVE." OR move.type=".ST_SUPPCREDIT.")
57 AND (item.mb_flag='B' OR item.mb_flag='M')";
59 $sql .= " AND item.category_id = ".db_escape($category);
61 $sql .= " AND move.loc_code = ".db_escape($location);
63 $sql .= " AND supplier.supplier_id = ".db_escape($fromsupp);
65 $sql .= " AND item.stock_id = ".db_escape($item);
66 $sql .= " ORDER BY item.category_id,
67 supplier.supp_name, item.stock_id, move.tran_date";
68 return db_query($sql,"No transactions were returned");
72 function get_supp_inv_reference($supplier_id, $stock_id, $date)
74 $sql = "SELECT trans.supp_reference
75 FROM ".TB_PREF."supp_trans trans,
76 ".TB_PREF."supp_invoice_items line,
77 ".TB_PREF."grn_batch batch,
78 ".TB_PREF."grn_items item
79 WHERE trans.type=line.supp_trans_type
80 AND trans.trans_no=line.supp_trans_no
81 AND item.grn_batch_id=batch.id
82 AND item.item_code=line.stock_id
83 AND trans.supplier_id=".db_escape($supplier_id)."
84 AND line.stock_id=".db_escape($stock_id)."
85 AND trans.tran_date=".db_escape($date);
86 $result = db_query($sql,"No transactions were returned");
87 $row = db_fetch_row($result);
94 //----------------------------------------------------------------------------------------------------
96 function print_inventory_purchase()
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 $orientation = $_POST['PARAM_7'];
108 $destination = $_POST['PARAM_8'];
110 include_once($path_to_root . "/reporting/includes/excel_report.inc");
112 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
114 $orientation = ($orientation ? 'L' : 'P');
115 $dec = user_price_dec();
117 if ($category == ALL_NUMERIC)
122 $cat = get_category_name($category);
127 $loc = get_location_name($location);
132 $froms = get_supplier_name($fromsupp);
139 $cols = array(0, 60, 180, 225, 275, 400, 420, 465, 520);
141 $headers = array(_('Category'), _('Description'), _('Date'), _('#'), _('Supplier'), _('Qty'), _('Unit Price'), _('Total'));
145 $aligns = array('left', 'left', 'left', 'left', 'left', 'left', 'right', 'right');
147 $params = array( 0 => $comments,
148 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
149 2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
150 3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''),
151 4 => array('text' => _('Supplier'), 'from' => $froms, 'to' => ''),
152 5 => array('text' => _('Item'), 'from' => $itm, 'to' => ''));
154 $rep = new FrontReport(_('Inventory Purchasing Report'), "InventoryPurchasingReport", user_pagesize(), 9, $orientation);
155 if ($orientation == 'L')
156 recalculate_cols($cols);
159 $rep->Info($params, $cols, $headers, $aligns);
162 $res = getTransactions($category, $location, $fromsupp, $item, $from, $to);
164 $total = $total_supp = $grandtotal = 0.0;
166 $catt = $stock_description = $stock_id = $supplier_name = '';
167 while ($trans=db_fetch($res))
169 if ($stock_description != $trans['description'])
171 if ($stock_description != '')
173 if ($supplier_name != '')
176 $rep->TextCol(0, 1, _('Total'));
177 $rep->TextCol(1, 4, $stock_description);
178 $rep->TextCol(4, 5, $supplier_name);
179 $rep->AmountCol(5, 7, $total_qty, get_qty_dec($stock_id));
180 $rep->AmountCol(7, 8, $total_supp, $dec);
181 $rep->Line($rep->row - 2);
183 $total_supp = $total_qty = 0.0;
184 $supplier_name = $trans['supplier_name'];
187 $stock_id = $trans['stock_id'];
188 $stock_description = $trans['description'];
191 if ($supplier_name != $trans['supplier_name'])
193 if ($supplier_name != '')
196 $rep->TextCol(0, 1, _('Total'));
197 $rep->TextCol(1, 4, $stock_description);
198 $rep->TextCol(4, 5, $supplier_name);
199 $rep->AmountCol(5, 7, $total_qty, get_qty_dec($stock_id));
200 $rep->AmountCol(7, 8, $total_supp, $dec);
201 $rep->Line($rep->row - 2);
203 $total_supp = $total_qty = 0.0;
205 $supplier_name = $trans['supplier_name'];
207 if ($catt != $trans['cat_description'])
212 $rep->TextCol(0, 1, _('Total'));
213 $rep->TextCol(1, 7, $catt);
214 $rep->AmountCol(7, 8, $total, $dec);
215 $rep->Line($rep->row - 2);
220 $rep->TextCol(0, 1, $trans['category_id']);
221 $rep->TextCol(1, 6, $trans['cat_description']);
222 $catt = $trans['cat_description'];
226 $curr = get_supplier_currency($trans['supplier_id']);
227 $rate = get_exchange_rate_from_home_currency($curr, sql2date($trans['tran_date']));
228 $trans['price'] *= $rate;
230 $trans['supp_reference'] = get_supp_inv_reference($trans['supplier_id'], $trans['stock_id'], $trans['tran_date']);
232 $rep->TextCol(0, 1, $trans['stock_id']);
233 if ($fromsupp == ALL_TEXT)
235 $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
236 $rep->TextCol(2, 3, sql2date($trans['tran_date']));
237 $rep->TextCol(3, 4, $trans['supp_reference']);
238 $rep->TextCol(4, 5, $trans['supplier_name']);
242 $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
243 $rep->TextCol(2, 3, sql2date($trans['tran_date']));
244 $rep->TextCol(3, 4, $trans['supp_reference']);
246 $rep->AmountCol(5, 6, $trans['qty'], get_qty_dec($trans['stock_id']));
247 $rep->AmountCol(6, 7, $trans['price'], $dec);
248 $amt = $trans['qty'] * $trans['price'];
249 $rep->AmountCol(7, 8, $amt, $dec);
254 $total_qty += $trans['qty'];
256 if ($stock_description != '')
258 if ($supplier_name != '')
261 $rep->TextCol(0, 1, _('Total'));
262 $rep->TextCol(1, 4, $stock_description);
263 $rep->TextCol(4, 5, $supplier_name);
264 $rep->AmountCol(5, 7, $total_qty, get_qty_dec($stock_id));
265 $rep->AmountCol(7, 8, $total_supp, $dec);
266 $rep->Line($rep->row - 2);
269 $total_supp = $total_qty = 0.0;
270 $supplier_name = $trans['supplier_name'];
273 if ($supplier_name != '')
276 $rep->TextCol(0, 1, _('Total'));
277 $rep->TextCol(1, 4, $stock_description);
278 $rep->TextCol(4, 5, $supplier_name);
279 $rep->AmountCol(5, 7, $total_qty, get_qty_dec($stock_id));
280 $rep->AmountCol(7, 8, $total_supp, $dec);
281 $rep->Line($rep->row - 2);
287 $rep->TextCol(0, 1, _('Total'));
288 $rep->TextCol(1, 7, $catt);
289 $rep->AmountCol(7, 8, $total, $dec);
290 $rep->Line($rep->row - 2);
293 $rep->TextCol(0, 7, _('Grand Total'));
294 $rep->AmountCol(7, 8, $grandtotal, $dec);
296 $rep->Line($rep->row - 4);