Search in sources :

Example 21 with MCostElement

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

the class FrozenUnFrozenCost method doIt.

@Override
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());
    //Iterate cost element
    costElements.stream().filter(costElement -> costElement != null).forEach(costElement -> {
        AtomicInteger records = new AtomicInteger(0);
        Arrays.stream(getProductIds()).filter(productId -> productId > 0).forEach(productId -> {
            MProduct product = MProduct.get(getCtx(), productId);
            final CostDimension costDimension = new CostDimension(product, accountSchema, costType.getM_CostType_ID(), getOrganizationId(), getWarehouseId(), 0, costElement.getM_CostElement_ID());
            Trx.run(trxName -> {
                final List<MCost> costs = costDimension.toQuery(MCost.class, trxName).list();
                costs.stream().filter(cost -> cost != null).forEach(cost -> {
                    cost.setIsCostFrozen(isCostFrozen());
                    cost.saveEx();
                    records.updateAndGet(record -> record + 1);
                });
            });
        });
        String message = "@M_CostElement_ID@ " + costElement.getName() + " @Records@ " + records.get() + " @IsCostFrozen@ = " + isCostFrozen();
        addLog(Msg.parseTranslation(getCtx(), message));
    });
    return "@OK@";
}
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) List(java.util.List) Query(org.compiere.model.Query) Msg(org.compiere.util.Msg) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MCostElement(org.compiere.model.MCostElement) Trx(org.compiere.util.Trx) MProduct(org.compiere.model.MProduct) MCostElement(org.compiere.model.MCostElement) MAcctSchema(org.compiere.model.MAcctSchema) MProduct(org.compiere.model.MProduct) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MCost(org.compiere.model.MCost) MCostType(org.compiere.model.MCostType) CostDimension(org.adempiere.engine.CostDimension)

Example 22 with MCostElement

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

the class CopyCostTypeToCostType method doIt.

@Override
protected String doIt() throws Exception {
    if (getCostTypeId() == getCostTypeIdTo())
        throw new AdempiereException("@M_CostType_ID@ @NotValid@");
    MAcctSchema accountSchema = MAcctSchema.get(getCtx(), getAccountingSchemaId());
    MCostType costTypeFrom = MCostType.get(getCtx(), getCostTypeId());
    MCostType costTypeTo = MCostType.get(getCtx(), getCostTypeIdTo());
    MCostElement costElementFrom = MCostElement.get(getCtx(), getCostElementId());
    MCostElement costElementTo = MCostElement.get(getCtx(), getCostElementIdTo());
    Arrays.stream(getProductIds()).filter(productId -> productId > 0).forEach(productId -> {
        Trx.run(trxName -> {
            copyCostTypeToCostType(productId, accountSchema, costTypeFrom, costTypeTo, costElementFrom, costElementTo, trxName);
        });
    });
    return "@Ok@";
}
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) MCostElement(org.compiere.model.MCostElement) MAcctSchema(org.compiere.model.MAcctSchema) AdempiereException(org.adempiere.exceptions.AdempiereException) MCostType(org.compiere.model.MCostType)

Example 23 with MCostElement

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

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

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

the class ValuationEffectiveDate method doIt.

// prepare
/**
	 * execute the Valuation Effective Date
	 */
protected String doIt() throws Exception {
    setup();
    for (MAcctSchema acctSchema : acctSchemas) for (MWarehouse warehouse : warehouses) for (MCostType costType : costTypes) for (MCostElement costElement : costElements) for (int productId : products) generateInventoryValue(productId, acctSchema.getC_AcctSchema_ID(), costType.getM_CostType_ID(), costElement.getM_CostElement_ID(), warehouse.getM_Warehouse_ID());
    pstmt.executeBatch();
    commitEx();
    DB.close(pstmt);
    DB.executeUpdate("UPDATE T_InventoryValue SET Cost = CASE WHEN QtyOnHand <> 0 THEN (CostAmt + CostAmtLL) / QtyOnHand ELSE  0 END  ,  CumulatedAmt = CASE WHEN QtyOnHand <> 0  THEN  CostAmt + CostAmtLL ELSE 0 END ,  DateValue = " + DB.TO_DATE(getValuationDate()) + " WHERE AD_PInstance_ID=?", getAD_PInstance_ID(), get_TrxName());
    return "@Ok@ " + count;
}
Also used : MCostElement(org.compiere.model.MCostElement) MAcctSchema(org.compiere.model.MAcctSchema) MCostType(org.compiere.model.MCostType) MWarehouse(org.compiere.model.MWarehouse)

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