From 46a420dbd914fbd31a0bd753c9d990bc024f073c Mon Sep 17 00:00:00 2001 From: lainwir3d Date: Thu, 20 Oct 2022 16:17:13 +0400 Subject: [PATCH] Inventory - adding same product multiple time will increase / decrease qty --- htdocs/product/inventory/inventory.php | 56 ++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/htdocs/product/inventory/inventory.php b/htdocs/product/inventory/inventory.php index b801c064af0..f8f827b0bc2 100644 --- a/htdocs/product/inventory/inventory.php +++ b/htdocs/product/inventory/inventory.php @@ -308,6 +308,29 @@ if (empty($reshook)) { include DOL_DOCUMENT_ROOT.'/core/actions_sendmails.inc.php';*/ if (GETPOST('addline', 'alpha')) { + + $sql = 'SELECT id.rowid, id.datec as date_creation, id.tms as date_modification, id.fk_inventory, id.fk_warehouse,'; + $sql .= ' id.fk_product, id.batch, id.qty_stock, id.qty_view, id.qty_regulated'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'inventorydet as id'; + $sql .= ' WHERE id.fk_inventory = '.((int) $object->id); + $sql .= ' AND id.fk_product = '.$fk_product; + + $inventoryline = new InventoryLine($db); + $lineExists = $false; + $db->begin(); + $resql = $db->query($sql); + if ($resql) { + $num = $db->num_rows($resql); + $i = 0; + if($num > 0){ + $line = $db->fetch_object($resql); + if($inventoryline->fetch($line->rowid) > 0){ + $lineExists = true; + //var_dump($inventoryline); die(); + } + } + } + $qty= (GETPOST('qtytoadd') != '' ? price2num(GETPOST('qtytoadd', 'MS')) : null); if ($fk_warehouse <= 0) { $error++; @@ -317,7 +340,7 @@ if (empty($reshook)) { $error++; setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Product")), null, 'errors'); } - if (price2num(GETPOST('qtytoadd'), 'MS') < 0) { + if (!$lineExists && price2num(GETPOST('qtytoadd'), 'MS') < 0) { $error++; setEventMessages($langs->trans("FieldCannotBeNegative", $langs->transnoentitiesnoconv("RealQty")), null, 'errors'); } @@ -341,7 +364,7 @@ if (empty($reshook)) { setEventMessages($langs->trans("ErrorProductDoesNotNeedBatchNumber", $tmpproduct->ref), null, 'errors'); } } - if (!$error) { + if (!$error && !$lineExists) { $tmp = new InventoryLine($db); $tmp->fk_inventory = $object->id; $tmp->fk_warehouse = $fk_warehouse; @@ -363,6 +386,31 @@ if (empty($reshook)) { $_POST['batch'] = ''; $_POST['qtytoadd'] = ''; } + }else if(!$error){ + + if(($inventoryline->qty_view + ((int) $qty)) < 0){ + $error++; + setEventMessages($langs->trans("FieldCannotBeNegative", $langs->transnoentitiesnoconv("RealQty")), null, 'errors'); + }else{ + $inventoryline->qty_view += ((int) $qty); // price2num + $inventoryline->update($user); + //var_dump($fk_product); die(); + + if ($result < 0) { + $langs->load("errors"); + setEventMessages("Erreur inconnue lors de la mise à jour du champ", null, 'errors'); + } else { + setEventMessages("Quantité ajoutée ou soustraite à la ligne existante", null); + // Clear var + $_POST['batch'] = ''; + $_POST['qtytoadd'] = ''; + } + } + } + if (! $error) { + $db->commit(); + } else { + $db->rollback(); } } } @@ -937,7 +985,6 @@ if ($object->id > 0) { $cacheOfProducts = array(); $cacheOfWarehouses = array(); - //$sql = ''; $resql = $db->query($sql); if ($resql) { @@ -1003,7 +1050,8 @@ if ($object->id > 0) { print ''; if ($object->status == $object::STATUS_DRAFT || $object->status == $object::STATUS_VALIDATED) { - $qty_view = GETPOST("id_".$obj->rowid) && price2num(GETPOST("id_".$obj->rowid), 'MS') >= 0 ? GETPOST("id_".$obj->rowid) : $obj->qty_view; + //$qty_view = GETPOST("id_".$obj->rowid) && price2num(GETPOST("id_".$obj->rowid), 'MS') >= 0 ? GETPOST("id_".$obj->rowid) : $obj->qty_view; + $qty_view = $obj->qty_view; //if (!$hasinput && $qty_view !== null && $obj->qty_stock != $qty_view) { if ($qty_view != '') {