From f37d9787cdf45039a5f3f5f23008f91293cc2459 Mon Sep 17 00:00:00 2001 From: lmarcouiller Date: Mon, 6 Dec 2021 16:47:56 +0100 Subject: [PATCH] error management and addproduct --- .../inventory/ajax/searchfrombarcode.php | 9 ++--- htdocs/product/inventory/inventory.php | 33 ++++++++++++++----- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/htdocs/product/inventory/ajax/searchfrombarcode.php b/htdocs/product/inventory/ajax/searchfrombarcode.php index 8dfac96c32b..68ffee43c23 100644 --- a/htdocs/product/inventory/ajax/searchfrombarcode.php +++ b/htdocs/product/inventory/ajax/searchfrombarcode.php @@ -51,7 +51,6 @@ $fk_entrepot = GETPOST("fk_entrepot", "int"); $fk_inventory = GETPOST("fk_inventory", "int"); $fk_product = GETPOST("fk_product", "int"); $reelqty = GETPOST("reelqty", "int"); -$qtyview = GETPOST("qty", "int"); $batch = GETPOST("batch", "int"); $mode = GETPOST("mode", "aZ"); @@ -73,6 +72,9 @@ if ($action == "existbarcode" && !empty($barcode)) { if (!empty($fk_entrepot)) { $sql .= " AND ps.fk_entrepot = '".$db->escape($fk_entrepot)."'"; } + if (!empty($fk_product)) { + $sql .= " AND ps.fk_product = '".$db->escape($fk_product)."'"; + } $result = $db->query($sql); if ($result) { $nbline = $db->num_rows($result); @@ -113,15 +115,14 @@ if ($action == "addnewlineproduct") { $inventoryline->fk_warehouse = $fk_entrepot; $inventoryline->fk_product = $fk_product; $inventoryline->qty_stock = $reelqty; - $inventoryline->qty_view = $qtyview; - if ($mode == "lotserial") { + if (!empty($batch)) { $inventoryline->batch = $batch; } $inventoryline->datec = dol_now(); $result = $inventoryline->create($user); if ($result > 0) { - $response = array('status'=>'success','message'=>'Success on creating line'); + $response = array('status'=>'success','message'=>'Success on creating line','id_line'=>$result); } else { $response = array('status'=>'error','errorcode'=>'ErrorCreation','message'=>"Error on line creation"); } diff --git a/htdocs/product/inventory/inventory.php b/htdocs/product/inventory/inventory.php index 51c2e9d2bc5..da6e347eb5c 100644 --- a/htdocs/product/inventory/inventory.php +++ b/htdocs/product/inventory/inventory.php @@ -452,7 +452,7 @@ if ($object->id > 0) { print dol_get_fiche_end(); - print '
'; + print ''; print ''; print ''; print ''; @@ -577,10 +577,12 @@ if ($object->id > 0) { tabproduct.push({\'Id\':id,\'Warehouse\':warehouse,\'Barcode\':productbarcode,\'Batch\':productbatchcode,\'Qty\':productinput,\'fetched\':false}); }) textarray.forEach(function(element,index){ + var verify_batch = false; + var verify_barcode = false; switch(barcodemode){ case "barcodeforautodetect": - barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,"barcode",true); - barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,"lotserial",true); + verify_barcode = barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,"barcode",true); + verify_batch = barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,"lotserial",true); break; case "barcodeforproduct": barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,"barcode"); @@ -592,6 +594,9 @@ if ($object->id > 0) { alert("'.$langs->trans("ErrorWrongBarcodemode").' \""+barcodemode+"\""); throw "'.$langs->trans('ErrorWrongBarcodemode').' \""+barcodemode+"\""; } + if(verify_batch == true && verify_barcode == true){ + errortab.push(element); + } }); if (Object.keys(errortab).length < 1){ tabproduct.forEach(product => { @@ -599,19 +604,23 @@ if ($object->id > 0) { console.log("We change #"+product.Id+"_input to match input in scanner box"); if(product.hasOwnProperty("reelqty")){ $.ajax({ url: \''.DOL_URL_ROOT.'/product/inventory/ajax/searchfrombarcode.php\', - data: { "action":"addnewlineproduct","fk_entrepot":product.Warehouse,"batch":product.Batch,"fk_inventory":'.dol_escape_js($object->id).',"fk_product":product.fk_product,"reelqty":product.reelqty,"qty":product.Qty}, + data: { "action":"addnewlineproduct","fk_entrepot":product.Warehouse,"batch":product.Batch,"fk_inventory":'.dol_escape_js($object->id).',"fk_product":product.fk_product,"reelqty":product.reelqty}, type: \'POST\', async: false, success: function(response) { response = JSON.parse(response); if(response.status == "success"){ console.log(response.message); + $("") + .attr("id", "id_"+response.id_line+"_input") + .attr("name", "id_"+response.id_line) + .appendTo("#formrecord"); }else{ console.error(response.message); } }, error : function(output) { - console.error("Error on barcodeserialforproduct function"); + console.error("Error on line creation function"); }, }); } else { @@ -635,9 +644,10 @@ if ($object->id > 0) { function barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,mode,autodetect=false){ BarcodeIsInProduct=0; newproductrow=0 + result=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,"mode":mode}, + data: { "action":"existbarcode",'.(!empty($object->fk_warehouse)?'"fk_entrepot":'.$object->fk_warehouse.',':'').(!empty($object->fk_product)?'"fk_product":'.$object->fk_product.',':'').'"barcode":element,"product":product,"mode":mode}, type: \'POST\', async: false, success: function(response) { @@ -648,7 +658,7 @@ if ($object->id > 0) { newproductrow = response.object; } }else{ - if (!errortab.includes(element)){ + if (mode!="lotserial" && autodetect==false && !errortab.includes(element)){ errortab.push(element); console.error(response.message); } @@ -680,9 +690,14 @@ if ($object->id > 0) { BarcodeIsInProduct+=1; } }) - if(BarcodeIsInProduct==0 && newproductrow){ - tabproduct.push({\'Id\':tabproduct.length-1,\'Warehouse\':newproductrow.fk_warehouse,\'Barcode\':element,\'Batch\':element,\'Qty\':barcodeproductqty,\'fetched\':true,\'reelqty\':newproductrow.reelqty,\'fk_product\':newproductrow.fk_product,\'mode\':mode}); + if(BarcodeIsInProduct==0 && newproductrow!=0){ + tabproduct.push({\'Id\':tabproduct.length-1,\'Warehouse\':newproductrow.fk_warehouse,\'Barcode\':mode=="barcode"?element:null,\'Batch\':mode=="lotserial"?element:null,\'Qty\':barcodeproductqty,\'fetched\':true,\'reelqty\':newproductrow.reelqty,\'fk_product\':newproductrow.fk_product,\'mode\':mode}); + result = true; } + if(BarcodeIsInProduct > 0){ + result = true; + } + return result; } '; print '';