Search in sources :

Example 1 with RoutingService

use of org.eevolution.model.RoutingService in project adempiere by adempiere.

the class StandardCostingMethod method createUsageVariances.

public void createUsageVariances(MPPCostCollector usageVariance) {
    // Apply only for material Usage Variance
    if (!usageVariance.isCostCollectorType(MPPCostCollector.COSTCOLLECTORTYPE_UsegeVariance))
        throw new IllegalArgumentException("Cost Collector is not Material Usage Variance");
    //
    final MProduct product;
    final BigDecimal quantity;
    if (usageVariance.getPP_Order_BOMLine_ID() > 0) {
        product = MProduct.get(usageVariance.getCtx(), usageVariance.getM_Product_ID());
        quantity = usageVariance.getMovementQty();
    } else {
        product = MProduct.forS_Resource_ID(usageVariance.getCtx(), usageVariance.getS_Resource_ID(), null);
        final RoutingService routingService = RoutingServiceFactory.get().getRoutingService(usageVariance.getAD_Client_ID());
        quantity = routingService.getResourceBaseValue(usageVariance.getS_Resource_ID(), usageVariance);
    }
    //
    for (MAcctSchema accountSchema : CostEngine.getAcctSchema(usageVariance)) {
        for (MCostElement element : MCostElement.getCostElement(usageVariance.getCtx(), usageVariance.get_TrxName())) {
            final BigDecimal price = getProductActualCostPrice(usageVariance, product, accountSchema, element, usageVariance.get_TrxName());
            final BigDecimal amt = CostEngine.roundCost(price.multiply(quantity), accountSchema.getC_AcctSchema_ID());
            // Create / Update Cost Detail
            if (amt.compareTo(Env.ZERO) != 0) {
                List<MCostType> costTypes = MCostType.get(accountSchema.getCtx(), accountSchema.get_TrxName());
                for (MCostType costType : costTypes) {
                    createVarianceCostDetail(usageVariance, amt.abs(), quantity, null, product, accountSchema, costType, element);
                }
            }
        }
    // for Elments
    }
// Account Schema
}
Also used : RoutingService(org.eevolution.model.RoutingService) BigDecimal(java.math.BigDecimal)

Example 2 with RoutingService

use of org.eevolution.model.RoutingService in project adempiere by adempiere.

the class StandardCostingMethod method createMethodVariances.

public void createMethodVariances(MPPCostCollector costCollector) {
    if (!costCollector.isCostCollectorType(MPPCostCollector.COSTCOLLECTORTYPE_ActivityControl))
        return;
    //
    final int std_resource_id = costCollector.getPP_Order_Node().getAD_WF_Node().getS_Resource_ID();
    final int actual_resource_id = costCollector.getS_Resource_ID();
    if (std_resource_id == actual_resource_id) {
        return;
    }
    //
    // Cost Collector - Method Change Variance
    MPPCostCollector methodChangeVariance = null;
    final RoutingService routingService = RoutingServiceFactory.get().getRoutingService(costCollector.getAD_Client_ID());
    for (MAcctSchema as : CostEngine.getAcctSchema(costCollector)) {
        for (MCostElement element : MCostElement.getCostElement(costCollector.getCtx(), costCollector.get_TrxName())) {
            final MProduct resourcePStd = MProduct.forS_Resource_ID(costCollector.getCtx(), std_resource_id, null);
            final MProduct resourcePActual = MProduct.forS_Resource_ID(costCollector.getCtx(), actual_resource_id, null);
            final BigDecimal priceStd = getProductActualCostPrice(costCollector, resourcePStd, as, element, costCollector.get_TrxName());
            final BigDecimal priceActual = getProductActualCostPrice(costCollector, resourcePActual, as, element, costCollector.get_TrxName());
            if (priceStd.compareTo(priceActual) == 0) {
                continue;
            }
            //
            if (methodChangeVariance == null) {
                methodChangeVariance = MPPCostCollector.createVarianceCostCollector(costCollector, MPPCostCollector.COSTCOLLECTORTYPE_MethodChangeVariance);
            }
            //
            final BigDecimal qty = routingService.getResourceBaseValue(costCollector.getS_Resource_ID(), costCollector);
            final BigDecimal amtStd = priceStd.multiply(qty);
            final BigDecimal amtActual = priceActual.multiply(qty);
            //
            List<MCostType> costtypes = MCostType.get(as.getCtx(), as.get_TrxName());
            for (MCostType costType : costtypes) {
                //implementation only for standard cost
                if (!MCostType.COSTINGMETHOD_StandardCosting.equals(costType.getCostingMethod()))
                    continue;
                createVarianceCostDetail(methodChangeVariance, amtActual.abs(), qty, null, resourcePActual, as, costType, element);
                createVarianceCostDetail(methodChangeVariance, amtStd.negate(), qty.negate(), null, resourcePStd, as, costType, element);
            }
        }
    }
    //
    if (methodChangeVariance != null) {
        boolean ok = methodChangeVariance.processIt(MPPCostCollector.ACTION_Complete);
        methodChangeVariance.saveEx();
        if (!ok)
            throw new AdempiereException(methodChangeVariance.getProcessMsg());
    }
}
Also used : AdempiereException(org.adempiere.exceptions.AdempiereException) RoutingService(org.eevolution.model.RoutingService) MPPCostCollector(org.eevolution.model.MPPCostCollector) BigDecimal(java.math.BigDecimal)

Example 3 with RoutingService

use of org.eevolution.model.RoutingService in project adempiere by adempiere.

the class StandardCostingMethod method createActivityControl.

public void createActivityControl(MPPCostCollector costCollector) {
    if (!costCollector.isCostCollectorType(MPPCostCollector.COSTCOLLECTORTYPE_ActivityControl))
        return;
    //
    final MProduct product = MProduct.forS_Resource_ID(costCollector.getCtx(), costCollector.getS_Resource_ID(), null);
    final RoutingService routingService = RoutingServiceFactory.get().getRoutingService(costCollector.getAD_Client_ID());
    final BigDecimal quantity = routingService.getResourceBaseValue(costCollector.getS_Resource_ID(), costCollector);
    for (MAcctSchema accountSchema : CostEngine.getAcctSchema(costCollector)) {
        for (MCostElement costElement : MCostElement.getCostElement(costCollector.getCtx(), costCollector.get_TrxName())) {
            if (!CostEngine.isActivityControlElement(costElement)) {
                continue;
            }
            final CostDimension dimension = new CostDimension(product, accountSchema, accountSchema.getM_CostType_ID(), costCollector.getAD_Org_ID(), costCollector.getM_Warehouse_ID(), costCollector.getM_AttributeSetInstanceTo_ID(), costElement.getM_CostElement_ID());
            final BigDecimal price = getResourceActualCostRate(costCollector.getS_Resource_ID(), dimension, costCollector.get_TrxName());
            BigDecimal costs = price.multiply(quantity);
            if (costs.scale() > accountSchema.getCostingPrecision())
                costs = costs.setScale(accountSchema.getCostingPrecision(), RoundingMode.HALF_UP);
            //
            List<MCostType> costTypes = MCostType.get(accountSchema.getCtx(), accountSchema.get_TrxName());
            for (MCostType costType : costTypes) {
                //implementation only for standard cost
                if (!MCostType.COSTINGMETHOD_StandardCosting.equals(costType.getCostingMethod()))
                    continue;
                MCostDetail cost = new // AD_Org_ID,
                MCostDetail(// AD_Org_ID,
                accountSchema, // AD_Org_ID,
                costCollector.getAD_Org_ID(), // M_AttributeSetInstance_ID,
                dimension.getM_Product_ID(), // M_AttributeSetInstance_ID,
                0, costElement.getM_CostElement_ID(), costs.negate(), // Description,
                quantity.negate(), // Description,
                costElement.getName(), costCollector.get_TrxName(), costType.getM_CostType_ID());
                cost.setPP_Cost_Collector_ID(costCollector.getPP_Cost_Collector_ID());
                cost.setDateAcct(costCollector.getDateAcct());
                cost.setCostAmt(costs.negate());
                cost.saveEx();
            }
        }
    }
}
Also used : RoutingService(org.eevolution.model.RoutingService) BigDecimal(java.math.BigDecimal)

Example 4 with RoutingService

use of org.eevolution.model.RoutingService 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)

Aggregations

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