* @param context Map containing the input parameters.
* @return Map with the result of the service, the output parameters.
*/
public static Map<String, Object> cancelProductionRun(DispatchContext ctx, Map<String, ? extends Object> context) {
Map<String, Object> result = FastMap.newInstance();
Delegator delegator = ctx.getDelegator();
LocalDispatcher dispatcher = ctx.getDispatcher();
Locale locale = (Locale) context.get("locale");
GenericValue userLogin = (GenericValue) context.get("userLogin");
String productionRunId = (String) context.get("productionRunId");
ProductionRun productionRun = new ProductionRun(productionRunId, delegator, dispatcher);
if (!productionRun.exist()) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingProductionRunNotExists", locale));
}
String currentStatusId = productionRun.getGenericValue().getString("currentStatusId");
// PRUN_CREATED, PRUN_DOC_PRINTED --> PRUN_CANCELLED
if (currentStatusId.equals("PRUN_CREATED") || currentStatusId.equals("PRUN_DOC_PRINTED") || currentStatusId.equals("PRUN_SCHEDULED")) {
try {
// First of all, make sure that there aren't production runs that depend on this one.
List<ProductionRun> mandatoryWorkEfforts = FastList.newInstance();
ProductionRunHelper.getLinkedProductionRuns(delegator, dispatcher, productionRunId, mandatoryWorkEfforts);
for (int i = 1; i < mandatoryWorkEfforts.size(); i++) {
GenericValue mandatoryWorkEffort = (mandatoryWorkEfforts.get(i)).getGenericValue();
if (!(mandatoryWorkEffort.getString("currentStatusId").equals("PRUN_CANCELLED"))) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingProductionRunStatusNotChangedMandatoryProductionRunFound", locale));
}
}
Map<String, Object> serviceContext = FastMap.newInstance();
// change the production run (header) status to PRUN_CANCELLED
serviceContext.put("workEffortId", productionRunId);
serviceContext.put("currentStatusId", "PRUN_CANCELLED");
serviceContext.put("userLogin", userLogin);
dispatcher.runSync("updateWorkEffort", serviceContext);
// Cancel the product promised
List<GenericValue> products = delegator.findByAnd("WorkEffortGoodStandard",
UtilMisc.toMap("workEffortId", productionRunId, "workEffortGoodStdTypeId", "PRUN_PROD_DELIV",
"statusId", "WEGS_CREATED"));
if (!UtilValidate.isEmpty(products)) {
for(GenericValue product : products) {
product.set("statusId", "WEGS_CANCELLED");
product.store();
}
}
// change the tasks status to PRUN_CANCELLED
List<GenericValue> tasks = productionRun.getProductionRunRoutingTasks();
String taskId = null;
for(GenericValue oneTask : tasks) {
taskId = oneTask.getString("workEffortId");
serviceContext.clear();
serviceContext.put("workEffortId", taskId);
serviceContext.put("currentStatusId", "PRUN_CANCELLED");
serviceContext.put("userLogin", userLogin);
dispatcher.runSync("updateWorkEffort", serviceContext);
// cancel all the components
List<GenericValue> components = delegator.findByAnd("WorkEffortGoodStandard",
UtilMisc.toMap("workEffortId", taskId, "workEffortGoodStdTypeId", "PRUNT_PROD_NEEDED",
"statusId", "WEGS_CREATED"));
if (!UtilValidate.isEmpty(components)) {
for(GenericValue component : components) {
component.set("statusId", "WEGS_CANCELLED");