📄 minventory.java
字号:
{
MInventoryLineMA mas[] = MInventoryLineMA.get(getCtx(),
line.getM_InventoryLine_ID(), get_TrxName());
for (int j = 0; j < mas.length; j++)
{
MInventoryLineMA ma = mas[j];
// Storage
MStorage storage = MStorage.get(getCtx(), line.getM_Locator_ID(),
line.getM_Product_ID(), ma.getM_AttributeSetInstance_ID(), get_TrxName());
if (storage == null)
storage = MStorage.getCreate(getCtx(), line.getM_Locator_ID(),
line.getM_Product_ID(), ma.getM_AttributeSetInstance_ID(), get_TrxName());
//
BigDecimal maxDiff = qtyDiff;
if (maxDiff.signum() < 0
&& ma.getMovementQty().compareTo(maxDiff.negate()) < 0)
maxDiff = ma.getMovementQty().negate();
BigDecimal qtyNew = ma.getMovementQty().add(maxDiff); // Storage+Diff
log.fine("MA Qty=" + ma.getMovementQty()
+ ",Diff=" + qtyDiff + "|" + maxDiff
+ " - OnHand=" + storage.getQtyOnHand() + "->" + qtyNew
+ " {" + ma.getM_AttributeSetInstance_ID() + "}");
//
storage.setQtyOnHand(qtyNew);
storage.setDateLastInventory(getMovementDate());
if (!storage.save(get_TrxName()))
{
m_processMsg = "Storage not updated (MA)";
return DocAction.STATUS_Invalid;
}
log.fine(storage.toString());
// Transaction
trx = new MTransaction (getCtx(), MTransaction.MOVEMENTTYPE_InventoryIn,
line.getM_Locator_ID(), line.getM_Product_ID(), ma.getM_AttributeSetInstance_ID(),
maxDiff, getMovementDate(), get_TrxName());
trx.setM_InventoryLine_ID(line.getM_InventoryLine_ID());
if (!trx.save())
{
m_processMsg = "Transaction not inserted (MA)";
return DocAction.STATUS_Invalid;
}
//
qtyDiff = qtyDiff.subtract(maxDiff);
if (qtyDiff.signum() == 0)
break;
}
} // negative qty
}
// Fallback
if (trx == null)
{
// Storage
MStorage storage = MStorage.get(getCtx(), line.getM_Locator_ID(),
line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(), get_TrxName());
if (storage == null)
storage = MStorage.getCreate(getCtx(), line.getM_Locator_ID(),
line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(), get_TrxName());
//
BigDecimal qtyDiff = line.getQtyInternalUse().negate();
if (Env.ZERO.compareTo(qtyDiff) == 0)
qtyDiff = line.getQtyCount().subtract(line.getQtyBook());
BigDecimal qtyNew = storage.getQtyOnHand().add(qtyDiff);
log.fine("Count=" + line.getQtyCount()
+ ",Book=" + line.getQtyBook() + ", Difference=" + qtyDiff
+ " - OnHand=" + storage.getQtyOnHand() + "->" + qtyNew);
//
storage.setQtyOnHand(qtyNew);
storage.setDateLastInventory(getMovementDate());
if (!storage.save(get_TrxName()))
{
m_processMsg = "Storage not updated(2)";
return DocAction.STATUS_Invalid;
}
log.fine(storage.toString());
// Transaction
trx = new MTransaction (getCtx(), MTransaction.MOVEMENTTYPE_InventoryIn,
line.getM_Locator_ID(), line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(),
qtyDiff, getMovementDate(), get_TrxName());
trx.setM_InventoryLine_ID(line.getM_InventoryLine_ID());
if (!trx.save())
{
m_processMsg = "Transaction not inserted(2)";
return DocAction.STATUS_Invalid;
}
} // Fallback
} // for all lines
// User Validation
String valid = ModelValidationEngine.get().fireDocValidate(this, ModelValidator.TIMING_AFTER_COMPLETE);
if (valid != null)
{
m_processMsg = valid;
return DocAction.STATUS_Invalid;
}
//
setProcessed(true);
setDocAction(DOCACTION_Close);
return DocAction.STATUS_Completed;
} // completeIt
/**
* Check Material Policy.
* (NOT USED)
* Sets line ASI
*/
private void checkMaterialPolicy()
{
int no = MInventoryLineMA.deleteInventoryMA(getM_Inventory_ID(), get_TrxName());
if (no > 0)
log.config("Delete old #" + no);
MInventoryLine[] lines = getLines(false);
// Incoming Trx
MClient client = MClient.get(getCtx());
// Check Lines
for (int i = 0; i < lines.length; i++)
{
MInventoryLine line = lines[i];
boolean needSave = false;
// Attribute Set Instance
if (line.getM_AttributeSetInstance_ID() == 0)
{
MProduct product = MProduct.get(getCtx(), line.getM_Product_ID());
BigDecimal qtyDiff = line.getQtyInternalUse().negate();
if (Env.ZERO.compareTo(qtyDiff) == 0)
qtyDiff = line.getQtyCount().subtract(line.getQtyBook());
log.fine("Count=" + line.getQtyCount()
+ ",Book=" + line.getQtyBook() + ", Difference=" + qtyDiff);
if (qtyDiff.signum() > 0) // In
{
MAttributeSetInstance asi = new MAttributeSetInstance(getCtx(), 0, get_TrxName());
asi.setClientOrg(getAD_Client_ID(), 0);
asi.setM_AttributeSet_ID(product.getM_AttributeSet_ID());
if (asi.save())
{
line.setM_AttributeSetInstance_ID(asi.getM_AttributeSetInstance_ID());
needSave = true;
}
}
else // Outgoing Trx
{
MProductCategory pc = MProductCategory.get(getCtx(), product.getM_Product_Category_ID());
String MMPolicy = pc.getMMPolicy();
if (MMPolicy == null || MMPolicy.length() == 0)
MMPolicy = client.getMMPolicy();
//
MStorage[] storages = MStorage.getAllWithASI(getCtx(),
line.getM_Product_ID(), line.getM_Locator_ID(),
MClient.MMPOLICY_FiFo.equals(MMPolicy), get_TrxName());
BigDecimal qtyToDeliver = qtyDiff.negate();
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);
MInventoryLineMA ma = new MInventoryLineMA (line,
storage.getM_AttributeSetInstance_ID(),
storage.getQtyOnHand().negate());
if (!ma.save())
;
qtyToDeliver = qtyToDeliver.subtract(storage.getQtyOnHand());
log.fine("#" + ii + ": " + ma + ", QtyToDeliver=" + qtyToDeliver);
}
}
else // create addl material allocation
{
MInventoryLineMA ma = new MInventoryLineMA (line,
storage.getM_AttributeSetInstance_ID(),
qtyToDeliver.negate());
if (storage.getQtyOnHand().compareTo(qtyToDeliver) >= 0)
qtyToDeliver = Env.ZERO;
else
{
ma.setMovementQty(storage.getQtyOnHand().negate());
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)
{
MInventoryLineMA ma = new MInventoryLineMA (line,
0, qtyToDeliver.negate());
if (!ma.save())
;
log.fine("##: " + ma);
}
} // outgoing Trx
} // attributeSetInstance
if (needSave && !line.save())
log.severe("NOT saved " + line);
} // for all lines
} // checkMaterialPolicy
/**
* Void Document.
* @return false
*/
public boolean voidIt()
{
log.info(toString());
if (DOCSTATUS_Closed.equals(getDocStatus())
|| DOCSTATUS_Reversed.equals(getDocStatus())
|| DOCSTATUS_Voided.equals(getDocStatus()))
{
m_processMsg = "Document Closed: " + getDocStatus();
return false;
}
// Not Processed
if (DOCSTATUS_Drafted.equals(getDocStatus())
|| DOCSTATUS_Invalid.equals(getDocStatus())
|| DOCSTATUS_InProgress.equals(getDocStatus())
|| DOCSTATUS_Approved.equals(getDocStatus())
|| DOCSTATUS_NotApproved.equals(getDocStatus()) )
{
// Set lines to 0
MInventoryLine[] lines = getLines(false);
for (int i = 0; i < lines.length; i++)
{
MInventoryLine line = lines[i];
BigDecimal oldCount = line.getQtyCount();
BigDecimal oldInternal = line.getQtyInternalUse();
if (oldCount.compareTo(line.getQtyBook()) != 0
|| oldInternal.signum() != 0)
{
line.setQtyInternalUse(Env.ZERO);
line.setQtyCount(line.getQtyBook());
line.addDescription("Void (" + oldCount + "/" + oldInternal + ")");
line.save(get_TrxName());
}
}
}
else
{
return reverseCorrectIt();
}
setProcessed(true);
setDocAction(DOCACTION_None);
return true;
} // voidIt
/**
* Close Document.
* @return true if success
*/
public boolean closeIt()
{
log.info(toString());
setDocAction(DOCACTION_None);
return true;
} // closeIt
/**
* Reverse Correction
* @return false
*/
public boolean reverseCorrectIt()
{
log.info(toString());
MDocType dt = MDocType.get(getCtx(), getC_DocType_ID());
if (!MPeriod.isOpen(getCtx(), getMovementDate(), dt.getDocBaseType()))
{
m_processMsg = "@PeriodClosed@";
return false;
}
// Deep Copy
MInventory reversal = new MInventory(getCtx(), 0, get_TrxName());
copyValues(this, reversal, getAD_Client_ID(), getAD_Org_ID());
reversal.setDocStatus(DOCSTATUS_Drafted);
reversal.setDocAction(DOCACTION_Complete);
reversal.setIsApproved (false);
reversal.setPosted(false);
reversal.setProcessed(false);
reversal.addDescription("{->" + getDocumentNo() + ")");
if (!reversal.save())
{
m_processMsg = "Could not create Inventory Reversal";
return false;
}
// Reverse Line Qty
MInventoryLine[] oLines = getLines(true);
for (int i = 0; i < oLines.length; i++)
{
MInventoryLine oLine = oLines[i];
MInventoryLine rLine = new MInventoryLine(getCtx(), 0, get_TrxName());
copyValues(oLine, rLine, oLine.getAD_Client_ID(), oLine.getAD_Org_ID());
rLine.setM_Inventory_ID(reversal.getM_Inventory_ID());
rLine.setParent(reversal);
//
rLine.setQtyBook (oLine.getQtyCount()); // switch
rLine.setQtyCount (oLine.getQtyBook());
rLine.setQtyInternalUse (oLine.getQtyInternalUse().negate());
if (!rLine.save())
{
m_processMsg = "Could not create Inventory Reversal Line";
return false;
}
}
//
if (!reversal.processIt(DocAction.ACTION_Complete))
{
m_processMsg = "Reversal ERROR: " + reversal.getProcessMsg();
return false;
}
reversal.closeIt();
reversal.setDocStatus(DOCSTATUS_Reversed);
reversal.setDocAction(DOCACTION_None);
reversal.save();
m_processMsg = reversal.getDocumentNo();
// Update Reversed (this)
addDescription("(" + reversal.getDocumentNo() + "<-)");
setProcessed(true);
setDocStatus(DOCSTATUS_Reversed); // may come from void
setDocAction(DOCACTION_None);
return true;
} // reverseCorrectionIt
/**
* Reverse Accrual
* @return false
*/
public boolean reverseAccrualIt()
{
log.info(toString());
return false;
} // reverseAccrualIt
/**
* Re-activate
* @return false
*/
public boolean reActivateIt()
{
log.info(toString());
return false;
} // reActivateIt
/*************************************************************************
* Get Summary
* @return Summary of Document
*/
public String getSummary()
{
StringBuffer sb = new StringBuffer();
sb.append(getDocumentNo());
// : Total Lines = 123.00 (#1)
sb.append(": ")
.append(Msg.translate(getCtx(),"ApprovalAmt")).append("=").append(getApprovalAmt())
.append(" (#").append(getLines(false).length).append(")");
// - Description
if (getDescription() != null && getDescription().length() > 0)
sb.append(" - ").append(getDescription());
return sb.toString();
} // getSummary
/**
* Get Process Message
* @return clear text error message
*/
public String getProcessMsg()
{
return m_processMsg;
} // getProcessMsg
/**
* Get Document Owner (Responsible)
* @return AD_User_ID
*/
public int getDoc_User_ID()
{
return getUpdatedBy();
} // getDoc_User_ID
/**
* Get Document Currency
* @return C_Currency_ID
*/
public int getC_Currency_ID()
{
// MPriceList pl = MPriceList.get(getCtx(), getM_PriceList_ID());
// return pl.getC_Currency_ID();
return 0;
} // getC_Currency_ID
} // MInventory
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -