dolibarr 21.0.0-beta
searchfrombarcode.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
3 */
4/*
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
24if (!defined('NOTOKENRENEWAL')) {
25 define('NOTOKENRENEWAL', 1); // Disables token renewal
26}
27if (!defined('NOREQUIREMENU')) {
28 define('NOREQUIREMENU', '1');
29}
30if (!defined('NOREQUIREHTML')) {
31 define('NOREQUIREHTML', '1');
32}
33if (!defined('NOREQUIREAJAX')) {
34 define('NOREQUIREAJAX', '1');
35}
36if (!defined('NOREQUIRESOC')) {
37 define('NOREQUIRESOC', '1');
38}
39require '../../main.inc.php';
40include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
49$action = GETPOST("action", "alpha");
50$barcode = GETPOST("barcode", "aZ09");
51$response = "";
52
53$fk_entrepot = GETPOSTINT("fk_entrepot");
54$fk_inventory = GETPOSTINT("fk_inventory");
55$fk_product = GETPOSTINT("fk_product");
56$reelqty = GETPOSTINT("reelqty");
57$batch = GETPOST("batch", "aZ09");
58$mode = GETPOST("mode", "aZ");
59
60$warehousefound = 0;
61$warehouseid = 0;
62$objectreturn = array();
63$usesublevelpermission = '';
64
65$object= new Product($db);
66
67// Security check
68if (!empty($user->socid)) {
69 $socid = $user->socid;
70}
71
72$result = restrictedArea($user, $object->module, $object, $object->table_element, $usesublevelpermission, 'fk_soc', 'rowid', 0, 1); // Call with mode return
73if (!$result) {
74 httponly_accessforbidden('Not allowed by restrictArea (module='.$object->module.' table_element='.$object->table_element.')');
75}
76
77
78/*
79 * Action
80 */
81
82// None
83
84
85/*
86 * View
87 */
88
89top_httphead('application/json');
90
91if ($action == "existbarcode" && !empty($barcode) && $user->hasRight('stock', 'lire')) {
92 if (!empty($mode) && $mode == "lotserial") {
93 $sql = "SELECT ps.fk_entrepot, ps.fk_product, p.barcode, ps.reel, pb.batch";
94 $sql .= " FROM ".MAIN_DB_PREFIX."product_batch as pb";
95 $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";
96 $sql .= " WHERE pb.batch = '".$db->escape($barcode)."'";
97 } else {
98 $sql = "SELECT ps.fk_entrepot, ps.fk_product, p.barcode,ps.reel";
99 $sql .= " FROM ".MAIN_DB_PREFIX."product_stock as ps JOIN ".MAIN_DB_PREFIX."product as p ON ps.fk_product = p.rowid";
100 $sql .= " WHERE p.barcode = '".$db->escape($barcode)."'";
101 }
102 if (!empty($fk_entrepot)) {
103 $sql .= " AND ps.fk_entrepot = '".$db->escape($fk_entrepot)."'";
104 }
105 $result = $db->query($sql);
106 if ($result) {
107 $nbline = $db->num_rows($result);
108 for ($i=0; $i < $nbline; $i++) {
109 $obj = $db->fetch_object($result);
110 if (($mode == "barcode" && $barcode == $obj->barcode) || ($mode == "lotserial" && $barcode == $obj->batch)) {
111 if (!empty($obj->fk_entrepot) && $fk_entrepot == $obj->fk_entrepot) {
112 $warehousefound++;
113 $warehouseid = $obj->fk_entrepot;
114 $fk_product = $obj->fk_product;
115 $reelqty = $obj->reel;
116
117 $objectreturn = array('fk_warehouse'=>$warehouseid,'fk_product'=>$fk_product,'reelqty'=>$reelqty);
118 }
119 }
120 }
121 if ($warehousefound < 1) {
122 $response = array('status'=>'error','errorcode'=>'NotFound','message'=>'No warehouse found for barcode'.$barcode);
123 } elseif ($warehousefound > 1) {
124 $response = array('status'=>'error','errorcode'=>'TooManyWarehouse','message'=>'Too many warehouse found');
125 } else {
126 $response = array('status'=>'success','message'=>'Warehouse found','object'=>$objectreturn);
127 }
128 } else {
129 $response = array('status'=>'error','errorcode'=>'NotFound','message'=>"No results found for barcode");
130 }
131} else {
132 $response = array('status'=>'error','errorcode'=>'ActionError','message'=>"Error on action");
133}
134
135$response = json_encode($response);
136echo $response;
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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.