Search in sources :

Example 16 with MCostType

use of org.compiere.model.MCostType 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 17 with MCostType

use of org.compiere.model.MCostType 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 18 with MCostType

use of org.compiere.model.MCostType 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 19 with MCostType

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

Example 20 with MCostType

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

the class CostResult method createDataMaster.

public void createDataMaster() {
    bp = (MBPartner) getEntity(I_C_BPartner.Table_Name, I_C_BPartner.COLUMNNAME_Value, "SeedFarm", trxName);
    bpls = MBPartnerLocation.getForBPartner(getCtx(), bp.getC_BPartner_ID(), trxName);
    w = (MWarehouse) getEntity(I_M_Warehouse.Table_Name, I_M_Warehouse.COLUMNNAME_Value, "HQ", trxName);
    u = (MUser) getEntity(I_AD_User.Table_Name, I_AD_User.COLUMNNAME_Name, "GardenAdmin", trxName);
    product = (MProduct) getEntity(I_M_Product.Table_Name, I_M_Product.COLUMNNAME_Value, "Oak", trxName);
    as = MAcctSchema.get(getCtx(), 101);
    today = Timestamp.valueOf("2010-01-01 00:00:00.0");
    if (bp == null || w == null || u == null)
        throw new AdempiereException("Object null ");
    for (MCostType ct : MCostType.get(getCtx(), trxName)) {
        if (!MCostType.COSTINGMETHOD_AverageInvoice.equals(ct.getCostingMethod())) {
            ct.setIsActive(false);
        }
    }
}
Also used : AdempiereException(org.adempiere.exceptions.AdempiereException) MCostType(org.compiere.model.MCostType)

Aggregations

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