KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > manufacturing > mrp > ProposedOrder


1 /*
2  * $Id: ProposedOrder.java 7238 2006-04-08 07:30:54Z jacopo $
3  * Copyright (c) 2003, 2004 The Open For Business Project - www.ofbiz.org
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
20  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */

23
24 package org.ofbiz.manufacturing.mrp;
25
26 import java.sql.Timestamp JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.ListIterator JavaDoc;
32 import java.util.Map JavaDoc;
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 /**
50  * Proposed Order Object generated by the MRP process or other re-Order process
51  *
52  * @author <a HREF="mailto:olivier.heintz@nereide.biz">Olivier Heintz</a>
53  * @author <a HREF=mailto:thierry.grauss@etu.univ-tours.fr">Thierry GRAUSS</a>
54  */

55 public class ProposedOrder {
56     
57     public static final String JavaDoc module = ProposedOrder.class.getName();
58     public static final String JavaDoc resource = "ManufacturingUiLabels";
59     
60     protected GenericValue product;
61     protected boolean isbuild;
62     protected String JavaDoc productId;
63     protected String JavaDoc facilityId;
64     protected String JavaDoc mrpName;
65     protected Timestamp JavaDoc requiredByDate;
66     protected Timestamp JavaDoc requirementStartDate;
67     protected double quantity;
68     
69     
70     public ProposedOrder(GenericValue product, String JavaDoc facilityId, boolean isbuild, Timestamp JavaDoc 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     /**
80      * get the quantity property.
81      * @return the quantity property
82      **/

83     public double getQuantity(){
84         return quantity;
85     }
86     /**
87      * get the requirementStartDate property.
88      * @return the quantity property
89      **/

90     public Timestamp JavaDoc getRequirementStartDate(){
91         return requirementStartDate;
92     }
93     /**
94      * calculate the ProposedOrder requirementStartDate and update the requirementStartDate property.
95      * <li>For the build product, <ul>
96      * <li>read the routing associated to the product,
97      * <li>read the routingTask associated to the routing
98      * <li> step by step calculate from the endDate the startDate</ul>
99      * <li>For the bought product, the first ProductFacility.daysToShip is used to calculated the startDate
100      * @return <ul>
101      * <li>if ProposedOrder.isBuild a Map with all the routingTaskId as keys and estimatedStartDate as value.
102      * <li>else null.
103      **/

104     public Map JavaDoc calculateStartDate(int daysToShip, GenericValue routing, GenericDelegator delegator, LocalDispatcher dispatcher, GenericValue userLogin){
105         Map JavaDoc result = null;
106         Timestamp JavaDoc endDate = (Timestamp JavaDoc) requiredByDate.clone();
107         Timestamp JavaDoc startDate = endDate;
108         if (isbuild) {
109             List JavaDoc listRoutingTaskAssoc = null;
110             if (routing == null) {
111                 try {
112                     Map JavaDoc routingInMap = UtilMisc.toMap("productId", product.getString("productId"), "ignoreDefaultRouting", "Y", "userLogin", userLogin);
113                     Map JavaDoc routingOutMap = dispatcher.runSync("getProductRouting", routingInMap);
114                     routing = (GenericValue)routingOutMap.get("routing");
115                     if (routing == null) {
116                         // try to find a routing linked to the virtual product
117
BOMTree tree = null;
118                         ArrayList JavaDoc components = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc)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 JavaDoc();
141                 //Looks for all the routingTask (ordered by inversed (begin from the end) sequence number)
142
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                         // Calculate the estimatedStartDate
152
long totalTime = ProductionRun.getEstimatedTaskTime(routingTask, quantity, dispatcher);
153                         startDate = TechDataServices.addBackward(TechDataServices.getTechDataCalendar(routingTask),endDate, totalTime);
154                         // record the routingTask with the startDate associated
155
result.put(routingTask.getString("workEffortId"),startDate);
156                         endDate = startDate;
157                         /*
158                          * This is a work in progress
159                         GenericValue routingTask = null;
160                         try {
161                             Map timeInMap = UtilMisc.toMap("taskId", routingTaskAssoc.getString("workEffortIdTo"), "quantity", new Double(quantity), "userLogin", userLogin);
162                             Map timeOutMap = dispatcher.runSync("getEstimatedTaskTime", timeInMap);
163                             routingTask = (GenericValue)timeOutMap.get("routing");
164                         } catch (GenericServiceException gse) {
165                             Debug.logError(gse.getMessage(), module);
166                         }
167                         */

168                     }
169                 }
170
171             } else {
172                 // routing is null
173
// TODO : write an error message for the endUser to say "Build product without routing"
174
Debug.logError("Build product without routing for product = "+product.getString("productId"), module);
175             }
176         } else {
177             // the product is purchased
178
// TODO: REVIEW this code
179
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     /**
193      * calculate the ProposedOrder quantity and update the quantity property.
194      * Read the first ProductFacility.reorderQuantity and calculate the quantity : if (quantity < reorderQuantity) quantity = reorderQuantity;
195      **/

196     // FIXME: facilityId
197
public void calculateQuantityToSupply(double reorderQuantity, double minimumStock, ListIterator JavaDoc listIterIEP){
198         // TODO : use a better algorithm using Order management cost et Product Stock cost to calculate the re-order quantity
199
// the variable listIterIEP will be used for that
200
if (quantity < reorderQuantity) {
201             quantity = reorderQuantity;
202         }
203         if (quantity < minimumStock) {
204             quantity = minimumStock;
205         }
206     }
207     
208     /**
209      * create a ProposedOrder in the Requirement Entity calling the createRequirement service.
210      * @param ctx The DispatchContext used to call service to create the Requirement Entity record.
211      * @return String the requirementId
212      **/

213     public String JavaDoc create(DispatchContext ctx, GenericValue userLogin) {
214         LocalDispatcher dispatcher = ctx.getDispatcher();
215         Map JavaDoc 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 JavaDoc(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 JavaDoc result = dispatcher.runSync("createRequirement", parameters);
230             return (String JavaDoc) 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 JavaDoc mrpName) {
238         this.mrpName = mrpName;
239     }
240 }
241
Popular Tags