Search in sources :

Example 11 with MCost

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

the class CostEngine method getParentActualCostByCostType.

public static BigDecimal getParentActualCostByCostType(MAcctSchema accountSchema, MCostType costType, MCostElement costElement, I_M_Production production) {
    //	Get BOM Cost - Sum of individual lines
    BigDecimal totalCost = Env.ZERO;
    for (MProductionLine productionLine : ((MProduction) production).getLines()) {
        if (productionLine.isParent())
            continue;
        String productType = productionLine.getM_Product().getProductType();
        BigDecimal cost = BigDecimal.ZERO;
        if (X_M_Product.PRODUCTTYPE_Item.equals(productType)) {
            cost = MCostDetail.getCostByModel(accountSchema.getC_AcctSchema_ID(), costType.getM_CostType_ID(), costElement.getM_CostElement_ID(), productionLine);
        } else if (X_M_Product.PRODUCTTYPE_Resource.equals(productType)) {
            MCost costDimension = MCost.validateCostForCostType(accountSchema, costType, costElement, productionLine.getM_Product_ID(), productionLine.getAD_Org_ID(), productionLine.getM_Locator().getM_Warehouse_ID(), productionLine.getM_AttributeSetInstance_ID(), productionLine.get_TrxName());
            if (costDimension != null && costDimension.getCurrentCostPrice().signum() != 0)
                cost = costDimension.getCurrentCostPrice().multiply(productionLine.getMovementQty().negate());
        }
        if (cost != null && cost.signum() != 0)
            totalCost = totalCost.add(cost);
    }
    BigDecimal unitCost = Env.ZERO;
    if (production.getProductionQty().signum() != 0 && totalCost.signum() != 0)
        unitCost = totalCost.divide(production.getProductionQty(), accountSchema.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
    return unitCost;
}
Also used : MProductionLine(org.compiere.model.MProductionLine) MCost(org.compiere.model.MCost) BigDecimal(java.math.BigDecimal) MProduction(org.compiere.model.MProduction)

Example 12 with MCost

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

the class AveragePOCostingMethod method getProductActualCostPrice.

public BigDecimal getProductActualCostPrice(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 costDimension = new CostDimension(product, acctSchema, acctSchema.getM_CostType_ID(), orgId, attributeSetInstanceId, //warehouse
    warehouseId, costElement.getM_CostElement_ID());
    MCost cost = costDimension.toQuery(MCost.class, trxName).firstOnly();
    if (cost == null)
        return Env.ZERO;
    BigDecimal price = cost.getCurrentCostPrice().add(cost.getCurrentCostPriceLL());
    return roundCost(price, acctSchema.getC_AcctSchema_ID());
}
Also used : MCost(org.compiere.model.MCost) BigDecimal(java.math.BigDecimal)

Example 13 with MCost

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

the class RollupBillOfMaterial method updateCoProductCosts.

/**
	 * Update costs for co-products on BOM Lines from given BOM
	 * @param bom product's BOM
	 * @param baseDimension base product cost (BOM Cost)
	 */
private void updateCoProductCosts(MPPProductBOM bom, MCost baseDimension, String trxName) {
    // Skip if not BOM found
    if (bom == null)
        return;
    AtomicReference<BigDecimal> costPriceTotal = new AtomicReference<>(Env.ZERO);
    //Iterate bom lines
    Arrays.stream(bom.getLines()).filter(bomLine -> bomLine != null && bomLine.isCoProduct()).forEach(bomLine -> {
        final BigDecimal costPrice = baseDimension.getFutureCostPriceLL().multiply(bomLine.getCostAllocationPerc(true));
        MCost dimension = MCost.getDimension((MProduct) bomLine.getM_Product(), baseDimension.getC_AcctSchema_ID(), baseDimension.getAD_Org_ID(), baseDimension.getM_Warehouse_ID(), 0, baseDimension.getM_CostType_ID(), baseDimension.getM_CostElement_ID());
        if (dimension == null) {
            dimension = new MCost(baseDimension.getCtx(), 0, trxName);
            dimension.setAD_Org_ID(baseDimension.getAD_Org_ID());
            dimension.setM_Product_ID(bomLine.getM_Product_ID());
            dimension.setM_Warehouse_ID(baseDimension.getM_Warehouse_ID());
            dimension.setM_CostType_ID(baseDimension.getM_CostType_ID());
            dimension.setC_AcctSchema_ID(baseDimension.getC_AcctSchema_ID());
            dimension.setM_CostElement_ID(baseDimension.getM_CostElement_ID());
            dimension.setM_AttributeSetInstance_ID(0);
        }
        dimension.setFutureCostPriceLL(costPrice);
        if (!dimension.isCostFrozen())
            dimension.setCurrentCostPriceLL(costPrice);
        dimension.saveEx();
        costPriceTotal.updateAndGet(costAmt -> costAmt.add(costPrice));
    });
    // Update Base Cost:
    if (costPriceTotal.get().signum() != 0)
        baseDimension.setFutureCostPriceLL(costPriceTotal.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) MCost(org.compiere.model.MCost) AtomicReference(java.util.concurrent.atomic.AtomicReference) BigDecimal(java.math.BigDecimal)

Example 14 with MCost

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

the class RollupWorkflow method rollup.

/**
     * Execute rollup process
     * @param accountSchema
     * @param costType
     * @param costElement
     * @param product
     * @param workflow
     * @param trxName
     */
protected void rollup(MAcctSchema accountSchema, MCostType costType, MCostElement costElement, MProduct product, MWorkflow workflow, String trxName) {
    log.info("Workflow: " + workflow);
    workflow.setCost(Env.ZERO);
    double yield = 1;
    int queuingTime = 0;
    int setupTime = 0;
    int duration = 0;
    int waitingTime = 0;
    int movingTime = 0;
    int workingTime = 0;
    MWFNode[] nodes = workflow.getNodes(false, getAD_Client_ID());
    for (MWFNode node : nodes) {
        node.setCost(Env.ZERO);
        if (node.getYield() != 0) {
            yield = yield * ((double) node.getYield() / 100);
        }
        // We use node.getDuration() instead of m_routingService.estimateWorkingTime(node) because
        // this will be the minimum duration of this node. So even if the node have defined units/cycle
        // we consider entire duration of the node.
        long nodeDuration = node.getDuration();
        queuingTime += node.getQueuingTime();
        setupTime += node.getSetupTime();
        duration += nodeDuration;
        waitingTime += node.getWaitingTime();
        movingTime += node.getMovingTime();
        workingTime += node.getWorkingTime();
    }
    workflow.setCost(Env.ZERO);
    workflow.setYield((int) (yield * 100));
    workflow.setQueuingTime(queuingTime);
    workflow.setSetupTime(setupTime);
    workflow.setDuration(duration);
    workflow.setWaitingTime(waitingTime);
    workflow.setMovingTime(movingTime);
    workflow.setWorkingTime(workingTime);
    final CostDimension costDimension = new CostDimension(product, accountSchema, costType.getM_CostType_ID(), getOrganizationId(), getWarehouseId(), 0, costElement.get_ID());
    MCost cost = MCost.getOrCreate(product, 0, accountSchema, getOrganizationId(), getWarehouseId(), costType.getM_CostType_ID(), costElement.getM_CostElement_ID());
    cost.setFutureCostPrice(BigDecimal.ZERO);
    if (!cost.isCostFrozen())
        cost.setCurrentCostPrice(BigDecimal.ZERO);
    AtomicReference<BigDecimal> segmentCost = new AtomicReference<>(Env.ZERO);
    Arrays.stream(nodes).filter(node -> node != null).forEach(node -> {
        final CostEngine costEngine = CostEngineFactory.getCostEngine(node.getAD_Client_ID());
        final BigDecimal rate = StandardCostingMethod.getResourceActualCostRate(node.getS_Resource_ID(), costDimension, trxName);
        final BigDecimal baseValue = routingService.getResourceBaseValue(node.getS_Resource_ID(), node);
        BigDecimal nodeCostPrecision = baseValue.multiply(rate);
        BigDecimal nodeCost;
        if (nodeCostPrecision.scale() > accountSchema.getCostingPrecision())
            nodeCost = nodeCostPrecision.setScale(accountSchema.getCostingPrecision(), RoundingMode.HALF_UP);
        else
            nodeCost = nodeCostPrecision;
        segmentCost.updateAndGet(costAmt -> costAmt.add(nodeCost));
        log.info(Msg.parseTranslation(getCtx(), " @M_CostElement_ID@ : ") + costElement.getName() + ", Node=" + node + ", BaseValue=" + baseValue + ", rate=" + rate + ", nodeCost=" + nodeCost + " => Cost=" + segmentCost);
        node.setCost(node.getCost().add(nodeCost));
    });
    cost.setFutureCostPrice(segmentCost.get());
    if (!cost.isCostFrozen())
        cost.setCurrentCostPrice(segmentCost.get());
    cost.saveEx();
    // Update Workflow cost
    workflow.setCost(workflow.getCost().add(segmentCost.get()));
    // Save Workflow & Nodes
    Arrays.stream(nodes).filter(node -> node != null).forEach(node -> node.saveEx());
    workflow.saveEx();
    log.info("Product: " + product.getName() + " WFCost: " + workflow.getCost());
}
Also used : MPPProductPlanning(org.eevolution.model.MPPProductPlanning) Arrays(java.util.Arrays) 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) RoutingService(org.eevolution.model.RoutingService) MCost(org.compiere.model.MCost) BigDecimal(java.math.BigDecimal) Query(org.compiere.model.Query) Msg(org.compiere.util.Msg) Trx(org.compiere.util.Trx) CostEngine(org.adempiere.engine.CostEngine) RoundingMode(java.math.RoundingMode) CostEngineFactory(org.adempiere.engine.CostEngineFactory) CostDimension(org.adempiere.engine.CostDimension) MWorkflow(org.compiere.wf.MWorkflow) StandardCostingMethod(org.adempiere.engine.StandardCostingMethod) RoutingServiceFactory(org.eevolution.model.RoutingServiceFactory) List(java.util.List) MWFNode(org.compiere.wf.MWFNode) MCostElement(org.compiere.model.MCostElement) TrxRunnable(org.compiere.util.TrxRunnable) MProduct(org.compiere.model.MProduct) CostEngine(org.adempiere.engine.CostEngine) MCost(org.compiere.model.MCost) MWFNode(org.compiere.wf.MWFNode) AtomicReference(java.util.concurrent.atomic.AtomicReference) CostDimension(org.adempiere.engine.CostDimension) BigDecimal(java.math.BigDecimal)

Example 15 with MCost

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

the class CostBillOfMaterial method createLines.

/**
     * createLines
     *
     * @param bom
     * @param bomLine
     */
private void createLines(MAcctSchema accountSchema, MPPProductBOM bom, MPPProductBOMLine bomLine) {
    MProduct product;
    BigDecimal qty;
    if (bomLine != null) {
        product = MProduct.get(getCtx(), bomLine.getM_Product_ID());
        qty = bomLine.getQty();
    } else if (bom != null) {
        product = MProduct.get(getCtx(), bom.getM_Product_ID());
        qty = Env.ONE;
    } else {
        throw new AdempiereException("@NotFound@ @PP_Product_BOM_ID@");
    }
    //for (MCostElement costElement : getCostElements())
    getCostElements().stream().filter(costElement -> costElement != null).forEach(costElement -> {
        X_T_BOMLine reportBOMLine = new X_T_BOMLine(getCtx(), 0, get_TrxName());
        reportBOMLine.setAD_Org_ID(getOrganizationId());
        reportBOMLine.setM_Warehouse_ID(getWarehouseId());
        reportBOMLine.setSel_Product_ID(getProductId());
        reportBOMLine.setImplosion(isImplosion);
        reportBOMLine.setC_AcctSchema_ID(getAccountingSchemaId());
        reportBOMLine.setM_CostType_ID(getCostTypeId());
        reportBOMLine.setCostingMethod(getCostingMethod());
        reportBOMLine.setAD_PInstance_ID(getAD_PInstance_ID());
        reportBOMLine.setM_CostElement_ID(costElement.get_ID());
        reportBOMLine.setM_Product_ID(product.get_ID());
        reportBOMLine.setM_Warehouse_ID(getWarehouseId());
        reportBOMLine.setQtyBOM(qty);
        reportBOMLine.setSeqNo(seqNo);
        reportBOMLine.setLevelNo(levelNo);
        reportBOMLine.setLevels(LEVELS.substring(0, levelNo) + levelNo);
        BigDecimal currentCostPrice = Env.ZERO;
        BigDecimal currentCostPriceLL = Env.ZERO;
        BigDecimal futureCostPrice = Env.ZERO;
        BigDecimal futureCostPriceLL = Env.ZERO;
        final CostEngine engine = CostEngineFactory.getCostEngine(getAD_Client_ID());
        List<MCost> costs = MCost.getByElement(product, accountSchema, getCostTypeId(), getOrganizationId(), getWarehouseId(), 0, costElement.getM_CostElement_ID());
        boolean isCostFrozen = false;
        for (MCost cost : costs) {
            currentCostPrice = currentCostPrice.add(cost.getCurrentCostPrice());
            currentCostPriceLL = currentCostPriceLL.add(cost.getCurrentCostPriceLL());
            futureCostPrice = futureCostPrice.add(cost.getFutureCostPrice());
            futureCostPriceLL = futureCostPriceLL.add(cost.getFutureCostPriceLL());
            isCostFrozen = cost.isCostFrozen();
        }
        reportBOMLine.setCurrentCostPrice(currentCostPrice);
        reportBOMLine.setCurrentCostPriceLL(currentCostPriceLL);
        reportBOMLine.setFutureCostPrice(currentCostPrice);
        reportBOMLine.setFutureCostPriceLL(currentCostPriceLL);
        reportBOMLine.setIsCostFrozen(isCostFrozen);
        if (bomLine != null) {
            reportBOMLine.setPP_Product_BOM_ID(bomLine.getPP_Product_BOM_ID());
            reportBOMLine.setPP_Product_BOMLine_ID(bomLine.getPP_Product_BOMLine_ID());
        } else if (bom != null) {
            reportBOMLine.setPP_Product_BOM_ID(bom.getPP_Product_BOM_ID());
        }
        reportBOMLine.saveEx();
        seqNo++;
    });
}
Also used : CostEngineFactory(org.adempiere.engine.CostEngineFactory) Arrays(java.util.Arrays) Env(org.compiere.util.Env) X_T_BOMLine(org.eevolution.model.X_T_BOMLine) 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) AdempiereException(org.adempiere.exceptions.AdempiereException) MCostElement(org.compiere.model.MCostElement) MProduct(org.compiere.model.MProduct) MPPProductBOMLine(org.eevolution.model.MPPProductBOMLine) CostEngine(org.adempiere.engine.CostEngine) MProduct(org.compiere.model.MProduct) CostEngine(org.adempiere.engine.CostEngine) AdempiereException(org.adempiere.exceptions.AdempiereException) MCost(org.compiere.model.MCost) BigDecimal(java.math.BigDecimal) X_T_BOMLine(org.eevolution.model.X_T_BOMLine)

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