use of org.compiere.model.MTransaction in project adempiere by adempiere.
the class Doc_Movement method createFacts.
// getBalance
/**
* Create Facts (the accounting logic) for
* MMM.
* <pre>
* Movement
* Inventory DR CR
* InventoryTo DR CR
* </pre>
* @param as account schema
* @return Fact
*/
public ArrayList<Fact> createFacts(MAcctSchema as) {
// create Fact Header
Fact fact = new Fact(this, as, Fact.POST_Actual);
setC_Currency_ID(as.getC_Currency_ID());
// Line pointers
FactLine dr = null;
FactLine cr = null;
for (int i = 0; i < p_lines.length; i++) {
DocLine line = p_lines[i];
BigDecimal costs = Env.ZERO;
for (MCostDetail cost : line.getCostDetail(as, false)) {
if (!MCostDetail.existsCost(cost))
continue;
//get costing method for product
String description = cost.getM_CostElement().getName() + " " + cost.getM_CostType().getName();
costs = MCostDetail.getTotalCost(cost, as);
MTransaction trx = new MTransaction(getCtx(), cost.getM_Transaction_ID(), getTrxName());
if (MTransaction.MOVEMENTTYPE_MovementFrom.equals(trx.getMovementType())) {
// ** Inventory DR CR
dr = fact.createLine(line, line.getAccount(ProductCost.ACCTTYPE_P_Asset, as), as.getC_Currency_ID(), // from (-) CR
costs.negate());
if (dr == null)
continue;
dr.setM_Locator_ID(line.getM_Locator_ID());
dr.addDescription(description);
dr.setM_Product_ID(cost.getM_Product_ID());
// outgoing
dr.setQty(cost.getQty().negate());
if (m_DocStatus.equals(MMovement.DOCSTATUS_Reversed) && m_Reversal_ID != 0 && line.getReversalLine_ID() != 0) {
// Set AmtAcctDr from Original Movement
if (!dr.updateReverseLine(MMovement.Table_ID, m_Reversal_ID, line.getReversalLine_ID(), cost.getQty().negate(), Env.ONE.negate())) {
p_Error = "Original Inventory Move not posted yet";
return null;
}
}
continue;
}
if (MTransaction.MOVEMENTTYPE_MovementTo.equals(trx.getMovementType())) {
// ** InventoryTo DR CR
cr = fact.createLine(line, line.getAccount(ProductCost.ACCTTYPE_P_Asset, as), as.getC_Currency_ID(), // to (+) DR
costs);
if (cr == null)
continue;
cr.setM_Locator_ID(line.getM_LocatorTo_ID());
cr.addDescription(description);
cr.setM_Product_ID(cost.getM_Product_ID());
cr.setQty(cost.getQty());
if (m_DocStatus.equals(MMovement.DOCSTATUS_Reversed) && m_Reversal_ID != 0 && line.getReversalLine_ID() != 0) {
// Set AmtAcctCr from Original Movement
if (!cr.updateReverseLine(MMovement.Table_ID, m_Reversal_ID, line.getReversalLine_ID(), cost.getQty(), Env.ONE.negate())) {
p_Error = "Original Inventory Move not posted yet";
return null;
}
//get original cost
costs = cr.getAcctBalance();
}
}
}
}
//
ArrayList<Fact> facts = new ArrayList<Fact>();
facts.add(fact);
return facts;
}
Aggregations