1 23 24 package org.ofbiz.manufacturing.mrp; 25 26 import java.sql.Timestamp ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.ArrayList ; 31 import java.util.ListIterator ; 32 import java.util.Map ; 33 34 import org.ofbiz.base.util.Debug; 35 import org.ofbiz.base.util.UtilMisc; 36 import org.ofbiz.entity.GenericDelegator; 37 import org.ofbiz.entity.GenericEntityException; 38 import org.ofbiz.entity.GenericValue; 39 import org.ofbiz.entity.util.EntityUtil; 40 import org.ofbiz.manufacturing.jobshopmgt.ProductionRun; 41 import org.ofbiz.manufacturing.techdata.ProductHelper; 42 import org.ofbiz.manufacturing.techdata.TechDataServices; 43 import org.ofbiz.service.DispatchContext; 44 import org.ofbiz.service.GenericServiceException; 45 import org.ofbiz.service.LocalDispatcher; 46 47 import org.ofbiz.manufacturing.bom.BOMTree; 48 49 55 public class ProposedOrder { 56 57 public static final String module = ProposedOrder.class.getName(); 58 public static final String resource = "ManufacturingUiLabels"; 59 60 protected GenericValue product; 61 protected boolean isbuild; 62 protected String productId; 63 protected String facilityId; 64 protected String mrpName; 65 protected Timestamp requiredByDate; 66 protected Timestamp requirementStartDate; 67 protected double quantity; 68 69 70 public ProposedOrder(GenericValue product, String facilityId, boolean isbuild, Timestamp requiredByDate, double quantity) { 71 this.product = product; 72 this.productId = product.getString("productId"); 73 this.facilityId = facilityId; 74 this.isbuild = isbuild; 75 this.requiredByDate = requiredByDate; 76 this.quantity = quantity; 77 this.requirementStartDate = null; 78 } 79 83 public double getQuantity(){ 84 return quantity; 85 } 86 90 public Timestamp getRequirementStartDate(){ 91 return requirementStartDate; 92 } 93 104 public Map calculateStartDate(int daysToShip, GenericValue routing, GenericDelegator delegator, LocalDispatcher dispatcher, GenericValue userLogin){ 105 Map result = null; 106 Timestamp endDate = (Timestamp ) requiredByDate.clone(); 107 Timestamp startDate = endDate; 108 if (isbuild) { 109 List listRoutingTaskAssoc = null; 110 if (routing == null) { 111 try { 112 Map routingInMap = UtilMisc.toMap("productId", product.getString("productId"), "ignoreDefaultRouting", "Y", "userLogin", userLogin); 113 Map routingOutMap = dispatcher.runSync("getProductRouting", routingInMap); 114 routing = (GenericValue)routingOutMap.get("routing"); 115 if (routing == null) { 116 BOMTree tree = null; 118 ArrayList components = new ArrayList (); 119 try { 120 tree = new BOMTree(product.getString("productId"), "MANUF_COMPONENT", requiredByDate, BOMTree.EXPLOSION_SINGLE_LEVEL, delegator, dispatcher, userLogin); 121 tree.setRootQuantity(quantity); 122 tree.print(components, true); 123 if (components.size() > 0) components.remove(0); 124 } catch(Exception exc) { 125 Debug.logWarning(exc.getMessage(), module); 126 tree = null; 127 } 128 if (tree != null && tree.getRoot() != null && tree.getRoot().getProduct() != null) { 129 routingInMap = UtilMisc.toMap("productId", tree.getRoot().getProduct().getString("productId"), "userLogin", userLogin); 130 routingOutMap = dispatcher.runSync("getProductRouting", routingInMap); 131 routing = (GenericValue)routingOutMap.get("routing"); 132 } 133 } 134 listRoutingTaskAssoc = (List )routingOutMap.get("tasks"); 135 } catch(GenericServiceException gse) { 136 Debug.logWarning(gse.getMessage(), module); 137 } 138 } 139 if (routing != null && listRoutingTaskAssoc != null) { 140 result = new HashMap (); 141 for (int i = 1; i <= listRoutingTaskAssoc.size(); i++) { 143 GenericValue routingTaskAssoc = (GenericValue) listRoutingTaskAssoc.get(listRoutingTaskAssoc.size() - i); 144 if (EntityUtil.isValueActive(routingTaskAssoc, endDate)) { 145 GenericValue routingTask = null; 146 try { 147 routingTask = routingTaskAssoc.getRelatedOneCache("ToWorkEffort"); 148 } catch (GenericEntityException e) { 149 Debug.logError(e.getMessage(), module); 150 } 151 long totalTime = ProductionRun.getEstimatedTaskTime(routingTask, quantity, dispatcher); 153 startDate = TechDataServices.addBackward(TechDataServices.getTechDataCalendar(routingTask),endDate, totalTime); 154 result.put(routingTask.getString("workEffortId"),startDate); 156 endDate = startDate; 157 168 } 169 } 170 171 } else { 172 Debug.logError("Build product without routing for product = "+product.getString("productId"), module); 175 } 176 } else { 177 long duringTime = daysToShip * 8 * 60 * 60 * 1000; 180 try { 181 GenericValue techDataCalendar = product.getDelegator().findByPrimaryKeyCache("TechDataCalendar",UtilMisc.toMap("calendarId", "SUPPLIER")); 182 startDate = TechDataServices.addBackward(techDataCalendar, endDate, duringTime); 183 } catch(GenericEntityException e) { 184 Debug.logError(e, "Error : reading SUPPLIER TechDataCalendar"+"--"+e.getMessage(), module); 185 } 186 } 187 requirementStartDate = startDate; 188 return result; 189 } 190 191 192 196 public void calculateQuantityToSupply(double reorderQuantity, double minimumStock, ListIterator listIterIEP){ 198 if (quantity < reorderQuantity) { 201 quantity = reorderQuantity; 202 } 203 if (quantity < minimumStock) { 204 quantity = minimumStock; 205 } 206 } 207 208 213 public String create(DispatchContext ctx, GenericValue userLogin) { 214 LocalDispatcher dispatcher = ctx.getDispatcher(); 215 Map parameters = UtilMisc.toMap("userLogin", userLogin); 216 217 parameters.put("productId", productId); 218 parameters.put("facilityId", facilityId); 219 parameters.put("requiredByDate", requiredByDate); 220 parameters.put("requirementStartDate", requirementStartDate); 221 parameters.put("quantity", new Double (quantity)); 222 parameters.put("requirementTypeId", (isbuild? "MRP_PRO_PROD_ORDER" : "MRP_PRO_PURCH_ORDER")); 223 if (mrpName != null) { 224 parameters.put("description", "MRP_" + mrpName); 225 } else { 226 parameters.put("description", "Automatically generated by MRP"); 227 } 228 try{ 229 Map result = dispatcher.runSync("createRequirement", parameters); 230 return (String ) result.get("requirementId"); 231 } catch (GenericServiceException e) { 232 Debug.logError(e,"Error : createRequirement with parameters = "+parameters+"--"+e.getMessage(), module); 233 return null; 234 } 235 } 236 237 public void setMrpName(String mrpName) { 238 this.mrpName = mrpName; 239 } 240 } 241 | Popular Tags |