use of org.adempiere.engine.CostDimension 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.CostDimension in project adempiere by adempiere.
the class MPPOrder method createStandardCosts.
/**
* Save standard costs records into PP_Order_Cost.
* This will be usefull for calculating standard costs variances
*/
public final void createStandardCosts() {
MAcctSchema as = MClient.get(getCtx(), getAD_Client_ID()).getAcctSchema();
log.info("Cost_Group_ID" + as.getM_CostType_ID());
final TreeSet<Integer> productsAdded = new TreeSet<Integer>();
//
// Create Standard Costs for Order Header (resulting product)
{
final MProduct product = getM_Product();
productsAdded.add(product.getM_Product_ID());
final CostDimension d = new CostDimension(product, as, as.getM_CostType_ID(), getAD_Org_ID(), getM_Warehouse_ID(), getM_AttributeSetInstance_ID(), CostDimension.ANY);
Collection<MCost> costs = d.toQuery(MCost.class, get_TrxName()).list();
for (MCost cost : costs) {
//Create or Update the Order Cost dimension
MPPOrderCost.createOrderCostDimension(get_ID(), cost);
}
}
// Create Standard Costs for Order BOM Line
for (MPPOrderBOMLine line : getLines()) {
final MProduct product = line.getM_Product();
// Check if we already added this product
if (productsAdded.contains(product.getM_Product_ID()))
continue;
productsAdded.add(product.getM_Product_ID());
//
CostDimension d = new CostDimension(line.getM_Product(), as, as.getM_CostType_ID(), line.getAD_Org_ID(), getM_Warehouse_ID(), line.getM_AttributeSetInstance_ID(), CostDimension.ANY);
Collection<MCost> costs = d.toQuery(MCost.class, get_TrxName()).list();
for (MCost cost : costs) {
//Create or Update the Order Cost dimension
MPPOrderCost.createOrderCostDimension(get_ID(), cost);
}
}
// Create Standard Costs from Activity Resources
for (MPPOrderNode node : getMPPOrderWorkflow().getNodes(true)) {
final int S_Resource_ID = node.getS_Resource_ID();
if (S_Resource_ID <= 0)
continue;
final MProduct resourceProduct = MProduct.forS_Resource_ID(getCtx(), S_Resource_ID, null);
// Check if we already added this product
if (productsAdded.contains(resourceProduct.getM_Product_ID()))
continue;
productsAdded.add(resourceProduct.getM_Product_ID());
CostDimension d = new CostDimension(resourceProduct, as, as.getM_CostType_ID(), node.getAD_Org_ID(), getM_Warehouse_ID(), // ASI
0, CostDimension.ANY);
Collection<MCost> costs = d.toQuery(MCost.class, get_TrxName()).list();
for (MCost cost : costs) {
//Create or Update the Order Cost dimension
MPPOrderCost.createOrderCostDimension(get_ID(), cost);
}
}
}
use of org.adempiere.engine.CostDimension 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.adempiere.engine.CostDimension 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.adempiere.engine.CostDimension 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;
}
Aggregations