use of org.compiere.wf.MWorkflow in project adempiere by adempiere.
the class RollupWorkflow method doIt.
// prepare
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());
routingService = RoutingServiceFactory.get().getRoutingService(getAD_Client_ID());
//Iterate product ids based on parameters
Arrays.stream(getProductIds()).filter(productId -> productId > 0).forEach(productId -> {
MProduct product = MProduct.get(getCtx(), productId);
log.info("Product: " + product);
int workflowId = 0;
MPPProductPlanning productPlanning = null;
if (workflowId <= 0)
workflowId = MWorkflow.getWorkflowSearchKey(product);
if (workflowId <= 0) {
productPlanning = MPPProductPlanning.find(getCtx(), getOrganizationId(), getWarehouseId(), getResourcePlantId(), product.get_ID(), get_TrxName());
if (productPlanning != null)
workflowId = productPlanning.getAD_Workflow_ID();
else
createNotice(product, "@NotFound@ @PP_Product_Planning_ID@");
}
if (workflowId <= 0)
createNotice(product, "@NotFound@ @AD_Workflow_ID@");
else {
Trx.run(new TrxRunnable() {
MAcctSchema accountSchema;
MCostType costType;
MProduct product;
MPPProductPlanning productPlanning;
int workflowId;
public TrxRunnable setParameters(MAcctSchema accountSchema, MCostType costType, MProduct product, MPPProductPlanning productPlanning, int workflowId) {
this.accountSchema = accountSchema;
this.costType = costType;
this.product = product;
this.productPlanning = productPlanning;
this.workflowId = workflowId;
return this;
}
public void run(String trxName) {
MWorkflow workflow = new MWorkflow(getCtx(), workflowId, trxName);
costElements.stream().filter(costElement -> costElement != null && CostEngine.isActivityControlElement(costElement)).forEach(costElement -> {
rollup(accountSchema, costType, costElement, product, workflow, trxName);
});
if (productPlanning != null) {
productPlanning.load(trxName);
productPlanning.setYield(workflow.getYield());
productPlanning.saveEx();
}
}
}.setParameters(accountSchema, costType, product, productPlanning, workflowId));
}
});
return "@OK@";
}
use of org.compiere.wf.MWorkflow 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.wf.MWorkflow in project adempiere by adempiere.
the class DefaultRoutingServiceImplTest method test_calculateWorkflowDuration.
public void test_calculateWorkflowDuration() {
MWorkflow wf = createWorkflow(1);
createNode(wf, "10", 0, 10, 0, 0);
createNode(wf, "20", 0, 10, 0, 0);
createNode(wf, "30", 0, 10, 0, 0);
wf = new MWorkflow(getCtx(), wf.get_ID(), getTrxName());
assertCalculateDuration(wf, 1000, 1000 * 10 + 1000 * 10 + 1000 * 10);
}
use of org.compiere.wf.MWorkflow in project adempiere by adempiere.
the class DefaultRoutingServiceImplTest method test_calculateWorkflowDuration_Overlap.
public void test_calculateWorkflowDuration_Overlap() {
MWorkflow wf = createWorkflow(1);
createNode(wf, "10", 0, 10, 0, 10);
createNode(wf, "20", 0, 10, 0, 10);
createNode(wf, "30", 0, 10, 0, 0);
wf = new MWorkflow(getCtx(), wf.get_ID(), getTrxName());
assertCalculateDuration(wf, 1000, 10 * 10 + 10 * 10 + 1000 * 10);
}
use of org.compiere.wf.MWorkflow in project adempiere by adempiere.
the class AbstractMakeToOrder method test01.
public void test01() throws Exception {
Qty = new BigDecimal(10);
//Define Product
product = MProduct.get(getCtx(), M_Product_ID);
//Define Business Partner
BPartner = new MBPartner(getCtx(), C_BPartner_ID, trxName);
//Setting the BOM
int PP_Product_BOM_ID = MPPProductBOM.getBOMSearchKey(product);
if (PP_Product_BOM_ID > 0)
bom = new MPPProductBOM(getCtx(), PP_Product_BOM_ID, trxName);
else
throw new AdempiereException("@NotFound@ @PP_ProductBOM_ID@");
if (bom != null) {
bom.setBOMType(MPPProductBOM.BOMTYPE_Make_To_Order);
bom.setBOMUse(MPPProductBOM.BOMUSE_Manufacturing);
bom.saveEx();
}
//Define Workflow as standardd
workflow = new MWorkflow(getCtx(), AD_Workflow_ID, trxName);
workflow.setValue(product.getValue());
workflow.saveEx();
//int workflow_id = MWorkflow.getWorkflowSearchKey(product);
if (AD_Workflow_ID > 0)
workflow = MWorkflow.get(getCtx(), AD_Workflow_ID);
else
throw new AdempiereException("@NotFound@ @AD_Workflow_ID@");
createOrder();
MPPOrder expected = createPPOrder();
I_PP_Order actual = MPPOrder.forC_OrderLine_ID(getCtx(), oline.get_ID(), oline.getM_Product_ID(), trxName);
if (actual == null) {
throw new AdempiereException("@NotFound@ @PP_Order_ID@ not was generate");
}
assertEquals("Confirming Manufacturing Order", expected, actual);
}
Aggregations