Search in sources :

Example 6 with MWorkflow

use of org.compiere.wf.MWorkflow in project adempiere by adempiere.

the class RollupWorkflow method doIt.

//	prepare
protected String doIt() throws Exception {
    //Get account schema
    MAcctSchema accountSchema = MAcctSchema.get(getCtx(), getAccountingSchemaId());
    //Get cost type
    MCostType costType = MCostType.get(getCtx(), getCostTypeId());
    //Get cost element to process
    final List<MCostElement> costElements = getCostElementId() > 0 ? Arrays.asList(MCostElement.get(getCtx(), getCostElementId())) : MCostElement.getCostElement(getCtx(), get_TrxName());
    routingService = RoutingServiceFactory.get().getRoutingService(getAD_Client_ID());
    //Iterate product ids based on parameters
    Arrays.stream(getProductIds()).filter(productId -> productId > 0).forEach(productId -> {
        MProduct product = MProduct.get(getCtx(), productId);
        log.info("Product: " + product);
        int workflowId = 0;
        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@");
        else {
            Trx.run(new TrxRunnable() {

                MAcctSchema accountSchema;

                MCostType costType;

                MProduct product;

                MPPProductPlanning productPlanning;

                int workflowId;

                public TrxRunnable setParameters(MAcctSchema accountSchema, MCostType costType, MProduct product, MPPProductPlanning productPlanning, int workflowId) {
                    this.accountSchema = accountSchema;
                    this.costType = costType;
                    this.product = product;
                    this.productPlanning = productPlanning;
                    this.workflowId = workflowId;
                    return this;
                }

                public void run(String trxName) {
                    MWorkflow workflow = new MWorkflow(getCtx(), workflowId, trxName);
                    costElements.stream().filter(costElement -> costElement != null && CostEngine.isActivityControlElement(costElement)).forEach(costElement -> {
                        rollup(accountSchema, costType, costElement, product, workflow, trxName);
                    });
                    if (productPlanning != null) {
                        productPlanning.load(trxName);
                        productPlanning.setYield(workflow.getYield());
                        productPlanning.saveEx();
                    }
                }
            }.setParameters(accountSchema, costType, product, productPlanning, workflowId));
        }
    });
    return "@OK@";
}
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) MCostElement(org.compiere.model.MCostElement) MAcctSchema(org.compiere.model.MAcctSchema) MProduct(org.compiere.model.MProduct) MPPProductPlanning(org.eevolution.model.MPPProductPlanning) MWorkflow(org.compiere.wf.MWorkflow) TrxRunnable(org.compiere.util.TrxRunnable) MCostType(org.compiere.model.MCostType)

Example 7 with MWorkflow

use of org.compiere.wf.MWorkflow 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 8 with MWorkflow

use of org.compiere.wf.MWorkflow in project adempiere by adempiere.

the class DefaultRoutingServiceImplTest method test_calculateWorkflowDuration.

public void test_calculateWorkflowDuration() {
    MWorkflow wf = createWorkflow(1);
    createNode(wf, "10", 0, 10, 0, 0);
    createNode(wf, "20", 0, 10, 0, 0);
    createNode(wf, "30", 0, 10, 0, 0);
    wf = new MWorkflow(getCtx(), wf.get_ID(), getTrxName());
    assertCalculateDuration(wf, 1000, 1000 * 10 + 1000 * 10 + 1000 * 10);
}
Also used : MWorkflow(org.compiere.wf.MWorkflow)

Example 9 with MWorkflow

use of org.compiere.wf.MWorkflow in project adempiere by adempiere.

the class DefaultRoutingServiceImplTest method test_calculateWorkflowDuration_Overlap.

public void test_calculateWorkflowDuration_Overlap() {
    MWorkflow wf = createWorkflow(1);
    createNode(wf, "10", 0, 10, 0, 10);
    createNode(wf, "20", 0, 10, 0, 10);
    createNode(wf, "30", 0, 10, 0, 0);
    wf = new MWorkflow(getCtx(), wf.get_ID(), getTrxName());
    assertCalculateDuration(wf, 1000, 10 * 10 + 10 * 10 + 1000 * 10);
}
Also used : MWorkflow(org.compiere.wf.MWorkflow)

Example 10 with MWorkflow

use of org.compiere.wf.MWorkflow in project adempiere by adempiere.

the class AbstractMakeToOrder method test01.

public void test01() throws Exception {
    Qty = new BigDecimal(10);
    //Define Product
    product = MProduct.get(getCtx(), M_Product_ID);
    //Define Business Partner
    BPartner = new MBPartner(getCtx(), C_BPartner_ID, trxName);
    //Setting the BOM
    int PP_Product_BOM_ID = MPPProductBOM.getBOMSearchKey(product);
    if (PP_Product_BOM_ID > 0)
        bom = new MPPProductBOM(getCtx(), PP_Product_BOM_ID, trxName);
    else
        throw new AdempiereException("@NotFound@ @PP_ProductBOM_ID@");
    if (bom != null) {
        bom.setBOMType(MPPProductBOM.BOMTYPE_Make_To_Order);
        bom.setBOMUse(MPPProductBOM.BOMUSE_Manufacturing);
        bom.saveEx();
    }
    //Define Workflow as standardd
    workflow = new MWorkflow(getCtx(), AD_Workflow_ID, trxName);
    workflow.setValue(product.getValue());
    workflow.saveEx();
    //int workflow_id =  MWorkflow.getWorkflowSearchKey(product);
    if (AD_Workflow_ID > 0)
        workflow = MWorkflow.get(getCtx(), AD_Workflow_ID);
    else
        throw new AdempiereException("@NotFound@ @AD_Workflow_ID@");
    createOrder();
    MPPOrder expected = createPPOrder();
    I_PP_Order actual = MPPOrder.forC_OrderLine_ID(getCtx(), oline.get_ID(), oline.getM_Product_ID(), trxName);
    if (actual == null) {
        throw new AdempiereException("@NotFound@ @PP_Order_ID@ not was generate");
    }
    assertEquals("Confirming Manufacturing Order", expected, actual);
}
Also used : I_PP_Order(org.eevolution.model.I_PP_Order) AdempiereException(org.adempiere.exceptions.AdempiereException) MWorkflow(org.compiere.wf.MWorkflow) MBPartner(org.compiere.model.MBPartner) MPPProductBOM(org.eevolution.model.MPPProductBOM) BigDecimal(java.math.BigDecimal) MPPOrder(org.eevolution.model.MPPOrder)

Aggregations

MWorkflow (org.compiere.wf.MWorkflow)36 MWFNode (org.compiere.wf.MWFNode)18 BigDecimal (java.math.BigDecimal)9 ArrayList (java.util.ArrayList)8 MWFNodeNext (org.compiere.wf.MWFNodeNext)7 Query (org.compiere.model.Query)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 HttpSession (javax.servlet.http.HttpSession)4 AdempiereException (org.adempiere.exceptions.AdempiereException)4 BufferedImage (java.awt.image.BufferedImage)3 Properties (java.util.Properties)3 MProduct (org.compiere.model.MProduct)3 Trx (org.compiere.util.Trx)3 MPPProductPlanning (org.eevolution.model.MPPProductPlanning)3 Dimension (java.awt.Dimension)2 RoundingMode (java.math.RoundingMode)2 Arrays (java.util.Arrays)2 List (java.util.List)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 CostDimension (org.adempiere.engine.CostDimension)2