1 27 28 package org.ofbiz.manufacturing.jobshopmgt; 29 30 import java.sql.Timestamp ; 31 import java.util.Iterator ; 32 import java.util.LinkedList ; 33 import java.util.List ; 34 import java.util.Map ; 35 36 import org.ofbiz.base.util.Debug; 37 import org.ofbiz.base.util.UtilMisc; 38 import org.ofbiz.base.util.UtilValidate; 39 import org.ofbiz.entity.GenericDelegator; 40 import org.ofbiz.entity.GenericEntityException; 41 import org.ofbiz.entity.GenericValue; 42 import org.ofbiz.entity.util.EntityUtil; 43 import org.ofbiz.service.GenericServiceException; 44 import org.ofbiz.service.LocalDispatcher; 45 import org.ofbiz.manufacturing.techdata.TechDataServices; 46 47 48 54 public class ProductionRun { 55 56 public static final String module = ProductionRun.class.getName(); 57 public static final String resource = "ManufacturingUiLabels"; 58 59 protected GenericValue productionRun; protected GenericValue productionRunProduct; protected GenericValue productProduced; protected Double quantity; 64 protected Timestamp estimatedStartDate; 65 protected Timestamp estimatedCompletionDate; 66 protected String productionRunName; 67 protected String description; 68 protected GenericValue currentStatus; 69 protected List productionRunComponents; 70 protected List productionRunRoutingTasks; 71 protected LocalDispatcher dispatcher; 72 73 77 private boolean updateCompletionDate = false; 78 81 private boolean quantityIsUpdated = false; 82 83 public ProductionRun(String productionRunId, GenericDelegator delegator, LocalDispatcher dispatcher) { 84 try { 85 if (! UtilValidate.isEmpty(productionRunId)) { 86 this.dispatcher = dispatcher; 87 GenericValue workEffort = delegator.findByPrimaryKey("WorkEffort", UtilMisc.toMap("workEffortId", productionRunId)); 88 if (workEffort != null) { 89 if (workEffort.getString("workEffortTypeId") != null && "PROD_ORDER_TASK".equals(workEffort.getString("workEffortTypeId"))) { 91 workEffort = delegator.findByPrimaryKey("WorkEffort", UtilMisc.toMap("workEffortId", workEffort.getString("workEffortParentId"))); 92 } 93 } 94 this.productionRun = workEffort; 95 if (exist()) { 96 this.estimatedStartDate = productionRun.getTimestamp("estimatedStartDate"); 97 this.estimatedCompletionDate = productionRun.getTimestamp("estimatedCompletionDate"); 98 this.productionRunName = productionRun.getString("workEffortName"); 99 this.description = productionRun.getString("description"); 100 } 101 } 102 } catch (GenericEntityException e) { 103 Debug.logWarning(e.getMessage(), module); 104 } 105 } 106 107 111 public boolean exist(){ 112 return productionRun != null; 113 } 114 115 119 public GenericValue getGenericValue(){ 120 return productionRun; 121 } 122 130 public boolean store(){ 131 if (exist()){ 132 if (updateCompletionDate){ 133 this.estimatedCompletionDate = recalculateEstimatedCompletionDate(); 134 } 135 productionRun.set("estimatedStartDate",this.estimatedStartDate); 136 productionRun.set("estimatedCompletionDate",this.estimatedCompletionDate); 137 productionRun.set("workEffortName",this.productionRunName); 138 productionRun.set("description",this.description); 139 try { 140 productionRun.store(); 141 if (quantityIsUpdated) { 142 productionRunProduct.set("estimatedQuantity",this.quantity); 143 productionRunProduct.store(); 144 quantityIsUpdated = false; 145 } 146 if (productionRunRoutingTasks != null) { 147 for (Iterator iter = productionRunRoutingTasks.iterator(); iter.hasNext();){ 148 GenericValue routingTask = (GenericValue) iter.next(); 149 routingTask.store(); 150 } 151 } 152 if (productionRunComponents != null) { 153 for (Iterator iter = productionRunComponents.iterator(); iter.hasNext();){ 154 GenericValue component = (GenericValue) iter.next(); 155 component.store(); 156 } 157 } 158 } catch (GenericEntityException e) { 159 Debug.logWarning(e.getMessage(), module); 160 return false; 161 } 162 return true; 163 } 164 return false; 165 } 166 167 172 public GenericValue getProductProduced(){ 173 if (exist()) { 174 if (productProduced == null) { 175 try { 176 List productionRunProducts = productionRun.getRelated("WorkEffortGoodStandard", UtilMisc.toMap("workEffortGoodStdTypeId", "PRUN_PROD_DELIV"),null); 177 this.productionRunProduct = EntityUtil.getFirst(productionRunProducts); 178 quantity = productionRunProduct.getDouble("estimatedQuantity"); 179 productProduced = productionRunProduct.getRelatedOneCache("Product"); 180 } catch (GenericEntityException e) { 181 Debug.logWarning(e.getMessage(), module); 182 } 183 } 184 return productProduced; 185 } 186 return null; 187 } 188 189 193 public Double getQuantity(){ 194 if (exist()) { 195 if (quantity == null) getProductProduced(); 196 return quantity; 197 } 198 else return null; 199 } 200 204 public void setQuantity(Double newQuantity) { 205 if (quantity == null) getProductProduced(); 206 double previousQuantity = quantity.doubleValue(), componentQuantity; 207 this.quantity = newQuantity; 208 this.quantityIsUpdated = true; 209 this.updateCompletionDate = true; 210 if (productionRunComponents == null) getProductionRunComponents(); 211 for (Iterator iter = productionRunComponents.iterator(); iter.hasNext();){ 212 GenericValue component = (GenericValue) iter.next(); 213 componentQuantity = component.getDouble("estimatedQuantity").doubleValue(); 214 component.set("estimatedQuantity", new Double (Math.floor((componentQuantity / previousQuantity * newQuantity.doubleValue() ) + 0.5))); 215 } 216 return; 217 } 218 222 public Timestamp getEstimatedStartDate(){ 223 return (exist()? this.estimatedStartDate: null); 224 } 225 229 public void setEstimatedStartDate(Timestamp estimatedStartDate){ 230 this.estimatedStartDate = estimatedStartDate; 231 this.updateCompletionDate = true; 232 } 233 237 public Timestamp getEstimatedCompletionDate(){ 238 if (exist()) { 239 if (updateCompletionDate) { 240 this.estimatedCompletionDate = recalculateEstimatedCompletionDate(); 241 } 242 return this.estimatedCompletionDate; 243 } 244 else return null; 245 } 246 251 public void setEstimatedCompletionDate(Timestamp estimatedCompletionDate){ 252 this.estimatedCompletionDate = estimatedCompletionDate; 253 } 254 262 public Timestamp recalculateEstimatedCompletionDate(Long priority, Timestamp startDate){ 263 if (exist()) { 264 getProductionRunRoutingTasks(); 265 if (quantity == null) getQuantity(); 266 Timestamp endDate=null; 267 for (Iterator iter=productionRunRoutingTasks.iterator(); iter.hasNext();){ 268 GenericValue routingTask = (GenericValue) iter.next(); 269 if (priority.compareTo(routingTask.getLong("priority")) <= 0){ 270 long totalTime = ProductionRun.getEstimatedTaskTime(routingTask, quantity, dispatcher); 272 endDate = TechDataServices.addForward(TechDataServices.getTechDataCalendar(routingTask),startDate, totalTime); 273 routingTask.set("estimatedStartDate",startDate); 275 routingTask.set("estimatedCompletionDate",endDate); 276 startDate = endDate; 277 } 278 } 279 return endDate; 280 } else { 281 return null; 282 } 283 } 284 287 public Timestamp recalculateEstimatedCompletionDate(){ 288 this.updateCompletionDate = false; 289 return recalculateEstimatedCompletionDate(new Long (0), estimatedStartDate); 290 } 291 295 public String getProductionRunName(){ 296 if (exist()) return this.productionRunName; 297 else return null; 298 } 299 public void setProductionRunName(String name){ 300 this.productionRunName = name; 301 } 302 306 public String getDescription(){ 307 if (exist()) return productionRun.getString("description"); 308 else return null; 309 } 310 public void setDescription(String description){ 311 this.description = description; 312 } 313 317 public GenericValue getCurrentStatus(){ 318 if (exist()) { 319 if (currentStatus == null) { 320 try { 321 currentStatus = productionRun.getRelatedOneCache("StatusItem"); 322 } catch (GenericEntityException e) { 323 Debug.logWarning(e.getMessage(), module); 324 } 325 } 326 return currentStatus; 327 } 328 return null; 329 } 330 334 public List getProductionRunComponents(){ 335 if (exist()) { 336 if (productionRunComponents == null) { 337 if (productionRunRoutingTasks == null) this.getProductionRunRoutingTasks(); 338 if (productionRunRoutingTasks != null) { 339 try { 340 productionRunComponents = new LinkedList (); 341 GenericValue routingTask; 342 for (Iterator iter=productionRunRoutingTasks.iterator(); iter.hasNext();) { 343 routingTask = (GenericValue)iter.next(); 344 productionRunComponents.addAll(routingTask.getRelated("WorkEffortGoodStandard", UtilMisc.toMap("workEffortGoodStdTypeId", "PRUNT_PROD_NEEDED"),null)); 345 } 346 } catch (GenericEntityException e) { 347 Debug.logWarning(e.getMessage(), module); 348 } 349 } 350 } 351 return productionRunComponents; 352 } 353 return null; 354 } 355 359 public List getProductionRunRoutingTasks(){ 360 if (exist()) { 361 if (productionRunRoutingTasks == null) { 362 try { 363 productionRunRoutingTasks = productionRun.getRelated("ChildWorkEffort",UtilMisc.toMap("workEffortTypeId","PROD_ORDER_TASK"),UtilMisc.toList("priority")); 364 } catch (GenericEntityException e) { 365 Debug.logWarning(e.getMessage(), module); 366 } 367 } 368 return productionRunRoutingTasks; 369 } 370 return null; 371 } 372 373 377 public GenericValue getLastProductionRunRoutingTask(){ 378 if (exist()) { 379 if (productionRunRoutingTasks == null) { 380 try { 381 productionRunRoutingTasks = productionRun.getRelated("ChildWorkEffort",UtilMisc.toMap("workEffortTypeId","PROD_ORDER_TASK"),UtilMisc.toList("priority")); 382 } catch (GenericEntityException e) { 383 Debug.logWarning(e.getMessage(), module); 384 } 385 } 386 return (GenericValue)(productionRunRoutingTasks != null && productionRunRoutingTasks.size() > 0? productionRunRoutingTasks.get(productionRunRoutingTasks.size() - 1): null); 387 } 388 return null; 389 } 390 391 396 public void clearRoutingTasksList(){ 397 this.productionRunRoutingTasks = null; 398 } 399 400 404 public static long getEstimatedTaskTime(GenericValue task, double quantity, LocalDispatcher dispatcher) { 405 return getEstimatedTaskTime(task, new Double (quantity), dispatcher); 406 } 407 public static long getEstimatedTaskTime(GenericValue task, Double quantity, LocalDispatcher dispatcher) { 408 return getEstimatedTaskTime(task, quantity, null, null, dispatcher); 409 } 410 public static long getEstimatedTaskTime(GenericValue task, Double quantity, String productId, String routingId, LocalDispatcher dispatcher) { 411 if (quantity == null) { 412 quantity = new Double (1); 413 } 414 if (task == null) return 0; 415 double setupTime = 0; 416 double taskTime = 1; 417 double totalTaskTime = 0; 418 if (task.get("estimatedSetupMillis") != null) { 419 setupTime = task.getDouble("estimatedSetupMillis").doubleValue(); 420 } 421 if (task.get("estimatedMilliSeconds") != null) { 422 taskTime = task.getDouble("estimatedMilliSeconds").doubleValue(); 423 } 424 totalTaskTime = (setupTime + taskTime * quantity.doubleValue()); 425 if (task.get("estimateCalcMethod") != null) { 427 String serviceName = null; 428 try { 429 GenericValue genericService = task.getRelatedOne("CustomMethod"); 430 if (genericService != null && genericService.getString("customMethodName") != null) { 431 serviceName = genericService.getString("customMethodName"); 432 Map estimateCalcServiceMap = UtilMisc.toMap("workEffort", task, "quantity", quantity, "productId", productId, "routingId", routingId); 435 Map serviceContext = UtilMisc.toMap("arguments", estimateCalcServiceMap); 436 Map resultService = dispatcher.runSync(serviceName, serviceContext); 438 totalTaskTime = ((Double )resultService.get("totalTime")).doubleValue(); 439 } 440 } catch(Exception exc) { 441 Debug.logError(exc, "Problem calling the customMethod service " + serviceName); 442 } 443 } 444 445 return (long) totalTaskTime; 446 } 447 448 } 449 | Popular Tags |