manual merge based in revision 8234

Issuing of stock should ignore storage with onhand <= 0 - ID: 2545020
http://sourceforge.net/tracker2/index.php?func=detail&aid=2545020&group_id=176962&atid=879332
Merge change from 8234 revision
http://adempiere.svn.sourceforge.net/viewvc/adempiere?view=rev&revision=8234

[ 2545020 ] Issuing of stock should ignore storage with onhand <= 0
- Only process storage with qtyOnHand > 0 when fulfilling for customer shipment, inventory movement and physical inventory.
- Remove remarked code.
- Fixed javadoc in MStorage, restore the original behaviour of getAllASI. Refactor getWarehouse method as the primary method use to
 retrieve storage records for customer shipment, inventory movement and physical inventory transaction.
- Fixed MStorage.getWarehouse to behave as stated in the javadoc, i.e always respect the locator priority.
- Remove the linePerASI logic in InOutGenerate so that the actual allocation of storage only happens in MInOut. Ensure proper reset
 of storage cache.
This commit is contained in:
vpj-cd 2009-02-01 00:16:34 +00:00
parent c129a03301
commit 6047a96148
5 changed files with 2178 additions and 2268 deletions

File diff suppressed because it is too large Load Diff

View File

@ -589,7 +589,6 @@ public class MInventory extends X_M_Inventory implements DocAction
// Check Line
boolean needSave = false;
BigDecimal qtyASI = Env.ZERO ;
// Attribute Set Instance
if (line.getM_AttributeSetInstance_ID() == 0)
{
@ -612,21 +611,13 @@ public class MInventory extends X_M_Inventory implements DocAction
else // Outgoing Trx
{
String MMPolicy = product.getMMPolicy();
MStorage[] storages = MStorage.getAllWithASI(getCtx(),
line.getM_Product_ID(), line.getM_Locator_ID(),
MClient.MMPOLICY_FiFo.equals(MMPolicy), get_TrxName());
MStorage[] storages = MStorage.getWarehouse(getCtx(), getM_Warehouse_ID(), line.getM_Product_ID(), 0,
null, MClient.MMPOLICY_FiFo.equals(MMPolicy), true, line.getM_Locator_ID(), get_TrxName());
BigDecimal qtyToDeliver = qtyDiff.negate();
for (MStorage storage: storages)
{
//cosume ASI Zero
if (storage.getM_AttributeSetInstance_ID() == 0)
{
qtyASI = qtyASI.add(storage.getQtyOnHand());
qtyToDeliver = qtyToDeliver.subtract(storage.getQtyOnHand());
continue;
}
{
if (storage.getQtyOnHand().compareTo(qtyToDeliver) >= 0)
{
MInventoryLineMA ma = new MInventoryLineMA (line,
@ -638,7 +629,7 @@ public class MInventory extends X_M_Inventory implements DocAction
}
qtyToDeliver = Env.ZERO;
log.fine( ma + ", QtyToDeliver=" + qtyToDeliver);
//return;
break;
}
else
{
@ -655,12 +646,12 @@ public class MInventory extends X_M_Inventory implements DocAction
}
// No AttributeSetInstance found for remainder
if (qtyToDeliver.signum() != 0 || qtyASI.signum() != 0)
if (qtyToDeliver.signum() != 0)
{
MInventoryLineMA ma = new MInventoryLineMA (line, 0 , qtyToDeliver.add(qtyASI));
MInventoryLineMA ma = new MInventoryLineMA (line, 0 , qtyToDeliver);
if (!ma.save())
;
throw new IllegalStateException("Error try create ASI Reservation");
log.fine("##: " + ma);
}
} // outgoing Trx
@ -820,7 +811,7 @@ public class MInventory extends X_M_Inventory implements DocAction
mas[j].getM_AttributeSetInstance_ID(),
mas[j].getMovementQty().negate());
if (!ma.save())
;
throw new IllegalStateException("Error try create ASI Reservation");
}
}
}

View File

@ -658,81 +658,19 @@ public class MMovement extends X_M_Movement implements DocAction
//{
// MMovementLine line = lines[i];
boolean needSave = false;
BigDecimal qtyASI = Env.ZERO ;
// Attribute Set Instance
if (line.getM_AttributeSetInstance_ID() == 0)
{
MProduct product = MProduct.get(getCtx(), line.getM_Product_ID());
String MMPolicy = product.getMMPolicy();
MStorage[] storages = MStorage.getAllWithASI(getCtx(),
line.getM_Product_ID(), line.getM_Locator_ID(),
MClient.MMPOLICY_FiFo.equals(MMPolicy), get_TrxName());
MStorage[] storages = MStorage.getWarehouse(getCtx(), 0, line.getM_Product_ID(), 0,
null, MClient.MMPOLICY_FiFo.equals(MMPolicy), true, line.getM_Locator_ID(), get_TrxName());
BigDecimal qtyToDeliver = line.getMovementQty();
/*for (int ii = 0; ii < storages.length; ii++)
{
MStorage storage = storages[ii];
if (ii == 0)
{
if (storage.getQtyOnHand().compareTo(qtyToDeliver) >= 0)
{
line.setM_AttributeSetInstance_ID(storage.getM_AttributeSetInstance_ID());
needSave = true;
log.config("Direct - " + line);
qtyToDeliver = Env.ZERO;
}
else
{
log.config("Split - " + line);
MMovementLineMA ma = new MMovementLineMA (line,
storage.getM_AttributeSetInstance_ID(),
storage.getQtyOnHand());
if (!ma.save())
;
qtyToDeliver = qtyToDeliver.subtract(storage.getQtyOnHand());
log.fine("#" + ii + ": " + ma + ", QtyToDeliver=" + qtyToDeliver);
}
}
else // create addl material allocation
{
MMovementLineMA ma = new MMovementLineMA (line,
storage.getM_AttributeSetInstance_ID(),
qtyToDeliver);
if (storage.getQtyOnHand().compareTo(qtyToDeliver) >= 0)
qtyToDeliver = Env.ZERO;
else
{
ma.setMovementQty(storage.getQtyOnHand());
qtyToDeliver = qtyToDeliver.subtract(storage.getQtyOnHand());
}
if (!ma.save())
;
log.fine("#" + ii + ": " + ma + ", QtyToDeliver=" + qtyToDeliver);
}
if (qtyToDeliver.signum() == 0)
break;
} // for all storages
// No AttributeSetInstance found for remainder
if (qtyToDeliver.signum() != 0)
{
MMovementLineMA ma = new MMovementLineMA (line,
0, qtyToDeliver);
if (!ma.save())
;
log.fine("##: " + ma);
}*/
for (MStorage storage: storages)
{
//consume ASI Zero
if (storage.getM_AttributeSetInstance_ID() == 0)
{
qtyASI = qtyASI.add(storage.getQtyOnHand());
qtyToDeliver = qtyToDeliver.subtract(storage.getQtyOnHand());
continue;
}
if (storage.getQtyOnHand().compareTo(qtyToDeliver) >= 0)
{
MMovementLineMA ma = new MMovementLineMA (line,
@ -744,7 +682,7 @@ public class MMovement extends X_M_Movement implements DocAction
}
qtyToDeliver = Env.ZERO;
log.fine( ma + ", QtyToDeliver=" + qtyToDeliver);
//return;
break;
}
else
{
@ -761,12 +699,12 @@ public class MMovement extends X_M_Movement implements DocAction
}
// No AttributeSetInstance found for remainder
if (qtyToDeliver.signum() != 0 || qtyASI.signum() != 0)
if (qtyToDeliver.signum() != 0)
{
MMovementLineMA ma = new MMovementLineMA (line, 0 , qtyToDeliver.add(qtyASI));
MMovementLineMA ma = new MMovementLineMA (line, 0 , qtyToDeliver);
if (!ma.save())
;
throw new IllegalStateException("Error try create ASI Reservation");
log.fine("##: " + ma);
}
} // attributeSetInstance

View File

@ -78,7 +78,6 @@ public class MStorage extends X_M_Storage
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
pstmt = null;
if (retValue == null)
s_log.fine("Not Found - M_Locator_ID=" + M_Locator_ID
+ ", M_Product_ID=" + M_Product_ID + ", M_AttributeSetInstance_ID=" + M_AttributeSetInstance_ID);
@ -89,7 +88,7 @@ public class MStorage extends X_M_Storage
} // get
/**
* Get all Storages for Product with ASI and QtyOnHand > 0
* Get all Storages for Product with ASI and QtyOnHand <> 0
* @param ctx context
* @param M_Product_ID product
* @param M_Locator_ID locator
@ -103,13 +102,9 @@ public class MStorage extends X_M_Storage
ArrayList<MStorage> list = new ArrayList<MStorage>();
String sql = "SELECT * FROM M_Storage "
+ "WHERE M_Product_ID=? AND M_Locator_ID=?"
// Remove for management rightly FIFO/LIFO now you can consume a layer with ASI ID = zero and Qty onhand in negative
// + " AND M_AttributeSetInstance_ID > 0"
// + " AND QtyOnHand > 0 "
+ " AND QtyOnHand <> 0 "
+ " AND M_AttributeSetInstance_ID > 0 "
+ " AND QtyOnHand <> 0 "
+ "ORDER BY M_AttributeSetInstance_ID";
if (!FiFo)
sql += " DESC";
PreparedStatement pstmt = null;
ResultSet rs = null;
try
@ -130,14 +125,13 @@ public class MStorage extends X_M_Storage
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
pstmt = null;
MStorage[] retValue = new MStorage[list.size()];
list.toArray(retValue);
return retValue;
} // getAllWithASI
/**
* Get all Storages for Product
* Get all Storages for Product where QtyOnHand <> 0
* @param ctx context
* @param M_Product_ID product
* @param M_Locator_ID locator
@ -172,7 +166,6 @@ public class MStorage extends X_M_Storage
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
pstmt = null;
MStorage[] retValue = new MStorage[list.size()];
list.toArray(retValue);
return retValue;
@ -227,23 +220,41 @@ public class MStorage extends X_M_Storage
* @param FiFo first in-first-out
* @param trxName transaction
* @return existing - ordered by location priority (desc) and/or guarantee date
*
* @deprecated
*/
public static MStorage[] getWarehouse (Properties ctx, int M_Warehouse_ID,
int M_Product_ID, int M_AttributeSetInstance_ID, int M_AttributeSet_ID,
boolean allAttributeInstances, Timestamp minGuaranteeDate,
boolean FiFo, String trxName)
{
if (M_Warehouse_ID == 0 || M_Product_ID == 0)
return getWarehouse(ctx, M_Warehouse_ID, M_Product_ID, M_AttributeSetInstance_ID,
minGuaranteeDate, FiFo, false, 0, trxName);
}
/**
* Get Storage Info for Warehouse or locator
* @param ctx context
* @param M_Warehouse_ID ignore if M_Locator_ID > 0
* @param M_Product_ID product
* @param M_AttributeSetInstance_ID instance id, 0 to retrieve all instance
* @param minGuaranteeDate optional minimum guarantee date if all attribute instances
* @param FiFo first in-first-out
* @param positiveOnly if true, only return storage records with qtyOnHand > 0
* @param M_Locator_ID optional locator id
* @param trxName transaction
* @return existing - ordered by location priority (desc) and/or guarantee date
*/
public static MStorage[] getWarehouse (Properties ctx, int M_Warehouse_ID,
int M_Product_ID, int M_AttributeSetInstance_ID, Timestamp minGuaranteeDate,
boolean FiFo, boolean positiveOnly, int M_Locator_ID, String trxName)
{
if ((M_Warehouse_ID == 0 && M_Locator_ID == 0) || M_Product_ID == 0)
return new MStorage[0];
if (M_AttributeSet_ID == 0)
allAttributeInstances = true;
else
{
MAttributeSet mas = MAttributeSet.get(ctx, M_AttributeSet_ID);
if (!mas.isInstanceAttribute())
allAttributeInstances = true;
}
boolean allAttributeInstances = false;
if (M_AttributeSetInstance_ID == 0)
allAttributeInstances = true;
ArrayList<MStorage> list = new ArrayList<MStorage>();
// Specific Attribute Set Instance
@ -251,11 +262,18 @@ public class MStorage extends X_M_Storage
+ "s.AD_Client_ID,s.AD_Org_ID,s.IsActive,s.Created,s.CreatedBy,s.Updated,s.UpdatedBy,"
+ "s.QtyOnHand,s.QtyReserved,s.QtyOrdered,s.DateLastInventory "
+ "FROM M_Storage s"
+ " INNER JOIN M_Locator l ON (l.M_Locator_ID=s.M_Locator_ID) "
+ "WHERE l.M_Warehouse_ID=?"
+ " AND s.M_Product_ID=?"
+ " AND COALESCE(s.M_AttributeSetInstance_ID,0)=? "
+ "ORDER BY l.PriorityNo DESC, M_AttributeSetInstance_ID";
+ " INNER JOIN M_Locator l ON (l.M_Locator_ID=s.M_Locator_ID) ";
if (M_Locator_ID > 0)
sql += "WHERE l.M_Locator_ID = ?";
else
sql += "WHERE l.M_Warehouse_ID=?";
sql += " AND s.M_Product_ID=?"
+ " AND COALESCE(s.M_AttributeSetInstance_ID,0)=? ";
if (positiveOnly)
{
sql += " AND s.QtyOnHand > 0 ";
}
sql += "ORDER BY l.PriorityNo DESC, M_AttributeSetInstance_ID";
if (!FiFo)
sql += " DESC";
// All Attribute Set Instances
@ -266,19 +284,31 @@ public class MStorage extends X_M_Storage
+ "s.QtyOnHand,s.QtyReserved,s.QtyOrdered,s.DateLastInventory "
+ "FROM M_Storage s"
+ " INNER JOIN M_Locator l ON (l.M_Locator_ID=s.M_Locator_ID)"
+ " LEFT OUTER JOIN M_AttributeSetInstance asi ON (s.M_AttributeSetInstance_ID=asi.M_AttributeSetInstance_ID) "
+ "WHERE l.M_Warehouse_ID=?"
+ " AND s.M_Product_ID=? ";
+ " LEFT OUTER JOIN M_AttributeSetInstance asi ON (s.M_AttributeSetInstance_ID=asi.M_AttributeSetInstance_ID) ";
if (M_Locator_ID > 0)
sql += "WHERE l.M_Locator_ID = ?";
else
sql += "WHERE l.M_Warehouse_ID=?";
sql += " AND s.M_Product_ID=? ";
if (minGuaranteeDate != null)
{
sql += "AND (asi.GuaranteeDate IS NULL OR asi.GuaranteeDate>?) "
+ "ORDER BY asi.GuaranteeDate, M_AttributeSetInstance_ID";
sql += "AND (asi.GuaranteeDate IS NULL OR asi.GuaranteeDate>?) ";
if (positiveOnly)
{
sql += " AND s.QtyOnHand > 0 ";
}
sql += "ORDER BY l.PriorityNo DESC, " +
"asi.GuaranteeDate, M_AttributeSetInstance_ID";
if (!FiFo)
sql += " DESC";
sql += ", l.PriorityNo DESC, s.QtyOnHand DESC";
sql += ", s.QtyOnHand DESC";
}
else
{
if (positiveOnly)
{
sql += " AND s.QtyOnHand > 0 ";
}
sql += "ORDER BY l.PriorityNo DESC, l.M_Locator_ID, s.M_AttributeSetInstance_ID";
if (!FiFo)
sql += " DESC";
@ -290,15 +320,22 @@ public class MStorage extends X_M_Storage
try
{
pstmt = DB.prepareStatement(sql, trxName);
pstmt.setInt(1, M_Warehouse_ID);
pstmt.setInt(1, M_Locator_ID > 0 ? M_Locator_ID : M_Warehouse_ID);
pstmt.setInt(2, M_Product_ID);
if (!allAttributeInstances)
{
pstmt.setInt(3, M_AttributeSetInstance_ID);
}
else if (minGuaranteeDate != null)
{
pstmt.setTimestamp(3, minGuaranteeDate);
}
rs = pstmt.executeQuery();
while (rs.next())
{
if(rs.getBigDecimal(11).signum() != 0)
list.add (new MStorage (ctx, rs, trxName));
}
}
catch (Exception e)
{

View File

@ -208,7 +208,7 @@ public class InOutGenerate extends SvrProcess
*/
private String generate (PreparedStatement pstmt)
{
MClient client = MClient.get(getCtx());
try
{
ResultSet rs = pstmt.executeQuery ();
@ -296,11 +296,10 @@ public class InOutGenerate extends SvrProcess
// Stored Product
String MMPolicy = product.getMMPolicy();
MStorage[] storages = getStorages(line.getM_Warehouse_ID(),
line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(),
product.getM_AttributeSet_ID(),
line.getM_AttributeSetInstance_ID()==0, minGuaranteeDate,
MClient.MMPOLICY_FiFo.equals(MMPolicy));
MStorage[] storages = getStorages(line.getM_Warehouse_ID(),
line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(),
minGuaranteeDate, MClient.MMPOLICY_FiFo.equals(MMPolicy));
for (int j = 0; j < storages.length; j++)
{
@ -382,9 +381,7 @@ public class InOutGenerate extends SvrProcess
String MMPolicy = product.getMMPolicy();
storages = getStorages(line.getM_Warehouse_ID(),
line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(),
product.getM_AttributeSet_ID(),
line.getM_AttributeSetInstance_ID()==0, minGuaranteeDate,
MClient.MMPOLICY_FiFo.equals(MMPolicy));
minGuaranteeDate, MClient.MMPOLICY_FiFo.equals(MMPolicy));
}
//
createLine (order, line, toDeliver, storages, false);
@ -460,15 +457,7 @@ public class InOutGenerate extends SvrProcess
return;
}
// Product
MProduct product = orderLine.getProduct();
boolean linePerASI = false;
if (product.getM_AttributeSet_ID() != 0)
{
MAttributeSet mas = MAttributeSet.get(getCtx(), product.getM_AttributeSet_ID());
linePerASI = mas.isInstanceAttribute();
}
// Inventory Lines
ArrayList<MInOutLine> list = new ArrayList<MInOutLine>();
BigDecimal toDeliver = qty;
@ -489,12 +478,12 @@ public class InOutGenerate extends SvrProcess
int M_Locator_ID = storage.getM_Locator_ID();
//
MInOutLine line = null;
if (!linePerASI) // find line with Locator
if (orderLine.getM_AttributeSetInstance_ID() == 0) // find line with Locator
{
for (int ll = 0; ll < list.size(); ll++)
{
MInOutLine test = (MInOutLine)list.get(ll);
if (test.getM_Locator_ID() == M_Locator_ID)
if (test.getM_Locator_ID() == M_Locator_ID && test.getM_AttributeSetInstance_ID() == 0)
{
line = test;
break;
@ -514,20 +503,34 @@ public class InOutGenerate extends SvrProcess
line.setQtyEntered(line.getMovementQty().multiply(orderLine.getQtyEntered())
.divide(orderLine.getQtyOrdered(), 12, BigDecimal.ROUND_HALF_UP));
line.setLine(m_line + orderLine.getLine());
if (linePerASI)
line.setM_AttributeSetInstance_ID(storage.getM_AttributeSetInstance_ID());
if (!line.save())
throw new IllegalStateException("Could not create Shipment Line");
log.fine("ToDeliver=" + qty + "/" + deliver + " - " + line);
toDeliver = toDeliver.subtract(deliver);
// Temp adjustment
// Temp adjustment, actual update happen in MInOut.completeIt
storage.setQtyOnHand(storage.getQtyOnHand().subtract(deliver));
//
if (toDeliver.signum() == 0)
break;
}
if (toDeliver.signum() != 0)
throw new IllegalStateException("Not All Delivered - Remainder=" + toDeliver);
{
if (!force)
{
throw new IllegalStateException("Not All Delivered - Remainder=" + toDeliver);
}
else
{
MInOutLine line = new MInOutLine (m_shipment);
line.setOrderLine(orderLine, 0, order.isSOTrx() ? toDeliver : Env.ZERO);
line.setQty(toDeliver);
if (!line.save())
throw new IllegalStateException("Could not create Shipment Line");
}
}
} // createLine
@ -536,20 +539,17 @@ public class InOutGenerate extends SvrProcess
* @param M_Warehouse_ID
* @param M_Product_ID
* @param M_AttributeSetInstance_ID
* @param M_AttributeSet_ID
* @param allAttributeInstances
* @param minGuaranteeDate
* @param FiFo
* @return storages
*/
private MStorage[] getStorages(int M_Warehouse_ID,
int M_Product_ID, int M_AttributeSetInstance_ID, int M_AttributeSet_ID,
boolean allAttributeInstances, Timestamp minGuaranteeDate,
boolean FiFo)
int M_Product_ID, int M_AttributeSetInstance_ID,
Timestamp minGuaranteeDate, boolean FiFo)
{
m_lastPP = new SParameter(M_Warehouse_ID,
M_Product_ID, M_AttributeSetInstance_ID, M_AttributeSet_ID,
allAttributeInstances, minGuaranteeDate, FiFo);
M_Product_ID, M_AttributeSetInstance_ID,
minGuaranteeDate, FiFo);
//
m_lastStorages = m_map.get(m_lastPP);
@ -557,8 +557,7 @@ public class InOutGenerate extends SvrProcess
{
m_lastStorages = MStorage.getWarehouse(getCtx(),
M_Warehouse_ID, M_Product_ID, M_AttributeSetInstance_ID,
M_AttributeSet_ID, allAttributeInstances, minGuaranteeDate,
FiFo, get_TrxName());
minGuaranteeDate, FiFo,true, 0, get_TrxName());
m_map.put(m_lastPP, m_lastStorages);
}
return m_lastStorages;
@ -579,9 +578,11 @@ public class InOutGenerate extends SvrProcess
//
addLog(m_shipment.getM_InOut_ID(), m_shipment.getMovementDate(), null, m_shipment.getDocumentNo());
m_created++;
//reset storage cache as MInOut.completeIt will update m_storage
m_map = new HashMap<SParameter,MStorage[]>();
if (m_lastPP != null && m_lastStorages != null)
m_map.put(m_lastPP, m_lastStorages);
m_lastPP = null;
m_lastStorages = null;
}
m_shipment = null;
m_line = 0;
@ -597,21 +598,16 @@ public class InOutGenerate extends SvrProcess
* @param p_Warehouse_ID warehouse
* @param p_Product_ID
* @param p_AttributeSetInstance_ID
* @param p_AttributeSet_ID
* @param p_allAttributeInstances
* @param p_minGuaranteeDate
* @param p_FiFo
*/
protected SParameter (int p_Warehouse_ID,
int p_Product_ID, int p_AttributeSetInstance_ID, int p_AttributeSet_ID,
boolean p_allAttributeInstances, Timestamp p_minGuaranteeDate,
boolean p_FiFo)
int p_Product_ID, int p_AttributeSetInstance_ID,
Timestamp p_minGuaranteeDate,boolean p_FiFo)
{
this.M_Warehouse_ID = p_Warehouse_ID;
this.M_Product_ID = p_Product_ID;
this.M_AttributeSetInstance_ID = p_AttributeSetInstance_ID;
this.M_AttributeSet_ID = p_AttributeSet_ID;
this.allAttributeInstances = p_allAttributeInstances;
this.minGuaranteeDate = p_minGuaranteeDate;
this.FiFo = p_FiFo;
}