1 25 package org.ofbiz.order.shoppinglist; 26 27 import java.util.Map ; 28 import java.util.List ; 29 import java.util.Locale ; 30 import java.util.Iterator ; 31 import java.util.HashMap ; 32 import java.sql.Timestamp ; 33 34 import org.ofbiz.service.DispatchContext; 35 import org.ofbiz.service.ServiceUtil; 36 import org.ofbiz.service.LocalDispatcher; 37 import org.ofbiz.service.GenericServiceException; 38 import org.ofbiz.service.calendar.RecurrenceInfo; 39 import org.ofbiz.service.calendar.RecurrenceInfoException; 40 import org.ofbiz.order.shoppingcart.ShoppingCart; 41 import org.ofbiz.order.shoppingcart.CartItemModifyException; 42 import org.ofbiz.order.shoppingcart.ItemNotFoundException; 43 import org.ofbiz.order.shoppingcart.CheckOutHelper; 44 import org.ofbiz.order.order.OrderReadHelper; 45 import org.ofbiz.entity.GenericDelegator; 46 import org.ofbiz.entity.GenericValue; 47 import org.ofbiz.entity.GenericEntityException; 48 import org.ofbiz.entity.condition.EntityExpr; 49 import org.ofbiz.entity.condition.EntityOperator; 50 import org.ofbiz.entity.condition.EntityCondition; 51 import org.ofbiz.entity.condition.EntityConditionList; 52 import org.ofbiz.entity.transaction.TransactionUtil; 53 import org.ofbiz.entity.util.EntityListIterator; 54 import org.ofbiz.entity.util.EntityUtil; 55 import org.ofbiz.base.util.UtilMisc; 56 import org.ofbiz.base.util.Debug; 57 import org.ofbiz.base.util.UtilValidate; 58 import org.ofbiz.base.util.UtilDateTime; 59 import org.ofbiz.base.util.GeneralException; 60 import org.ofbiz.base.util.UtilProperties; 61 import org.ofbiz.product.store.ProductStoreWorker; 62 63 70 public class ShoppingListServices { 71 72 public static final String module = ShoppingListServices.class.getName(); 73 public static final String resource_error = "OrderErrorUiLabels"; 74 75 public static Map setShoppingListRecurrence(DispatchContext dctx, Map context) { 76 GenericDelegator delegator = dctx.getDelegator(); 77 Timestamp startDate = (Timestamp ) context.get("startDateTime"); 78 Timestamp endDate = (Timestamp ) context.get("endDateTime"); 79 Integer frequency = (Integer ) context.get("frequency"); 80 Integer interval = (Integer ) context.get("intervalNumber"); 81 Locale locale = (Locale ) context.get("locale"); 82 83 if (frequency == null || interval == null) { 84 Debug.logWarning(UtilProperties.getMessage(resource_error,"OrderFrequencyOrIntervalWasNotSpecified", locale), module); 85 return ServiceUtil.returnSuccess(); 86 } 87 88 if (startDate == null) { 89 switch (frequency.intValue()) { 90 case 5: 91 startDate = UtilDateTime.getWeekStart(UtilDateTime.nowTimestamp(), 0, interval.intValue()); 92 break; 93 case 6: 94 startDate = UtilDateTime.getMonthStart(UtilDateTime.nowTimestamp(), 0, interval.intValue()); 95 break; 96 case 7: 97 startDate = UtilDateTime.getYearStart(UtilDateTime.nowTimestamp(), 0, interval.intValue()); 98 break; 99 default: 100 return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderInvalidFrequencyForShoppingListRecurrence",locale)); 101 } 102 } 103 104 long startTime = startDate.getTime(); 105 long endTime = 0; 106 if (endDate != null) { 107 endTime = endDate.getTime(); 108 } 109 110 RecurrenceInfo recInfo = null; 111 try { 112 recInfo = RecurrenceInfo.makeInfo(delegator, startTime, frequency.intValue(), interval.intValue(), -1, endTime); 113 } catch (RecurrenceInfoException e) { 114 Debug.logError(e, module); 115 return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderUnableToCreateShoppingListRecurrenceInformation",locale)); 116 } 117 118 Debug.log("Next Recurrence - " + UtilDateTime.getTimestamp(recInfo.next()), module); 119 Map result = ServiceUtil.returnSuccess(); 120 result.put("recurrenceInfoId", recInfo.getID()); 121 122 return result; 123 } 124 125 public static Map createListReorders(DispatchContext dctx, Map context) { 126 LocalDispatcher dispatcher = dctx.getDispatcher(); 127 GenericDelegator delegator = dctx.getDelegator(); 128 129 GenericValue userLogin = (GenericValue) context.get("userLogin"); 130 Locale locale = (Locale ) context.get("locale"); 131 132 boolean beganTransaction = false; 133 try { 134 beganTransaction = TransactionUtil.begin(); 135 136 List exprs = UtilMisc.toList(new EntityExpr("shoppingListTypeId", EntityOperator.EQUALS, "SLT_AUTO_REODR"), 137 new EntityExpr("isActive", EntityOperator.EQUALS, "Y")); 138 EntityCondition cond = new EntityConditionList(exprs, EntityOperator.AND); 139 List order = UtilMisc.toList("-lastOrderedDate"); 140 141 EntityListIterator eli = null; 142 eli = delegator.findListIteratorByCondition("ShoppingList", cond, null, order); 143 144 if (eli != null) { 145 GenericValue shoppingList; 146 while (((shoppingList = (GenericValue) eli.next()) != null)) { 147 Timestamp lastOrder = shoppingList.getTimestamp("lastOrderedDate"); 148 GenericValue recurrenceInfo = null; 149 recurrenceInfo = shoppingList.getRelatedOne("RecurrenceInfo"); 150 151 Timestamp startDateTime = recurrenceInfo.getTimestamp("startDateTime"); 152 RecurrenceInfo recurrence = null; 153 if (recurrenceInfo != null) { 154 try { 155 recurrence = new RecurrenceInfo(recurrenceInfo); 156 } catch (RecurrenceInfoException e) { 157 Debug.logError(e, module); 158 } 159 } 160 161 if (recurrence != null) { 163 long next = lastOrder == null ? recurrence.next(startDateTime.getTime()) : recurrence.next(lastOrder.getTime()); 164 Timestamp now = UtilDateTime.nowTimestamp(); 165 Timestamp nextOrder = UtilDateTime.getDayStart(UtilDateTime.getTimestamp(next)); 166 167 if (nextOrder.after(now)) { 168 continue; 169 } 170 } else { 171 continue; 172 } 173 174 ShoppingCart listCart = makeShoppingListCart(dispatcher, shoppingList, locale); 175 CheckOutHelper helper = new CheckOutHelper(dispatcher, delegator, listCart); 176 177 Map createResp = helper.createOrder(userLogin); 179 if (createResp != null && ServiceUtil.isError(createResp)) { 180 Debug.logError("Cannot create order for shopping list - " + shoppingList, module); 181 } else { 182 String orderId = (String ) createResp.get("orderId"); 183 184 Map payRes = null; 186 try { 187 payRes = helper.processPayment(ProductStoreWorker.getProductStore(listCart.getProductStoreId(), delegator), userLogin); 188 } catch (GeneralException e) { 189 Debug.logError(e, module); 190 } 191 192 if (payRes != null && ServiceUtil.isError(payRes)) { 193 Debug.logError("Payment processing problems with shopping list - " + shoppingList, module); 194 } 195 196 shoppingList.set("lastOrderedDate", UtilDateTime.nowTimestamp()); 197 shoppingList.store(); 198 199 try { 201 dispatcher.runAsync("sendOrderPayRetryNotification", UtilMisc.toMap("orderId", orderId)); 202 } catch (GenericServiceException e) { 203 Debug.logError(e, module); 204 } 205 206 recurrence.incrementCurrentCount(); 208 } 209 } 210 211 eli.close(); 212 } 213 214 return ServiceUtil.returnSuccess(); 215 } catch (GenericEntityException e) { 216 try { 217 TransactionUtil.rollback(beganTransaction, "Error creating shopping list auto-reorders", e); 219 } catch (GenericEntityException e2) { 220 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 221 } 222 223 String errMsg = "Error while creating new shopping list based automatic reorder" + e.toString(); 224 Debug.logError(e, errMsg, module); 225 return ServiceUtil.returnError(errMsg); 226 } finally { 227 try { 228 TransactionUtil.commit(beganTransaction); 230 } catch (GenericEntityException e) { 231 Debug.logError(e, "Could not commit transaction for creating new shopping list based automatic reorder", module); 232 } 233 } 234 } 235 236 public static Map splitShipmentMethodString(DispatchContext dctx, Map context) { 237 String shipmentMethodString = (String ) context.get("shippingMethodString"); 238 Map result = ServiceUtil.returnSuccess(); 239 240 if (UtilValidate.isNotEmpty(shipmentMethodString)) { 241 int delimiterPos = shipmentMethodString.indexOf('@'); 242 String shipmentMethodTypeId = null; 243 String carrierPartyId = null; 244 245 if (delimiterPos > 0) { 246 shipmentMethodTypeId = shipmentMethodString.substring(0, delimiterPos); 247 carrierPartyId = shipmentMethodString.substring(delimiterPos + 1); 248 result.put("shipmentMethodTypeId", shipmentMethodTypeId); 249 result.put("carrierPartyId", carrierPartyId); 250 } 251 } 252 return result; 253 } 254 255 public static Map makeListFromOrder(DispatchContext dctx, Map context) { 256 LocalDispatcher dispatcher = dctx.getDispatcher(); 257 GenericDelegator delegator = dctx.getDelegator(); 258 259 String shoppingListTypeId = (String ) context.get("shoppingListTypeId"); 260 String shoppingListId = (String ) context.get("shoppingListId"); 261 String orderId = (String ) context.get("orderId"); 262 String partyId = (String ) context.get("partyId"); 263 264 Timestamp startDate = (Timestamp ) context.get("startDateTime"); 265 Timestamp endDate = (Timestamp ) context.get("endDateTime"); 266 Integer frequency = (Integer ) context.get("frequency"); 267 Integer interval = (Integer ) context.get("intervalNumber"); 268 269 GenericValue userLogin = (GenericValue) context.get("userLogin"); 270 Locale locale = (Locale ) context.get("locale"); 271 272 boolean beganTransaction = false; 273 try { 274 beganTransaction = TransactionUtil.begin(); 275 276 GenericValue orderHeader = null; 277 orderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", orderId)); 278 279 if (orderHeader == null) { 280 return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderUnableToLocateOrder", UtilMisc.toMap("orderId",orderId), locale)); 281 } 282 String productStoreId = orderHeader.getString("productStoreId"); 283 284 if (UtilValidate.isEmpty(shoppingListId)) { 285 if (partyId == null) { 287 partyId = userLogin.getString("partyId"); 288 } 289 290 Map serviceCtx = UtilMisc.toMap("userLogin", userLogin, "partyId", partyId, 291 "productStoreId", productStoreId, "listName", "List Created From Order #" + orderId); 292 293 if (UtilValidate.isNotEmpty(shoppingListTypeId)) { 294 serviceCtx.put("shoppingListTypeId", shoppingListTypeId); 295 } 296 297 Map newListResult = null; 298 try { 299 300 newListResult = dispatcher.runSync("createShoppingList", serviceCtx); 301 } catch (GenericServiceException e) { 302 Debug.logError(e, "Problems creating new ShoppingList", module); 303 return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderUnableToCreateNewShoppingList",locale)); 304 } 305 306 if (ServiceUtil.isError(newListResult)) { 308 return ServiceUtil.returnError(ServiceUtil.getErrorMessage(newListResult)); 309 } 310 311 if (newListResult != null) { 313 shoppingListId = (String ) newListResult.get("shoppingListId"); 314 } 315 } 316 317 GenericValue shoppingList = null; 318 shoppingList = delegator.findByPrimaryKey("ShoppingList", UtilMisc.toMap("shoppingListId", shoppingListId)); 319 320 if (shoppingList == null) { 321 return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderNoShoppingListAvailable",locale)); 322 } 323 shoppingListTypeId = shoppingList.getString("shoppingListTypeId"); 324 325 OrderReadHelper orh = new OrderReadHelper(orderHeader); 326 if (orh == null) { 327 return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderUnableToLoadOrderReadHelper", UtilMisc.toMap("orderId",orderId), locale)); 328 } 329 330 List orderItems = orh.getOrderItems(); 331 Iterator i = orderItems.iterator(); 332 while (i.hasNext()) { 333 GenericValue orderItem = (GenericValue) i.next(); 334 if (orderItem.get("productId") != null) { 335 Map ctx = UtilMisc.toMap("userLogin", userLogin, "shoppingListId", shoppingListId, "productId", 336 orderItem.get("productId"), "quantity", orderItem.get("quantity")); 337 Map serviceResult = null; 338 try { 339 serviceResult = dispatcher.runSync("createShoppingListItem", ctx); 340 } catch (GenericServiceException e) { 341 Debug.logError(e, module); 342 } 343 if (serviceResult == null || ServiceUtil.isError(serviceResult)) { 344 return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderUnableToAddItemToShoppingList",UtilMisc.toMap("shoppingListId",shoppingListId), locale)); 345 } 346 } 347 } 348 349 if ("SLT_AUTO_REODR".equals(shoppingListTypeId)) { 350 GenericValue paymentPref = EntityUtil.getFirst(orh.getPaymentPreferences()); 351 GenericValue shipGroup = EntityUtil.getFirst(orh.getOrderItemShipGroups()); 352 353 Map slCtx = new HashMap (); 354 slCtx.put("shipmentMethodTypeId", shipGroup.get("shipmentMethodTypeId")); 355 slCtx.put("carrierRoleTypeId", shipGroup.get("carrierRoleTypeId")); 356 slCtx.put("carrierPartyId", shipGroup.get("carrierPartyId")); 357 slCtx.put("contactMechId", shipGroup.get("contactMechId")); 358 slCtx.put("paymentMethodId", paymentPref.get("paymentMethodId")); 359 slCtx.put("currencyUom", orh.getCurrency()); 360 slCtx.put("startDateTime", startDate); 361 slCtx.put("endDateTime", endDate); 362 slCtx.put("frequency", frequency); 363 slCtx.put("intervalNumber", interval); 364 slCtx.put("isActive", "Y"); 365 slCtx.put("shoppingListId", shoppingListId); 366 slCtx.put("userLogin", userLogin); 367 368 Map slUpResp = null; 369 try { 370 slUpResp = dispatcher.runSync("updateShoppingList", slCtx); 371 } catch (GenericServiceException e) { 372 Debug.logError(e, module); 373 } 374 375 if (slUpResp == null || ServiceUtil.isError(slUpResp)) { 376 return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderUnableToUpdateShoppingListInformation",UtilMisc.toMap("shoppingListId",shoppingListId), locale)); 377 } 378 } 379 380 Map result = ServiceUtil.returnSuccess(); 381 result.put("shoppingListId", shoppingListId); 382 return result; 383 384 } catch (GenericEntityException e) { 385 try { 386 TransactionUtil.rollback(beganTransaction, "Error making shopping list from order", e); 388 } catch (GenericEntityException e2) { 389 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 390 } 391 392 String errMsg = "Error while creating new shopping list based on order" + e.toString(); 393 Debug.logError(e, errMsg, module); 394 return ServiceUtil.returnError(errMsg); 395 } finally { 396 try { 397 TransactionUtil.commit(beganTransaction); 399 } catch (GenericEntityException e) { 400 Debug.logError(e, "Could not commit transaction for creating new shopping list based on order", module); 401 } 402 } 403 } 404 405 public static ShoppingCart makeShoppingListCart(LocalDispatcher dispatcher, GenericValue shoppingList, Locale locale) { 406 GenericDelegator delegator = dispatcher.getDelegator(); 407 ShoppingCart listCart = null; 408 if (shoppingList != null && shoppingList.get("productStoreId") != null) { 409 String productStoreId = shoppingList.getString("productStoreId"); 410 String currencyUom = shoppingList.getString("currencyUom"); 411 if (currencyUom == null) { 412 GenericValue productStore = ProductStoreWorker.getProductStore(productStoreId, delegator); 413 if (productStore == null) { 414 return null; 415 } 416 currencyUom = productStore.getString("defaultCurrencyUomId"); 417 } 418 if (locale == null) { 419 locale = Locale.getDefault(); 420 } 421 422 List items = null; 423 try { 424 items = shoppingList.getRelated("ShoppingListItem", UtilMisc.toList("shoppingListItemSeqId")); 425 } catch (GenericEntityException e) { 426 Debug.logError(e, module); 427 } 428 429 if (UtilValidate.isNotEmpty(items)) { 430 listCart = new ShoppingCart(delegator, productStoreId, locale, currencyUom); 431 listCart.setOrderPartyId(shoppingList.getString("partyId")); 432 433 Iterator i = items.iterator(); 434 while (i.hasNext()) { 435 GenericValue shoppingListItem = (GenericValue) i.next(); 436 String productId = shoppingListItem.getString("productId"); 437 Double quantity = shoppingListItem.getDouble("quantity"); 438 Timestamp reservStart = shoppingListItem.getTimestamp("reservStart"); 439 double reservLength = 0.00; 440 if (shoppingListItem.get("reservLength") != null) 441 reservLength = shoppingListItem.getDouble("reservLength").doubleValue(); 442 double reservPersons = 0.00; 443 if (shoppingListItem.get("reservPersons") != null) 444 reservPersons = shoppingListItem.getDouble("reservPersons").doubleValue(); 445 if (UtilValidate.isNotEmpty(productId) && quantity != null) { 446 String listId = shoppingListItem.getString("shoppingListId"); 448 String itemId = shoppingListItem.getString("shoppingListItemSeqId"); 449 Map attributes = UtilMisc.toMap("shoppingListId", listId, "shoppingListItemSeqId", itemId); 450 451 try { 452 listCart.addOrIncreaseItem(productId, quantity.doubleValue(), reservStart, reservLength, reservPersons, null, attributes, null, dispatcher); 453 } catch (CartItemModifyException e) { 454 Debug.logError(e, "Unable to add product to List Cart - " + productId, module); 455 } catch (ItemNotFoundException e) { 456 Debug.logError(e, "Product not found - " + productId, module); 457 } 458 } 459 } 460 461 if (listCart.size() > 0) { 462 if (shoppingList.get("paymentMethodId") != null) { 463 listCart.addPayment(shoppingList.getString("paymentMethodId")); 464 } 465 if (shoppingList.get("contactMechId") != null) { 466 listCart.setShippingContactMechId(0, shoppingList.getString("contactMechId")); 467 } 468 if (shoppingList.get("shipmentMethodTypeId") != null) { 469 listCart.setShipmentMethodTypeId(0, shoppingList.getString("shipmentMethodTypeId")); 470 } 471 if (shoppingList.get("carrierPartyId") != null) { 472 listCart.setCarrierPartyId(0, shoppingList.getString("carrierPartyId")); 473 } 474 } 475 } 476 } 477 return listCart; 478 } 479 480 public static ShoppingCart makeShoppingListCart(LocalDispatcher dispatcher, String shoppingListId, Locale locale) { 481 GenericDelegator delegator = dispatcher.getDelegator(); 482 GenericValue shoppingList = null; 483 try { 484 shoppingList = delegator.findByPrimaryKey("ShoppingList", UtilMisc.toMap("shoppingListId", shoppingListId)); 485 } catch (GenericEntityException e) { 486 Debug.logError(e, module); 487 } 488 return makeShoppingListCart(dispatcher, shoppingList, locale); 489 } 490 491 501 public static Map updateShoppingListQuantitiesFromOrder(DispatchContext ctx, Map context) { 502 Map result = new HashMap (); 503 GenericDelegator delegator = ctx.getDelegator(); 504 String orderId = (String ) context.get("orderId"); 505 try { 506 List orderItems = delegator.findByAnd("OrderItem", UtilMisc.toMap("orderId", orderId)); 507 Iterator iter = orderItems.iterator(); 508 while (iter.hasNext()) { 509 GenericValue orderItem = (GenericValue) iter.next(); 510 String shoppingListId = (String ) orderItem.getString("shoppingListId"); 511 String shoppingListItemSeqId = (String ) orderItem.getString("shoppingListItemSeqId"); 512 if ((shoppingListId != null) && (shoppingListId.length() > 0)) { 513 GenericValue shoppingListItem=delegator.findByPrimaryKey("ShoppingListItem", UtilMisc.toMap("shoppingListId", 514 shoppingListId, "shoppingListItemSeqId", shoppingListItemSeqId)); 515 if (shoppingListItem != null) { 516 Double quantityPurchased = shoppingListItem.getDouble("quantityPurchased"); 517 Double orderQuantity = orderItem.getDouble("quantity"); 518 if (quantityPurchased != null) { 519 shoppingListItem.set("quantityPurchased", new Double (orderQuantity.doubleValue() + quantityPurchased.doubleValue())); 520 }else{ 521 shoppingListItem.set("quantityPurchased", orderQuantity); 522 } 523 shoppingListItem.store(); 524 } 525 } 526 } 527 } catch (Exception e) { 528 Debug.log("updateShoppingListQuantitiesFromOrder error:"+e.getMessage()); 529 } 530 return result; 531 } 532 } 533 | Popular Tags |