Search in sources :

Example 1 with CostEngine

use of org.adempiere.engine.CostEngine 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 2 with CostEngine

use of org.adempiere.engine.CostEngine 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

BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 List (java.util.List)2 CostEngine (org.adempiere.engine.CostEngine)2 CostEngineFactory (org.adempiere.engine.CostEngineFactory)2 MAcctSchema (org.compiere.model.MAcctSchema)2 MCost (org.compiere.model.MCost)2 MCostElement (org.compiere.model.MCostElement)2 MProduct (org.compiere.model.MProduct)2 Query (org.compiere.model.Query)2 Env (org.compiere.util.Env)2 RoundingMode (java.math.RoundingMode)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 CostDimension (org.adempiere.engine.CostDimension)1 StandardCostingMethod (org.adempiere.engine.StandardCostingMethod)1 AdempiereException (org.adempiere.exceptions.AdempiereException)1 MCostType (org.compiere.model.MCostType)1 Msg (org.compiere.util.Msg)1 Trx (org.compiere.util.Trx)1