Search in sources :

Example 6 with MCostElement

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

the class CostEngine method createCostDetailForLandedCostAllocation.

//Create cost detail for by document
public void createCostDetailForLandedCostAllocation(MLandedCostAllocation allocation) {
    MInOutLine ioLine = (MInOutLine) allocation.getM_InOutLine();
    for (MTransaction transaction : MTransaction.getByInOutLine(ioLine)) {
        for (MAcctSchema accountSchema : MAcctSchema.getClientAcctSchema(allocation.getCtx(), allocation.getAD_Client_ID())) {
            List<MCostType> costTypes = MCostType.get(allocation.getCtx(), allocation.get_TrxName());
            for (MCostType costType : costTypes) {
                MCostElement costElement = (MCostElement) allocation.getM_CostElement();
                CostEngineFactory.getCostEngine(allocation.getAD_Client_ID()).createCostDetail(accountSchema, costType, costElement, transaction, allocation, true);
            }
        }
    }
}
Also used : MCostElement(org.compiere.model.MCostElement) MAcctSchema(org.compiere.model.MAcctSchema) MInOutLine(org.compiere.model.MInOutLine) MTransaction(org.compiere.model.MTransaction) MCostType(org.compiere.model.MCostType)

Example 7 with MCostElement

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

the class CostEngine method createCostDetail.

/**
	 * Generate by transaction
	 * @param transaction
	 */
public void createCostDetail(MTransaction transaction, IDocumentLine model) {
    MClient client = new MClient(transaction.getCtx(), transaction.getAD_Client_ID(), transaction.get_TrxName());
    StringBuilder description = new StringBuilder();
    if (model != null && model.getDescription() != null && !Util.isEmpty(model.getDescription(), true))
        description.append(model.getDescription());
    if (model != null) {
        description.append(model.isSOTrx() ? "(|->)" : "(|<-)");
    }
    List<MAcctSchema> acctSchemas = new ArrayList(Arrays.asList(MAcctSchema.getClientAcctSchema(transaction.getCtx(), transaction.getAD_Client_ID(), transaction.get_TrxName())));
    List<MCostElement> costElements = MCostElement.getCostElement(transaction.getCtx(), transaction.get_TrxName());
    List<MCostType> costTypes = MCostType.get(transaction.getCtx(), transaction.get_TrxName());
    for (MAcctSchema accountSchema : acctSchemas) {
        for (MCostType costType : costTypes) {
            if (!costType.isActive())
                continue;
            for (MCostElement costElement : costElements) {
                createCostDetail(accountSchema, costType, costElement, transaction, model, client.isCostImmediate());
            }
        }
    }
}
Also used : MCostElement(org.compiere.model.MCostElement) MAcctSchema(org.compiere.model.MAcctSchema) ArrayList(java.util.ArrayList) MCostType(org.compiere.model.MCostType) MClient(org.compiere.model.MClient)

Example 8 with MCostElement

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

the class AveragePOCostingMethod method adjustCostDetail.

/**
	 * Recalculate Cost Detail
	 * @param costDetail
     */
public void adjustCostDetail(MCostDetail costDetail) {
    Properties ctx = costDetail.getCtx();
    String trxName = costDetail.get_TrxName();
    int transactionId = costDetail.getM_Transaction_ID();
    int clientId = costDetail.getAD_Client_ID();
    MTransaction transaction = new MTransaction(ctx, transactionId, trxName);
    MCostType costType = (MCostType) costDetail.getM_CostType();
    MCostElement costElement = (MCostElement) costDetail.getM_CostElement();
    MAcctSchema accountSchema = (MAcctSchema) costDetail.getC_AcctSchema();
    CostEngineFactory.getCostEngine(accountSchema.getAD_Client_ID()).clearAccounting(accountSchema, transaction);
    if (MTransaction.MOVEMENTTYPE_VendorReceipts.equals(transaction.getMovementType())) {
        MInOutLine line = (MInOutLine) transaction.getDocumentLine();
        if (MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType())) {
            if (costDetail.getM_InOutLine_ID() > 0 && costDetail.getQty().signum() != 0) {
                CostEngineFactory.getCostEngine(clientId).createCostDetail(accountSchema, costType, costElement, transaction, line, true);
            } else if (costDetail.getM_InOutLine_ID() > 0 && costDetail.getQty().signum() != 0 && costDetail.getC_OrderLine_ID() > 0) {
                List<MMatchPO> orderMatches = MMatchPO.getInOutLine(line);
                for (MMatchPO match : orderMatches) {
                    if (match.getM_InOutLine_ID() == line.getM_InOutLine_ID() && match.getM_Product_ID() == transaction.getM_Product_ID()) {
                        CostEngineFactory.getCostEngine(clientId).createCostDetail(accountSchema, costType, costElement, transaction, match, true);
                    }
                }
            } else if (costDetail.getM_InOutLine_ID() > 0 && costDetail.getQty().signum() == 0 && costDetail.getC_OrderLine_ID() > 0) {
                List<MMatchPO> poMatches = MMatchPO.getInOutLine(line);
                for (MMatchPO match : poMatches) {
                    if (match.getM_Product_ID() == transaction.getM_Product_ID()) {
                        CostEngineFactory.getCostEngine(clientId).createCostDetail(accountSchema, costType, costElement, transaction, match, true);
                    }
                }
            }
        }
        for (MLandedCostAllocation allocation : MLandedCostAllocation.getOfInOutline(line, costElement.getM_CostElement_ID())) {
            CostEngineFactory.getCostEngine(clientId).createCostDetail(accountSchema, costType, costElement, transaction, allocation, true);
        }
    } else
        CostEngineFactory.getCostEngine(clientId).createCostDetail(accountSchema, costType, costElement, transaction, transaction.getDocumentLine(), true);
}
Also used : MCostElement(org.compiere.model.MCostElement) MAcctSchema(org.compiere.model.MAcctSchema) MLandedCostAllocation(org.compiere.model.MLandedCostAllocation) MInOutLine(org.compiere.model.MInOutLine) MMatchPO(org.compiere.model.MMatchPO) MTransaction(org.compiere.model.MTransaction) List(java.util.List) MCostType(org.compiere.model.MCostType) Properties(java.util.Properties)

Example 9 with MCostElement

use of org.compiere.model.MCostElement 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 10 with MCostElement

use of org.compiere.model.MCostElement 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)

Aggregations

MCostElement (org.compiere.model.MCostElement)25 MAcctSchema (org.compiere.model.MAcctSchema)17 MCostType (org.compiere.model.MCostType)16 MProduct (org.compiere.model.MProduct)15 BigDecimal (java.math.BigDecimal)14 ArrayList (java.util.ArrayList)13 MCost (org.compiere.model.MCost)11 List (java.util.List)10 Query (org.compiere.model.Query)9 Arrays (java.util.Arrays)8 Trx (org.compiere.util.Trx)8 CostDimension (org.adempiere.engine.CostDimension)6 MTransaction (org.compiere.model.MTransaction)6 MAccount (org.compiere.model.MAccount)5 MCostDetail (org.compiere.model.MCostDetail)5 Env (org.compiere.util.Env)5 Msg (org.compiere.util.Msg)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 AdempiereException (org.adempiere.exceptions.AdempiereException)4 MInOutLine (org.compiere.model.MInOutLine)4