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