More complete error management.

This commit is contained in:
Laurent Destailleur 2021-12-19 03:36:25 +01:00
parent a662a980db
commit 771805cd83
5 changed files with 104 additions and 31 deletions

View File

@ -73,14 +73,15 @@ class FormOther
$out .= '<div class="div-for-modal-topright" style="padding: 15px">';
$out .= '<center><strong>Barcode scanner tool...</strong></center><br>';
$out .= '<input type="radio" name="barcodemode" value="barcodeforautodetect" checked="checked"> Autodetect if we scan a product barcode or a lot/serial barcode<br>';
$out .= '<input type="radio" name="barcodemode" value="barcodeforproduct"> Scan a product barcode<br>';
$out .= '<input type="radio" name="barcodemode" value="barcodeforlotserial"> Scan a product lot or serial number<br>';
$out .= '<input type="radio" name="barcodemode" value="barcodeforautodetect" id="barcodeforautodetect" checked="checked"> <label for="barcodeforautodetect">Autodetect if we scan a product barcode or a lot/serial barcode</label><br>';
$out .= '<input type="radio" name="barcodemode" value="barcodeforproduct" id="barcodeforproduct"> <label for="barcodeforproduct">Scan a product barcode</label><br>';
$out .= '<input type="radio" name="barcodemode" value="barcodeforlotserial" id="barcodeforlotserial"> <label for="barcodeforlotserial">Scan a product lot or serial number</label><br>';
$stringaddbarcode = $langs->trans("QtyToAddAfterBarcodeScan", "tmphtml");
$htmltoreplaceby = '<select name="selectaddorreplace"><option selected value="add">'.$langs->trans("Add").'</option><option value="replace">'.$langs->trans("ToReplace").'</option></select>';
$stringaddbarcode = str_replace("tmphtml", $htmltoreplaceby, $stringaddbarcode);
$out .= $stringaddbarcode.' <input type="text" name="barcodeproductqty" class="width50 right" value="1"><br>';
$out .= '<br>';
$out .= '<textarea type="text" name="barcodelist" class="centpercent" autofocus rows="'.ROWS_3.'"></textarea>';
/*print '<br>'.$langs->trans("or").'<br>';
@ -92,13 +93,20 @@ class FormOther
$out .= '<br>';
$out .= '<center>';
$out .= '<input type="submit" class="button marginleftonly marginrightonly" id ="exec'.dol_escape_js($jstoexecuteonadd).'" name="addscan" value="'.$langs->trans("Add").'">';
$out .= '<input type="submit" class="button marginleftonly marginrightonly" name="cancel" value="'.$langs->trans("Cancel").'">';
$out .= '<input type="submit" class="button marginleftonly marginrightonly" name="cancel" value="'.$langs->trans("CloseWindow").'">';
$out .= '</center>';
$out .= '<br>';
$out .= '<div type="text" id="scantoolmessage" class="scantoolmessage ok nopadding"></div>';
$out .= '<script>';
$out .= 'jQuery("#barcodeforautodetect, #barcodeforproduct, #barcodeforlotserial").click(function(){';
$out .= 'console.log("select choice");';
$out .= 'jQuery("#scantoolmessage").text("");';
$out .= '});'."\n";
$out .= '$("#exec'.dol_escape_js($jstoexecuteonadd).'").click(function(){
console.log("We call js to execute '.dol_escape_js($jstoexecuteonadd).'");
'.dol_escape_js($jstoexecuteonadd).'();
return false; /* We want to stay on the scan tool */
})';
$out .= '</script>';

View File

@ -259,11 +259,13 @@ ShowAllBatchByDefault=By default, show batch details on product "stock" tab
CollapseBatchDetailHelp=You can set batch detail default display in stocks module configuration
ErrorWrongBarcodemode=Unknown Barcode mode
ProductDoesNotExist=Product does not exist
ErrorSameBatchNumber=Same batch number found in inventory list
ErrorSameBatchNumber=Several record for the batch number were found in the inventory sheet. No way to know which one to increase.
ProductBatchDoesNotExist=Product with batch/serial does not exist
ProductBarcodeDoesNotExist=Product with barcode does not exist
WarehouseId=Warehouse ID
WarehouseRef=Warehouse Ref
SaveQtyFirst=Save the real inventoried quantities first, before asking creation of the stock movement.
InventoryStartedShort=Started
ErrorOnElementsInventory=Scan was aborted due to following barcode or batch number on error
ErrorOnElementsInventory=Operation canceled for the following reason:
ErrorCantFindCodeInInventory=Can't find the following code in inventory
QtyWasAddedToTheScannedBarcode=Success !! The quantity was added to all the requested barcode. You can close the Scanner tool.

View File

@ -343,10 +343,15 @@ if (empty($reshook)) {
$result = $tmp->create($user);
if ($result < 0) {
if ($db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
setEventMessages($langs->trans("DuplicateRecord"), null, 'errors');
$langs->load("errors");
setEventMessages($langs->trans("ErrorRecordAlreadyExists"), null, 'errors');
} else {
dol_print_error($db, $tmp->error, $tmp->errors);
}
} else {
// Clear var
$_POST['batch'] = '';
$_POST['qtytoadd'] = '';
}
}
}
@ -606,18 +611,28 @@ if ($object->id > 0) {
print '<script>';
print '
var errortab = [];
var errortab1 = [];
var errortab2 = [];
var errortab3 = [];
function barcodescannerjs(){
console.log("We catch inputs in sacnner box");
console.log("We catch inputs in scanner box");
jQuery("#scantoolmessage").text();
var selectaddorreplace = $("select[name=selectaddorreplace]").val();
var barcodemode = $("input[name=barcodemode]:checked").val();
var barcodeproductqty = $("input[name=barcodeproductqty]").val();
var textarea = $("textarea[name=barcodelist]").val();
var textarray = textarea.split("\n");
var textarray = textarea.split(/[\s,]+/);
var tabproduct = [];
errortab1 = [];
errortab2 = [];
errortab3 = [];
if(textarray[0] != ""){
$(".expectedqty").each(function(){
id = this.id;
console.log("Analyze line "+id+" in inventory");
warehouse = $("#"+id+"_warehouse").children().first().text();
productbarcode = $("#"+id+"_product").children().first().attr("title");
productbarcode = productbarcode.split("<br>");
@ -625,11 +640,10 @@ if ($object->id > 0) {
productbarcode = productbarcode.slice(productbarcode.indexOf("</b> ")+5);
productbatchcode = $("#"+id+"_batch").text();
if(barcodemode != "barcodeforproduct"){
if (barcodemode != "barcodeforproduct") {
tabproduct.forEach(product=>{
if(product.Batch != "" && product.Batch == productbatchcode){
alert("'.$langs->transnoentities('ErrorSameBatchNumber').': "+productbatchcode);
throw"'.$langs->transnoentities('ErrorSameBatchNumber').': "+productbatchcode;
errortab1.push(productbatchcode);
}
})
}
@ -638,7 +652,8 @@ if ($object->id > 0) {
productinput = 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;
@ -648,26 +663,31 @@ if ($object->id > 0) {
verify_batch = barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,"lotserial",true);
break;
case "barcodeforproduct":
barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,"barcode");
verify_barcode = barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,"barcode");
break;
case "barcodeforlotserial":
barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,"lotserial");
verify_batch = barcodeserialforproduct(tabproduct,index,element,barcodeproductqty,selectaddorreplace,"lotserial");
break;
default:
alert("'.$langs->trans("ErrorWrongBarcodemode").' \""+barcodemode+"\"");
throw "'.$langs->trans('ErrorWrongBarcodemode').' \""+barcodemode+"\"";
}
if(verify_batch == true && verify_barcode == true){
errortab.push(element);
if (verify_batch == false && verify_barcode == false) { /* If the 2 flags are false, error */
errortab2.push(element);
}
if (verify_batch == true && verify_barcode == true) { /* If the 2 flags are true, error: we don t know which one to take */
errortab3.push(element);
}
});
if (Object.keys(errortab).length < 1){
if (Object.keys(errortab1).length < 1 && Object.keys(errortab2).length < 1 && Object.keys(errortab3).length < 1) {
tabproduct.forEach(product => {
if(product.Qty!=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},
data: { "token":"'.newToken().'", "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) {
@ -690,15 +710,35 @@ if ($object->id > 0) {
$("#"+product.Id+"_input").val(product.Qty);
}
}
})
document.forms["formrecord"].submit();
}else{
let stringerror = "";
errortab.forEach(element => {
stringerror += (element + ",")
});
stringerror = stringerror.slice(0, -1);
alert("'.$langs->trans("ErrorOnElementsInventory").' :\n" + stringerror);
jQuery("#scantoolmessage").text("'.$langs->trans("QtyWasAddedToTheScannedBarcode").'\n");
/* document.forms["formrecord"].submit(); */
} else {
let stringerror = "";
if (Object.keys(errortab1).length > 0) {
stringerror += "<br>'.$langs->transnoentities('ErrorSameBatchNumber').': ";
errortab1.forEach(element => {
stringerror += (element + ", ")
});
stringerror = stringerror.slice(0, -2); /* Remove last ", " */
}
if (Object.keys(errortab2).length > 0) {
stringerror += "<br>'.$langs->transnoentities('ErrorCantFindCodeInInventory').': ";
errortab2.forEach(element => {
stringerror += (element + ", ")
});
stringerror = stringerror.slice(0, -2); /* Remove last ", " */
}
if (Object.keys(errortab3).length > 0) {
stringerror += "<br>'.$langs->transnoentities('ErrorCodeScannedIsBothProductAndSerial').': ";
errortab3.forEach(element => {
stringerror += (element + ", ")
});
stringerror = stringerror.slice(0, -2); /* Remove last ", " */
}
jQuery("#scantoolmessage").text("'.$langs->trans("ErrorOnElementsInventory").'\n" + stringerror);
//alert("'.$langs->trans("ErrorOnElementsInventory").' :\n" + stringerror);
}
}
@ -710,7 +750,7 @@ if ($object->id > 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.',':'').(!empty($object->fk_product)?'"fk_product":'.$object->fk_product.',':'').'"barcode":element,"product":product,"mode":mode},
data: { "token":"'.newToken().'", "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) {

View File

@ -1032,6 +1032,11 @@ div.divsearchfield {
opacity: 0.6;
}
/* ============================================================================== */
/* Styles for scan tool */
/* ============================================================================== */
div.div-for-modal {
/* display: none; */
position:absolute;
@ -1045,19 +1050,26 @@ div.div-for-modal {
z-index: 100;
}
#scantoolmessage {
height: 3em;
border: none;
overflow-y: auto;
}
div.div-for-modal-topright {
/* display: none; */
position: fixed;
top: 0;
right: 0;
width:50%; /* adjust as per your needs */
height:250px; /* adjust as per your needs */
height:320px; /* adjust as per your needs */
background: #fff;
border: 1px solid #bbb;
box-shadow: 2px 2px 20px #ddd;
z-index: 1100;
}
<?php
// Add a nowrap on smartphone, so long list of field used for filter are overflowed with clip
if ($conf->browser->layout == 'phone') {

View File

@ -1158,6 +1158,10 @@ if ($conf->browser->layout == 'phone') {
}
/* ============================================================================== */
/* Styles for scan tool */
/* ============================================================================== */
div.div-for-modal {
/* display: none; */
position:absolute;
@ -1171,13 +1175,19 @@ div.div-for-modal {
z-index: 100;
}
#scantoolmessage {
height: 3em;
border: none;
overflow-y: auto;
}
div.div-for-modal-topright {
/* display: none; */
position: fixed;
top: 0;
right: 0;
width:50%; /* adjust as per your needs */
height:250px; /* adjust as per your needs */
height:300px; /* adjust as per your needs */
background: #fff;
border: 1px solid #bbb;
box-shadow: 2px 2px 20px #ddd;
@ -1185,6 +1195,7 @@ div.div-for-modal-topright {
}
div.confirmmessage {
padding-top: 6px;
}