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