From 253125b60aa31b92f3637be67e5dcee5d2942037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Tak=C3=A1cs?= <93127072+PeterTakacs300@users.noreply.github.com> Date: Tue, 29 Nov 2022 03:39:21 +0100 Subject: [PATCH] IDEMPIERE-5483 - Physical Inventory Doesn't Update Last Inventory Date on Specific Cases (#1575) * IDEMPIERE-5483 - Physical Inventory Doesn't Update Last Inventory Date on Specific Cases * IDEMPIERE-5483 - requested changes * IDEMPIERE-5483 - fixes * IDEMPIERE-5483 - fixed fallen test case --- .../src/org/compiere/model/MInventory.java | 59 +++++++------- .../org/compiere/model/MStorageOnHand.java | 76 ++++++++++++++++++- 2 files changed, 103 insertions(+), 32 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/model/MInventory.java b/org.adempiere.base/src/org/compiere/model/MInventory.java index 325fb7401d..5f90eaf33a 100644 --- a/org.adempiere.base/src/org/compiere/model/MInventory.java +++ b/org.adempiere.base/src/org/compiere/model/MInventory.java @@ -526,6 +526,34 @@ public class MInventory extends X_M_Inventory implements DocAction } //If Quantity Count minus Quantity Book = Zero, then no change in Inventory + if(MDocType.DOCSUBTYPEINV_PhysicalInventory.equals(docSubTypeInv) && !isReversal()) { + // We want to update Date Last Inventory on this records as well. + if (line.getM_AttributeSetInstance_ID() == 0 ) { + MStorageOnHand[] storages = MStorageOnHand.getWarehouse(getCtx(), getM_Warehouse_ID(), line.getM_Product_ID(), 0, + null, MClient.MMPOLICY_FiFo.equals(product.getMMPolicy()), true, line.getM_Locator_ID(), get_TrxName(), false); + if(storages != null) { + for(MStorageOnHand storage: storages) { + storage.setDateLastInventory(getMovementDate()); + if (!storage.save(get_TrxName())) { + m_processMsg = "Storage on hand not updated for DateLastInventory"; + return DocAction.STATUS_Invalid; + } + } + } + } else { + MStorageOnHand[] storages = MStorageOnHand.getAll(getCtx(), line.getM_Product_ID(), + line.getM_Locator_ID(), line.getM_AttributeSetInstance_ID(), null, false, get_TrxName()); + if(storages != null) { + for(MStorageOnHand storage: storages) { + storage.setDateLastInventory(getMovementDate()); + if (!storage.save(get_TrxName())) { + m_processMsg = "Storage on hand not updated for DateLastInventory"; + return DocAction.STATUS_Invalid; + } + } + } + } + } if (qtyDiff.signum() == 0) continue; @@ -571,26 +599,13 @@ public class MInventory extends X_M_Inventory implements DocAction line.getM_Locator_ID(), line.getM_Product_ID(), ma.getM_AttributeSetInstance_ID(), - QtyMA.negate(),ma.getDateMaterialPolicy(), get_TrxName())) + QtyMA.negate(),ma.getDateMaterialPolicy(), getMovementDate(), get_TrxName())) { String lastError = CLogger.retrieveErrorString(""); m_processMsg = "Cannot correct Inventory (MA) - " + lastError; return DocAction.STATUS_Invalid; } - // Only Update Date Last Inventory if is a Physical Inventory - if (MDocType.DOCSUBTYPEINV_PhysicalInventory.equals(docSubTypeInv)) - { - MStorageOnHand storage = MStorageOnHand.get(getCtx(), line.getM_Locator_ID(), - line.getM_Product_ID(), ma.getM_AttributeSetInstance_ID(),ma.getDateMaterialPolicy(),get_TrxName()); - storage.setDateLastInventory(getMovementDate()); - if (!storage.save(get_TrxName())) - { - m_processMsg = "Storage not updated(2)"; - return DocAction.STATUS_Invalid; - } - } - String m_MovementType =null; if(QtyMA.negate().compareTo(Env.ZERO) > 0 ) m_MovementType = MTransaction.MOVEMENTTYPE_InventoryIn; @@ -629,27 +644,13 @@ public class MInventory extends X_M_Inventory implements DocAction line.getM_Locator_ID(), line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(), - qtyDiff,dateMPolicy,get_TrxName())) + qtyDiff,dateMPolicy,getMovementDate(),get_TrxName())) { String lastError = CLogger.retrieveErrorString(""); m_processMsg = "Cannot correct Inventory OnHand (MA) - " + lastError; return DocAction.STATUS_Invalid; } - // Only Update Date Last Inventory if is a Physical Inventory - if (MDocType.DOCSUBTYPEINV_PhysicalInventory.equals(docSubTypeInv)) - { - MStorageOnHand storage = MStorageOnHand.get(getCtx(), line.getM_Locator_ID(), - line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(),dateMPolicy, get_TrxName()); - - storage.setDateLastInventory(getMovementDate()); - if (!storage.save(get_TrxName())) - { - m_processMsg = "Storage not updated(2)"; - return DocAction.STATUS_Invalid; - } - } - String m_MovementType = null; if(qtyDiff.compareTo(Env.ZERO) > 0 ) m_MovementType = MTransaction.MOVEMENTTYPE_InventoryIn; diff --git a/org.adempiere.base/src/org/compiere/model/MStorageOnHand.java b/org.adempiere.base/src/org/compiere/model/MStorageOnHand.java index 21d58ddb2e..aa9cc47fd2 100644 --- a/org.adempiere.base/src/org/compiere/model/MStorageOnHand.java +++ b/org.adempiere.base/src/org/compiere/model/MStorageOnHand.java @@ -242,7 +242,55 @@ public class MStorageOnHand extends X_M_StorageOnHand list.toArray(retValue); return retValue; } // getAll - + + /** + * Get Storage Info + * @param ctx context + * @param M_Product_ID product + * @param M_Locator_ID locator + * @param M_AttributeSetInstance_ID instance + * @param dateMPolicy + * @param ignoreZeroQty + * @param trxName transaction + * @return existing or null + */ + public static MStorageOnHand[] getAll (Properties ctx, + int M_Product_ID, int M_Locator_ID, int M_AttributeSetInstance_ID, Timestamp dateMPolicy, boolean ignoreZeroQty, String trxName) + { + String sqlWhere = "M_Locator_ID=? AND M_Product_ID=? AND "; + + if (M_AttributeSetInstance_ID == 0) + sqlWhere += "(M_AttributeSetInstance_ID=? OR M_AttributeSetInstance_ID IS NULL)"; + else + sqlWhere += "M_AttributeSetInstance_ID=?"; + + if(ignoreZeroQty) + sqlWhere += " AND QtyOnHand<>0 "; + + if (dateMPolicy != null) + sqlWhere += " AND DateMaterialPolicy=trunc(cast(? as date))"; + + Query query = new Query(ctx, MStorageOnHand.Table_Name, sqlWhere, trxName); + + if (dateMPolicy != null) + query.setParameters(M_Locator_ID, M_Product_ID, M_AttributeSetInstance_ID, dateMPolicy); + else + query.setParameters(M_Locator_ID, M_Product_ID, M_AttributeSetInstance_ID); + + List list = query.list(); + + if (list == null || list.isEmpty()) { + if (s_log.isLoggable(Level.FINE)) s_log.fine("Not Found - M_Locator_ID=" + M_Locator_ID + + ", M_Product_ID=" + M_Product_ID + ", M_AttributeSetInstance_ID=" + M_AttributeSetInstance_ID); + } else { + if (s_log.isLoggable(Level.FINE)) s_log.fine("Found " + list.size() + " - M_Locator_ID=" + M_Locator_ID + + ", M_Product_ID=" + M_Product_ID + ", M_AttributeSetInstance_ID=" + M_AttributeSetInstance_ID); + } + + MStorageOnHand[] retValue = new MStorageOnHand[list.size()]; + list.toArray(retValue); + return retValue; + } // getAll /** * Get Storage Info for Product across warehouses @@ -739,7 +787,14 @@ public class MStorageOnHand extends X_M_StorageOnHand int M_Product_ID, int M_AttributeSetInstance_ID, BigDecimal diffQtyOnHand,Timestamp dateMPolicy, String trxName) { - return add(ctx, M_Locator_ID, M_Product_ID, M_AttributeSetInstance_ID, diffQtyOnHand, dateMPolicy, trxName); + return add(ctx, M_Locator_ID, M_Product_ID, M_AttributeSetInstance_ID, diffQtyOnHand, dateMPolicy, null, trxName); + } + + public static boolean add (Properties ctx, int M_Locator_ID, + int M_Product_ID, int M_AttributeSetInstance_ID, + BigDecimal diffQtyOnHand,Timestamp dateMPolicy, String trxName) + { + return add(ctx, M_Locator_ID, M_Product_ID, M_AttributeSetInstance_ID, diffQtyOnHand, dateMPolicy, null, trxName); } /** @@ -756,7 +811,7 @@ public class MStorageOnHand extends X_M_StorageOnHand */ public static boolean add (Properties ctx, int M_Locator_ID, int M_Product_ID, int M_AttributeSetInstance_ID, - BigDecimal diffQtyOnHand,Timestamp dateMPolicy, String trxName) + BigDecimal diffQtyOnHand,Timestamp dateMPolicy, Timestamp dateLastInventory, String trxName) { if (diffQtyOnHand == null || diffQtyOnHand.signum() == 0) return true; @@ -776,6 +831,8 @@ public class MStorageOnHand extends X_M_StorageOnHand return false; } + if(dateLastInventory != null) + storage.updateDateLastInventory(dateLastInventory); storage.addQtyOnHand(diffQtyOnHand); if (s_log.isLoggable(Level.FINE)) { StringBuilder diffText = new StringBuilder("(OnHand=").append(diffQtyOnHand).append(") -> ").append(storage.toString()); @@ -804,6 +861,19 @@ public class MStorageOnHand extends X_M_StorageOnHand } } + /** + * Update Date Last Inventory + * @param dateLastInv + */ + public void updateDateLastInventory(Timestamp dateLastInv) { + final String sql = "UPDATE M_StorageOnHand SET DateLastInventory=? " + + "WHERE M_Product_ID=? AND M_Locator_ID=? AND M_AttributeSetInstance_ID=? AND DateMaterialPolicy=?"; + DB.executeUpdateEx(sql, + new Object[] {dateLastInv, getM_Product_ID(), getM_Locator_ID(), getM_AttributeSetInstance_ID(), getDateMaterialPolicy()}, + get_TrxName()); + load(get_TrxName()); + } + /************************************************************************** * Get Location with highest Locator Priority and a sufficient OnHand Qty * @param M_Warehouse_ID warehouse