dolibarr 21.0.0-alpha
searchfrombarcode.php
Go to the documentation of this file.
1<?php
2/*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <https://www.gnu.org/licenses/>.
15 */
16
22if (!defined('NOTOKENRENEWAL')) {
23 define('NOTOKENRENEWAL', 1); // Disables token renewal
24}
25if (!defined('NOREQUIREMENU')) {
26 define('NOREQUIREMENU', '1');
27}
28if (!defined('NOREQUIREHTML')) {
29 define('NOREQUIREHTML', '1');
30}
31if (!defined('NOREQUIREAJAX')) {
32 define('NOREQUIREAJAX', '1');
33}
34if (!defined('NOREQUIRESOC')) {
35 define('NOREQUIRESOC', '1');
36}
37require '../../main.inc.php';
38include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
39
40$action = GETPOST("action", "alpha");
41$barcode = GETPOST("barcode", "aZ09");
42$response = "";
43
44$fk_entrepot = GETPOSTINT("fk_entrepot");
45$fk_inventory = GETPOSTINT("fk_inventory");
46$fk_product = GETPOSTINT("fk_product");
47$reelqty = GETPOSTINT("reelqty");
48$batch = GETPOSTINT("batch");
49$mode = GETPOST("mode", "aZ");
50
51$warehousefound = 0;
52$warehouseid = 0;
53$objectreturn = array();
54$usesublevelpermission = '';
55
56$object= new Product($db);
57
58// Security check
59if (!empty($user->socid)) {
60 $socid = $user->socid;
61}
62
63$result = restrictedArea($user, $object->module, $object, $object->table_element, $usesublevelpermission, 'fk_soc', 'rowid', 0, 1); // Call with mode return
64if (!$result) {
65 httponly_accessforbidden('Not allowed by restrictArea (module='.$object->module.' table_element='.$object->table_element.')');
66}
67
68
69/*
70 * Action
71 */
72
73// None
74
75
76/*
77 * View
78 */
79
80top_httphead('application/json');
81
82if ($action == "existbarcode" && !empty($barcode) && $user->hasRight('stock', 'lire')) {
83 if (!empty($mode) && $mode == "lotserial") {
84 $sql = "SELECT ps.fk_entrepot, ps.fk_product, p.barcode, ps.reel, pb.batch";
85 $sql .= " FROM ".MAIN_DB_PREFIX."product_batch as pb";
86 $sql .= " JOIN ".MAIN_DB_PREFIX."product_stock as ps ON pb.fk_product_stock = ps.rowid JOIN ".MAIN_DB_PREFIX."product as p ON ps.fk_product = p.rowid";
87 $sql .= " WHERE pb.batch = '".$db->escape($barcode)."'";
88 } else {
89 $sql = "SELECT ps.fk_entrepot, ps.fk_product, p.barcode,ps.reel";
90 $sql .= " FROM ".MAIN_DB_PREFIX."product_stock as ps JOIN ".MAIN_DB_PREFIX."product as p ON ps.fk_product = p.rowid";
91 $sql .= " WHERE p.barcode = '".$db->escape($barcode)."'";
92 }
93 if (!empty($fk_entrepot)) {
94 $sql .= " AND ps.fk_entrepot = '".$db->escape($fk_entrepot)."'";
95 }
96 $result = $db->query($sql);
97 if ($result) {
98 $nbline = $db->num_rows($result);
99 for ($i=0; $i < $nbline; $i++) {
100 $obj = $db->fetch_object($result);
101 if (($mode == "barcode" && $barcode == $obj->barcode) || ($mode == "lotserial" && $barcode == $obj->batch)) {
102 if (!empty($obj->fk_entrepot) && $fk_entrepot == $obj->fk_entrepot) {
103 $warehousefound++;
104 $warehouseid = $obj->fk_entrepot;
105 $fk_product = $obj->fk_product;
106 $reelqty = $obj->reel;
107
108 $objectreturn = array('fk_warehouse'=>$warehouseid,'fk_product'=>$fk_product,'reelqty'=>$reelqty);
109 }
110 }
111 }
112 if ($warehousefound < 1) {
113 $response = array('status'=>'error','errorcode'=>'NotFound','message'=>'No warehouse found for barcode'.$barcode);
114 } elseif ($warehousefound > 1) {
115 $response = array('status'=>'error','errorcode'=>'TooManyWarehouse','message'=>'Too many warehouse found');
116 } else {
117 $response = array('status'=>'success','message'=>'Warehouse found','object'=>$objectreturn);
118 }
119 } else {
120 $response = array('status'=>'error','errorcode'=>'NotFound','message'=>"No results found for barcode");
121 }
122} else {
123 $response = array('status'=>'error','errorcode'=>'ActionError','message'=>"Error on action");
124}
125
126$response = json_encode($response);
127echo $response;
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class to manage products or services.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
httponly_accessforbidden($message='1', $http_response_code=403, $stringalreadysanitized=0)
Show a message to say access is forbidden and stop program.
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.