KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > order > shoppingcart > ShoppingCartServices


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

25 package org.ofbiz.order.shoppingcart;
26
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.LinkedList JavaDoc;
33
34 import org.ofbiz.base.util.Debug;
35 import org.ofbiz.base.util.UtilMisc;
36 import org.ofbiz.base.util.UtilProperties;
37 import org.ofbiz.base.util.UtilValidate;
38 import org.ofbiz.base.util.GeneralException;
39 import org.ofbiz.entity.GenericDelegator;
40 import org.ofbiz.entity.GenericEntityException;
41 import org.ofbiz.entity.GenericValue;
42 import org.ofbiz.entity.condition.EntityCondition;
43 import org.ofbiz.entity.condition.EntityConditionList;
44 import org.ofbiz.entity.condition.EntityExpr;
45 import org.ofbiz.entity.condition.EntityOperator;
46 import org.ofbiz.order.order.OrderReadHelper;
47 import org.ofbiz.service.DispatchContext;
48 import org.ofbiz.service.LocalDispatcher;
49 import org.ofbiz.service.ServiceUtil;
50
51 /**
52  * Shopping Cart Services
53  *
54  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
55  * @version $Rev: 7094 $
56  * @since 3.3
57  */

58 public class ShoppingCartServices {
59
60     public static final String JavaDoc module = ShoppingCartServices.class.getName();
61     public static final String JavaDoc resource = "OrderUiLabels";
62     public static final String JavaDoc resource_error = "OrderErrorUiLabels";
63
64     public static Map JavaDoc assignItemShipGroup(DispatchContext dctx, Map JavaDoc context) {
65         ShoppingCart cart = (ShoppingCart) context.get("shoppingCart");
66         Integer JavaDoc fromGroupIndex = (Integer JavaDoc) context.get("fromGroupIndex");
67         Integer JavaDoc toGroupIndex = (Integer JavaDoc) context.get("toGroupIndex");
68         Integer JavaDoc itemIndex = (Integer JavaDoc) context.get("itemIndex");
69         Double JavaDoc quantity = (Double JavaDoc) context.get("quantity");
70         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
71
72         Debug.log("From Group - " + fromGroupIndex + " To Group - " + toGroupIndex + "Item - " + itemIndex + "(" + quantity + ")", module);
73         if (fromGroupIndex.equals(toGroupIndex)) {
74             // nothing to do
75
return ServiceUtil.returnSuccess();
76         }
77
78         cart.positionItemToGroup(itemIndex.intValue(), quantity.doubleValue(),
79                 fromGroupIndex.intValue(), toGroupIndex.intValue());
80         Debug.log("Called cart.positionItemToGroup()", module);
81
82         return ServiceUtil.returnSuccess();
83     }
84
85     public static Map JavaDoc setShippingOptions(DispatchContext dctx, Map JavaDoc context) {
86         ShoppingCart cart = (ShoppingCart) context.get("shoppingCart");
87         Integer JavaDoc groupIndex = (Integer JavaDoc) context.get("groupIndex");
88         String JavaDoc shippingContactMechId = (String JavaDoc) context.get("shippingContactMechId");
89         String JavaDoc shipmentMethodString = (String JavaDoc) context.get("shipmentMethodString");
90         String JavaDoc shippingInstructions = (String JavaDoc) context.get("shippingInstructions");
91         String JavaDoc giftMessage = (String JavaDoc) context.get("giftMessage");
92         Boolean JavaDoc maySplit = (Boolean JavaDoc) context.get("maySplit");
93         Boolean JavaDoc isGift = (Boolean JavaDoc) context.get("isGift");
94         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
95
96         ShoppingCart.CartShipInfo csi = cart.getShipInfo(groupIndex.intValue());
97         if (csi != null) {
98             int idx = groupIndex.intValue();
99
100             if (UtilValidate.isNotEmpty(shipmentMethodString)) {
101                 int delimiterPos = shipmentMethodString.indexOf('@');
102                 String JavaDoc shipmentMethodTypeId = null;
103                 String JavaDoc carrierPartyId = null;
104
105                 if (delimiterPos > 0) {
106                     shipmentMethodTypeId = shipmentMethodString.substring(0, delimiterPos);
107                     carrierPartyId = shipmentMethodString.substring(delimiterPos + 1);
108                  }
109
110                 cart.setShipmentMethodTypeId(idx, shipmentMethodTypeId);
111                 cart.setCarrierPartyId(idx, carrierPartyId);
112             }
113
114             cart.setShippingInstructions(idx, shippingInstructions);
115             cart.setShippingContactMechId(idx, shippingContactMechId);
116             cart.setGiftMessage(idx, giftMessage);
117
118             if (maySplit != null) {
119                 cart.setMaySplit(idx, maySplit);
120             }
121             if (isGift != null) {
122                 cart.setIsGift(idx, isGift);
123             }
124         } else {
125             return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderCartShipGroupNotFound", UtilMisc.toMap("groupIndex",groupIndex), locale));
126         }
127         return ServiceUtil.returnSuccess();
128     }
129
130     public static Map JavaDoc setPaymentOptions(DispatchContext dctx, Map JavaDoc context) {
131         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
132
133         return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderServiceNotYetImplemented",locale));
134     }
135
136     public static Map JavaDoc setOtherOptions(DispatchContext dctx, Map JavaDoc context) {
137         ShoppingCart cart = (ShoppingCart) context.get("shoppingCart");
138         String JavaDoc orderAdditionalEmails = (String JavaDoc) context.get("orderAdditionalEmails");
139         String JavaDoc correspondingPoId = (String JavaDoc) context.get("correspondingPoId");
140         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
141
142         cart.setOrderAdditionalEmails(orderAdditionalEmails);
143         if (UtilValidate.isNotEmpty(correspondingPoId)) {
144             cart.setPoNumber(correspondingPoId);
145         } else {
146             cart.setPoNumber(null);
147         }
148
149         return ServiceUtil.returnSuccess();
150     }
151
152     public static Map JavaDoc loadCartFromOrder(DispatchContext dctx, Map JavaDoc context) {
153         LocalDispatcher dispatcher = dctx.getDispatcher();
154         GenericDelegator delegator = dctx.getDelegator();
155
156         GenericValue userLogin = (GenericValue) context.get("userLogin");
157         String JavaDoc orderId = (String JavaDoc) context.get("orderId");
158         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
159
160         // get the order header
161
GenericValue orderHeader = null;
162         try {
163             orderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", orderId));
164         } catch (GenericEntityException e) {
165             Debug.logError(e, module);
166             return ServiceUtil.returnError(e.getMessage());
167         }
168
169         // initial require cart info
170
OrderReadHelper orh = new OrderReadHelper(orderHeader);
171         String JavaDoc productStoreId = orh.getProductStoreId();
172         String JavaDoc orderTypeId = orh.getOrderTypeId();
173         String JavaDoc currency = orh.getCurrency();
174         String JavaDoc website = orh.getWebSiteId();
175
176         // create the cart
177
ShoppingCart cart = new ShoppingCart(delegator, productStoreId, website, locale, currency);
178         cart.setOrderType(orderTypeId);
179
180         try {
181             cart.setUserLogin(userLogin, dispatcher);
182         } catch (CartItemModifyException e) {
183             Debug.logError(e, module);
184             return ServiceUtil.returnError(e.getMessage());
185         }
186
187         // set the role information
188
GenericValue placingParty = orh.getPlacingParty();
189         if (placingParty != null) {
190             cart.setPlacingCustomerPartyId(placingParty.getString("partyId"));
191         }
192
193         GenericValue billFromParty = orh.getBillFromParty();
194         if (billFromParty != null) {
195             cart.setBillFromVendorPartyId(billFromParty.getString("partyId"));
196         }
197
198         GenericValue billToParty = orh.getBillToParty();
199         if (billToParty != null) {
200             cart.setBillToCustomerPartyId(billToParty.getString("partyId"));
201         }
202
203         GenericValue shipToParty = orh.getShipToParty();
204         if (shipToParty != null) {
205             cart.setShipToCustomerPartyId(shipToParty.getString("partyId"));
206         }
207
208         GenericValue endUserParty = orh.getEndUserParty();
209         if (endUserParty != null) {
210             cart.setEndUserCustomerPartyId(endUserParty.getString("partyId"));
211             cart.setOrderPartyId(endUserParty.getString("partyId"));
212         }
213
214         // load the payment infos
215
List JavaDoc orderPaymentPrefs = null;
216         try {
217             List JavaDoc exprs = UtilMisc.toList(new EntityExpr("orderId", EntityOperator.EQUALS, orderId));
218             exprs.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "PAYMENT_RECEIVED"));
219             exprs.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "PAYMENT_CANCELLED"));
220             exprs.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "PAYMENT_DECLINED"));
221             exprs.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "PAYMENT_SETTLED"));
222             EntityCondition cond = new EntityConditionList(exprs, EntityOperator.AND);
223             orderPaymentPrefs = delegator.findByCondition("OrderPaymentPreference", cond, null, null);
224         } catch (GenericEntityException e) {
225             Debug.logError(e, module);
226             return ServiceUtil.returnError(e.getMessage());
227         }
228         if (orderPaymentPrefs != null && orderPaymentPrefs.size() > 0) {
229             Iterator JavaDoc oppi = orderPaymentPrefs.iterator();
230             while (oppi.hasNext()) {
231                 GenericValue opp = (GenericValue) oppi.next();
232                 String JavaDoc paymentId = opp.getString("paymentMethodId");
233                 if(paymentId==null)
234                     paymentId = opp.getString("paymentMethodTypeId");
235                 Double JavaDoc maxAmount = opp.getDouble("maxAmount");
236                 String JavaDoc overflow = opp.getString("overflowFlag");
237                 if ((overflow == null || !"Y".equals(overflow)) && oppi.hasNext()) {
238                     cart.addPaymentAmount(paymentId, maxAmount);
239                     Debug.log("Added Payment: " + paymentId + " / " + maxAmount, module);
240                 } else {
241                     cart.addPayment(paymentId);
242                     Debug.log("Added Payment: " + paymentId + " / [no max]", module);
243                 }
244             }
245         } else {
246             Debug.log("No payment preferences found for order #" + orderId, module);
247         }
248
249         List JavaDoc orderItems = orh.getOrderItems();
250         long nextItemSeq = 0;
251         if (orderItems != null) {
252             Iterator JavaDoc i = orderItems.iterator();
253             while (i.hasNext()) {
254                 GenericValue item = (GenericValue) i.next();
255
256                 // get the next item sequence id
257
String JavaDoc orderItemSeqId = item.getString("orderItemSeqId");
258                 try {
259                     long seq = Long.parseLong(orderItemSeqId);
260                     if (seq > nextItemSeq) {
261                         nextItemSeq = seq;
262                     }
263                 } catch (NumberFormatException JavaDoc e) {
264                     Debug.logError(e, module);
265                     return ServiceUtil.returnError(e.getMessage());
266                 }
267
268                 // do not include PROMO items
269
if (item.get("isPromo") != null && "Y".equals(item.getString("isPromo"))) {
270                     continue;
271                 }
272
273                 // not a promo item; go ahead and add it in
274
Double JavaDoc amount = item.getDouble("selectedAmount");
275                 if (amount == null) {
276                     amount = new Double JavaDoc(0);
277                 }
278                 Double JavaDoc quantity = OrderReadHelper.getOrderItemQuantity(item);
279                 if (quantity == null) {
280                     quantity = new Double JavaDoc(0);
281                 }
282                 int itemIndex = -1;
283                 if (item.get("productId") == null) {
284                     // non-product item
285
String JavaDoc itemType = item.getString("orderItemTypeId");
286                     String JavaDoc desc = item.getString("itemDescription");
287                     try {
288                         itemIndex = cart.addNonProductItem(itemType, desc, null, 0.00, quantity.doubleValue(), null, null, dispatcher);
289                     } catch (CartItemModifyException e) {
290                         Debug.logError(e, module);
291                         return ServiceUtil.returnError(e.getMessage());
292                     }
293                 } else {
294                     // product item
295
String JavaDoc prodCatalogId = item.getString("prodCatalogId");
296                     String JavaDoc productId = item.getString("productId");
297                     try {
298                         itemIndex = cart.addItemToEnd(productId, amount.doubleValue(), quantity.doubleValue(), null, null, prodCatalogId, dispatcher);
299                     } catch (ItemNotFoundException e) {
300                         Debug.logError(e, module);
301                         return ServiceUtil.returnError(e.getMessage());
302                     } catch (CartItemModifyException e) {
303                         Debug.logError(e, module);
304                         return ServiceUtil.returnError(e.getMessage());
305                     }
306                 }
307
308                 // flag the item w/ the orderItemSeqId so we can reference it
309
ShoppingCartItem cartItem = cart.findCartItem(itemIndex);
310                 cartItem.setOrderItemSeqId(item.getString("orderItemSeqId"));
311
312                 // attach addition item information
313
cartItem.setStatusId(item.getString("statusId"));
314                 cartItem.setItemType(item.getString("orderItemTypeId"));
315                 cartItem.setItemComment(item.getString("comments"));
316                 cartItem.setQuoteId(item.getString("quoteId"));
317                 cartItem.setQuoteItemSeqId(item.getString("quoteItemSeqId"));
318                 cartItem.setProductCategoryId(item.getString("productCategoryId"));
319                 cartItem.setDesiredDeliveryDate(item.getTimestamp("estimatedDeliveryDate"));
320                 cartItem.setShipBeforeDate(item.getTimestamp("shipBeforeDate"));
321                 cartItem.setShipAfterDate(item.getTimestamp("shipAfterDate"));
322                 cartItem.setShoppingList(item.getString("shoppingListId"), item.getString("shoppingListItemSeqId"));
323                 cartItem.setIsModifiedPrice("Y".equals(item.getString("isModifiedPrice")));
324                 if(cartItem.getIsModifiedPrice())
325                     cartItem.setBasePrice(item.getDouble("unitPrice").doubleValue());
326                 
327                 // set the PO number on the cart
328
cart.setPoNumber(item.getString("correspondingPoId"));
329
330                 // set the item's ship group info
331
List JavaDoc shipGroups = orh.getOrderItemShipGroupAssocs(item);
332                 for (int g = 0; g < shipGroups.size(); g++) {
333                     GenericValue sgAssoc = (GenericValue) shipGroups.get(g);
334                     Double JavaDoc shipGroupQty = OrderReadHelper.getOrderItemShipGroupQuantity(sgAssoc);
335                     if (shipGroupQty == null) {
336                         shipGroupQty = new Double JavaDoc(0);
337                     }
338
339                     GenericValue sg = null;
340                     try {
341                         sg = sgAssoc.getRelatedOne("OrderItemShipGroup");
342                     } catch (GenericEntityException e) {
343                         Debug.logError(e, module);
344                         return ServiceUtil.returnError(e.getMessage());
345                     }
346                     cart.setShipAfterDate(g, sg.getTimestamp("shipAfterDate"));
347                     cart.setShipBeforeDate(g, sg.getTimestamp("shipByDate"));
348                     cart.setShipmentMethodTypeId(g, sg.getString("shipmentMethodTypeId"));
349                     cart.setCarrierPartyId(g, sg.getString("carrierPartyId"));
350                     cart.setMaySplit(g, sg.getBoolean("maySplit"));
351                     cart.setGiftMessage(g, sg.getString("giftMessage"));
352                     cart.setShippingContactMechId(g, sg.getString("contactMechId"));
353                     cart.setShippingInstructions(g, sg.getString("shippingInstructions"));
354                     cart.setItemShipGroupQty(itemIndex, shipGroupQty.doubleValue(), g);
355                 }
356             }
357
358             // set the item seq in the cart
359
if (nextItemSeq > 0) {
360                 try {
361                     cart.setNextItemSeq(nextItemSeq);
362                 } catch (GeneralException e) {
363                     Debug.logError(e, module);
364                     return ServiceUtil.returnError(e.getMessage());
365                 }
366             }
367         }
368
369         Map JavaDoc result = ServiceUtil.returnSuccess();
370         result.put("shoppingCart", cart);
371         return result;
372     }
373     
374     public static Map JavaDoc loadCartFromQuote(DispatchContext dctx, Map JavaDoc context) {
375         LocalDispatcher dispatcher = dctx.getDispatcher();
376         GenericDelegator delegator = dctx.getDelegator();
377
378         GenericValue userLogin = (GenericValue) context.get("userLogin");
379         String JavaDoc quoteId = (String JavaDoc) context.get("quoteId");
380         String JavaDoc applyQuoteAdjustmentsString = (String JavaDoc) context.get("applyQuoteAdjustments");
381         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
382
383         boolean applyQuoteAdjustments = applyQuoteAdjustmentsString == null || "true".equals(applyQuoteAdjustmentsString);
384         
385         // get the quote header
386
GenericValue quote = null;
387         try {
388             quote = delegator.findByPrimaryKey("Quote", UtilMisc.toMap("quoteId", quoteId));
389         } catch (GenericEntityException e) {
390             Debug.logError(e, module);
391             return ServiceUtil.returnError(e.getMessage());
392         }
393
394         // initial require cart info
395
String JavaDoc productStoreId = quote.getString("productStoreId");
396         String JavaDoc currency = quote.getString("currencyUomId");
397
398         // create the cart
399
ShoppingCart cart = new ShoppingCart(delegator, productStoreId, locale, currency);
400
401         try {
402             cart.setUserLogin(userLogin, dispatcher);
403         } catch (CartItemModifyException e) {
404             Debug.logError(e, module);
405             return ServiceUtil.returnError(e.getMessage());
406         }
407
408         cart.setQuoteId(quoteId);
409         
410         List JavaDoc quoteItems = null;
411         List JavaDoc quoteAdjs = null;
412         List JavaDoc quoteRoles = null;
413         List JavaDoc quoteAttributes = null;
414         try {
415             quoteItems = quote.getRelated("QuoteItem");
416             quoteAdjs = quote.getRelated("QuoteAdjustment");
417             quoteRoles = quote.getRelated("QuoteRole");
418             quoteAttributes = quote.getRelated("QuoteAttribute");
419         } catch (GenericEntityException e) {
420             Debug.logError(e, module);
421             return ServiceUtil.returnError(e.getMessage());
422         }
423         // set the role information
424
cart.setOrderPartyId(quote.getString("partyId"));
425         if (quoteRoles != null) {
426             Iterator JavaDoc quoteRolesIt = quoteRoles.iterator();
427             while (quoteRolesIt.hasNext()) {
428                 GenericValue quoteRole = (GenericValue)quoteRolesIt.next();
429                 String JavaDoc quoteRoleTypeId = quoteRole.getString("roleTypeId");
430                 String JavaDoc quoteRolePartyId = quoteRole.getString("partyId");
431                 if ("PLACING_CUSTOMER".equals(quoteRoleTypeId)) {
432                     cart.setPlacingCustomerPartyId(quoteRolePartyId);
433                 } else if ("BILL_TO_CUSTOMER".equals(quoteRoleTypeId)) {
434                     cart.setBillToCustomerPartyId(quoteRolePartyId);
435                 } else if ("SHIP_TO_CUSTOMER".equals(quoteRoleTypeId)) {
436                     cart.setShipToCustomerPartyId(quoteRolePartyId);
437                 } else if ("END_USER_CUSTOMER".equals(quoteRoleTypeId)) {
438                     cart.setEndUserCustomerPartyId(quoteRolePartyId);
439                 } else {
440                     cart.addAdditionalPartyRole(quoteRolePartyId, quoteRoleTypeId);
441                 }
442             }
443         }
444
445         // set the attribute information
446
if (quoteAttributes != null) {
447             Iterator JavaDoc quoteAttributesIt = quoteAttributes.iterator();
448             while (quoteAttributesIt.hasNext()) {
449                 GenericValue quoteAttribute = (GenericValue)quoteAttributesIt.next();
450                 cart.setOrderAttribute(quoteAttribute.getString("attrName"), quoteAttribute.getString("attrValue"));
451             }
452         }
453
454         // Convert the quote adjustment to order header adjustments and
455
// put them in a map: the key/values pairs are quoteItemSeqId/List of adjs
456
Map JavaDoc orderAdjsMap = new HashMap JavaDoc();
457         Iterator JavaDoc quoteAdjsIter = quoteAdjs.iterator();
458         while (quoteAdjsIter.hasNext()) {
459             GenericValue quoteAdj = (GenericValue)quoteAdjsIter.next();
460             List JavaDoc orderAdjs = (List JavaDoc)orderAdjsMap.get(quoteAdj.get("quoteItemSeqId"));
461             if (orderAdjs == null) {
462                 orderAdjs = new LinkedList JavaDoc();
463                 orderAdjsMap.put(quoteAdj.get("quoteItemSeqId"), orderAdjs);
464             }
465             // convert quote adjustments to order adjustments
466
GenericValue orderAdj = delegator.makeValue("OrderAdjustment", null);
467             orderAdj.put("orderAdjustmentId", quoteAdj.get("quoteAdjustmentId"));
468             orderAdj.put("orderAdjustmentTypeId", quoteAdj.get("quoteAdjustmentTypeId"));
469             orderAdj.put("orderItemSeqId", quoteAdj.get("quoteItemSeqId"));
470             orderAdj.put("comments", quoteAdj.get("comments"));
471             orderAdj.put("description", quoteAdj.get("description"));
472             orderAdj.put("amount", quoteAdj.get("amount"));
473             orderAdj.put("productPromoId", quoteAdj.get("productPromoId"));
474             orderAdj.put("productPromoRuleId", quoteAdj.get("productPromoRuleId"));
475             orderAdj.put("productPromoActionSeqId", quoteAdj.get("productPromoActionSeqId"));
476             orderAdj.put("productFeatureId", quoteAdj.get("productFeatureId"));
477             orderAdj.put("correspondingProductId", quoteAdj.get("correspondingProductId"));
478             orderAdj.put("sourceReferenceId", quoteAdj.get("sourceReferenceId"));
479             orderAdj.put("sourcePercentage", quoteAdj.get("sourcePercentage"));
480             orderAdj.put("customerReferenceId", quoteAdj.get("customerReferenceId"));
481             orderAdj.put("primaryGeoId", quoteAdj.get("primaryGeoId"));
482             orderAdj.put("secondaryGeoId", quoteAdj.get("secondaryGeoId"));
483             orderAdj.put("exemptAmount", quoteAdj.get("exemptAmount"));
484             orderAdj.put("taxAuthGeoId", quoteAdj.get("taxAuthGeoId"));
485             orderAdj.put("taxAuthPartyId", quoteAdj.get("taxAuthPartyId"));
486             orderAdj.put("overrideGlAccountId", quoteAdj.get("overrideGlAccountId"));
487             orderAdj.put("includeInTax", quoteAdj.get("includeInTax"));
488             orderAdj.put("includeInShipping", quoteAdj.get("includeInShipping"));
489             orderAdj.put("createdDate", quoteAdj.get("createdDate"));
490             orderAdj.put("createdByUserLogin", quoteAdj.get("createdByUserLogin"));
491             orderAdjs.add(orderAdj);
492         }
493
494         long nextItemSeq = 0;
495         if (quoteItems != null) {
496             Iterator JavaDoc i = quoteItems.iterator();
497             while (i.hasNext()) {
498                 GenericValue item = (GenericValue) i.next();
499
500                 // get the next item sequence id
501
String JavaDoc orderItemSeqId = item.getString("quoteItemSeqId");
502                 try {
503                     long seq = Long.parseLong(orderItemSeqId);
504                     if (seq > nextItemSeq) {
505                         nextItemSeq = seq;
506                     }
507                 } catch (NumberFormatException JavaDoc e) {
508                     Debug.logError(e, module);
509                     return ServiceUtil.returnError(e.getMessage());
510                 }
511
512                 boolean isPromo = item.get("isPromo") != null && "Y".equals(item.getString("isPromo"));
513                 if (isPromo && !applyQuoteAdjustments) {
514                     // do not include PROMO items
515
continue;
516                 }
517
518                 // not a promo item; go ahead and add it in
519
Double JavaDoc amount = item.getDouble("selectedAmount");
520                 if (amount == null) {
521                     amount = new Double JavaDoc(0);
522                 }
523                 Double JavaDoc quantity = item.getDouble("quantity");
524                 if (quantity == null) {
525                     quantity = new Double JavaDoc(0);
526                 }
527                 Double JavaDoc quoteUnitPrice = item.getDouble("quoteUnitPrice");
528                 if (quoteUnitPrice == null) {
529                     quoteUnitPrice = new Double JavaDoc(0);
530                 }
531                 if (amount.doubleValue() > 0) {
532                     // If, in the quote, an amount is set, we need to
533
// pass to the cart the quoteUnitPrice/amount value.
534
quoteUnitPrice = new Double JavaDoc(quoteUnitPrice.doubleValue() / amount.doubleValue());
535                 }
536                 int itemIndex = -1;
537                 if (item.get("productId") == null) {
538                     // non-product item
539
String JavaDoc desc = item.getString("comments");
540                     try {
541                         itemIndex = cart.addNonProductItem(null, desc, null, 0.00, quantity.doubleValue(), null, null, dispatcher);
542                     } catch (CartItemModifyException e) {
543                         Debug.logError(e, module);
544                         return ServiceUtil.returnError(e.getMessage());
545                     }
546                 } else {
547                     // product item
548
String JavaDoc productId = item.getString("productId");
549                     try {
550                         itemIndex = cart.addItemToEnd(productId, amount.doubleValue(), quantity.doubleValue(), quoteUnitPrice.doubleValue(), null, null, null, dispatcher, !applyQuoteAdjustments, (quoteUnitPrice.doubleValue() == 0));
551                     } catch (ItemNotFoundException e) {
552                         Debug.logError(e, module);
553                         return ServiceUtil.returnError(e.getMessage());
554                     } catch (CartItemModifyException e) {
555                         Debug.logError(e, module);
556                         return ServiceUtil.returnError(e.getMessage());
557                     }
558                 }
559
560                 // flag the item w/ the orderItemSeqId so we can reference it
561
ShoppingCartItem cartItem = cart.findCartItem(itemIndex);
562                 cartItem.setOrderItemSeqId(orderItemSeqId);
563                 // attach additional item information
564
cartItem.setItemComment(item.getString("comments"));
565                 cartItem.setQuoteId(item.getString("quoteId"));
566                 cartItem.setQuoteItemSeqId(item.getString("quoteItemSeqId"));
567                 cartItem.setIsPromo(isPromo);
568                 //cartItem.setDesiredDeliveryDate(item.getTimestamp("estimatedDeliveryDate"));
569
//cartItem.setStatusId(item.getString("statusId"));
570
//cartItem.setItemType(item.getString("orderItemTypeId"));
571
//cartItem.setProductCategoryId(item.getString("productCategoryId"));
572
//cartItem.setShoppingList(item.getString("shoppingListId"), item.getString("shoppingListItemSeqId"));
573
}
574
575         }
576
577         // If applyQuoteAdjustments is set to false then standard cart adjustments are used.
578
if (applyQuoteAdjustments) {
579             // The cart adjustments, derived from quote adjustments, are added to the cart
580
List JavaDoc adjs = (List JavaDoc)orderAdjsMap.get(null);
581             if (adjs != null) {
582                 cart.getAdjustments().addAll(adjs);
583             }
584             // The cart item adjustments, derived from quote item adjustments, are added to the cart
585
if (quoteItems != null) {
586                 Iterator JavaDoc i = cart.iterator();
587                 while (i.hasNext()) {
588                     ShoppingCartItem item = (ShoppingCartItem) i.next();
589                     adjs = (List JavaDoc)orderAdjsMap.get(item.getOrderItemSeqId());
590                     if (adjs != null) {
591                         item.getAdjustments().addAll(adjs);
592                     }
593                 }
594             }
595         }
596         // set the item seq in the cart
597
if (nextItemSeq > 0) {
598             try {
599                 cart.setNextItemSeq(nextItemSeq);
600             } catch (GeneralException e) {
601                 Debug.logError(e, module);
602                 return ServiceUtil.returnError(e.getMessage());
603             }
604         }
605
606         Map JavaDoc result = ServiceUtil.returnSuccess();
607         result.put("shoppingCart", cart);
608         return result;
609     }
610
611     public static Map JavaDoc loadCartFromShoppingList(DispatchContext dctx, Map JavaDoc context) {
612         LocalDispatcher dispatcher = dctx.getDispatcher();
613         GenericDelegator delegator = dctx.getDelegator();
614
615         GenericValue userLogin = (GenericValue) context.get("userLogin");
616         String JavaDoc shoppingListId = (String JavaDoc) context.get("shoppingListId");
617         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
618
619         // get the shopping list header
620
GenericValue shoppingList = null;
621         try {
622             shoppingList = delegator.findByPrimaryKey("ShoppingList", UtilMisc.toMap("shoppingListId", shoppingListId));
623         } catch (GenericEntityException e) {
624             Debug.logError(e, module);
625             return ServiceUtil.returnError(e.getMessage());
626         }
627
628         // initial required cart info
629
String JavaDoc productStoreId = shoppingList.getString("productStoreId");
630         String JavaDoc currency = shoppingList.getString("currencyUom");
631         // If no currency has been set in the ShoppingList, use the ProductStore default currency
632
if (currency == null) {
633             try {
634                 GenericValue productStore = shoppingList.getRelatedOne("ProductStore");
635                 if (productStore != null) {
636                     currency = productStore.getString("defaultCurrencyUomId");
637                 }
638             } catch (GenericEntityException e) {
639                 Debug.logError(e, module);
640                 return ServiceUtil.returnError(e.getMessage());
641             }
642         }
643         // If we still have no currency, use the default from general.properties. Failing that, use USD
644
if (currency == null) {
645                 currency = UtilProperties.getPropertyValue("general", "currency.uom.id.default", "USD");
646         }
647
648         // create the cart
649
ShoppingCart cart = new ShoppingCart(delegator, productStoreId, locale, currency);
650
651         try {
652             cart.setUserLogin(userLogin, dispatcher);
653         } catch (CartItemModifyException e) {
654             Debug.logError(e, module);
655             return ServiceUtil.returnError(e.getMessage());
656         }
657
658         // set the role information
659
cart.setOrderPartyId(shoppingList.getString("partyId"));
660
661         List JavaDoc shoppingListItems = null;
662         try {
663             shoppingListItems = shoppingList.getRelated("ShoppingListItem");
664         } catch (GenericEntityException e) {
665             Debug.logError(e, module);
666             return ServiceUtil.returnError(e.getMessage());
667         }
668
669         long nextItemSeq = 0;
670         if (shoppingListItems != null) {
671             Iterator JavaDoc i = shoppingListItems.iterator();
672             while (i.hasNext()) {
673                 GenericValue item = (GenericValue) i.next();
674
675                 // get the next item sequence id
676
String JavaDoc orderItemSeqId = item.getString("shoppingListItemSeqId");
677                 try {
678                     long seq = Long.parseLong(orderItemSeqId);
679                     if (seq > nextItemSeq) {
680                         nextItemSeq = seq;
681                     }
682                 } catch (NumberFormatException JavaDoc e) {
683                     Debug.logError(e, module);
684                     return ServiceUtil.returnError(e.getMessage());
685                 }
686                 /*
687                 Double amount = item.getDouble("selectedAmount");
688                 if (amount == null) {
689                     amount = new Double(0);
690                 }
691                  */

692                 Double JavaDoc quantity = item.getDouble("quantity");
693                 if (quantity == null) {
694                     quantity = new Double JavaDoc(0);
695                 }
696                 int itemIndex = -1;
697                 if (item.get("productId") != null) {
698                     // product item
699
String JavaDoc productId = item.getString("productId");
700                     try {
701                         itemIndex = cart.addItemToEnd(productId, 0.0, quantity.doubleValue(), null, new HashMap JavaDoc(), null, dispatcher);
702                     } catch (ItemNotFoundException e) {
703                         Debug.logError(e, module);
704                         return ServiceUtil.returnError(e.getMessage());
705                     } catch (CartItemModifyException e) {
706                         Debug.logError(e, module);
707                         return ServiceUtil.returnError(e.getMessage());
708                     }
709                 }
710
711                 // flag the item w/ the orderItemSeqId so we can reference it
712
ShoppingCartItem cartItem = cart.findCartItem(itemIndex);
713                 cartItem.setOrderItemSeqId(orderItemSeqId);
714                 // attach additional item information
715
cartItem.setShoppingList(item.getString("shoppingListId"), item.getString("shoppingListItemSeqId"));
716             }
717
718         }
719
720         // set the item seq in the cart
721
if (nextItemSeq > 0) {
722             try {
723                 cart.setNextItemSeq(nextItemSeq);
724             } catch (GeneralException e) {
725                 Debug.logError(e, module);
726                 return ServiceUtil.returnError(e.getMessage());
727             }
728         }
729
730         Map JavaDoc result = ServiceUtil.returnSuccess();
731         result.put("shoppingCart", cart);
732         return result;
733     }
734 }
735
Popular Tags