Debug inventory feature

This commit is contained in:
Laurent Destailleur 2021-12-12 00:05:31 +01:00
parent e46bde3a4a
commit 3abb3f5018
5 changed files with 172 additions and 132 deletions

View File

@ -476,3 +476,6 @@ ALTER TABLE llx_propal ADD COLUMN online_sign_name varchar(64);
ALTER TABLE llx_entrepot ADD COLUMN warehouse_usage integer DEFAULT 1; ALTER TABLE llx_entrepot ADD COLUMN warehouse_usage integer DEFAULT 1;
ALTER TABLE llx_session MODIFY COLUMN user_agent VARCHAR(255) NULL; ALTER TABLE llx_session MODIFY COLUMN user_agent VARCHAR(255) NULL;
ALTER TABLE llx_inventorydet ADD COLUMN fk_movement integer NULL;

View File

@ -21,7 +21,7 @@ CREATE TABLE llx_inventory
( (
rowid integer NOT NULL AUTO_INCREMENT PRIMARY KEY, rowid integer NOT NULL AUTO_INCREMENT PRIMARY KEY,
entity integer DEFAULT 0, entity integer DEFAULT 0,
ref varchar(48), ref varchar(48), -- We will also use this code as inventory code
date_creation datetime DEFAULT NULL, date_creation datetime DEFAULT NULL,
tms timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, tms timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
fk_user_creat integer, -- user making creation fk_user_creat integer, -- user making creation

View File

@ -26,8 +26,9 @@ CREATE TABLE llx_inventorydet
fk_warehouse integer DEFAULT 0, fk_warehouse integer DEFAULT 0,
fk_product integer DEFAULT 0, fk_product integer DEFAULT 0,
batch varchar(128) DEFAULT NULL, -- Lot or serial number batch varchar(128) DEFAULT NULL, -- Lot or serial number
qty_stock double DEFAULT NULL, -- The targeted value. can be filled during draft edition qty_stock double DEFAULT NULL, -- Value or real stock we have, when we start the inventory (may be updated during intermediary steps).
qty_view double DEFAULT NULL, -- must be filled once regulation is done qty_view double DEFAULT NULL, -- Quantity found during inventory. It is the targeted value, filled during edition of inventory.
qty_regulated double DEFAULT NULL -- must be filled once regulation is done qty_regulated double DEFAULT NULL, -- Never used. Deprecated because we already have the fk_movement now.
fk_movement integer NULL -- can contain the id of stock movement we recorded to make the inventory regulation of this line
) )
ENGINE=innodb; ENGINE=innodb;

View File

@ -207,6 +207,8 @@ if ($action == 'create') {
// Other attributes // Other attributes
include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_add.tpl.php'; include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_add.tpl.php';
//print '<tr><td class="titlefield fieldname_invcode">'.$langs->trans("InventoryCode").'</td><td>INV'.$object->id.'</td></tr>';
print '</table>'."\n"; print '</table>'."\n";
print dol_get_fiche_end(); print dol_get_fiche_end();

View File

@ -103,10 +103,20 @@ if ($cancel) {
$action = ''; $action = '';
} }
$parameters = array();
$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
if ($reshook < 0) {
setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
}
if (empty($reshook)) {
$error = 0;
if ($action == 'cancel_record' && $permissiontoadd) { if ($action == 'cancel_record' && $permissiontoadd) {
$object->setCanceled($user); $object->setCanceled($user);
} }
// Close inventory by recording the stock movements
if ($action == 'update' && !empty($user->rights->stock->mouvement->creer)) { if ($action == 'update' && !empty($user->rights->stock->mouvement->creer)) {
$stockmovment = new MouvementStock($db); $stockmovment = new MouvementStock($db);
$stockmovment->setOrigin($object->element, $object->id); $stockmovment->setOrigin($object->element, $object->id);
@ -125,6 +135,7 @@ if ($action == 'update' && !empty($user->rights->stock->mouvement->creer)) {
$totalarray = array(); $totalarray = array();
while ($i < $num) { while ($i < $num) {
$line = $db->fetch_object($resql); $line = $db->fetch_object($resql);
$qty_stock = $line->qty_stock; $qty_stock = $line->qty_stock;
$qty_view = $line->qty_view; // The quantity viewed by inventorier, the qty we target $qty_view = $line->qty_view; // The quantity viewed by inventorier, the qty we target
@ -138,13 +149,24 @@ if ($action == 'update' && !empty($user->rights->stock->mouvement->creer)) {
} }
$datemovement = ''; $datemovement = '';
//$inventorycode = 'INV'.$object->id;
$inventorycode = 'INV-'.$object->ref;
$idstockmove = $stockmovment->_create($user, $line->fk_product, $line->fk_warehouse, $stock_movement_qty, $movement_type, 0, $langs->trans('LabelOfInventoryMovemement', $object->id), 'INV'.$object->id, $datemovement, '', '', $line->batch); $idstockmove = $stockmovment->_create($user, $line->fk_product, $line->fk_warehouse, $stock_movement_qty, $movement_type, 0, $langs->trans('LabelOfInventoryMovemement', $object->id), $inventorycode, $datemovement, '', '', $line->batch);
if ($idstockmove < 0) { if ($idstockmove < 0) {
$error++; $error++;
setEventMessages($stockmovment->error, $stockmovment->errors, 'errors'); setEventMessages($stockmovment->error, $stockmovment->errors, 'errors');
break; break;
} }
// Update line with id of stock movement
$sqlupdate = 'UPDATE '.MAIN_DB_PREFIX.'inventorydet SET fk_movement = '.((int) $idstockmove).' WHERE rowid = '.$line->rowid;
$resqlupdate = $db->query($sqlupdate);
if (! $resqlupdate) {
$error++;
setEventMessages($db->lasterror(), null, 'errors');
break;
}
} }
} }
$i++; $i++;
@ -165,6 +187,7 @@ if ($action == 'update' && !empty($user->rights->stock->mouvement->creer)) {
} }
} }
// Save quantity found during inventory
if ($action =='updateinventorylines' && $permissiontoadd) { if ($action =='updateinventorylines' && $permissiontoadd) {
$sql = 'SELECT id.rowid, id.datec as date_creation, id.tms as date_modification, id.fk_inventory, id.fk_warehouse,'; $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 .= ' id.fk_product, id.batch, id.qty_stock, id.qty_view, id.qty_regulated';
@ -192,14 +215,15 @@ if ($action =='updateinventorylines' && $permissiontoadd) {
setEventMessages($langs->trans("FieldCannotBeNegative", $langs->transnoentitiesnoconv("RealQty")), null, 'errors'); setEventMessages($langs->trans("FieldCannotBeNegative", $langs->transnoentitiesnoconv("RealQty")), null, 'errors');
} }
if ($result > 0) { if ($result > 0) {
$inventoryline->qty_view = $qtytoupdate; $inventoryline->qty_stock = price2num(GETPOST('stock_qty_'.$lineid, 'alpha'), 'MS'); // The new value we want
$inventoryline->qty_view = $qtytoupdate; // The new value we want
$resultupdate = $inventoryline->update($user); $resultupdate = $inventoryline->update($user);
} }
} else { } else {
// Delete record // Delete record
$result = $inventoryline->fetch($lineid); $result = $inventoryline->fetch($lineid);
if ($result > 0) { if ($result > 0) {
$inventoryline->qty_view = null; $inventoryline->qty_view = null; // The new value we want
$resultupdate = $inventoryline->update($user); $resultupdate = $inventoryline->update($user);
} }
} }
@ -219,14 +243,6 @@ if ($action =='updateinventorylines' && $permissiontoadd) {
} }
} }
$parameters = array();
$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
if ($reshook < 0) {
setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
}
if (empty($reshook)) {
$error = 0;
$backurlforlist = DOL_URL_ROOT.'/product/inventory/list.php'; $backurlforlist = DOL_URL_ROOT.'/product/inventory/list.php';
$backtopage = DOL_URL_ROOT.'/product/inventory/inventory.php?id='.$object->id; $backtopage = DOL_URL_ROOT.'/product/inventory/inventory.php?id='.$object->id;
@ -443,6 +459,8 @@ if ($object->id > 0) {
// Other attributes. Fields from hook formObjectOptions and Extrafields. // Other attributes. Fields from hook formObjectOptions and Extrafields.
include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php'; include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
//print '<tr><td class="titlefield fieldname_invcode">'.$langs->trans("InventoryCode").'</td><td>INV'.$object->id.'</td></tr>';
print '</table>'; print '</table>';
print '</div>'; print '</div>';
print '</div>'; print '</div>';
@ -493,6 +511,10 @@ if ($object->id > 0) {
} }
} }
print '</div>'."\n"; print '</div>'."\n";
if ($object->status != Inventory::STATUS_DRAFT && $object->status != Inventory::STATUS_VALIDATED) {
print '<br><br>';
}
} }
@ -743,15 +765,15 @@ if ($object->id > 0) {
print '<td class="right">'; print '<td class="right">';
print $form->textwithpicto($langs->trans("RealQty"), $langs->trans("InventoryRealQtyHelp")); print $form->textwithpicto($langs->trans("RealQty"), $langs->trans("InventoryRealQtyHelp"));
print '</td>'; print '</td>';
if ($object->status == $object::STATUS_VALIDATED) { if ($object->status == $object::STATUS_DRAFT || $object->status == $object::STATUS_VALIDATED) {
// Actions // Actions
print '<td class="center">'; print '<td class="center">';
print '</td>'; print '</td>';
print '</tr>';
} }
print '</tr>';
// Line to add a new line in inventory // Line to add a new line in inventory
if ($object->status == $object::STATUS_VALIDATED) { if ($object->status == $object::STATUS_DRAFT || $object->status == $object::STATUS_VALIDATED) {
print '<tr>'; print '<tr>';
print '<td>'; print '<td>';
print $formproduct->selectWarehouses((GETPOSTISSET('fk_warehouse') ? GETPOST('fk_warehouse', 'int') : $object->fk_warehouse), 'fk_warehouse', 'warehouseopen', 1, 0, 0, '', 0, 0, array(), 'maxwidth300'); print $formproduct->selectWarehouses((GETPOSTISSET('fk_warehouse') ? GETPOST('fk_warehouse', 'int') : $object->fk_warehouse), 'fk_warehouse', 'warehouseopen', 1, 0, 0, '', 0, 0, array(), 'maxwidth300');
@ -810,7 +832,7 @@ if ($object->id > 0) {
$product_static = new Product($db); $product_static = new Product($db);
$result = $product_static->fetch($obj->fk_product, '', '', '', 1, 1, 1); $result = $product_static->fetch($obj->fk_product, '', '', '', 1, 1, 1);
$option = 'nobatch'; //$option = 'nobatch';
$option .= ',novirtual'; $option .= ',novirtual';
$product_static->load_stock($option); // Load stock_reel + stock_warehouse. This can also call load_virtual_stock() $product_static->load_stock($option); // Load stock_reel + stock_warehouse. This can also call load_virtual_stock()
@ -831,14 +853,24 @@ if ($object->id > 0) {
print '</td>'; print '</td>';
} }
// Expected quantity // Expected quantity = Quantity in stock when we start inventory
print '<td class="right expectedqty" id="id_'.$obj->rowid.'">'; print '<td class="right expectedqty" id="id_'.$obj->rowid.'" title="Stock viewed at last update: '.$obj->qty_stock.'">';
print $obj->qty_stock; if ($object->status == $object::STATUS_DRAFT || $object->status == $object::STATUS_VALIDATED) {
if ($conf->productbatch->enabled && $product_static->hasbatch()) {
$valuetoshow = $product_static->stock_warehouse[$obj->fk_warehouse]->detail_batch[$obj->batch]->qty;
} else {
$valuetoshow = $product_static->stock_warehouse[$obj->fk_warehouse]->real;
}
} else {
$valuetoshow = $obj->qty_stock;
}
print price2num($valuetoshow, 'MS');
print '<input type="hidden" name="stock_qty_'.$obj->rowid.'" value="'.$valuetoshow.'">';
print '</td>'; print '</td>';
// Real quantity // Real quantity
if ($object->status == $object::STATUS_DRAFT || $object->status == $object::STATUS_VALIDATED) {
print '<td class="right">'; print '<td class="right">';
if ($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;
//if (!$hasinput && $qty_view !== null && $obj->qty_stock != $qty_view) { //if (!$hasinput && $qty_view !== null && $obj->qty_stock != $qty_view) {
@ -852,13 +884,15 @@ if ($object->id > 0) {
print '<input type="text" class="maxwidth75 right realqty" name="id_'.$obj->rowid.'" id="id_'.$obj->rowid.'_input" value="'.$qty_view.'">'; print '<input type="text" class="maxwidth75 right realqty" name="id_'.$obj->rowid.'" id="id_'.$obj->rowid.'_input" value="'.$qty_view.'">';
print '</td>'; print '</td>';
// Picto delete line
print '<td class="right">'; print '<td class="right">';
print '<a class="reposition" href="'.DOL_URL_ROOT.'/product/inventory/inventory.php?id='.$object->id.'&lineid='.$obj->rowid.'&action=deleteline&token='.newToken().'">'.img_delete().'</a>'; print '<a class="reposition" href="'.DOL_URL_ROOT.'/product/inventory/inventory.php?id='.$object->id.'&lineid='.$obj->rowid.'&action=deleteline&token='.newToken().'">'.img_delete().'</a>';
print '</td>';
$qty_tmp = price2num(GETPOST("id_".$obj->rowid."_input_tmp", 'MS')) >= 0 ? GETPOST("id_".$obj->rowid."_input_tmp") : $qty_view; $qty_tmp = price2num(GETPOST("id_".$obj->rowid."_input_tmp", 'MS')) >= 0 ? GETPOST("id_".$obj->rowid."_input_tmp") : $qty_view;
print '<input type="hidden" class="maxwidth75 right realqty" name="id_'.$obj->rowid.'_input_tmp" id="id_'.$obj->rowid.'_input_tmp" value="'.$qty_tmp.'">'; print '<input type="hidden" class="maxwidth75 right realqty" name="id_'.$obj->rowid.'_input_tmp" id="id_'.$obj->rowid.'_input_tmp" value="'.$qty_tmp.'">';
print '</td>';
} else { } else {
print $obj->qty_view; print '<td class="right">';
print $obj->qty_view; // qty found
print '</td>'; print '</td>';
} }
print '</tr>'; print '</tr>';