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 ".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,
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')";
62 $sql .= " AND ".TB_PREF."stock_master.category_id = ".db_escape($category);
64 $sql .= " AND ".TB_PREF."stock_moves.loc_code = ".db_escape($location);
66 $sql .= " AND ".TB_PREF."suppliers.supplier_id = ".db_escape($fromsupp);
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");
75 //----------------------------------------------------------------------------------------------------
77 function print_inventory_purchase()
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'];
90 include_once($path_to_root . "/reporting/includes/excel_report.inc");
92 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
94 $dec = user_price_dec();
96 if ($category == ALL_NUMERIC)
101 $cat = get_category_name($category);
106 $loc = get_location_name($location);
111 $froms = get_supplier_name($fromsupp);
118 $cols = array(0, 75, 175, 220, 270, 370, 390, 470, 535);
120 $headers = array(_('Category'), _('Description'), _('Date'), _('#'), _('Supplier'), _('Qty'), _('Unit Price'), _('Total'));
124 $aligns = array('left', 'left', 'left', 'left', 'left', 'left', 'right', 'right');
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' => ''));
133 $rep = new FrontReport(_('Inventory Purchasing Report'), "InventoryPurchasingReport", user_pagesize());
136 $rep->Info($params, $cols, $headers, $aligns);
139 $res = getTransactions($category, $location, $fromsupp, $item, $from, $to);
140 $total = $total_supp = $grandtotal = 0.0;
142 $catt = $stock_description = $supplier_name = '';
143 while ($trans=db_fetch($res))
145 if ($stock_description != $trans['description'])
147 if ($stock_description != '')
149 if ($supplier_name != '')
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);
159 $total_supp = $total_qty = 0.0;
160 $supplier_name = $trans['supplier_name'];
163 $stock_description = $trans['description'];
166 if ($supplier_name != $trans['supplier_name'])
168 if ($supplier_name != '')
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);
178 $total_supp = $total_qty = 0.0;
180 $supplier_name = $trans['supplier_name'];
182 if ($catt != $trans['cat_description'])
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);
195 $rep->TextCol(0, 1, $trans['category_id']);
196 $rep->TextCol(1, 6, $trans['cat_description']);
197 $catt = $trans['cat_description'];
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;
206 $rep->TextCol(0, 1, $trans['stock_id']);
207 if ($fromsupp == ALL_TEXT)
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']);
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']);
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);
228 $total_qty += $trans['qty'];
230 if ($stock_description != '')
232 if ($supplier_name != '')
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);
243 $total_supp = $total_qty = 0.0;
244 $supplier_name = $trans['supplier_name'];
247 if ($supplier_name != '')
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);
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);
267 $rep->TextCol(0, 7, _('Grand Total'));
268 $rep->AmountCol(7, 8, $grandtotal, $dec);
270 $rep->Line($rep->row - 4);