Search in sources :

Example 16 with MCost

use of org.compiere.model.MCost 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 17 with MCost

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

the class RollupBillOfMaterial method getFutureCostPriceLowLevel.

/**
	 * Get the sum Current Cost Price Level Low for this Cost Element
	 * @param acctSchema
	 * @param bom MPPProductBOM
	 * @param costElement MCostElement
	 * @param trxName
	 * @return Cost Price Lower Level
	 */
private BigDecimal getFutureCostPriceLowLevel(MAcctSchema acctSchema, MPPProductBOM bom, MCostElement costElement, String trxName) {
    log.info("Element: " + costElement);
    AtomicReference<BigDecimal> costPriceLowLevel = new AtomicReference<>(Env.ZERO);
    if (bom == null)
        return costPriceLowLevel.get();
    //Iterate bom lines
    Arrays.stream(bom.getLines()).filter(bomLine -> bomLine != null && !bomLine.isCoProduct()).forEach(bomLine -> {
        MProduct component = MProduct.get(getCtx(), bomLine.getM_Product_ID());
        MCost cost = MCost.getOrCreate(component, 0, acctSchema, getOrganizationId(), getWarehouseId(), getCostTypeId(), costElement.getM_CostElement_ID());
        Boolean includingScrapQty = true;
        BigDecimal qty = bomLine.getQty(includingScrapQty);
        if (bomLine.isByProduct())
            cost.setFutureCostPriceLL(Env.ZERO);
        BigDecimal costPrice = cost.getFutureCostPrice().add(cost.getFutureCostPriceLL());
        if (costPrice.equals(BigDecimal.ZERO))
            costPrice = cost.getCurrentCostPrice().add(cost.getCurrentCostPriceLL());
        if (bomLine.getM_Product().getC_UOM_ID() != bomLine.getC_UOM_ID()) {
            BigDecimal rate = MUOMConversion.getProductRateFrom(getCtx(), component.getM_Product_ID(), bomLine.getC_UOM_ID());
            if (rate == null)
                costPrice = costPrice.multiply(BigDecimal.ONE);
            else
                costPrice = costPrice.multiply(rate);
        }
        if (bomLine.isPacking()) {
            int workflowId = 0;
            MProduct product = MProduct.get(getCtx(), bom.getM_Product_ID());
            MPPProductPlanning productPlanning = null;
            if (workflowId <= 0)
                workflowId = MWorkflow.getWorkflowSearchKey(product);
            if (workflowId <= 0) {
                productPlanning = MPPProductPlanning.find(getCtx(), getOrganizationId(), getWarehouseId(), getResourcePlantId(), product.get_ID(), get_TrxName());
                if (productPlanning != null)
                    workflowId = productPlanning.getAD_Workflow_ID();
                else
                    createNotice(product, "@NotFound@ @PP_Product_Planning_ID@");
            }
            if (workflowId <= 0)
                createNotice(product, "@NotFound@ @AD_Workflow_ID@");
            BigDecimal qtyBatchSize = DB.getSQLValueBD(trxName, "SELECT QtyBatchSize FROM AD_Workflow WHERE AD_Workflow_ID=?", workflowId);
            if (qtyBatchSize != null && qtyBatchSize.signum() != 0)
                qty = qty.divide(qtyBatchSize, acctSchema.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
        }
        BigDecimal componentCost = costPrice.multiply(qty);
        costPriceLowLevel.updateAndGet(costAmt -> costAmt.add(componentCost));
        log.info("CostElement: " + costElement.getName() + ", Component: " + component.getValue() + ", CostPrice: " + costPrice + ", Qty: " + qty + ", Cost: " + componentCost + " => Total Cost Element: " + costPriceLowLevel.get());
    });
    // BOM line
    return costPriceLowLevel.get();
}
Also used : MUOMConversion(org.compiere.model.MUOMConversion) MPPProductPlanning(org.eevolution.model.MPPProductPlanning) Arrays(java.util.Arrays) I_PP_Product_Planning(org.eevolution.model.I_PP_Product_Planning) MWorkflow(org.compiere.wf.MWorkflow) Env(org.compiere.util.Env) MCostType(org.compiere.model.MCostType) AtomicReference(java.util.concurrent.atomic.AtomicReference) MAcctSchema(org.compiere.model.MAcctSchema) ArrayList(java.util.ArrayList) MPPProductBOM(org.eevolution.model.MPPProductBOM) MCost(org.compiere.model.MCost) BigDecimal(java.math.BigDecimal) List(java.util.List) Query(org.compiere.model.Query) DB(org.compiere.util.DB) Msg(org.compiere.util.Msg) MCostElement(org.compiere.model.MCostElement) TrxRunnable(org.compiere.util.TrxRunnable) MPPMRP(org.eevolution.model.MPPMRP) Trx(org.compiere.util.Trx) MProduct(org.compiere.model.MProduct) MProduct(org.compiere.model.MProduct) MPPProductPlanning(org.eevolution.model.MPPProductPlanning) MCost(org.compiere.model.MCost) AtomicReference(java.util.concurrent.atomic.AtomicReference) BigDecimal(java.math.BigDecimal)

Example 18 with MCost

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

the class RollupBillOfMaterial method rollup.

/**
	 * rollup
	 * @param acctSchema
	 * @param costType
	 * @param costElement
	 * @param product
	 * @param bom
	 * @param trxName
	 */
protected void rollup(MAcctSchema acctSchema, MCostType costType, MCostElement costElement, MProduct product, MPPProductBOM bom, String trxName) {
    MCost cost = MCost.getOrCreate(product, 0, acctSchema, getOrganizationId(), getWarehouseId(), costType.getM_CostType_ID(), costElement.getM_CostElement_ID());
    cost.setFutureCostPriceLL(BigDecimal.ZERO);
    if (!cost.isCostFrozen())
        cost.setCurrentCostPriceLL(BigDecimal.ZERO);
    log.info("Calculate Lower Cost for : " + bom);
    BigDecimal price = getFutureCostPriceLowLevel(acctSchema, bom, costElement, trxName);
    log.info(" Product :" + product.getName() + " Element Cost : " + costElement.getName() + " Cost Low Level : " + price);
    cost.setFutureCostPriceLL(price);
    if (!cost.isCostFrozen())
        cost.setCurrentCostPriceLL(price);
    updateCoProductCosts(bom, cost, trxName);
    cost.saveEx();
}
Also used : MCost(org.compiere.model.MCost) BigDecimal(java.math.BigDecimal)

Example 19 with MCost

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

the class AveragePOCostingMethod method getProductFutureCostPrice.

public BigDecimal getProductFutureCostPrice(MPPCostCollector costCollector, MProduct product, MAcctSchema acctSchema, MCostElement costElement, String trxName) {
    String CostingLevel = product.getCostingLevel(acctSchema);
    // Org Element
    int orgId = 0;
    int warehouseId = 0;
    if (product.getS_Resource_ID() != 0) {
        orgId = product.getS_Resource().getAD_Org_ID();
        warehouseId = product.getS_Resource().getM_Warehouse_ID();
    } else {
        orgId = (costCollector == null) ? costElement.getAD_Org_ID() : costCollector.getAD_Org_ID();
        warehouseId = (costCollector == null) ? 0 : costCollector.getM_Warehouse_ID();
    }
    int attributeSetInstanceId = (costCollector == null) ? 0 : costCollector.getM_AttributeSetInstance_ID();
    if (MAcctSchema.COSTINGLEVEL_Client.equals(CostingLevel)) {
        orgId = 0;
        attributeSetInstanceId = 0;
        warehouseId = 0;
    } else if (MAcctSchema.COSTINGLEVEL_Organization.equals(CostingLevel))
        attributeSetInstanceId = 0;
    else if (MAcctSchema.COSTINGLEVEL_BatchLot.equals(CostingLevel))
        orgId = 0;
    CostDimension d = new CostDimension(product, acctSchema, acctSchema.getM_CostType_ID(), orgId, attributeSetInstanceId, //warehouse
    warehouseId, costElement.getM_CostElement_ID());
    MCost cost = d.toQuery(MCost.class, trxName).firstOnly();
    if (cost == null)
        return Env.ZERO;
    BigDecimal price = cost.getFutureCostPrice().add(cost.getFutureCostPriceLL());
    return roundCost(price, acctSchema.getC_AcctSchema_ID());
}
Also used : MCost(org.compiere.model.MCost) BigDecimal(java.math.BigDecimal)

Example 20 with MCost

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

the class AveragePOCostingMethod method createUpdateAverageCostDetail.

public void createUpdateAverageCostDetail(MPPCostCollector costCollectorVariance, BigDecimal costVarianceThisLevel, BigDecimal costVarianceLowLevel, MProduct product, MAcctSchema acctSchema, MCostType costType, MCostElement costElement) {
    String whereClause = " exists (select 1 from pp_cost_collector pc" + " where pc.pp_cost_collector_ID=m_transaction.pp_Cost_collector_ID and costcollectortype =? " + " and pc.pp_order_ID=?)";
    MTransaction mtrx = new Query(costCollectorVariance.getCtx(), MTransaction.Table_Name, whereClause, costCollectorVariance.get_TrxName()).setParameters(MPPCostCollector.COSTCOLLECTORTYPE_MaterialReceipt, costCollectorVariance.getPP_Order_ID()).setOrderBy("M_Transaction_ID desc").first();
    BigDecimal costThisLevel = Env.ZERO;
    BigDecimal costLowLevel = Env.ZERO;
    costCollectorVariance.set_ValueOfColumn("Cost", costVarianceThisLevel.compareTo(Env.ZERO) != 0 ? costVarianceThisLevel : costVarianceLowLevel);
    costCollectorVariance.saveEx();
    IDocumentLine model = costCollectorVariance;
    MCost cost = MCost.validateCostForCostType(acctSchema, costType, costElement, product.getM_Product_ID(), 0, 0, 0, mtrx.get_TrxName());
    final ICostingMethod method = CostingMethodFactory.get().getCostingMethod(costType.getCostingMethod());
    method.setCostingMethod(acctSchema, mtrx, model, cost, costThisLevel, costLowLevel, model.isSOTrx());
    method.process();
}
Also used : Query(org.compiere.model.Query) MCost(org.compiere.model.MCost) MTransaction(org.compiere.model.MTransaction) BigDecimal(java.math.BigDecimal)

Aggregations

MCost (org.compiere.model.MCost)29 BigDecimal (java.math.BigDecimal)19 ArrayList (java.util.ArrayList)14 MProduct (org.compiere.model.MProduct)11 MAcctSchema (org.compiere.model.MAcctSchema)9 MCostElement (org.compiere.model.MCostElement)9 Query (org.compiere.model.Query)8 Arrays (java.util.Arrays)6 List (java.util.List)6 MCostType (org.compiere.model.MCostType)6 CostDimension (org.adempiere.engine.CostDimension)5 Trx (org.compiere.util.Trx)5 Env (org.compiere.util.Env)4 Msg (org.compiere.util.Msg)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 AdempiereException (org.adempiere.exceptions.AdempiereException)3 MTransaction (org.compiere.model.MTransaction)3 TrxRunnable (org.compiere.util.TrxRunnable)3 MWorkflow (org.compiere.wf.MWorkflow)3 MPPProductBOM (org.eevolution.model.MPPProductBOM)3