use of org.compiere.model.MCost in project adempiere by adempiere.
the class CostEngine method getParentActualCostByCostType.
public static BigDecimal getParentActualCostByCostType(MAcctSchema accountSchema, MCostType costType, MCostElement costElement, I_M_Production production) {
// Get BOM Cost - Sum of individual lines
BigDecimal totalCost = Env.ZERO;
for (MProductionLine productionLine : ((MProduction) production).getLines()) {
if (productionLine.isParent())
continue;
String productType = productionLine.getM_Product().getProductType();
BigDecimal cost = BigDecimal.ZERO;
if (X_M_Product.PRODUCTTYPE_Item.equals(productType)) {
cost = MCostDetail.getCostByModel(accountSchema.getC_AcctSchema_ID(), costType.getM_CostType_ID(), costElement.getM_CostElement_ID(), productionLine);
} else if (X_M_Product.PRODUCTTYPE_Resource.equals(productType)) {
MCost costDimension = MCost.validateCostForCostType(accountSchema, costType, costElement, productionLine.getM_Product_ID(), productionLine.getAD_Org_ID(), productionLine.getM_Locator().getM_Warehouse_ID(), productionLine.getM_AttributeSetInstance_ID(), productionLine.get_TrxName());
if (costDimension != null && costDimension.getCurrentCostPrice().signum() != 0)
cost = costDimension.getCurrentCostPrice().multiply(productionLine.getMovementQty().negate());
}
if (cost != null && cost.signum() != 0)
totalCost = totalCost.add(cost);
}
BigDecimal unitCost = Env.ZERO;
if (production.getProductionQty().signum() != 0 && totalCost.signum() != 0)
unitCost = totalCost.divide(production.getProductionQty(), accountSchema.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
return unitCost;
}
use of org.compiere.model.MCost in project adempiere by adempiere.
the class AveragePOCostingMethod method getProductActualCostPrice.
public BigDecimal getProductActualCostPrice(MPPCostCollector costCollector, MProduct product, MAcctSchema acctSchema, MCostElement costElement, String trxName) {
String CostingLevel = product.getCostingLevel(acctSchema);
// Org Element
int orgId = 0;
int warehouseId = 0;
if (product.getS_Resource_ID() != 0) {
orgId = product.getS_Resource().getAD_Org_ID();
warehouseId = product.getS_Resource().getM_Warehouse_ID();
} else {
orgId = (costCollector == null) ? costElement.getAD_Org_ID() : costCollector.getAD_Org_ID();
warehouseId = (costCollector == null) ? 0 : costCollector.getM_Warehouse_ID();
}
int attributeSetInstanceId = (costCollector == null) ? 0 : costCollector.getM_AttributeSetInstance_ID();
if (MAcctSchema.COSTINGLEVEL_Client.equals(CostingLevel)) {
orgId = 0;
attributeSetInstanceId = 0;
warehouseId = 0;
} else if (MAcctSchema.COSTINGLEVEL_Organization.equals(CostingLevel))
attributeSetInstanceId = 0;
else if (MAcctSchema.COSTINGLEVEL_BatchLot.equals(CostingLevel))
orgId = 0;
CostDimension costDimension = new CostDimension(product, acctSchema, acctSchema.getM_CostType_ID(), orgId, attributeSetInstanceId, //warehouse
warehouseId, costElement.getM_CostElement_ID());
MCost cost = costDimension.toQuery(MCost.class, trxName).firstOnly();
if (cost == null)
return Env.ZERO;
BigDecimal price = cost.getCurrentCostPrice().add(cost.getCurrentCostPriceLL());
return roundCost(price, acctSchema.getC_AcctSchema_ID());
}
use of org.compiere.model.MCost in project adempiere by adempiere.
the class RollupBillOfMaterial method updateCoProductCosts.
/**
* Update costs for co-products on BOM Lines from given BOM
* @param bom product's BOM
* @param baseDimension base product cost (BOM Cost)
*/
private void updateCoProductCosts(MPPProductBOM bom, MCost baseDimension, String trxName) {
// Skip if not BOM found
if (bom == null)
return;
AtomicReference<BigDecimal> costPriceTotal = new AtomicReference<>(Env.ZERO);
//Iterate bom lines
Arrays.stream(bom.getLines()).filter(bomLine -> bomLine != null && bomLine.isCoProduct()).forEach(bomLine -> {
final BigDecimal costPrice = baseDimension.getFutureCostPriceLL().multiply(bomLine.getCostAllocationPerc(true));
MCost dimension = MCost.getDimension((MProduct) bomLine.getM_Product(), baseDimension.getC_AcctSchema_ID(), baseDimension.getAD_Org_ID(), baseDimension.getM_Warehouse_ID(), 0, baseDimension.getM_CostType_ID(), baseDimension.getM_CostElement_ID());
if (dimension == null) {
dimension = new MCost(baseDimension.getCtx(), 0, trxName);
dimension.setAD_Org_ID(baseDimension.getAD_Org_ID());
dimension.setM_Product_ID(bomLine.getM_Product_ID());
dimension.setM_Warehouse_ID(baseDimension.getM_Warehouse_ID());
dimension.setM_CostType_ID(baseDimension.getM_CostType_ID());
dimension.setC_AcctSchema_ID(baseDimension.getC_AcctSchema_ID());
dimension.setM_CostElement_ID(baseDimension.getM_CostElement_ID());
dimension.setM_AttributeSetInstance_ID(0);
}
dimension.setFutureCostPriceLL(costPrice);
if (!dimension.isCostFrozen())
dimension.setCurrentCostPriceLL(costPrice);
dimension.saveEx();
costPriceTotal.updateAndGet(costAmt -> costAmt.add(costPrice));
});
// Update Base Cost:
if (costPriceTotal.get().signum() != 0)
baseDimension.setFutureCostPriceLL(costPriceTotal.get());
}
use of org.compiere.model.MCost in project adempiere by adempiere.
the class RollupWorkflow method rollup.
/**
* Execute rollup process
* @param accountSchema
* @param costType
* @param costElement
* @param product
* @param workflow
* @param trxName
*/
protected void rollup(MAcctSchema accountSchema, MCostType costType, MCostElement costElement, MProduct product, MWorkflow workflow, String trxName) {
log.info("Workflow: " + workflow);
workflow.setCost(Env.ZERO);
double yield = 1;
int queuingTime = 0;
int setupTime = 0;
int duration = 0;
int waitingTime = 0;
int movingTime = 0;
int workingTime = 0;
MWFNode[] nodes = workflow.getNodes(false, getAD_Client_ID());
for (MWFNode node : nodes) {
node.setCost(Env.ZERO);
if (node.getYield() != 0) {
yield = yield * ((double) node.getYield() / 100);
}
// We use node.getDuration() instead of m_routingService.estimateWorkingTime(node) because
// this will be the minimum duration of this node. So even if the node have defined units/cycle
// we consider entire duration of the node.
long nodeDuration = node.getDuration();
queuingTime += node.getQueuingTime();
setupTime += node.getSetupTime();
duration += nodeDuration;
waitingTime += node.getWaitingTime();
movingTime += node.getMovingTime();
workingTime += node.getWorkingTime();
}
workflow.setCost(Env.ZERO);
workflow.setYield((int) (yield * 100));
workflow.setQueuingTime(queuingTime);
workflow.setSetupTime(setupTime);
workflow.setDuration(duration);
workflow.setWaitingTime(waitingTime);
workflow.setMovingTime(movingTime);
workflow.setWorkingTime(workingTime);
final CostDimension costDimension = new CostDimension(product, accountSchema, costType.getM_CostType_ID(), getOrganizationId(), getWarehouseId(), 0, costElement.get_ID());
MCost cost = MCost.getOrCreate(product, 0, accountSchema, getOrganizationId(), getWarehouseId(), costType.getM_CostType_ID(), costElement.getM_CostElement_ID());
cost.setFutureCostPrice(BigDecimal.ZERO);
if (!cost.isCostFrozen())
cost.setCurrentCostPrice(BigDecimal.ZERO);
AtomicReference<BigDecimal> segmentCost = new AtomicReference<>(Env.ZERO);
Arrays.stream(nodes).filter(node -> node != null).forEach(node -> {
final CostEngine costEngine = CostEngineFactory.getCostEngine(node.getAD_Client_ID());
final BigDecimal rate = StandardCostingMethod.getResourceActualCostRate(node.getS_Resource_ID(), costDimension, trxName);
final BigDecimal baseValue = routingService.getResourceBaseValue(node.getS_Resource_ID(), node);
BigDecimal nodeCostPrecision = baseValue.multiply(rate);
BigDecimal nodeCost;
if (nodeCostPrecision.scale() > accountSchema.getCostingPrecision())
nodeCost = nodeCostPrecision.setScale(accountSchema.getCostingPrecision(), RoundingMode.HALF_UP);
else
nodeCost = nodeCostPrecision;
segmentCost.updateAndGet(costAmt -> costAmt.add(nodeCost));
log.info(Msg.parseTranslation(getCtx(), " @M_CostElement_ID@ : ") + costElement.getName() + ", Node=" + node + ", BaseValue=" + baseValue + ", rate=" + rate + ", nodeCost=" + nodeCost + " => Cost=" + segmentCost);
node.setCost(node.getCost().add(nodeCost));
});
cost.setFutureCostPrice(segmentCost.get());
if (!cost.isCostFrozen())
cost.setCurrentCostPrice(segmentCost.get());
cost.saveEx();
// Update Workflow cost
workflow.setCost(workflow.getCost().add(segmentCost.get()));
// Save Workflow & Nodes
Arrays.stream(nodes).filter(node -> node != null).forEach(node -> node.saveEx());
workflow.saveEx();
log.info("Product: " + product.getName() + " WFCost: " + workflow.getCost());
}
use of org.compiere.model.MCost in project adempiere by adempiere.
the class CostBillOfMaterial method createLines.
/**
* createLines
*
* @param bom
* @param bomLine
*/
private void createLines(MAcctSchema accountSchema, MPPProductBOM bom, MPPProductBOMLine bomLine) {
MProduct product;
BigDecimal qty;
if (bomLine != null) {
product = MProduct.get(getCtx(), bomLine.getM_Product_ID());
qty = bomLine.getQty();
} else if (bom != null) {
product = MProduct.get(getCtx(), bom.getM_Product_ID());
qty = Env.ONE;
} else {
throw new AdempiereException("@NotFound@ @PP_Product_BOM_ID@");
}
//for (MCostElement costElement : getCostElements())
getCostElements().stream().filter(costElement -> costElement != null).forEach(costElement -> {
X_T_BOMLine reportBOMLine = new X_T_BOMLine(getCtx(), 0, get_TrxName());
reportBOMLine.setAD_Org_ID(getOrganizationId());
reportBOMLine.setM_Warehouse_ID(getWarehouseId());
reportBOMLine.setSel_Product_ID(getProductId());
reportBOMLine.setImplosion(isImplosion);
reportBOMLine.setC_AcctSchema_ID(getAccountingSchemaId());
reportBOMLine.setM_CostType_ID(getCostTypeId());
reportBOMLine.setCostingMethod(getCostingMethod());
reportBOMLine.setAD_PInstance_ID(getAD_PInstance_ID());
reportBOMLine.setM_CostElement_ID(costElement.get_ID());
reportBOMLine.setM_Product_ID(product.get_ID());
reportBOMLine.setM_Warehouse_ID(getWarehouseId());
reportBOMLine.setQtyBOM(qty);
reportBOMLine.setSeqNo(seqNo);
reportBOMLine.setLevelNo(levelNo);
reportBOMLine.setLevels(LEVELS.substring(0, levelNo) + levelNo);
BigDecimal currentCostPrice = Env.ZERO;
BigDecimal currentCostPriceLL = Env.ZERO;
BigDecimal futureCostPrice = Env.ZERO;
BigDecimal futureCostPriceLL = Env.ZERO;
final CostEngine engine = CostEngineFactory.getCostEngine(getAD_Client_ID());
List<MCost> costs = MCost.getByElement(product, accountSchema, getCostTypeId(), getOrganizationId(), getWarehouseId(), 0, costElement.getM_CostElement_ID());
boolean isCostFrozen = false;
for (MCost cost : costs) {
currentCostPrice = currentCostPrice.add(cost.getCurrentCostPrice());
currentCostPriceLL = currentCostPriceLL.add(cost.getCurrentCostPriceLL());
futureCostPrice = futureCostPrice.add(cost.getFutureCostPrice());
futureCostPriceLL = futureCostPriceLL.add(cost.getFutureCostPriceLL());
isCostFrozen = cost.isCostFrozen();
}
reportBOMLine.setCurrentCostPrice(currentCostPrice);
reportBOMLine.setCurrentCostPriceLL(currentCostPriceLL);
reportBOMLine.setFutureCostPrice(currentCostPrice);
reportBOMLine.setFutureCostPriceLL(currentCostPriceLL);
reportBOMLine.setIsCostFrozen(isCostFrozen);
if (bomLine != null) {
reportBOMLine.setPP_Product_BOM_ID(bomLine.getPP_Product_BOM_ID());
reportBOMLine.setPP_Product_BOMLine_ID(bomLine.getPP_Product_BOMLine_ID());
} else if (bom != null) {
reportBOMLine.setPP_Product_BOM_ID(bom.getPP_Product_BOM_ID());
}
reportBOMLine.saveEx();
seqNo++;
});
}
Aggregations