dolibarr 18.0.6
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';
38require_once DOL_DOCUMENT_ROOT."/product/stock/class/entrepot.class.php";
39$warehouse = new Entrepot($db);
40
41$action = GETPOST("action", "alpha");
42$barcode = GETPOST("barcode", "aZ09");
43$product = GETPOST("product");
44$response = "";
45
46$fk_entrepot = GETPOST("fk_entrepot", "int");
47$fk_inventory = GETPOST("fk_inventory", "int");
48$fk_product = GETPOST("fk_product", "int");
49$reelqty = GETPOST("reelqty", "int");
50$batch = GETPOST("batch", "int");
51$mode = GETPOST("mode", "aZ");
52
53$warehousefound = 0;
54$warehouseid = 0;
55$objectreturn = array();
56
57
58/*
59 * View
60 */
61
62top_httphead('application/json');
63
64if ($action == "existbarcode" && !empty($barcode)) {
65 if (!empty($mode) && $mode == "lotserial") {
66 $sql = "SELECT ps.fk_entrepot, ps.fk_product, p.barcode, ps.reel, pb.batch";
67 $sql .= " FROM ".MAIN_DB_PREFIX."product_batch as pb";
68 $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";
69 $sql .= " WHERE pb.batch = '".$db->escape($barcode)."'";
70 } else {
71 $sql = "SELECT ps.fk_entrepot, ps.fk_product, p.barcode,ps.reel";
72 $sql .= " FROM ".MAIN_DB_PREFIX."product_stock as ps JOIN ".MAIN_DB_PREFIX."product as p ON ps.fk_product = p.rowid";
73 $sql .= " WHERE p.barcode = '".$db->escape($barcode)."'";
74 }
75 if (!empty($fk_entrepot)) {
76 $sql .= " AND ps.fk_entrepot = '".$db->escape($fk_entrepot)."'";
77 }
78 if (!empty($fk_product)) {
79 $sql .= " AND ps.fk_product = '".$db->escape($fk_product)."'";
80 }
81 $result = $db->query($sql);
82 if ($result) {
83 $nbline = $db->num_rows($result);
84 for ($i=0; $i < $nbline; $i++) {
85 $object = $db->fetch_object($result);
86 if (($mode == "barcode" && $barcode == $object->barcode) || ($mode == "lotserial" && $barcode == $object->batch)) {
87 $warehouse->fetch(0, $product["Warehouse"]);
88 if (!empty($object->fk_entrepot) && $warehouse->id == $object->fk_entrepot) {
89 $warehousefound++;
90 $warehouseid = $object->fk_entrepot;
91 $fk_product = $object->fk_product;
92 $reelqty = $object->reel;
93
94 $objectreturn = array('fk_warehouse'=>$warehouseid,'fk_product'=>$fk_product,'reelqty'=>$reelqty);
95 }
96 }
97 }
98 if ($warehousefound < 1) {
99 $response = array('status'=>'error','errorcode'=>'NotFound','message'=>'No warehouse found for barcode'.$barcode);
100 } elseif ($warehousefound > 1) {
101 $response = array('status'=>'error','errorcode'=>'TooManyWarehouse','message'=>'Too many warehouse found');
102 } else {
103 $response = array('status'=>'success','message'=>'Warehouse found','object'=>$objectreturn);
104 }
105 } else {
106 $response = array('status'=>'error','errorcode'=>'NotFound','message'=>"No results found for barcode");
107 }
108} else {
109 $response = array('status'=>'error','errorcode'=>'ActionError','message'=>"Error on action");
110}
111
112if ($action == "addnewlineproduct") {
113 require_once DOL_DOCUMENT_ROOT."/product/inventory/class/inventory.class.php";
114 $inventoryline = new InventoryLine($db);
115 if (!empty($fk_inventory)) {
116 $inventoryline->fk_inventory = $fk_inventory;
117
118 $inventoryline->fk_warehouse = $fk_entrepot;
119 $inventoryline->fk_product = $fk_product;
120 $inventoryline->qty_stock = $reelqty;
121 if (!empty($batch)) {
122 $inventoryline->batch = $batch;
123 }
124 $inventoryline->datec = dol_now();
125
126 $result = $inventoryline->create($user);
127 if ($result > 0) {
128 $response = array('status'=>'success','message'=>'Success on creating line','id_line'=>$result);
129 } else {
130 $response = array('status'=>'error','errorcode'=>'ErrorCreation','message'=>"Error on line creation");
131 }
132 } else {
133 $response = array('status'=>'error','errorcode'=>'NoIdForInventory','message'=>"No id for inventory");
134 }
135}
136
137$response = json_encode($response);
138echo $response;
Class to manage warehouses.
Class InventoryLine.
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.