diff --git a/htdocs/product/inventory/ajax/searchfrombarcode.php b/htdocs/product/inventory/ajax/searchfrombarcode.php index 884aff9ec12..1a613c7a837 100644 --- a/htdocs/product/inventory/ajax/searchfrombarcode.php +++ b/htdocs/product/inventory/ajax/searchfrombarcode.php @@ -50,8 +50,6 @@ if (!defined('NOBROWSERNOTIF')) { define('NOBROWSERNOTIF', '1'); } require '../../../main.inc.php'; -//include_once DOL_DOCUMENT_ROOT.'/product/inventory/class/inventory.class.php'; -$object = new Inventory($db); $action = GETPOST("action", "alpha"); @@ -87,12 +85,11 @@ if ($action == "existbarcode" && !empty($barcode)) { } else { $response = array('status'=>'success','message'=>'Warehouse found','warehouse'=>$warehouseid); } - $response = json_encode($response); } else { - $response = "No results found for barcode"; + $response = array('status'=>'error','errorcode'=>'NotFound','message'=>"No results found for barcode"); } } else { - $response = "Error on action"; + $response = array('status'=>'error','errorcode'=>'ActionError','message'=>"Error on action"); } - +$response = json_encode($response); echo $response; diff --git a/htdocs/product/inventory/class/inventory.class.php b/htdocs/product/inventory/class/inventory.class.php index b972e44245b..092d98ea854 100644 --- a/htdocs/product/inventory/class/inventory.class.php +++ b/htdocs/product/inventory/class/inventory.class.php @@ -26,6 +26,7 @@ // Put here all includes required by your class file require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php'; //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php'; //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php'; diff --git a/htdocs/product/inventory/inventory.php b/htdocs/product/inventory/inventory.php index 4dcda1bba41..910084c12fe 100644 --- a/htdocs/product/inventory/inventory.php +++ b/htdocs/product/inventory/inventory.php @@ -571,19 +571,19 @@ if ($object->id > 0) { if(productinput == ""){ productinput = 0 } - tabproduct.push({\'Id\':id,\'Warehouse\':warehouse,\'Barcode\':productbarcode,\'Batch\':productbatchcode,\'Qty\':productinput}); + tabproduct.push({\'Id\':id,\'Warehouse\':warehouse,\'Barcode\':productbarcode,\'Batch\':productbatchcode,\'Qty\':productinput,\'fetched\':false}); }) - tabproduct.forEach(product => { + textarray.forEach(function(element,index){ switch(barcodemode){ case "barcodeforautodetect": - barcodeserialforproduct(textarray,product,barcodeproductqty,selectaddorreplace,"barcode",true); - barcodeserialforproduct(textarray,product,barcodeproductqty,selectaddorreplace,"lotserial",true); + barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,"barcode",true); + barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,"lotserial",true); break; case "barcodeforproduct": //TODO: create product !exist + ajout - barcodeserialforproduct(textarray,product,barcodeproductqty,selectaddorreplace,"barcode"); + barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,"barcode"); break; case "barcodeforlotserial": - barcodeserialforproduct(textarray,product,barcodeproductqty,selectaddorreplace,"lotserial"); + barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,"lotserial"); break; default: alert("'.$langs->trans("ErrorWrongBarcodemode").' \""+barcodemode+"\""); @@ -601,8 +601,10 @@ if ($object->id > 0) { } - function barcodeserialforproduct(textarray,product,barcodeproductqty,selectaddorreplace,mode,autodetect=false){ - textarray.forEach(function(element,index){ + function barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,mode,autodetect=false){ + BarcodeIsInProduct=0; + BarcodeSuccess=false; + tabproduct.forEach(product => { $.ajax({ url: \''.DOL_URL_ROOT.'/product/inventory/ajax/searchfrombarcode.php\', data: { "action":"existbarcode",'.(!empty($object->fk_warehouse)?'"fk_entrepot":'.$object->fk_warehouse.',':'').'"barcode":element,"product":product}, type: \'POST\', @@ -611,6 +613,7 @@ if ($object->id > 0) { response = JSON.parse(response); if(response.status == "success"){ console.log(response.message); + BarcodeSucess=true; }else{ console.error(response.message); } @@ -618,9 +621,8 @@ if ($object->id > 0) { error : function(output) { console.error("Error on barcodeserialforproduct function"); }, - }); + }); console.log("Product "+(index+=1)+": "+element); - BarCodeDoesNotExist=0; if(mode == "barcode"){ testonproduct = product.Barcode }else if (mode == "lotserial"){ @@ -628,15 +630,23 @@ if ($object->id > 0) { } if(testonproduct == element){ if(selectaddorreplace == "add"){ - productqty = parseInt(product.Qty,10) - product.Qty = productqty + barcodeproductqty + productqty = parseInt(product.Qty,10); + product.Qty = productqty + parseInt(barcodeproductqty,10); }else if(selectaddorreplace == "replace"){ - product.Qty = barcodeproductqty + if(product.fetched == false){ + product.Qty = barcodeproductqty + product.fetched=true + }else{ + productqty = parseInt(product.Qty,10); + product.Qty = productqty + parseInt(barcodeproductqty,10); + } } - }else{ - BarCodeDoesNotExist+=1; + BarcodeIsInProduct+=1; } }) + if(BarcodeIsInProduct==0 && BarcodeSuccess){ + //addproduct to db and tabproduct + } } '; print '';