001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.shipment.packing;
019:
020: import java.util.Iterator;
021: import java.util.Map;
022:
023: import org.ofbiz.base.util.Debug;
024: import org.ofbiz.base.util.GeneralException;
025: import org.ofbiz.base.util.UtilValidate;
026: import org.ofbiz.base.util.UtilMisc;
027: import org.ofbiz.service.DispatchContext;
028: import org.ofbiz.service.ServiceUtil;
029:
030: public class PackingServices {
031:
032: public static final String module = PackingServices.class.getName();
033:
034: public static Map addPackLine(DispatchContext dctx, Map context) {
035: PackingSession session = (PackingSession) context
036: .get("packingSession");
037: String shipGroupSeqId = (String) context.get("shipGroupSeqId");
038: String orderId = (String) context.get("orderId");
039: String productId = (String) context.get("productId");
040: Double quantity = (Double) context.get("quantity");
041: Double weight = (Double) context.get("weight");
042: Integer packageSeq = (Integer) context.get("packageSeq");
043:
044: // set the instructions -- will clear out previous if now null
045: String instructions = (String) context
046: .get("handlingInstructions");
047: session.setHandlingInstructions(instructions);
048:
049: // set the picker party id -- will clear out previous if now null
050: String pickerPartyId = (String) context.get("pickerPartyId");
051: session.setPickerPartyId(pickerPartyId);
052:
053: if (quantity == null) {
054: quantity = new Double(1);
055: }
056:
057: Debug.log("OrderId [" + orderId + "] ship group ["
058: + shipGroupSeqId + "] Pack input [" + productId
059: + "] @ [" + quantity + "] packageSeq [" + packageSeq
060: + "] weight [" + weight + "]", module);
061:
062: if (weight == null) {
063: Debug.logWarning("OrderId [" + orderId + "] ship group ["
064: + shipGroupSeqId + "] product [" + productId
065: + "] being packed without a weight, assuming 0",
066: module);
067: weight = new Double(0.0);
068: }
069:
070: try {
071: session.addOrIncreaseLine(orderId, null, shipGroupSeqId,
072: productId, quantity.doubleValue(), packageSeq
073: .intValue(), weight.doubleValue(), false);
074: } catch (GeneralException e) {
075: Debug.logError(e, module);
076: return ServiceUtil.returnError(e.getMessage());
077: }
078:
079: return ServiceUtil.returnSuccess();
080: }
081:
082: public static Map packBulk(DispatchContext dctx, Map context) {
083: PackingSession session = (PackingSession) context
084: .get("packingSession");
085: String orderId = (String) context.get("orderId");
086: String shipGroupSeqId = (String) context.get("shipGroupSeqId");
087: Boolean updateQuantity = (Boolean) context
088: .get("updateQuantity");
089: if (updateQuantity == null) {
090: updateQuantity = Boolean.FALSE;
091: }
092:
093: // set the instructions -- will clear out previous if now null
094: String instructions = (String) context
095: .get("handlingInstructions");
096: session.setHandlingInstructions(instructions);
097:
098: // set the picker party id -- will clear out previous if now null
099: String pickerPartyId = (String) context.get("pickerPartyId");
100: session.setPickerPartyId(pickerPartyId);
101:
102: Map selInfo = (Map) context.get("selInfo");
103: Map prdInfo = (Map) context.get("prdInfo");
104: Map qtyInfo = (Map) context.get("qtyInfo");
105: Map pkgInfo = (Map) context.get("pkgInfo");
106: Map wgtInfo = (Map) context.get("wgtInfo");
107:
108: if (selInfo != null) {
109: Iterator i = selInfo.keySet().iterator();
110: while (i.hasNext()) {
111: String orderItemSeqId = (String) i.next();
112: String prdStr = (String) prdInfo.get(orderItemSeqId);
113: if (UtilValidate.isEmpty(prdStr)) {
114: // set the productId to null if empty
115: prdStr = null;
116: }
117:
118: // base package/quantity/weight strings
119: String pkgStr = (String) pkgInfo.get(orderItemSeqId);
120: String qtyStr = (String) qtyInfo.get(orderItemSeqId);
121: String wgtStr = (String) wgtInfo.get(orderItemSeqId);
122:
123: Debug.log("Item: " + orderItemSeqId + " / Product: "
124: + prdStr + " / Quantity: " + qtyStr
125: + " / Package: " + pkgStr + " / Weight: "
126: + wgtStr, module);
127:
128: // array place holders
129: String[] quantities;
130: String[] packages;
131: String[] weights;
132:
133: // process the package array
134: if (pkgStr.indexOf(",") != -1) {
135: // this is a multi-box update
136: packages = pkgStr.split(",");
137: } else {
138: packages = new String[] { pkgStr };
139: }
140:
141: // check to make sure there is at least one package
142: if (packages == null || packages.length == 0) {
143: return ServiceUtil
144: .returnError("No packages defined for processing.");
145: }
146:
147: // process the quantity array
148: if (qtyStr == null) {
149: quantities = new String[packages.length];
150: for (int p = 0; p < packages.length; p++) {
151: quantities[p] = (String) qtyInfo
152: .get(orderItemSeqId + ":" + packages[p]);
153: }
154: if (quantities.length != packages.length) {
155: return ServiceUtil
156: .returnError("Packages and quantities do not match.");
157: }
158: } else {
159: quantities = new String[] { qtyStr };
160: }
161:
162: // process the weight array
163: if (UtilValidate.isEmpty(wgtStr))
164: wgtStr = "0";
165: weights = new String[] { wgtStr };
166:
167: for (int p = 0; p < packages.length; p++) {
168: double quantity;
169: int packageSeq;
170: double weightSeq;
171: try {
172: quantity = Double.parseDouble(quantities[p]);
173: packageSeq = Integer.parseInt(packages[p]);
174: weightSeq = Double.parseDouble(weights[p]);
175: } catch (Exception e) {
176: return ServiceUtil.returnError(e.getMessage());
177: }
178:
179: try {
180: session.addOrIncreaseLine(orderId,
181: orderItemSeqId, shipGroupSeqId, prdStr,
182: quantity, packageSeq, weightSeq,
183: updateQuantity.booleanValue());
184: } catch (GeneralException e) {
185: Debug.logError(e, module);
186: return ServiceUtil.returnError(e.getMessage());
187: }
188: }
189: }
190: }
191:
192: return ServiceUtil.returnSuccess();
193: }
194:
195: public static Map incrementPackageSeq(DispatchContext dctx,
196: Map context) {
197: PackingSession session = (PackingSession) context
198: .get("packingSession");
199: int nextSeq = session.nextPackageSeq();
200: Map result = ServiceUtil.returnSuccess();
201: result.put("nextPackageSeq", new Integer(nextSeq));
202: return result;
203: }
204:
205: public static Map clearLastPackage(DispatchContext dctx, Map context) {
206: PackingSession session = (PackingSession) context
207: .get("packingSession");
208: int nextSeq = session.clearLastPackage();
209: Map result = ServiceUtil.returnSuccess();
210: result.put("nextPackageSeq", new Integer(nextSeq));
211: return result;
212: }
213:
214: public static Map clearPackLine(DispatchContext dctx, Map context) {
215: PackingSession session = (PackingSession) context
216: .get("packingSession");
217: String orderId = (String) context.get("orderId");
218: String orderItemSeqId = (String) context.get("orderItemSeqId");
219: String shipGroupSeqId = (String) context.get("shipGroupSeqId");
220: String inventoryItemId = (String) context
221: .get("inventoryItemId");
222: Integer packageSeqId = (Integer) context.get("packageSeqId");
223:
224: PackingSessionLine line = session.findLine(orderId,
225: orderItemSeqId, shipGroupSeqId, inventoryItemId,
226: packageSeqId.intValue());
227:
228: // remove the line
229: if (line != null) {
230: session.clearLine(line);
231: } else {
232: return ServiceUtil
233: .returnError("Packing line item not found; cannot clear.");
234: }
235:
236: return ServiceUtil.returnSuccess();
237: }
238:
239: public static Map clearPackAll(DispatchContext dctx, Map context) {
240: PackingSession session = (PackingSession) context
241: .get("packingSession");
242: session.clearAllLines();
243:
244: return ServiceUtil.returnSuccess();
245: }
246:
247: public static Map calcPackSessionAdditionalShippingCharge(
248: DispatchContext dctx, Map context) {
249: PackingSession session = (PackingSession) context
250: .get("packingSession");
251: Map packageWeights = (Map) context.get("packageWeights");
252: String weightUomId = (String) context.get("weightUomId");
253: String shippingContactMechId = (String) context
254: .get("shippingContactMechId");
255: String shipmentMethodTypeId = (String) context
256: .get("shipmentMethodTypeId");
257: String carrierPartyId = (String) context.get("carrierPartyId");
258: String carrierRoleTypeId = (String) context
259: .get("carrierRoleTypeId");
260: String productStoreId = (String) context.get("productStoreId");
261:
262: double shippableWeight = setSessionPackageWeights(session,
263: packageWeights);
264: Double estimatedShipCost = session.getShipmentCostEstimate(
265: shippingContactMechId, shipmentMethodTypeId,
266: carrierPartyId, carrierRoleTypeId, productStoreId,
267: null, null, new Double(shippableWeight), null);
268: session.setAdditionalShippingCharge(estimatedShipCost);
269: session.setWeightUomId(weightUomId);
270:
271: Map result = ServiceUtil.returnSuccess();
272: result.put("additionalShippingCharge", estimatedShipCost);
273: return result;
274: }
275:
276: public static Map completePack(DispatchContext dctx, Map context) {
277: PackingSession session = (PackingSession) context
278: .get("packingSession");
279:
280: // set the instructions -- will clear out previous if now null
281: String instructions = (String) context
282: .get("handlingInstructions");
283: String pickerPartyId = (String) context.get("pickerPartyId");
284: Double additionalShippingCharge = (Double) context
285: .get("additionalShippingCharge");
286: Map packageWeights = (Map) context.get("packageWeights");
287: String weightUomId = (String) context.get("weightUomId");
288: session.setHandlingInstructions(instructions);
289: session.setPickerPartyId(pickerPartyId);
290: session.setAdditionalShippingCharge(additionalShippingCharge);
291: session.setWeightUomId(weightUomId);
292: setSessionPackageWeights(session, packageWeights);
293:
294: Boolean force = (Boolean) context.get("forceComplete");
295: if (force == null) {
296: force = Boolean.FALSE;
297: }
298:
299: String shipmentId = null;
300: try {
301: shipmentId = session.complete(force.booleanValue());
302: } catch (GeneralException e) {
303: Debug.logError(e, module);
304: return ServiceUtil.returnError(e.getMessage(), e
305: .getMessageList());
306: }
307:
308: Map resp;
309: if ("EMPTY".equals(shipmentId)) {
310: resp = ServiceUtil
311: .returnError("No items currently set to be shipped. Cannot complete!");
312: } else {
313: resp = ServiceUtil.returnSuccess("Shipment #" + shipmentId
314: + " created and marked as PACKED.");
315: }
316:
317: resp.put("shipmentId", shipmentId);
318: return resp;
319: }
320:
321: private static double setSessionPackageWeights(
322: PackingSession session, Map packageWeights) {
323: double shippableWeight = 0;
324: if (!UtilValidate.isEmpty(packageWeights)) {
325: Iterator pwit = packageWeights.keySet().iterator();
326: while (pwit.hasNext()) {
327: String packageSeqId = (String) pwit.next();
328: String packageWeightStr = (String) packageWeights
329: .get(packageSeqId);
330: if (UtilValidate.isNotEmpty(packageWeightStr)) {
331: double packageWeight = UtilMisc
332: .toDouble(packageWeights.get(packageSeqId));
333: session.setPackageWeight(Integer
334: .parseInt(packageSeqId), new Double(
335: packageWeight));
336: shippableWeight += packageWeight;
337: } else {
338: session.setPackageWeight(Integer
339: .parseInt(packageSeqId), null);
340: }
341: }
342: }
343: return shippableWeight;
344: }
345: }
|