Timestamp lastOrder = shoppingList.getTimestamp("lastOrderedDate");
GenericValue recurrenceInfo = null;
recurrenceInfo = shoppingList.getRelatedOne("RecurrenceInfo");
Timestamp startDateTime = recurrenceInfo.getTimestamp("startDateTime");
RecurrenceInfo recurrence = null;
if (recurrenceInfo != null) {
try {
recurrence = new RecurrenceInfo(recurrenceInfo);
} catch (RecurrenceInfoException e) {
Debug.logError(e, module);
}
}
// check the next recurrence
if (recurrence != null) {
long next = lastOrder == null ? recurrence.next(startDateTime.getTime()) : recurrence.next(lastOrder.getTime());
Timestamp now = UtilDateTime.nowTimestamp();
Timestamp nextOrder = UtilDateTime.getDayStart(UtilDateTime.getTimestamp(next));
if (nextOrder.after(now)) {
continue;
}
} else {
continue;
}
ShoppingCart listCart = makeShoppingListCart(dispatcher, shoppingList, locale);
CheckOutHelper helper = new CheckOutHelper(dispatcher, delegator, listCart);
// store the order
Map<String, Object> createResp = helper.createOrder(userLogin);
if (createResp != null && ServiceUtil.isError(createResp)) {
Debug.logError("Cannot create order for shopping list - " + shoppingList, module);
} else {
String orderId = (String) createResp.get("orderId");
// authorize the payments
Map<String, Object> payRes = null;
try {
payRes = helper.processPayment(ProductStoreWorker.getProductStore(listCart.getProductStoreId(), delegator), userLogin);
} catch (GeneralException e) {
Debug.logError(e, module);
}
if (payRes != null && ServiceUtil.isError(payRes)) {
Debug.logError("Payment processing problems with shopping list - " + shoppingList, module);
}
shoppingList.set("lastOrderedDate", UtilDateTime.nowTimestamp());
shoppingList.store();
// send notification
try {
dispatcher.runAsync("sendOrderPayRetryNotification", UtilMisc.toMap("orderId", orderId));
} catch (GenericServiceException e) {
Debug.logError(e, module);
}
// increment the recurrence
recurrence.incrementCurrentCount();
}
}
eli.close();
}