dolibarr  16.0.5
searchfrombarcode.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  */
17 
23 if (!defined('NOTOKENRENEWAL')) {
24  define('NOTOKENRENEWAL', 1); // Disables token renewal
25 }
26 if (!defined('NOREQUIREMENU')) {
27  define('NOREQUIREMENU', '1');
28 }
29 if (!defined('NOREQUIREHTML')) {
30  define('NOREQUIREHTML', '1');
31 }
32 if (!defined('NOREQUIREAJAX')) {
33  define('NOREQUIREAJAX', '1');
34 }
35 if (!defined('NOREQUIRESOC')) {
36  define('NOREQUIRESOC', '1');
37 }
38 if (!defined('NOCSRFCHECK')) {
39  define('NOCSRFCHECK', '1');
40 }
41 require '../../../main.inc.php';
42 require_once DOL_DOCUMENT_ROOT."/product/stock/class/entrepot.class.php";
43 $warehouse = new Entrepot($db);
44 
45 $action = GETPOST("action", "alpha");
46 $barcode = GETPOST("barcode", "aZ09");
47 $product = GETPOST("product");
48 $response = "";
49 
50 $fk_entrepot = GETPOST("fk_entrepot", "int");
51 $fk_inventory = GETPOST("fk_inventory", "int");
52 $fk_product = GETPOST("fk_product", "int");
53 $reelqty = GETPOST("reelqty", "int");
54 $batch = GETPOST("batch", "int");
55 $mode = GETPOST("mode", "aZ");
56 
57 $warehousefound = 0;
58 $warehouseid = 0;
59 $objectreturn = array();
60 
61 if ($action == "existbarcode" && !empty($barcode)) {
62  if (!empty($mode) && $mode == "lotserial") {
63  $sql = "SELECT ps.fk_entrepot, ps.fk_product, p.barcode, ps.reel, pb.batch";
64  $sql .= " FROM ".MAIN_DB_PREFIX."product_batch as pb";
65  $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";
66  $sql .= " WHERE pb.batch = '".$db->escape($barcode)."'";
67  } else {
68  $sql = "SELECT ps.fk_entrepot, ps.fk_product, p.barcode,ps.reel";
69  $sql .= " FROM ".MAIN_DB_PREFIX."product_stock as ps JOIN ".MAIN_DB_PREFIX."product as p ON ps.fk_product = p.rowid";
70  $sql .= " WHERE p.barcode = '".$db->escape($barcode)."'";
71  }
72  if (!empty($fk_entrepot)) {
73  $sql .= " AND ps.fk_entrepot = '".$db->escape($fk_entrepot)."'";
74  }
75  if (!empty($fk_product)) {
76  $sql .= " AND ps.fk_product = '".$db->escape($fk_product)."'";
77  }
78  $result = $db->query($sql);
79  if ($result) {
80  $nbline = $db->num_rows($result);
81  for ($i=0; $i < $nbline; $i++) {
82  $object = $db->fetch_object($result);
83  if (($mode == "barcode" && $barcode == $object->barcode) || ($mode == "lotserial" && $barcode == $object->batch)) {
84  $warehouse->fetch(0, $product["Warehouse"]);
85  if (!empty($object->fk_entrepot) && $warehouse->id == $object->fk_entrepot) {
86  $warehousefound++;
87  $warehouseid = $object->fk_entrepot;
88  $fk_product = $object->fk_product;
89  $reelqty = $object->reel;
90 
91  $objectreturn = array('fk_warehouse'=>$warehouseid,'fk_product'=>$fk_product,'reelqty'=>$reelqty);
92  }
93  }
94  }
95  if ($warehousefound < 1) {
96  $response = array('status'=>'error','errorcode'=>'NotFound','message'=>'No warehouse found for barcode'.$barcode);
97  } elseif ($warehousefound > 1) {
98  $response = array('status'=>'error','errorcode'=>'TooManyWarehouse','message'=>'Too many warehouse found');
99  } else {
100  $response = array('status'=>'success','message'=>'Warehouse found','object'=>$objectreturn);
101  }
102  } else {
103  $response = array('status'=>'error','errorcode'=>'NotFound','message'=>"No results found for barcode");
104  }
105 } else {
106  $response = array('status'=>'error','errorcode'=>'ActionError','message'=>"Error on action");
107 }
108 
109 if ($action == "addnewlineproduct") {
110  require_once DOL_DOCUMENT_ROOT."/product/inventory/class/inventory.class.php";
111  $inventoryline = new InventoryLine($db);
112  if (!empty($fk_inventory)) {
113  $inventoryline->fk_inventory = $fk_inventory;
114 
115  $inventoryline->fk_warehouse = $fk_entrepot;
116  $inventoryline->fk_product = $fk_product;
117  $inventoryline->qty_stock = $reelqty;
118  if (!empty($batch)) {
119  $inventoryline->batch = $batch;
120  }
121  $inventoryline->datec = dol_now();
122 
123  $result = $inventoryline->create($user);
124  if ($result > 0) {
125  $response = array('status'=>'success','message'=>'Success on creating line','id_line'=>$result);
126  } else {
127  $response = array('status'=>'error','errorcode'=>'ErrorCreation','message'=>"Error on line creation");
128  }
129  } else {
130  $response = array('status'=>'error','errorcode'=>'NoIdForInventory','message'=>"No id for inventory");
131  }
132 }
133 
134 $response = json_encode($response);
135 echo $response;
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
InventoryLine
Class InventoryLine.
Definition: inventory.class.php:704
Entrepot
Class to manage warehouses.
Definition: entrepot.class.php:35
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845