use of org.adempiere.engine.CostEngine 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.adempiere.engine.CostEngine 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