Search in sources :

Example 16 with MTransaction

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;
}
Also used : MTransaction(org.compiere.model.MTransaction) ArrayList(java.util.ArrayList) MCostDetail(org.compiere.model.MCostDetail) BigDecimal(java.math.BigDecimal)

Aggregations

MTransaction (org.compiere.model.MTransaction)16 MCostElement (org.compiere.model.MCostElement)6 MCostType (org.compiere.model.MCostType)6 MInOutLine (org.compiere.model.MInOutLine)6 BigDecimal (java.math.BigDecimal)5 ArrayList (java.util.ArrayList)4 MAcctSchema (org.compiere.model.MAcctSchema)4 MCostDetail (org.compiere.model.MCostDetail)4 Query (org.compiere.model.Query)4 MCost (org.compiere.model.MCost)3 MLandedCostAllocation (org.compiere.model.MLandedCostAllocation)3 MMatchPO (org.compiere.model.MMatchPO)3 List (java.util.List)2 Properties (java.util.Properties)2 MInventoryLine (org.compiere.model.MInventoryLine)2 MMatchInv (org.compiere.model.MMatchInv)2 MMovementLine (org.compiere.model.MMovementLine)2 MProduct (org.compiere.model.MProduct)2 MPPCostCollector (org.eevolution.model.MPPCostCollector)2 SQLException (java.sql.SQLException)1