From 0731626ca0fc15565511c7e05c90f2f99427260d Mon Sep 17 00:00:00 2001 From: "Owner.Owner-PC" Date: Thu, 25 Oct 2012 16:42:38 +0700 Subject: [PATCH] fix qtyonhand() and add qtyavailable() --- .../org/compiere/model/MStorageOnHand.java | 20 ++-- .../compiere/model/MStorageReservation.java | 112 ++++++------------ 2 files changed, 48 insertions(+), 84 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/model/MStorageOnHand.java b/org.adempiere.base/src/org/compiere/model/MStorageOnHand.java index 4957173dd9..e530a5bebe 100644 --- a/org.adempiere.base/src/org/compiere/model/MStorageOnHand.java +++ b/org.adempiere.base/src/org/compiere/model/MStorageOnHand.java @@ -587,7 +587,10 @@ public class MStorageOnHand extends X_M_StorageOnHand return m_M_Warehouse_ID; } // getM_Warehouse_ID + /** + * + * * Before Save * @param newRecord new * @param success success @@ -637,16 +640,19 @@ public class MStorageOnHand extends X_M_StorageOnHand * @return */ public static BigDecimal getQtyOnHand(int M_Product_ID, int M_Warehouse_ID, int M_AttributeSetInstance_ID, String trxName){ - + ArrayList params = new ArrayList(); StringBuffer sql = new StringBuffer(); sql.append(" SELECT SUM(QtyOnHand) FROM M_StorageOnHand oh") - .append(" WHERE M_Product_ID=? AND M_AttributeSetInstance_ID=?") - .append(" AND EXISTS(SELECT 1 FROM M_Locator loc WHERE oh.M_Locator_ID=loc.M_Locator_ID AND loc.M_Warehouse_ID=?)"); + .append(" WHERE oh.M_Product_ID=?") + .append(" AND EXISTS(SELECT 1 FROM M_Locator loc WHERE oh.M_Locator_ID=loc.M_Locator_ID AND loc.M_Warehouse_ID=?)"); + + params.add(M_Product_ID,M_Warehouse_ID); - ArrayList params = new ArrayList(); - params.add(M_Product_ID); - params.add(M_AttributeSetInstance_ID); - params.add(M_Warehouse_ID); + // With ASI + if (M_AttributeSetInstance_ID != 0) { + sql.append(" AND M_AttributeSetInstance_ID=?"); + params.add(M_AttributeSetInstance_ID); + } BigDecimal qty = DB.getSQLValueBD(trxName, sql.toString(), params); if(qty==null) diff --git a/org.adempiere.base/src/org/compiere/model/MStorageReservation.java b/org.adempiere.base/src/org/compiere/model/MStorageReservation.java index df52be023f..c8306f6448 100644 --- a/org.adempiere.base/src/org/compiere/model/MStorageReservation.java +++ b/org.adempiere.base/src/org/compiere/model/MStorageReservation.java @@ -74,6 +74,37 @@ public class MStorageReservation extends X_M_StorageReservation { } // getOfProduct + + + /** + * Get Quantity Reserved of Warehouse + * @param M_Product_ID + * @param M_Warehouse_ID + * @param M_AttributeSetInstance_ID + * @param trxName + * @return + */ + public static BigDecimal getQtyReserved(int M_Product_ID, int M_Warehouse_ID, int M_AttributeSetInstance_ID, String trxName){ + ArrayList params = new ArrayList(); + StringBuffer sql = new StringBuffer(); + sql.append(" SELECT SUM(QtyReserved) FROM M_StorageReservation oh") + .append(" WHERE oh.M_Product_ID=? AND oh.M_Warehouse_ID=?"); + + params.add(M_Product_ID,M_Warehouse_ID); + + // With ASI + if (M_AttributeSetInstance_ID != 0) { + sql.append(" AND M_AttributeSetInstance_ID=?"); + params.add(M_AttributeSetInstance_ID); + } + + BigDecimal qty = DB.getSQLValueBD(trxName, sql.toString(), params); + if(qty==null) + qty = Env.ZERO; + + return qty; + } + /** * Get Available Qty. * The call is accurate only if there is a storage record @@ -83,87 +114,14 @@ public class MStorageReservation extends X_M_StorageReservation { * @param M_AttributeSetInstance_ID masi * @param trxName transaction * @return qty available (QtyOnHand-QtyReserved) or null - * @deprecated Since 331b. Please use {@link #getQtyAvailable(int, int, int, int, String)}. */ public static BigDecimal getQtyAvailable (int M_Warehouse_ID, int M_Product_ID, int M_AttributeSetInstance_ID, String trxName) { - return getQtyAvailable(M_Warehouse_ID, 0, M_Product_ID, M_AttributeSetInstance_ID, trxName); + BigDecimal qtyOnHand = MStorageOnHand.getQtyOnHand(M_Product_ID, M_Warehouse_ID, M_AttributeSetInstance_ID, trxName); + BigDecimal qtyReserved = getQtyReserved(M_Product_ID, M_Warehouse_ID, M_AttributeSetInstance_ID, trxName); + BigDecimal retValue = qtyOnHand.subtract(qtyReserved); + return retValue; } - /** - * Get Warehouse/Locator Available Qty. - * The call is accurate only if there is a storage record - * and assumes that the product is stocked - * @param M_Warehouse_ID wh (if the M_Locator_ID!=0 then M_Warehouse_ID is ignored) - * @param M_Locator_ID locator (if 0, the whole warehouse will be evaluated) - * @param M_Product_ID product - * @param M_AttributeSetInstance_ID masi - * @param trxName transaction - * @return qty available (QtyOnHand-QtyReserved) or null if error - */ - public static BigDecimal getQtyAvailable (int M_Warehouse_ID, int M_Locator_ID, - int M_Product_ID, int M_AttributeSetInstance_ID, String trxName) - { - ArrayList params = new ArrayList(); - StringBuffer sql = new StringBuffer(""); - - if (M_Locator_ID != 0) { - MLocator locator = new MLocator(Env.getCtx(), M_Locator_ID, trxName); - MWarehouse wh = new MWarehouse(Env.getCtx(), locator.getM_Warehouse_ID(), trxName); - if (wh.get_ValueAsInt("M_ReserveLocator_ID") != M_Locator_ID) { - sql.append("SELECT COALESCE(SUM(s.QtyOnHand),0)") - .append(" FROM M_StorageOnHand s") - .append(" WHERE s.M_Product_ID=? AND s.M_Locator_ID=?"); - - params.add(M_Product_ID); - params.add(M_Locator_ID); - - // With ASI - if (M_AttributeSetInstance_ID != 0) { - sql.append(" AND s.M_AttributeSetInstance_ID=?"); - params.add(M_AttributeSetInstance_ID); - } - } - else { - sql.append("SELECT COALESCE(SUM(s.QtyOnHand),0)-COALESCE(SUM(r.Qty),0)") - .append(" FROM M_StorageOnHand s") - .append(" LEFT JOIN M_StorageReservation r ON s.M_Product_ID=r.M_Product_ID") - .append(" WHERE s.M_Product_ID=? AND AND s.M_Locator_ID=?") - .append(" AND EXISTS (SELECT 1 FROM M_Locator l JOIN M_Warehouse w ON w.M_ReserveLocator_ID=l.M_Locator_ID") - .append(" WHERE s.M_Locator_ID=l.M_Locator_ID AND l.M_Warehouse_ID=r.M_Warehouse_ID"); - - params.add(M_Product_ID, M_Locator_ID); - - // With ASI - if (M_AttributeSetInstance_ID != 0) { - sql.append(" AND s.M_AttributeSetInstance_ID=? AND r.M_AttributeSetInstance_ID=?"); - params.add(M_AttributeSetInstance_ID, M_AttributeSetInstance_ID); - } - } - - } else { - sql.append("SELECT COALESCE(SUM(s.QtyOnHand),0)-COALESCE(SUM(r.Qty),0)") - .append(" FROM M_StorageOnHand s") - .append(" LEFT JOIN M_StorageReservation r ON s.M_Product_ID=r.M_Product_ID") - .append(" WHERE s.M_Product_ID=?") - .append(" AND EXISTS (SELECT 1 FROM M_Locator l") - .append(" WHERE s.M_Locator_ID=l.M_Locator_ID AND l.M_Warehouse_ID=r.M_Warehouse_ID") - .append(" AND l.M_Warehouse_ID=?)"); - - params.add(M_Product_ID, M_Warehouse_ID); - // With ASI - if (M_AttributeSetInstance_ID != 0) { - sql.append(" AND s.M_AttributeSetInstance_ID=? AND r.M_AttributeSetInstance_ID=?"); - params.add(M_AttributeSetInstance_ID, M_AttributeSetInstance_ID); - } - } - - // - BigDecimal retValue = DB.getSQLValueBD(trxName, sql.toString(), params); - if (CLogMgt.isLevelFine()) - s_log.fine("M_Warehouse_ID=" + M_Warehouse_ID + ", M_Locator_ID=" + M_Locator_ID - + ",M_Product_ID=" + M_Product_ID + " = " + retValue); - return retValue; - } // getQtyAvailable }