Search in sources :

Example 26 with MCost

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

the class CopyCostTypeToCostType method copyCostTypeToCostType.

/**
     * Copy Cost Type to Cost Type
     *
     * @param productId
     * @param accountSchema
     * @param costTypeFrom
     * @param costTypeTo
     * @param costElementFrom
     * @param costElementTo
     * @param trxName
     */
private void copyCostTypeToCostType(int productId, MAcctSchema accountSchema, MCostType costTypeFrom, MCostType costTypeTo, MCostElement costElementFrom, MCostElement costElementTo, String trxName) {
    MProduct product = MProduct.get(getCtx(), productId);
    CostDimension costDimensionFrom = new CostDimension(product, accountSchema, costTypeFrom.get_ID(), getOrganizationId(), getWarehouseId(), 0, costElementFrom.get_ID());
    Optional<MCost> costDimensionFromOptional = Optional.ofNullable(costDimensionFrom.toQuery(MCost.class, trxName).first());
    CostDimension costDimensionTo = new CostDimension(product, accountSchema, costTypeTo.get_ID(), getOrganizationId(), getWarehouseId(), 0, costElementTo.get_ID());
    Optional<MCost> costDimensionToOptional = Optional.ofNullable(costDimensionTo.toQuery(MCost.class, trxName).first());
    if (isUpdateCosting()) {
        // exist cost form and cost to or not exist cost to and exit cost from
        if (costDimensionToOptional.isPresent() && costDimensionFromOptional.isPresent()) {
            MCost costTo = costDimensionToOptional.get();
            if (MCostType.COSTINGMETHOD_StandardCosting.equals(costTypeFrom.getCostingMethod()) && costTo.isCostFrozen())
                ;
            else {
                costTo.setCurrentCostPrice(costDimensionFromOptional.get().getCurrentCostPrice());
                costTo.saveEx();
            }
        } else if (!costDimensionToOptional.isPresent() && costDimensionFromOptional.isPresent()) {
            MCost costTo = MCost.getOrCreate(product, 0, accountSchema, getOrganizationId(), getWarehouseId(), costTypeTo.get_ID(), costElementTo.get_ID());
            if (MCostType.COSTINGMETHOD_StandardCosting.equals(costTypeFrom.getCostingMethod()) && costTo.isCostFrozen())
                ;
            else {
                costDimensionFromOptional.ifPresent(costFrom -> costTo.setCurrentCostPrice(costFrom.getCurrentCostPrice()));
                costTo.saveEx();
            }
        } else if (// cost to and not exist cost from
        costDimensionToOptional.isPresent() && !costDimensionFromOptional.isPresent()) {
            MCost costTo = costDimensionToOptional.get();
            costTo.setCurrentCostPrice(BigDecimal.ZERO);
            costTo.saveEx();
        } else if (!costDimensionToOptional.isPresent() && !costDimensionFromOptional.isPresent()) {
            MCost costTo = MCost.getOrCreate(product, 0, accountSchema, getOrganizationId(), getWarehouseId(), costTypeTo.get_ID(), costElementTo.get_ID());
            costTo.setCurrentCostPrice(BigDecimal.ZERO);
            costTo.saveEx();
        }
    } else if (!costDimensionToOptional.isPresent()) {
        MCost costTo = MCost.getOrCreate(product, 0, accountSchema, getOrganizationId(), getWarehouseId(), costTypeTo.get_ID(), costElementTo.get_ID());
        costDimensionFromOptional.ifPresent(costFrom -> costTo.setCurrentCostPrice(costFrom.getCurrentCostPrice()));
        costTo.saveEx();
    }
}
Also used : Arrays(java.util.Arrays) CostDimension(org.adempiere.engine.CostDimension) MCostType(org.compiere.model.MCostType) MAcctSchema(org.compiere.model.MAcctSchema) ArrayList(java.util.ArrayList) 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) Optional(java.util.Optional) Trx(org.compiere.util.Trx) MProduct(org.compiere.model.MProduct) MProduct(org.compiere.model.MProduct) MCost(org.compiere.model.MCost) CostDimension(org.adempiere.engine.CostDimension)

Example 27 with MCost

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

the class CopyPriceToStandard method doIt.

protected String doIt() throws Exception {
    MAcctSchema accountSchema = MAcctSchema.get(getCtx(), getAccountingSchemaId());
    MCostElement costElement = MCostElement.get(getCtx(), getCostElementId());
    if (!MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType()))
        throw new AdempiereException("Only Material Cost Elements are allowed");
    AtomicInteger countUpdated = new AtomicInteger(0);
    MPriceListVersion priceListVersion = new MPriceListVersion(getCtx(), getPriceListVersionId(), get_TrxName());
    Arrays.stream(priceListVersion.getProductPrice(" AND " + MProductPrice.COLUMNNAME_PriceStd + " <> 0")).forEach(productPrice -> {
        final BigDecimal price;
        int currencyId = priceListVersion.getPriceList().getC_Currency_ID();
        if (currencyId != accountSchema.getC_Currency_ID()) {
            price = MConversionRate.convert(getCtx(), productPrice.getPriceStd(), currencyId, accountSchema.getC_Currency_ID(), getAD_Client_ID(), getOrganizationId());
        } else
            price = productPrice.getPriceStd();
        MProduct product = MProduct.get(getCtx(), productPrice.getM_Product_ID());
        CostDimension costDimension = new CostDimension(product, accountSchema, getCostTypeId(), getOrganizationId(), 0, 0, getCostElementId());
        List<MCost> costs = costDimension.toQuery(MCost.class, get_TrxName()).list();
        costs.stream().filter(cost -> cost != null && cost.getM_CostElement_ID() == costElement.get_ID()).findFirst().ifPresent(cost -> {
            cost.setFutureCostPrice(price);
            cost.saveEx();
            countUpdated.getAndUpdate(count -> count + 1);
        });
    });
    return "@Updated@ # " + countUpdated;
}
Also used : MCostElement(org.compiere.model.MCostElement) MAcctSchema(org.compiere.model.MAcctSchema) MProduct(org.compiere.model.MProduct) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AdempiereException(org.adempiere.exceptions.AdempiereException) MCost(org.compiere.model.MCost) MPriceListVersion(org.compiere.model.MPriceListVersion) CostDimension(org.adempiere.engine.CostDimension) BigDecimal(java.math.BigDecimal)

Example 28 with MCost

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

the class IndentedBOM method explodeProduct.

// doIt
/**
	 * Generate an Explosion for this product
	 * @param product
	 * @param isComponent component / header
	 */
private llCost explodeProduct(int M_Product_ID, BigDecimal qty, BigDecimal accumQty) {
    MProduct product = MProduct.get(getCtx(), M_Product_ID);
    X_T_BOM_Indented tboml = new X_T_BOM_Indented(getCtx(), 0, get_TrxName());
    tboml.setAD_Org_ID(p_AD_Org_ID);
    tboml.setC_AcctSchema_ID(p_C_AcctSchema_ID);
    tboml.setAD_PInstance_ID(getAD_PInstance_ID());
    tboml.setM_CostElement_ID(p_M_CostElement_ID);
    tboml.setSel_Product_ID(product.get_ID());
    tboml.setM_Product_ID(p_M_Product_ID);
    tboml.setQtyBOM(qty);
    tboml.setQty(accumQty);
    //
    tboml.setSeqNo(m_SeqNo);
    tboml.setLevelNo(m_LevelNo);
    tboml.setLevels((m_LevelNo > 0 ? ":" : "") + StringUtils.repeat("    ", m_LevelNo) + " " + product.getValue());
    //
    // Set Costs:
    MCost cost = MCost.get(product, 0, m_as, p_AD_Org_ID, 0, p_M_CostElement_ID, get_TrxName());
    tboml.setCurrentCostPrice(cost.getCurrentCostPrice());
    tboml.setCost(cost.getCurrentCostPrice().multiply(accumQty));
    tboml.setFutureCostPrice(cost.getFutureCostPrice());
    tboml.setCostFuture(cost.getFutureCostPrice().multiply(accumQty));
    m_SeqNo++;
    BigDecimal llCost = Env.ZERO;
    BigDecimal llFutureCost = Env.ZERO;
    List<MPPProductBOMLine> list = getBOMs(product);
    for (MPPProductBOMLine bom : list) {
        m_LevelNo++;
        llCost ll = explodeProduct(bom.getM_Product_ID(), bom.getQtyBOM(), accumQty.multiply(bom.getQtyBOM()));
        llCost = llCost.add(ll.currentCost.multiply(accumQty.multiply(bom.getQtyBOM())));
        llFutureCost = llFutureCost.add(ll.futureCost.multiply(accumQty.multiply(bom.getQtyBOM())));
        m_LevelNo--;
    }
    llCost retVal = new llCost();
    if (list.size() == 0) {
        tboml.setCurrentCostPriceLL(cost.getCurrentCostPrice());
        tboml.setFutureCostPriceLL(cost.getFutureCostPrice());
        //
        retVal.currentCost = cost.getCurrentCostPrice();
        retVal.futureCost = cost.getFutureCostPrice();
    } else {
        tboml.setCurrentCostPriceLL(llCost);
        tboml.setFutureCostPriceLL(llFutureCost);
        //
        retVal.currentCost = llCost;
        retVal.futureCost = llFutureCost;
    }
    tboml.saveEx();
    return retVal;
}
Also used : X_T_BOM_Indented(org.compiere.model.X_T_BOM_Indented) MProduct(org.compiere.model.MProduct) MCost(org.compiere.model.MCost) MPPProductBOMLine(org.eevolution.model.MPPProductBOMLine) BigDecimal(java.math.BigDecimal)

Example 29 with MCost

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

the class CostResult method assertCostMovement.

/**
	 * assert Cost Receipt
	 * @param product
	 * @param receiptLine
	 * @param as
	 * @param trxName
	 */
private void assertCostMovement(CostResult costResult, int M_InOutLine_ID, MAcctSchema as, String trxName) {
    MCost cost = assertCost(costResult);
    String whereClause = "M_Product_ID=? AND M_CostElement_ID=? AND M_CostType_ID=? AND CostingMethod=? AND M_MovementLine_ID=?";
    ArrayList<Object> parameters = new ArrayList();
    parameters.add(costResult.M_Product_ID);
    parameters.add(cost.getM_CostElement_ID());
    parameters.add(cost.getM_CostType_ID());
    parameters.add(as.getCostingMethod());
    parameters.add(M_InOutLine_ID);
    assertCostDetail(costResult, whereClause, parameters);
}
Also used : MCost(org.compiere.model.MCost) ArrayList(java.util.ArrayList)

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