Search in sources :

Example 11 with MCostDetail

use of org.compiere.model.MCostDetail in project adempiere by adempiere.

the class Doc_Inventory method createFacts.

//  getBalance
/**
	 *  Create Facts (the accounting logic) for
	 *  MMI.
	 *  <pre>
	 *  Inventory
	 *      Inventory       DR      CR
	 *      InventoryDiff   DR      CR   (or Charge)
	 *  </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;
    BigDecimal total = Env.ZERO;
    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();
            if (line.getQty().signum() < 0)
                costs = MCostDetail.getTotalCost(cost, as).negate();
            else
                costs = MCostDetail.getTotalCost(cost, as);
            total = total.add(costs);
            //  Inventory       DR      CR
            dr = fact.createLine(line, line.getAccount(ProductCost.ACCTTYPE_P_Asset, as), as.getC_Currency_ID(), costs);
            //  may be zero difference - no line created.
            if (dr == null)
                continue;
            dr.setM_Locator_ID(line.getM_Locator_ID());
            dr.addDescription(description);
            dr.setM_Product_ID(cost.getM_Product_ID());
            dr.setQty(cost.getQty());
            if (m_DocStatus.equals(MInventory.DOCSTATUS_Reversed) && m_Reversal_ID != 0 && line.getReversalLine_ID() != 0) {
                //	Set AmtAcctDr from Original Phys.Inventory
                if (!dr.updateReverseLine(MInventory.Table_ID, m_Reversal_ID, line.getReversalLine_ID(), cost.getQty(), Env.ONE.negate())) {
                    p_Error = "Original Physical Inventory not posted yet";
                    return null;
                }
            }
            //  InventoryDiff   DR      CR
            //	or Charge
            MAccount invDiff = null;
            if (m_DocStatus.equals(MInventory.DOCSTATUS_Reversed) && m_Reversal_ID != 0 && line.getReversalLine_ID() != 0 && line.getC_Charge_ID() != 0) {
                invDiff = line.getChargeAccount(as, costs);
            } else {
                invDiff = line.getChargeAccount(as, costs.negate());
            }
            if (invDiff == null)
                invDiff = getAccount(Doc.ACCTTYPE_InvDifferences, as);
            cr = fact.createLine(line, invDiff, as.getC_Currency_ID(), costs.negate());
            if (cr == null)
                continue;
            cr.setM_Locator_ID(line.getM_Locator_ID());
            cr.setM_Product_ID(cost.getM_Product_ID());
            cr.setQty(cost.getQty().negate());
            if (//	explicit overwrite for charge
            line.getC_Charge_ID() != 0)
                cr.setAD_Org_ID(line.getAD_Org_ID());
            if (m_DocStatus.equals(MInventory.DOCSTATUS_Reversed) && m_Reversal_ID != 0 && line.getReversalLine_ID() != 0) {
                //	Set AmtAcctCr from Original Phys.Inventory
                if (!cr.updateReverseLine(MInventory.Table_ID, m_Reversal_ID, line.getReversalLine_ID(), cost.getQty().negate(), Env.ONE.negate())) {
                    p_Error = "Original Physical Inventory not posted yet";
                    return null;
                }
                //get original cost
                costs = cr.getAcctBalance();
            }
        /*if (total == null || total.signum() == 0)
				{
					p_Error = "No Costs for " + line.getProduct().getName();
					return null;
				}*/
        }
    }
    //
    ArrayList<Fact> facts = new ArrayList<Fact>();
    facts.add(fact);
    return facts;
}
Also used : MAccount(org.compiere.model.MAccount) ArrayList(java.util.ArrayList) MCostDetail(org.compiere.model.MCostDetail) BigDecimal(java.math.BigDecimal)

Example 12 with MCostDetail

use of org.compiere.model.MCostDetail in project adempiere by adempiere.

the class Doc_Production method createFacts.

//  getBalance
/**
	 *  Create Facts (the accounting logic) for
	 *  MMP.
	 *  <pre>
	 *  Production
	 *      Inventory       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 pointer
    FactLine factLine = null;
    BigDecimal total = Env.ZERO;
    DocLine parentProductionLine = null;
    for (DocLine line : p_lines) {
        MProduct product = line.getProduct();
        if (line.isProductionBOM())
            parentProductionLine = line;
        if (X_M_Product.PRODUCTTYPE_Item.equals(product.getProductType())) {
            BigDecimal totalCosts = Env.ZERO;
            BigDecimal qty = line.getQty();
            BigDecimal costTransaction = Env.ZERO;
            for (MCostDetail cost : line.getCostDetail(as, false)) {
                if (!MCostDetail.existsCost(cost))
                    continue;
                costTransaction = MCostDetail.getTotalCost(cost, as);
                totalCosts = totalCosts.add(costTransaction);
            }
            if (qty.signum() < 0)
                totalCosts = totalCosts.negate();
            total = total.add(totalCosts);
            factLine = fact.createLine(line, line.getAccount(ProductCost.ACCTTYPE_P_Asset, as), line.getAccount(ProductCost.ACCTTYPE_P_Asset, as), as.getC_Currency_ID(), totalCosts);
            factLine.setM_Product_ID(line.getM_Product_ID());
            factLine.setM_Locator_ID(line.getM_LocatorTo_ID());
            factLine.setDescription("");
            factLine.saveEx();
        } else if (!X_M_Product.PRODUCTTYPE_Item.equals(product.getProductType())) {
            BigDecimal totalCosts = Env.ZERO;
            MAccount acct = null;
            for (MCostElement costElement : MCostElement.getCostElement(line.getCtx(), line.getTrxName())) {
                if (MCostElement.COSTELEMENTTYPE_BurdenMOverhead.equals(costElement.getCostElementType()))
                    acct = line.getAccount(ProductCost.ACCTTYPE_P_Burden, as);
                else if (MCostElement.COSTELEMENTTYPE_OutsideProcessing.equals(costElement.getCostElementType()))
                    acct = line.getAccount(ProductCost.ACCTTYPE_P_OutsideProcessing, as);
                else if (MCostElement.COSTELEMENTTYPE_Overhead.equals(costElement.getCostElementType()))
                    acct = line.getAccount(ProductCost.ACCTTYPE_P_Overhead, as);
                else if (MCostElement.COSTELEMENTTYPE_Resource.equals(costElement.getCostElementType()))
                    acct = line.getAccount(ProductCost.ACCTTYPE_P_Labor, as);
                else
                    acct = line.getAccount(ProductCost.ACCTTYPE_P_OutsideProcessing, as);
                int warehouseId = DB.getSQLValue(line.getTrxName(), "SELECT M_Warehouse_ID from M_Locator WHERE M_Locator_ID=?", line.getM_Locator_ID());
                BigDecimal costTransaction = Env.ZERO;
                MCost costDimension = MCost.validateCostForCostType(as, (MCostType) as.getM_CostType(), costElement, product.getM_Product_ID(), line.getAD_Org_ID(), warehouseId, line.getM_AttributeSetInstance_ID(), line.getTrxName());
                if (costDimension != null)
                    costTransaction = costDimension.getCurrentCostPrice().add(costDimension.getCurrentCostPriceLL());
                totalCosts = totalCosts.add(costTransaction.multiply(line.getQty()));
            }
            factLine = fact.createLine(line, acct, acct, as.getC_Currency_ID(), totalCosts);
            factLine.setM_Product_ID(line.getM_Product_ID());
            factLine.setM_Locator_ID(line.getM_LocatorTo_ID());
            if (m_DocStatus.equals(MProduction.DOCSTATUS_Reversed) && m_Reversal_ID != 0 && line.getReversalLine_ID() != 0) {
                //	Set AmtAcctDr from Original Phys.Inventory
                if (!factLine.updateReverseLine(MProduction.Table_ID, m_Reversal_ID, line.getReversalLine_ID(), line.getQty(), Env.ONE)) {
                    p_Error = "Original Physical Inventory not posted yet";
                    return null;
                }
            }
            factLine.setDescription("");
            factLine.saveEx();
            total = total.add(totalCosts);
        }
    }
    // When total no is zero then create an adjustment cost , can happen when of resource rate was changes from original cost finish good
    if (total.signum() != 0) {
        factLine = fact.createLine(parentProductionLine, parentProductionLine.getAccount(ProductCost.ACCTTYPE_P_CostAdjustment, as), as.getC_Currency_ID(), total);
        factLine.setM_Product_ID(parentProductionLine.getM_Product_ID());
        factLine.setM_Locator_ID(parentProductionLine.getM_LocatorTo_ID());
        factLine.setDescription(" Adjustment Cost");
        factLine.saveEx();
    }
    ArrayList<Fact> facts = new ArrayList<Fact>();
    facts.add(fact);
    return facts;
}
Also used : MCostElement(org.compiere.model.MCostElement) MProduct(org.compiere.model.MProduct) MAccount(org.compiere.model.MAccount) MCost(org.compiere.model.MCost) ArrayList(java.util.ArrayList) MCostType(org.compiere.model.MCostType) MCostDetail(org.compiere.model.MCostDetail) BigDecimal(java.math.BigDecimal)

Example 13 with MCostDetail

use of org.compiere.model.MCostDetail in project adempiere by adempiere.

the class Doc_MatchPO method createFacts.

//  getBalance
/**
	 *  Create Facts (the accounting logic) for
	 *  MXP.
	 *  <pre>
	 *      Product PPV     <difference>
	 *      PPV_Offset                  <difference>
	 *  </pre>
	 *  @param as accounting schema
	 *  @return Fact
	 */
public ArrayList<Fact> createFacts(MAcctSchema as) {
    ArrayList<Fact> facts = new ArrayList<Fact>();
    //
    if (//  Nothing to do if no Product
    getM_Product_ID() == 0 || getQty().signum() == 0 || //  No posting if not matched to Shipment
    m_M_InOutLine_ID == 0) {
        log.fine("No Product/Qty - M_Product_ID=" + getM_Product_ID() + ",Qty=" + getQty());
        return facts;
    }
    //  create Fact Header
    Fact fact = new Fact(this, as, Fact.POST_Actual);
    setC_Currency_ID(as.getC_Currency_ID());
    boolean isInterOrg = isInterOrg(as);
    //	Purchase Order Line
    BigDecimal poCost = m_oLine.getPriceCost();
    if (poCost == null || poCost.signum() == 0)
        poCost = m_oLine.getPriceActual();
    MInOutLine receiptLine = new MInOutLine(getCtx(), m_M_InOutLine_ID, getTrxName());
    MInOut inOut = receiptLine.getParent();
    boolean isReturnTrx = inOut.getMovementType().equals(X_M_InOut.MOVEMENTTYPE_VendorReturns);
    // calculate po cost
    //	Delivered so far
    poCost = poCost.multiply(getQty());
    //	Different currency
    if (m_oLine.getC_Currency_ID() != as.getC_Currency_ID()) {
        MOrder order = m_oLine.getParent();
        Timestamp dateAcct = order.getDateAcct();
        BigDecimal rate = MConversionRate.getRate(order.getC_Currency_ID(), as.getC_Currency_ID(), dateAcct, order.getC_ConversionType_ID(), m_oLine.getAD_Client_ID(), m_oLine.getAD_Org_ID());
        if (rate == null) {
            p_Error = "Purchase Order not convertible - " + as.getName();
            return null;
        }
        poCost = poCost.multiply(rate);
        if (poCost.scale() > as.getCostingPrecision())
            poCost = poCost.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
    }
    //	Calculate PPV for standard costing
    //get standard cost and also make sure cost for other costing method is updated
    MProduct product = MProduct.get(getCtx(), getM_Product_ID());
    MCostType ct = MCostType.get(as, getM_Product_ID(), getAD_Org_ID());
    String costingMethod = ct.getCostingMethod();
    MInOutLine ioLine = new MInOutLine(getCtx(), m_M_InOutLine_ID, getTrxName());
    BigDecimal costs = Env.ZERO;
    for (MTransaction transaction : MTransaction.getByInOutLine(ioLine)) {
        MCostElement element = MCostElement.getByMaterialCostElementType(transaction);
        MCostDetail cd = MCostDetail.getByTransaction(ioLine, transaction, as.getC_AcctSchema_ID(), ct.getM_CostType_ID(), element.getM_CostElement_ID());
        if (cd != null)
            costs = costs.add(MCostDetail.getTotalCost(cd, as));
    }
    if (MCostType.COSTINGMETHOD_StandardCosting.equals(costingMethod)) {
        //	No Costs yet - no PPV
        if (costs == null || costs.signum() == 0) {
            // TODO make these a translatable message
            p_Error = "No Standard Cost information was found for " + product.getName() + ".  Please add a standard cost for material and repost this document.";
            log.log(Level.SEVERE, p_Error);
            return null;
        }
        //	Difference
        BigDecimal difference = poCost.subtract(costs.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP));
        //	Nothing to post
        if (difference.signum() == 0) {
            log.log(Level.FINE, "No Cost Difference for M_Product_ID=" + getM_Product_ID());
            return facts;
        }
        //  Product PPV
        FactLine cr = fact.createLine(null, m_pc.getAccount(ProductCost.ACCTTYPE_P_PPV, as), as.getC_Currency_ID(), isReturnTrx ? difference.negate() : difference);
        if (cr != null) {
            cr.setQty(isReturnTrx ? getQty().negate() : getQty());
            cr.setC_BPartner_ID(m_oLine.getC_BPartner_ID());
            cr.setC_Activity_ID(m_oLine.getC_Activity_ID());
            cr.setC_Campaign_ID(m_oLine.getC_Campaign_ID());
            cr.setC_Project_ID(m_oLine.getC_Project_ID());
            cr.setC_ProjectPhase_ID(m_oLine.getC_ProjectPhase_ID());
            cr.setC_ProjectTask_ID(m_oLine.getC_ProjectTask_ID());
            cr.setC_UOM_ID(m_oLine.getC_UOM_ID());
            cr.setUser1_ID(m_oLine.getUser1_ID());
            cr.setUser2_ID(m_oLine.getUser2_ID());
            cr.setUser3_ID(m_oLine.getUser3_ID());
            cr.setUser4_ID(m_oLine.getUser4_ID());
        }
        //  PPV Offset
        FactLine dr = fact.createLine(null, getAccount(Doc.ACCTTYPE_PPVOffset, as), as.getC_Currency_ID(), isReturnTrx ? difference : difference.negate());
        if (dr != null) {
            dr.setQty(isReturnTrx ? getQty() : getQty().negate());
            dr.setC_BPartner_ID(m_oLine.getC_BPartner_ID());
            dr.setC_Activity_ID(m_oLine.getC_Activity_ID());
            dr.setC_Campaign_ID(m_oLine.getC_Campaign_ID());
            dr.setC_Project_ID(m_oLine.getC_Project_ID());
            dr.setC_ProjectPhase_ID(m_oLine.getC_ProjectPhase_ID());
            dr.setC_ProjectTask_ID(m_oLine.getC_ProjectTask_ID());
            dr.setC_UOM_ID(m_oLine.getC_UOM_ID());
            dr.setUser1_ID(m_oLine.getUser1_ID());
            dr.setUser2_ID(m_oLine.getUser2_ID());
            dr.setUser3_ID(m_oLine.getUser3_ID());
            dr.setUser4_ID(m_oLine.getUser4_ID());
        }
        // Avoid usage of clearing accounts
        // If both accounts Purchase Price Variance and Purchase Price Variance Offset are equal
        // then remove the posting
        // PPV
        MAccount acct_db = dr.getAccount();
        // PPV Offset
        MAccount acct_cr = cr.getAccount();
        if ((!as.isPostIfClearingEqual()) && acct_db.equals(acct_cr) && (!isInterOrg)) {
            BigDecimal debit = dr.getAmtSourceDr();
            BigDecimal credit = cr.getAmtSourceCr();
            if (debit.compareTo(credit) == 0) {
                fact.remove(dr);
                fact.remove(cr);
            }
        }
        // End Avoid usage of clearing accounts
        //
        facts.add(fact);
        return facts;
    } else {
        return facts;
    }
}
Also used : MInOut(org.compiere.model.MInOut) MCostElement(org.compiere.model.MCostElement) MProduct(org.compiere.model.MProduct) MInOutLine(org.compiere.model.MInOutLine) MAccount(org.compiere.model.MAccount) ArrayList(java.util.ArrayList) MTransaction(org.compiere.model.MTransaction) MCostType(org.compiere.model.MCostType) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) MOrder(org.compiere.model.MOrder) MCostDetail(org.compiere.model.MCostDetail)

Example 14 with MCostDetail

use of org.compiere.model.MCostDetail in project adempiere by adempiere.

the class AveragePOCostingMethod method createCostAdjustment.

public void createCostAdjustment() {
    // only re process cost detail if account schema need adjust cost
    if (!accountSchema.isAdjustCOGS())
        return;
    // void the cycle process, only process the adjustment
    if (costDetail == null || costDetail.isProcessing())
        return;
    // Check if cost detail is an earlier transaction
    // get the cost details that need be re process before this cost
    // transaction
    List<MCostDetail> costDetails = MCostDetail.getAfterDate(costDetail, costingLevel);
    if (costDetails == null || costDetails.size() == 0)
        return;
    MCostDetail last_cd = costDetail;
    costDetail = null;
    //Renumber sequence
    for (MCostDetail cost : costDetails) {
        // remunerate sequence
        cost.setSeqNo(last_cd.getSeqNo() + 10);
        cost.setProcessing(true);
        cost.saveEx();
        last_cd = cost;
    // Only uncomment to debug
    // Trx.get(cd.get_TrxName(), false).commit();
    }
    for (MCostDetail cost : costDetails) {
        adjustCostDetail(cost);
        cost.setProcessing(false);
        cost.saveEx();
    //clearAccounting(cd);
    // Only uncomment to debug
    // Trx.get(cd.get_TrxName(), false).commit();
    }
}
Also used : MCostDetail(org.compiere.model.MCostDetail)

Example 15 with MCostDetail

use of org.compiere.model.MCostDetail in project adempiere by adempiere.

the class AveragePOCostingMethod method calculate.

public void calculate() {
    // try find the last cost detail transaction
    lastCostDetail = MCostDetail.getLastTransaction(model, transaction, accountSchema.getC_AcctSchema_ID(), dimension.getM_CostType_ID(), dimension.getM_CostElement_ID(), dateAccounting, costingLevel);
    //Validate if model have a reverses and processing of reverse
    if (model.getReversalLine_ID() > 0 && costDetail == null)
        return;
    else if (costDetail != null && costDetail.isReversal() && model.getReversalLine_ID() > 0) {
        setReversalCostDetail();
        return;
    }
    // created a new instance cost detail to process calculated cost
    if (lastCostDetail == null) {
        lastCostDetail = new MCostDetail(transaction, accountSchema.getC_AcctSchema_ID(), dimension.getM_CostType_ID(), dimension.getM_CostElement_ID(), Env.ZERO, Env.ZERO, Env.ZERO, transaction.get_TrxName());
        lastCostDetail.setDateAcct(dateAccounting);
    }
    BigDecimal quantityOnHand = getNewAccumulatedQuantity(lastCostDetail);
    // generate adjustment
    if (transaction.getM_Transaction_ID() == lastCostDetail.getM_Transaction_ID()) {
        //Processing provision of purchase cost  
        //Provision is calculated when the last cost detail  is a material receipt and not exist of invoice line
        //if an invoice line exist for this cost detail then an invoice line was processed for this material receipt 
        //and not exist different between purchase cost and invoice cost, this logic was implemented to prevent 
        //that a provision of purchase cost decreases more than one times in a cost adjustment
        BigDecimal provisionOfPurchaseCost = BigDecimal.ZERO;
        BigDecimal provisionOfPurchaseCostLL = BigDecimal.ZERO;
        // Quantity accumulated from last cost transaction
        accumulatedQuantity = getNewAccumulatedQuantity(lastCostDetail).add(movementQuantity);
        if (model instanceof MMatchPO) {
            provisionOfPurchaseCost = lastCostDetail.getCostAmt();
            provisionOfPurchaseCostLL = lastCostDetail.getCostAmtLL();
            MMatchPO iMatch = (MMatchPO) model;
            lastCostDetail.setC_InvoiceLine_ID(iMatch.getC_InvoiceLine_ID());
            lastCostDetail.saveEx();
            // reset the accumulated quantity with last cost detail
            if (lastCostDetail != null && lastCostDetail.getM_CostDetail_ID() > 0)
                accumulatedQuantity = getNewAccumulatedQuantity(lastCostDetail);
        }
        adjustCost = model.getMovementQty().multiply(costThisLevel).subtract(provisionOfPurchaseCost);
        adjustCostLowerLevel = model.getMovementQty().multiply(costLowLevel).subtract(provisionOfPurchaseCostLL);
        accumulatedAmount = getNewAccumulatedAmount(lastCostDetail);
        accumulatedAmount = accumulatedQuantity.signum() > 0 ? accumulatedAmount.add(adjustCost) : accumulatedAmount.add(adjustCost.negate());
        accumulatedAmountLowerLevel = getNewAccumulatedAmountLowerLevel(lastCostDetail);
        accumulatedAmountLowerLevel = accumulatedQuantity.signum() > 0 ? accumulatedAmountLowerLevel.add(adjustCostLowerLevel) : accumulatedAmountLowerLevel.add(adjustCostLowerLevel.negate());
        currentCostPrice = accumulatedAmount.divide(accumulatedQuantity.signum() != 0 ? accumulatedQuantity : BigDecimal.ONE, accountSchema.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
        currentCostPriceLowerLevel = accumulatedAmountLowerLevel.divide(accumulatedQuantity.signum() != 0 ? accumulatedQuantity : BigDecimal.ONE, accountSchema.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
        if (adjustCost.add(adjustCostLowerLevel).signum() == 0)
            return;
        // validation when the cost detail is reprocess
        if (costDetail == null)
            return;
        // reset with the current values
        costDetail.setCostAdjustment(adjustCost);
        costDetail.setAmt(costDetail.getCostAmt().add(costDetail.getCostAdjustment()));
        costDetail.setCostAdjustmentLL(adjustCostLowerLevel);
        costDetail.setAmtLL(costDetail.getCostAmtLL().add(costDetail.getCostAdjustmentLL()));
        updateAmountCost();
        return;
    }
    // calculated costing
    if (transaction.getMovementType().endsWith("+")) {
        //the On hand is different zero and inventory values is zero then
        if (quantityOnHand.signum() != 0 && getNewAccumulatedAmount(lastCostDetail).signum() == 0 && costThisLevel.signum() != 0) {
            adjustCost = quantityOnHand.add(movementQuantity).multiply(costThisLevel).subtract(costThisLevel.multiply(movementQuantity));
        } else // Logic to calculate adjustment when inventory is negative
        if (quantityOnHand.add(movementQuantity).signum() < 0 && getNewCurrentCostPrice(lastCostDetail, accountSchema.getCostingPrecision(), BigDecimal.ROUND_HALF_UP).signum() != 0 && costThisLevel.signum() == 0) {
            currentCostPrice = getNewCurrentCostPrice(lastCostDetail, accountSchema.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
            adjustCost = currentCostPrice.multiply(movementQuantity).abs();
        }
        // proportionally
        if (model instanceof MLandedCostAllocation || model instanceof MMatchPO) {
            if (!isOpenPeriod) {
                int attributeSetInstanceId = 0;
                if (model instanceof MLandedCostAllocation) {
                    MLandedCostAllocation costAllocation = (MLandedCostAllocation) this.model;
                    attributeSetInstanceId = costAllocation.getM_AttributeSetInstance_ID();
                }
                if (model instanceof MMatchPO) {
                    MMatchPO matchPO = (MMatchPO) this.model;
                    attributeSetInstanceId = matchPO.getM_AttributeSetInstance_ID();
                }
                this.movementQuantity = MCostDetail.getQtyOnHandByASIAndSeqNo(transaction.getCtx(), transaction.getM_Product_ID(), dimension.getM_CostType_ID(), dimension.getM_CostElement_ID(), attributeSetInstanceId, lastCostDetail.getSeqNo(), transaction.get_TrxName());
                accumulatedQuantity = getNewAccumulatedQuantity(lastCostDetail);
                currentCostPrice = movementQuantity.multiply(costThisLevel);
                currentCostPriceLowerLevel = movementQuantity.multiply(costLowLevel);
                adjustCost = currentCostPrice;
                adjustCostLowerLevel = currentCostPriceLowerLevel;
            }
        } else {
            accumulatedQuantity = getNewAccumulatedQuantity(lastCostDetail).add(movementQuantity);
            currentCostPrice = costThisLevel;
            currentCostPriceLowerLevel = costLowLevel;
        }
        amount = movementQuantity.multiply(costThisLevel);
        amountLowerLevel = movementQuantity.multiply(costLowLevel);
        accumulatedAmount = getNewAccumulatedAmount(lastCostDetail);
        accumulatedAmount = accumulatedQuantity.signum() > 0 ? accumulatedAmount.add(amount) : accumulatedAmount.add(amount.negate());
        accumulatedAmountLowerLevel = getNewAccumulatedAmountLowerLevel(lastCostDetail);
        accumulatedAmountLowerLevel = accumulatedQuantity.signum() > 0 ? accumulatedAmountLowerLevel.add(amountLowerLevel) : accumulatedAmountLowerLevel.add(amountLowerLevel.negate());
    } else if (transaction.getMovementType().endsWith("-")) {
        // Use the last current cost price for out transaction			
        if (quantityOnHand.add(movementQuantity).signum() >= 0) {
            currentCostPrice = getNewCurrentCostPrice(lastCostDetail, accountSchema.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
            currentCostPriceLowerLevel = getNewCurrentCostPriceLowerLevel(lastCostDetail, accountSchema.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
        } else {
            currentCostPrice = CostEngine.getCostThisLevel(accountSchema, dimension.getM_CostType(), dimension.getM_CostElement(), transaction, model, costingLevel);
        }
        amount = transaction.getMovementQty().multiply(currentCostPrice);
        amountLowerLevel = movementQuantity.multiply(currentCostPriceLowerLevel);
        accumulatedQuantity = getNewAccumulatedQuantity(lastCostDetail).add(movementQuantity);
        accumulatedAmount = getNewAccumulatedAmount(lastCostDetail);
        accumulatedAmount = accumulatedQuantity.signum() > 0 ? accumulatedAmount.add(amount) : accumulatedAmount.add(amount.negate());
        accumulatedAmountLowerLevel = getNewAccumulatedAmountLowerLevel(lastCostDetail);
        accumulatedAmountLowerLevel = accumulatedQuantity.signum() > 0 ? accumulatedAmountLowerLevel.add(amountLowerLevel) : accumulatedAmountLowerLevel.add(amountLowerLevel.negate());
        if (costDetail != null) {
            costDetail.setAmt(currentCostPrice.multiply(movementQuantity.abs()));
            costDetail.setAmtLL(currentCostPriceLowerLevel.multiply(movementQuantity).abs());
        }
    }
    //create new cost
    if (costDetail == null)
        return;
    updateAmountCost();
}
Also used : MLandedCostAllocation(org.compiere.model.MLandedCostAllocation) MMatchPO(org.compiere.model.MMatchPO) MCostDetail(org.compiere.model.MCostDetail) BigDecimal(java.math.BigDecimal)

Aggregations

MCostDetail (org.compiere.model.MCostDetail)27 BigDecimal (java.math.BigDecimal)15 ArrayList (java.util.ArrayList)9 MProduct (org.compiere.model.MProduct)9 MAccount (org.compiere.model.MAccount)8 MCostElement (org.compiere.model.MCostElement)5 MLandedCostAllocation (org.compiere.model.MLandedCostAllocation)4 MTransaction (org.compiere.model.MTransaction)4 AdempiereException (org.adempiere.exceptions.AdempiereException)3 MCostQueue (org.compiere.model.MCostQueue)3 MMatchInv (org.compiere.model.MMatchInv)3 MMatchPO (org.compiere.model.MMatchPO)3 Timestamp (java.sql.Timestamp)2 MCostType (org.compiere.model.MCostType)2 Date (java.sql.Date)1 StandardCostingMethod (org.adempiere.engine.StandardCostingMethod)1 I_C_OrderLine (org.compiere.model.I_C_OrderLine)1 I_M_CostElement (org.compiere.model.I_M_CostElement)1 I_M_InOutLine (org.compiere.model.I_M_InOutLine)1 MAcctSchema (org.compiere.model.MAcctSchema)1