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 * Copyright (C) 2024 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';
41require_once DOL_DOCUMENT_ROOT."/product/stock/class/entrepot.class.php";
50$warehouse = new Entrepot($db);
51
52$action = GETPOST("action", "alpha");
53$barcode = GETPOST("barcode", "aZ09");
54$product = GETPOST("product");
55$response = "";
56
57$fk_entrepot = GETPOSTINT("fk_entrepot");
58$fk_inventory = GETPOSTINT("fk_inventory");
59$fk_product = GETPOSTINT("fk_product");
60$reelqty = GETPOSTINT("reelqty");
61$batch = GETPOST("batch", "aZ09");
62$mode = GETPOST("mode", "aZ");
63
64$warehousefound = 0;
65$warehouseid = 0;
66$objectreturn = array();
67
68/*
69 * Action
70 */
71
72// None
73
74
75/*
76 * View
77 */
78
79top_httphead('application/json');
80
81if ($action == "existbarcode" && !empty($barcode) && $user->hasRight('stock', 'lire')) {
82 if (!empty($mode) && $mode == "lotserial") {
83 $sql = "SELECT ps.fk_entrepot, ps.fk_product, p.barcode, ps.reel, pb.batch";
84 $sql .= " FROM ".MAIN_DB_PREFIX."product_batch as pb";
85 $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";
86 $sql .= " WHERE pb.batch = '".$db->escape($barcode)."'";
87 } else {
88 $sql = "SELECT ps.fk_entrepot, ps.fk_product, p.barcode,ps.reel";
89 $sql .= " FROM ".MAIN_DB_PREFIX."product_stock as ps JOIN ".MAIN_DB_PREFIX."product as p ON ps.fk_product = p.rowid";
90 $sql .= " WHERE p.barcode = '".$db->escape($barcode)."'";
91 }
92 if (!empty($fk_entrepot)) {
93 $sql .= " AND ps.fk_entrepot = '".$db->escape($fk_entrepot)."'";
94 }
95 if (!empty($fk_product)) {
96 $sql .= " AND ps.fk_product = '".$db->escape($fk_product)."'";
97 }
98 $result = $db->query($sql);
99 if ($result) {
100 $nbline = $db->num_rows($result);
101 for ($i = 0; $i < $nbline; $i++) {
102 $object = $db->fetch_object($result);
103 if (($mode == "barcode" && $barcode == $object->barcode) || ($mode == "lotserial" && $barcode == $object->batch)) {
104 $warehouse->fetch(0, $product["Warehouse"]);
105 if (!empty($object->fk_entrepot) && $warehouse->id == $object->fk_entrepot) {
106 $warehousefound++;
107 $warehouseid = $object->fk_entrepot;
108 $fk_product = $object->fk_product;
109 $reelqty = $object->reel;
110
111 $objectreturn = array('fk_warehouse' => $warehouseid,'fk_product' => $fk_product,'reelqty' => $reelqty);
112 }
113 }
114 }
115 if ($warehousefound < 1) {
116 $response = array('status' => 'error','errorcode' => 'NotFound','message' => 'No warehouse found for barcode'.$barcode);
117 } elseif ($warehousefound > 1) {
118 $response = array('status' => 'error','errorcode' => 'TooManyWarehouse','message' => 'Too many warehouse found');
119 } else {
120 $response = array('status' => 'success','message' => 'Warehouse found','object' => $objectreturn);
121 }
122 } else {
123 $response = array('status' => 'error','errorcode' => 'NotFound','message' => "No results found for barcode");
124 }
125} else {
126 $response = array('status' => 'error','errorcode' => 'ActionError','message' => "Error on action");
127}
128
129if ($action == "addnewlineproduct" && $user->hasRight('stock', 'creer')) {
130 require_once DOL_DOCUMENT_ROOT."/product/inventory/class/inventory.class.php";
131 $inventoryline = new InventoryLine($db);
132 if (!empty($fk_inventory)) {
133 $inventoryline->fk_inventory = $fk_inventory;
134
135 $inventoryline->fk_warehouse = $fk_entrepot;
136 $inventoryline->fk_product = $fk_product;
137 $inventoryline->qty_stock = $reelqty;
138 if (!empty($batch)) {
139 $inventoryline->batch = $batch;
140 }
141 $inventoryline->datec = dol_now();
142
143 $result = $inventoryline->create($user);
144 if ($result > 0) {
145 $response = array('status' => 'success','message' => 'Success on creating line','id_line' => $result);
146 } else {
147 $response = array('status' => 'error','errorcode' => 'ErrorCreation','message' => "Error on line creation");
148 }
149 } else {
150 $response = array('status' => 'error','errorcode' => 'NoIdForInventory','message' => "No id for inventory");
151 }
152}
153
154$response = json_encode($response);
155echo $response;
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
Class to manage warehouses.
Class InventoryLine.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_now($mode='auto')
Return date for now.
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.