/** Create a replacement order from an existing order against a lost shipment etc. **/
public static String createReplacementOrder(HttpServletRequest request, HttpServletResponse response) {
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
Delegator delegator = (Delegator) request.getAttribute("delegator");
HttpSession session = request.getSession();
GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("shoppingCart");
Map<String, Object> context = cart.makeCartMap(dispatcher, false);
String originalOrderId = request.getParameter("orderId");
// create the replacement order adjustment
List <GenericValue>orderAdjustments = UtilGenerics.checkList(context.get("orderAdjustments"));
List <GenericValue>orderItems = UtilGenerics.checkList(context.get("orderItems"));
OrderReadHelper orderReadHelper = new OrderReadHelper(orderAdjustments, orderItems);
BigDecimal grandTotal = orderReadHelper.getOrderGrandTotal();
if (grandTotal.compareTo(new BigDecimal(0)) != 0) {
GenericValue adjustment = delegator.makeValue("OrderAdjustment");
adjustment.set("orderAdjustmentTypeId", "REPLACE_ADJUSTMENT");
adjustment.set("amount", grandTotal.negate());
adjustment.set("comments", "ReShip Order for Order #" + originalOrderId);
adjustment.set("createdDate", UtilDateTime.nowTimestamp());
adjustment.set("createdByUserLogin", userLogin.getString("userLoginId"));
cart.addAdjustment(adjustment);
}
// create the order association
List<ShoppingCartItem> cartLines = cart.items();
for (ShoppingCartItem sci : cartLines) {
int index = cart.getItemIndex(sci);
try {
Map<String, Object> orderItemMap = FastMap.newInstance();
orderItemMap.put("orderId", originalOrderId);
orderItemMap.put("isPromo", sci.getIsPromo() ? "Y" : "N");
orderItemMap.put("productId", sci.getProductId());
orderItemMap.put("orderItemTypeId", sci.getItemType());
GenericValue orderItem = EntityUtil.getFirst(delegator.findByAnd("OrderItem", orderItemMap));
if (UtilValidate.isNotEmpty(orderItem)) {
sci.setAssociatedOrderId(orderItem.getString("orderId"));
sci.setAssociatedOrderItemSeqId(orderItem.getString("orderItemSeqId"));
sci.setOrderItemAssocTypeId("REPLACEMENT");
cart.addItem(index, sci);
}
} catch (GenericEntityException e) {
Debug.logError(e, module);