Merge pull request #19568 from Hystepik/develop#2

Fix inventory scanning function
This commit is contained in:
Laurent Destailleur 2021-12-08 15:30:55 +01:00 committed by GitHub
commit 597ab407f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 282 additions and 118 deletions

View File

@ -77,7 +77,10 @@ class FormOther
$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 .= $langs->trans("QtyToAddAfterBarcodeScan").' <input type="text" name="barcodeproductqty" class="width50 right" value="1"><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 .= '<textarea type="text" name="barcodelist" class="centpercent" autofocus rows="'.ROWS_3.'"></textarea>';
/*print '<br>'.$langs->trans("or").'<br>';

View File

@ -30,7 +30,7 @@ ManageLotMask=Custom mask
CustomMasks=Option to define a different numbering mask for each product
BatchLotNumberingModules=Numbering rule for automatic generation of lot number
BatchSerialNumberingModules=Numbering rule for automatic generation of serial number (for products with property 1 unique lot/serial for each product)
QtyToAddAfterBarcodeScan=Qty to add for each barcode/lot/serial scanned
QtyToAddAfterBarcodeScan=Qty to %s for each barcode/lot/serial scanned
LifeTime=Life span (in days)
EndOfLife=End of life
ManufacturingDate=Manufacturing date
@ -42,3 +42,4 @@ HideLots=Hide lots
#Traceability - qc status
OutOfOrder=Out of order
InWorkingOrder=In working order
ToReplace=Replace

View File

@ -265,4 +265,5 @@ 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
InventoryStartedShort=Started
ErrorOnElementsInventory=Scan was aborted due to following barcode or batch number on error

View File

@ -30,7 +30,7 @@ ManageLotMask=Masque personnalisé
CustomMasks=Option pour définir un masque de numérotation différent pour chaque produit
BatchLotNumberingModules=Règle de numérotation pour la génération automatique de numéro de lot
BatchSerialNumberingModules=Règle de numérotation pour la génération automatique de numéro de série (pour les produits avec propriété 1 lot/série unique pour chaque produit)
QtyToAddAfterBarcodeScan=Quantité à ajouter pour chaque code à barres/lot/série scanné
QtyToAddAfterBarcodeScan=Quantité à %s pour chaque code à barres/lot/série scanné
LifeTime=Durée de vie (en jours)
EndOfLife=Fin d'utilisation
ManufacturingDate=Date de fabrication

View File

@ -1,47 +0,0 @@
<?php
require '../../../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/product/inventory/class/inventory.class.php';
$get = GETPOST('get', 'alpha');
$put = GETPOST('put', 'alpha');
switch ($put) {
case 'qty':
if (empty($user->rights->stock->creer)) {
echo -1; exit;
}
$fk_det_inventory = GETPOST('fk_det_inventory');
$det = new InventoryLine($db);
if ($det->fetch($fk_det_inventory)) {
$det->qty_view += GETPOST('qty');
$res = $det->update($user);
echo $det->qty_view;
} else {
echo -2;
}
break;
case 'pmp':
if (empty($user->rights->stock->creer) || empty($user->rights->stock->changePMP)) {
echo -1; exit;
}
$fk_det_inventory = GETPOST('fk_det_inventory');
$det = new InventoryLine($db);
if ($det->fetch($fk_det_inventory)) {
$det->new_pmp = price2num(GETPOST('pmp'));
$det->update($user);
echo $det->new_pmp;
} else {
echo -2;
}
break;
}

View File

@ -0,0 +1,135 @@
<?php
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file /htdocs/product/inventory/ajax/searchfrombarcode.php
* \brief File to make Ajax action on product and stock
*/
if (!defined('NOTOKENRENEWAL')) {
define('NOTOKENRENEWAL', 1); // Disables token renewal
}
if (!defined('NOREQUIREMENU')) {
define('NOREQUIREMENU', '1');
}
if (!defined('NOREQUIREHTML')) {
define('NOREQUIREHTML', '1');
}
if (!defined('NOREQUIREAJAX')) {
define('NOREQUIREAJAX', '1');
}
if (!defined('NOREQUIRESOC')) {
define('NOREQUIRESOC', '1');
}
if (!defined('NOCSRFCHECK')) {
define('NOCSRFCHECK', '1');
}
require '../../../main.inc.php';
require_once DOL_DOCUMENT_ROOT."/product/stock/class/entrepot.class.php";
$warehouse = new Entrepot($db);
$action = GETPOST("action", "alpha");
$barcode = GETPOST("barcode", "aZ09");
$product = GETPOST("product");
$response = "";
$fk_entrepot = GETPOST("fk_entrepot", "int");
$fk_inventory = GETPOST("fk_inventory", "int");
$fk_product = GETPOST("fk_product", "int");
$reelqty = GETPOST("reelqty", "int");
$batch = GETPOST("batch", "int");
$mode = GETPOST("mode", "aZ");
$warehousefound = 0;
$warehouseid = 0;
$objectreturn = array();
if ($action == "existbarcode" && !empty($barcode)) {
if (!empty($mode) && $mode == "lotserial") {
$sql = "SELECT ps.fk_entrepot, ps.fk_product, p.barcode, ps.reel, pb.batch";
$sql .= " FROM ".MAIN_DB_PREFIX."product_batch as pb";
$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";
$sql .= " WHERE pb.batch = '".$db->escape($barcode)."'";
} else {
$sql = "SELECT ps.fk_entrepot, ps.fk_product, p.barcode,ps.reel";
$sql .= " FROM ".MAIN_DB_PREFIX."product_stock as ps JOIN ".MAIN_DB_PREFIX."product as p ON ps.fk_product = p.rowid";
$sql .= " WHERE p.barcode = '".$db->escape($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);
for ($i=0; $i < $nbline; $i++) {
$object = $db->fetch_object($result);
if (($mode == "barcode" && $barcode == $object->barcode) || ($mode == "lotserial" && $barcode == $object->batch)) {
$warehouse->fetch(0, $product["Warehouse"]);
if (!empty($object->fk_entrepot) && $warehouse->id == $object->fk_entrepot) {
$warehousefound++;
$warehouseid = $object->fk_entrepot;
$fk_product = $object->fk_product;
$reelqty = $object->reel;
$objectreturn = array('fk_warehouse'=>$warehouseid,'fk_product'=>$fk_product,'reelqty'=>$reelqty);
}
}
}
if ($warehousefound < 1) {
$response = array('status'=>'error','errorcode'=>'NotFound','message'=>'No warehouse found for barcode'.$barcode);
} elseif ($warehousefound > 1) {
$response = array('status'=>'error','errorcode'=>'TooManyWarehouse','message'=>'Too many warehouse found');
} else {
$response = array('status'=>'success','message'=>'Warehouse found','object'=>$objectreturn);
}
} else {
$response = array('status'=>'error','errorcode'=>'NotFound','message'=>"No results found for barcode");
}
} else {
$response = array('status'=>'error','errorcode'=>'ActionError','message'=>"Error on action");
}
if ($action == "addnewlineproduct") {
require_once DOL_DOCUMENT_ROOT."/product/inventory/class/inventory.class.php";
$inventoryline = new InventoryLine($db);
if (!empty($fk_inventory)) {
$inventoryline->fk_inventory = $fk_inventory;
$inventoryline->fk_warehouse = $fk_entrepot;
$inventoryline->fk_product = $fk_product;
$inventoryline->qty_stock = $reelqty;
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','id_line'=>$result);
} else {
$response = array('status'=>'error','errorcode'=>'ErrorCreation','message'=>"Error on line creation");
}
} else {
$response = array('status'=>'error','errorcode'=>'NoIdForInventory','message'=>"No id for inventory");
}
}
$response = json_encode($response);
echo $response;

View File

@ -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';

View File

@ -452,7 +452,7 @@ if ($object->id > 0) {
print dol_get_fiche_end();
print '<form name="formrecord" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
print '<form id="formrecord" name="formrecord" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
print '<input type="hidden" name="token" value="'.newToken().'">';
print '<input type="hidden" name="action" value="updateinventorylines">';
print '<input type="hidden" name="id" value="'.$object->id.'">';
@ -541,14 +541,18 @@ if ($object->id > 0) {
if ($action == 'updatebyscaning') {
if ($permissiontoadd) {
print '<script>';
print 'function barcodescannerjs(){
print '
var errortab = [];
function barcodescannerjs(){
console.log("We catch inputs in sacnner box");
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 tabproduct = [];
if(textarray[0] != ""){
var tabproduct = [];
$(".expectedqty").each(function(){
id = this.id;
warehouse = $("#"+id+"_warehouse").children().first().text();
@ -560,81 +564,147 @@ if ($object->id > 0) {
productbatchcode = $("#"+id+"_batch").text();
if(barcodemode != "barcodeforproduct"){
tabproduct.forEach(product=>{
if(product.Batch == productbatchcode){
alert("'.$langs->trans('ErrorSameBatchNumber').': "+productbatchcode);
throw"'.$langs->trans('ErrorSameBatchNumber').': "+productbatchcode;
if(product.Batch != "" && product.Batch == productbatchcode){
alert("'.$langs->transnoentities('ErrorSameBatchNumber').': "+productbatchcode);
throw"'.$langs->transnoentities('ErrorSameBatchNumber').': "+productbatchcode;
}
})
}
tabproduct.push({\'Id\':id,\'Warehouse\':warehouse,\'Barcode\':productbarcode,\'Batch\':productbatchcode,\'Qty\':0});
productinput = $("#"+id+"_input").val();
if(productinput == ""){
productinput = 0
}
tabproduct.push({\'Id\':id,\'Warehouse\':warehouse,\'Barcode\':productbarcode,\'Batch\':productbatchcode,\'Qty\':productinput,\'fetched\':false});
})
switch(barcodemode){
case "barcodeforautodetect":
textarray.forEach(function(element,index){
console.log("Product autodetect "+(index+=1)+": "+element);
BatchCodeDoesNotExist=0;
tabproduct.forEach(product => {
if(product.Batch == element || product.Barcode == element){
product.Qty+=1;
}else{
BatchCodeDoesNotExist+=1;
}
})
if(BatchCodeDoesNotExist >= tabproduct.length){
alert("'.$langs->trans('ProductDoesNotExist').': "+element);
textarray.forEach(function(element,index){
var verify_batch = false;
var verify_barcode = false;
switch(barcodemode){
case "barcodeforautodetect":
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");
break;
case "barcodeforlotserial":
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 (Object.keys(errortab).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},
type: \'POST\',
async: false,
success: function(response) {
response = JSON.parse(response);
if(response.status == "success"){
console.log(response.message);
$("<input type=\'text\' value=\'"+product.Qty+"\' />")
.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 line creation function");
},
});
} else {
$("#"+product.Id+"_input").val(product.Qty);
}
})
break;
case "barcodeforproduct":
textarray.forEach(function(element,index){
console.log("Product "+(index+=1)+": "+element);
BarCodeDoesNotExist=0;
tabproduct.forEach(product => {
if(product.Barcode == element){
product.Qty+=1;
}else{
BarCodeDoesNotExist+=1;
}
})
if(BarCodeDoesNotExist >= tabproduct.length){
alert("'.$langs->trans('ProductBarcodeDoesNotExist').': "+element);
}
})
break;
case "barcodeforlotserial":
textarray.forEach(function(element,index){
console.log("Product batch/serial "+(index+=1)+": "+element);
BatchCodeDoesNotExist=0;
tabproduct.forEach(product => {
if(product.Batch == element){
product.Qty+=1;
}else{
BatchCodeDoesNotExist+=1;
}
})
if(BatchCodeDoesNotExist >= tabproduct.length){
alert("'.$langs->trans('ProductBatchDoesNotExist').': "+element);
}
})
break;
default:
alert("'.$langs->trans("ErrorWrongBarcodemode").' \""+barcodemode+"\"");
throw"'.$langs->trans('ErrorWrongBarcodemode').' \""+barcodemode+"\"";
}
})
document.forms["formrecord"].submit();
}else{
let stringerror = "";
errortab.forEach(element => {
stringerror += (element + ",")
});
stringerror = stringerror.slice(0, -1);
alert("'.$langs->trans("ErrorOnElementsInventory").' :\n" + stringerror);
}
tabproduct.forEach(product => {
if(product.Qty!=0){
console.log("We change #"+product.Id+"_input to match input in scanner box");
$("#"+product.Id+"_input").val(product.Qty*barcodeproductqty);
}
})
document.forms["formrecord"].submit();
}
}';
}
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.',':'').(!empty($object->fk_product)?'"fk_product":'.$object->fk_product.',':'').'"barcode":element,"product":product,"mode":mode},
type: \'POST\',
async: false,
success: function(response) {
response = JSON.parse(response);
if(response.status == "success"){
console.log(response.message);
if(!newproductrow){
newproductrow = response.object;
}
}else{
if (mode!="lotserial" && autodetect==false && !errortab.includes(element)){
errortab.push(element);
console.error(response.message);
}
}
},
error : function(output) {
console.error("Error on barcodeserialforproduct function");
},
});
console.log("Product "+(index+=1)+": "+element);
if(mode == "barcode"){
testonproduct = product.Barcode
}else if (mode == "lotserial"){
testonproduct = product.Batch
}
if(testonproduct == element){
if(selectaddorreplace == "add"){
productqty = parseInt(product.Qty,10);
product.Qty = productqty + parseInt(barcodeproductqty,10);
}else if(selectaddorreplace == "replace"){
if(product.fetched == false){
product.Qty = barcodeproductqty
product.fetched=true
}else{
productqty = parseInt(product.Qty,10);
product.Qty = productqty + parseInt(barcodeproductqty,10);
}
}
BarcodeIsInProduct+=1;
}
})
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 '</script>';
}
include DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
$formother = new FormOther($db);
print $formother->getHTMLScannerForm();
print $formother->getHTMLScannerForm("barcodescannerjs");
}
//Call method to undo changes in real qty