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@";
}
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@";
}
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();
}
}
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;
}
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;
}
Aggregations