1 25 package org.ofbiz.order.shoppingcart; 26 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Locale ; 30 import java.util.Map ; 31 import java.util.HashMap ; 32 import java.util.LinkedList ; 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 58 public class ShoppingCartServices { 59 60 public static final String module = ShoppingCartServices.class.getName(); 61 public static final String resource = "OrderUiLabels"; 62 public static final String resource_error = "OrderErrorUiLabels"; 63 64 public static Map assignItemShipGroup(DispatchContext dctx, Map context) { 65 ShoppingCart cart = (ShoppingCart) context.get("shoppingCart"); 66 Integer fromGroupIndex = (Integer ) context.get("fromGroupIndex"); 67 Integer toGroupIndex = (Integer ) context.get("toGroupIndex"); 68 Integer itemIndex = (Integer ) context.get("itemIndex"); 69 Double quantity = (Double ) context.get("quantity"); 70 Locale locale = (Locale ) context.get("locale"); 71 72 Debug.log("From Group - " + fromGroupIndex + " To Group - " + toGroupIndex + "Item - " + itemIndex + "(" + quantity + ")", module); 73 if (fromGroupIndex.equals(toGroupIndex)) { 74 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 setShippingOptions(DispatchContext dctx, Map context) { 86 ShoppingCart cart = (ShoppingCart) context.get("shoppingCart"); 87 Integer groupIndex = (Integer ) context.get("groupIndex"); 88 String shippingContactMechId = (String ) context.get("shippingContactMechId"); 89 String shipmentMethodString = (String ) context.get("shipmentMethodString"); 90 String shippingInstructions = (String ) context.get("shippingInstructions"); 91 String giftMessage = (String ) context.get("giftMessage"); 92 Boolean maySplit = (Boolean ) context.get("maySplit"); 93 Boolean isGift = (Boolean ) context.get("isGift"); 94 Locale locale = (Locale ) 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 shipmentMethodTypeId = null; 103 String 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 setPaymentOptions(DispatchContext dctx, Map context) { 131 Locale locale = (Locale ) context.get("locale"); 132 133 return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderServiceNotYetImplemented",locale)); 134 } 135 136 public static Map setOtherOptions(DispatchContext dctx, Map context) { 137 ShoppingCart cart = (ShoppingCart) context.get("shoppingCart"); 138 String orderAdditionalEmails = (String ) context.get("orderAdditionalEmails"); 139 String correspondingPoId = (String ) context.get("correspondingPoId"); 140 Locale locale = (Locale ) 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 loadCartFromOrder(DispatchContext dctx, Map context) { 153 LocalDispatcher dispatcher = dctx.getDispatcher(); 154 GenericDelegator delegator = dctx.getDelegator(); 155 156 GenericValue userLogin = (GenericValue) context.get("userLogin"); 157 String orderId = (String ) context.get("orderId"); 158 Locale locale = (Locale ) context.get("locale"); 159 160 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 OrderReadHelper orh = new OrderReadHelper(orderHeader); 171 String productStoreId = orh.getProductStoreId(); 172 String orderTypeId = orh.getOrderTypeId(); 173 String currency = orh.getCurrency(); 174 String website = orh.getWebSiteId(); 175 176 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 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 List orderPaymentPrefs = null; 216 try { 217 List 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 oppi = orderPaymentPrefs.iterator(); 230 while (oppi.hasNext()) { 231 GenericValue opp = (GenericValue) oppi.next(); 232 String paymentId = opp.getString("paymentMethodId"); 233 if(paymentId==null) 234 paymentId = opp.getString("paymentMethodTypeId"); 235 Double maxAmount = opp.getDouble("maxAmount"); 236 String 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 orderItems = orh.getOrderItems(); 250 long nextItemSeq = 0; 251 if (orderItems != null) { 252 Iterator i = orderItems.iterator(); 253 while (i.hasNext()) { 254 GenericValue item = (GenericValue) i.next(); 255 256 String orderItemSeqId = item.getString("orderItemSeqId"); 258 try { 259 long seq = Long.parseLong(orderItemSeqId); 260 if (seq > nextItemSeq) { 261 nextItemSeq = seq; 262 } 263 } catch (NumberFormatException e) { 264 Debug.logError(e, module); 265 return ServiceUtil.returnError(e.getMessage()); 266 } 267 268 if (item.get("isPromo") != null && "Y".equals(item.getString("isPromo"))) { 270 continue; 271 } 272 273 Double amount = item.getDouble("selectedAmount"); 275 if (amount == null) { 276 amount = new Double (0); 277 } 278 Double quantity = OrderReadHelper.getOrderItemQuantity(item); 279 if (quantity == null) { 280 quantity = new Double (0); 281 } 282 int itemIndex = -1; 283 if (item.get("productId") == null) { 284 String itemType = item.getString("orderItemTypeId"); 286 String 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 String prodCatalogId = item.getString("prodCatalogId"); 296 String 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 ShoppingCartItem cartItem = cart.findCartItem(itemIndex); 310 cartItem.setOrderItemSeqId(item.getString("orderItemSeqId")); 311 312 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 cart.setPoNumber(item.getString("correspondingPoId")); 329 330 List shipGroups = orh.getOrderItemShipGroupAssocs(item); 332 for (int g = 0; g < shipGroups.size(); g++) { 333 GenericValue sgAssoc = (GenericValue) shipGroups.get(g); 334 Double shipGroupQty = OrderReadHelper.getOrderItemShipGroupQuantity(sgAssoc); 335 if (shipGroupQty == null) { 336 shipGroupQty = new Double (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 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 result = ServiceUtil.returnSuccess(); 370 result.put("shoppingCart", cart); 371 return result; 372 } 373 374 public static Map loadCartFromQuote(DispatchContext dctx, Map context) { 375 LocalDispatcher dispatcher = dctx.getDispatcher(); 376 GenericDelegator delegator = dctx.getDelegator(); 377 378 GenericValue userLogin = (GenericValue) context.get("userLogin"); 379 String quoteId = (String ) context.get("quoteId"); 380 String applyQuoteAdjustmentsString = (String ) context.get("applyQuoteAdjustments"); 381 Locale locale = (Locale ) context.get("locale"); 382 383 boolean applyQuoteAdjustments = applyQuoteAdjustmentsString == null || "true".equals(applyQuoteAdjustmentsString); 384 385 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 String productStoreId = quote.getString("productStoreId"); 396 String currency = quote.getString("currencyUomId"); 397 398 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 quoteItems = null; 411 List quoteAdjs = null; 412 List quoteRoles = null; 413 List 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 cart.setOrderPartyId(quote.getString("partyId")); 425 if (quoteRoles != null) { 426 Iterator quoteRolesIt = quoteRoles.iterator(); 427 while (quoteRolesIt.hasNext()) { 428 GenericValue quoteRole = (GenericValue)quoteRolesIt.next(); 429 String quoteRoleTypeId = quoteRole.getString("roleTypeId"); 430 String 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 if (quoteAttributes != null) { 447 Iterator 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 Map orderAdjsMap = new HashMap (); 457 Iterator quoteAdjsIter = quoteAdjs.iterator(); 458 while (quoteAdjsIter.hasNext()) { 459 GenericValue quoteAdj = (GenericValue)quoteAdjsIter.next(); 460 List orderAdjs = (List )orderAdjsMap.get(quoteAdj.get("quoteItemSeqId")); 461 if (orderAdjs == null) { 462 orderAdjs = new LinkedList (); 463 orderAdjsMap.put(quoteAdj.get("quoteItemSeqId"), orderAdjs); 464 } 465 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 i = quoteItems.iterator(); 497 while (i.hasNext()) { 498 GenericValue item = (GenericValue) i.next(); 499 500 String orderItemSeqId = item.getString("quoteItemSeqId"); 502 try { 503 long seq = Long.parseLong(orderItemSeqId); 504 if (seq > nextItemSeq) { 505 nextItemSeq = seq; 506 } 507 } catch (NumberFormatException 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 continue; 516 } 517 518 Double amount = item.getDouble("selectedAmount"); 520 if (amount == null) { 521 amount = new Double (0); 522 } 523 Double quantity = item.getDouble("quantity"); 524 if (quantity == null) { 525 quantity = new Double (0); 526 } 527 Double quoteUnitPrice = item.getDouble("quoteUnitPrice"); 528 if (quoteUnitPrice == null) { 529 quoteUnitPrice = new Double (0); 530 } 531 if (amount.doubleValue() > 0) { 532 quoteUnitPrice = new Double (quoteUnitPrice.doubleValue() / amount.doubleValue()); 535 } 536 int itemIndex = -1; 537 if (item.get("productId") == null) { 538 String 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 String 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 ShoppingCartItem cartItem = cart.findCartItem(itemIndex); 562 cartItem.setOrderItemSeqId(orderItemSeqId); 563 cartItem.setItemComment(item.getString("comments")); 565 cartItem.setQuoteId(item.getString("quoteId")); 566 cartItem.setQuoteItemSeqId(item.getString("quoteItemSeqId")); 567 cartItem.setIsPromo(isPromo); 568 } 574 575 } 576 577 if (applyQuoteAdjustments) { 579 List adjs = (List )orderAdjsMap.get(null); 581 if (adjs != null) { 582 cart.getAdjustments().addAll(adjs); 583 } 584 if (quoteItems != null) { 586 Iterator i = cart.iterator(); 587 while (i.hasNext()) { 588 ShoppingCartItem item = (ShoppingCartItem) i.next(); 589 adjs = (List )orderAdjsMap.get(item.getOrderItemSeqId()); 590 if (adjs != null) { 591 item.getAdjustments().addAll(adjs); 592 } 593 } 594 } 595 } 596 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 result = ServiceUtil.returnSuccess(); 607 result.put("shoppingCart", cart); 608 return result; 609 } 610 611 public static Map loadCartFromShoppingList(DispatchContext dctx, Map context) { 612 LocalDispatcher dispatcher = dctx.getDispatcher(); 613 GenericDelegator delegator = dctx.getDelegator(); 614 615 GenericValue userLogin = (GenericValue) context.get("userLogin"); 616 String shoppingListId = (String ) context.get("shoppingListId"); 617 Locale locale = (Locale ) context.get("locale"); 618 619 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 String productStoreId = shoppingList.getString("productStoreId"); 630 String currency = shoppingList.getString("currencyUom"); 631 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 (currency == null) { 645 currency = UtilProperties.getPropertyValue("general", "currency.uom.id.default", "USD"); 646 } 647 648 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 cart.setOrderPartyId(shoppingList.getString("partyId")); 660 661 List 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 i = shoppingListItems.iterator(); 672 while (i.hasNext()) { 673 GenericValue item = (GenericValue) i.next(); 674 675 String orderItemSeqId = item.getString("shoppingListItemSeqId"); 677 try { 678 long seq = Long.parseLong(orderItemSeqId); 679 if (seq > nextItemSeq) { 680 nextItemSeq = seq; 681 } 682 } catch (NumberFormatException e) { 683 Debug.logError(e, module); 684 return ServiceUtil.returnError(e.getMessage()); 685 } 686 692 Double quantity = item.getDouble("quantity"); 693 if (quantity == null) { 694 quantity = new Double (0); 695 } 696 int itemIndex = -1; 697 if (item.get("productId") != null) { 698 String productId = item.getString("productId"); 700 try { 701 itemIndex = cart.addItemToEnd(productId, 0.0, quantity.doubleValue(), null, new HashMap (), 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 ShoppingCartItem cartItem = cart.findCartItem(itemIndex); 713 cartItem.setOrderItemSeqId(orderItemSeqId); 714 cartItem.setShoppingList(item.getString("shoppingListId"), item.getString("shoppingListItemSeqId")); 716 } 717 718 } 719 720 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 result = ServiceUtil.returnSuccess(); 731 result.put("shoppingCart", cart); 732 return result; 733 } 734 } 735 | Popular Tags |