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_ITEMSVALREP';
13 // ----------------------------------------------------------------
17 // Title: Stock Check Sheet
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 . "/gl/includes/gl_db.inc");
25 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
26 include_once($path_to_root . "/includes/db/manufacturing_db.inc");
28 //----------------------------------------------------------------------------------------------------
33 * Bar codes checker - Checks if a barcode can be valid and returns type of barcode
35 * @link http://www.phpclasses.org/package/8560-PHP-Detect-type-and-check-EAN-and-UPC-barcodes.html
36 * @type tests EAN, EAN-8, EAN-13, GTIN-8, GTIN-12, GTIN-14, UPC, UPC-12 coupon code, JAN
37 * @author Ferry Bouwhuis
39 * @LastChange 2014-04-13
42 function barcode_check($code, $return_value = false, $get_type = false)
44 //Setting return value
46 * If true it will retun al barcodes as 14 digit strings
47 * If false it will return only what is needed UPC -> 12 / EAN -> 13 /
50 //Filter UPC coupon codes
52 * If true it will return false on UPC coupon codes
53 * Type will always return UPC coupon code
55 $skip_coupon_codes = true;
57 //Trims parsed string to remove unwanted whitespace or characters
58 $code = (string)trim($code);
59 if (preg_match('/[^0-9]/', $code))
62 if (!is_string($code))
63 $code = strval($code);
65 $length = strlen($code);
66 if(($length > 11 && $length <= 14) || $length == 8)
68 $zeroes = 18 - $length;
70 for ($i = 0; $i < $zeroes; $i++)
72 $code = $fill . $code;
75 for ($i = 0; $i < (strlen($code) - 1); $i++)
76 $calc += ($i % 2 ? $code[$i] * 1 : $code[$i] * 3);
78 if (substr(10 - (substr($calc, -1)), -1) != substr($code, -1))
80 elseif (substr($code, 5, 1) > 2)
82 //EAN / JAN / EAN-13 code
86 return (string)substr($code, ($return_value ? -14 : -13));
88 elseif (substr($code, 6, 1) == 0 && substr($code, 0, 10) == 0)
94 return (string)substr($code, ($return_value ? -14 : -8));
96 elseif (substr($code, 5, 1) <= 0)
98 //UPC / UCC-12 GTIN-12 code
101 if (substr($code, 6, 1) == 5)
102 return 'UPC coupon code';
108 if ($skip_coupon_codes && substr($code, 6, 1) == 5)
110 return (string)substr($code, ($return_value ? -14 : -12));
113 elseif (substr($code, 0, 6) == 0)
119 return (string)substr($code, -14);
127 return (string)substr($code,($return_value ? -14 : -13));
134 function getTransactions($category, $location, $item_like)
136 $sql = "SELECT item.category_id,
137 category.description AS cat_description,
138 item.stock_id, item.units,
139 item.description, item.inactive,
140 IF(move.stock_id IS NULL, '', move.loc_code) AS loc_code,
141 SUM(IF(move.stock_id IS NULL,0,move.qty)) AS QtyOnHand
143 .TB_PREF."stock_master item,"
144 .TB_PREF."stock_category category)
145 LEFT JOIN ".TB_PREF."stock_moves move ON item.stock_id=move.stock_id
146 WHERE item.category_id=category.category_id
147 AND (item.mb_flag='B' OR item.mb_flag='M')";
149 $sql .= " AND item.category_id = ".db_escape($category);
150 if ($location != 'all')
151 $sql .= " AND IF(move.stock_id IS NULL, '1=1',move.loc_code = ".db_escape($location).")";
156 if(sscanf($item_like, "/%s", $regexp)==1)
157 $sql .= " AND item.stock_id RLIKE ".db_escape($regexp);
159 $sql .= " AND item.stock_id LIKE ".db_escape($item_like);
161 $sql .= " GROUP BY item.category_id,
162 category.description,
165 ORDER BY item.category_id,
168 return db_query($sql,"No transactions were returned");
171 //----------------------------------------------------------------------------------------------------
173 function print_stock_check()
175 global $path_to_root, $SysPrefs;
177 $category = $_POST['PARAM_0'];
178 $location = $_POST['PARAM_1'];
179 $pictures = $_POST['PARAM_2'];
180 $check = $_POST['PARAM_3'];
181 $shortage = $_POST['PARAM_4'];
182 $no_zeros = $_POST['PARAM_5'];
183 $like = $_POST['PARAM_6'];
184 $comments = $_POST['PARAM_7'];
185 $orientation = $_POST['PARAM_8'];
186 $destination = $_POST['PARAM_9'];
189 include_once($path_to_root . "/reporting/includes/excel_report.inc");
191 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
193 $orientation = ($orientation ? 'L' : 'P');
194 if ($category == ALL_NUMERIC)
199 $cat = get_category_name($category);
201 if ($location == ALL_TEXT)
203 if ($location == 'all')
206 $loc = get_location_name($location);
210 $available = _('Shortage');
215 $available = _('Available');
217 $barcodes = !empty($SysPrefs->prefs['barcodes_on_stock']);
218 if ($no_zeros) $nozeros = _('Yes');
219 else $nozeros = _('No');
222 $cols = array(0, 75, 225, 250, 295, 345, 390, 445, 515);
223 $headers = array(_('Stock ID'), _('Description'), _('UOM'), _('Quantity'), _('Check'), _('Demand'), $available, _('On Order'));
224 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right', 'right');
228 $cols = array(0, 75, 225, 250, 315, 380, 445, 515);
229 $headers = array(_('Stock ID'), _('Description'), _('UOM'), _('Quantity'), _('Demand'), $available, _('On Order'));
230 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right');
235 1 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
236 2 => array('text' => _('Location'), 'from' => $loc, 'to' => ''),
237 3 => array('text' => _('Only Shortage'), 'from' => $short, 'to' => ''),
238 4 => array('text' => _('Suppress Zeros'), 'from' => $nozeros, 'to' => '')
243 // define barcode style
245 'position' => 'L', // If blank string, barcode starts on left edge of page
248 'cellfitalign' => '',
251 'fgcolor' => array(0,0,0),
252 'bgcolor' => false, //array(255,255,255),
254 'font' => 'helvetica',
258 // write1DBarcode($code, $type, $x='', $y='', $w='', $h='', $xres=0.4, $style='', $align='')
261 $rep = new FrontReport(_('Stock Check Sheets'), "StockCheckSheet", user_pagesize(), 9, $orientation);
262 if ($orientation == 'L')
263 recalculate_cols($cols);
266 $rep->Info($params, $cols, $headers, $aligns);
269 $res = getTransactions($category, $location, $like);
271 while ($trans=db_fetch($res))
273 if ($location == 'all')
276 $loc_code = $location;
277 $demandqty = get_demand_qty($trans['stock_id'], $loc_code);
278 $demandqty += get_demand_asm_qty($trans['stock_id'], $loc_code);
279 $onorder = get_on_porder_qty($trans['stock_id'], $loc_code);
280 $onorder += get_on_worder_qty($trans['stock_id'], $loc_code);
281 if ($no_zeros && $trans['QtyOnHand'] == 0 && $demandqty == 0 && $onorder == 0)
283 if ($shortage && $trans['QtyOnHand'] - $demandqty >= 0)
285 if ($catt != $trans['cat_description'])
289 $rep->Line($rep->row - 2);
292 $rep->TextCol(0, 1, $trans['category_id']);
293 $rep->TextCol(1, 2, $trans['cat_description']);
294 $catt = $trans['cat_description'];
298 $dec = get_qty_dec($trans['stock_id']);
299 $rep->TextCol(0, 1, $trans['stock_id']);
300 $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
301 $rep->TextCol(2, 3, $trans['units']);
302 $rep->AmountCol(3, 4, $trans['QtyOnHand'], $dec);
305 $rep->TextCol(4, 5, "_________");
306 $rep->AmountCol(5, 6, $demandqty, $dec);
307 $rep->AmountCol(6, 7, $trans['QtyOnHand'] - $demandqty, $dec);
308 $rep->AmountCol(7, 8, $onorder, $dec);
312 $rep->AmountCol(4, 5, $demandqty, $dec);
313 $rep->AmountCol(5, 6, $trans['QtyOnHand'] - $demandqty, $dec);
314 $rep->AmountCol(6, 7, $onorder, $dec);
316 if ($pictures || $barcodes)
319 if ($rep->row - $SysPrefs->pic_height < $rep->bottomMargin)
323 if ($barcodes && barcode_check($trans['stock_id']))
326 $bar_y = $rep->GetY();
327 $barcode = str_pad($trans['stock_id'], 7, '0', STR_PAD_LEFT);
328 $barcode = substr($barcode, 0, 8); // EAN 8 Check digit is auto computed and barcode printed
329 $rep->write1DBarcode($barcode, 'EAN8', $rep->cols[$firstcol++], $bar_y + 22, 22, $SysPrefs->pic_height, 1.2, $style, 'N');
334 $image = company_path() . '/images/' . item_img_name($trans['stock_id']) . '.jpg';
335 if (file_exists($image))
337 $rep->AddImage($image, $rep->cols[$firstcol], $rep->row - $SysPrefs->pic_height, 0, $SysPrefs->pic_height);
341 $rep->row -= $SysPrefs->pic_height;
344 $rep->Line($rep->row - 4);