private static void addCartDetails(NVPEncoder encoder, ShoppingCart cart) throws GenericEntityException { encoder.add("CURRENCYCODE", cart.getCurrency()); int line = 0; for (ShoppingCartItem item : cart.items()) { encoder.add("L_NUMBER" + line, item.getProductId()); encoder.add("L_NAME" + line, item.getName()); encoder.add( "L_AMT" + line, item.getBasePrice().setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()); encoder.add("L_QTY" + line, item.getQuantity().toBigInteger().toString()); line++; BigDecimal otherAdjustments = item.getOtherAdjustments(); if (otherAdjustments.compareTo(BigDecimal.ZERO) != 0) { encoder.add("L_NUMBER" + line, item.getProductId()); encoder.add("L_NAME" + line, item.getName() + " Adjustments"); encoder.add( "L_AMT" + line, otherAdjustments.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()); encoder.add("L_QTY" + line, "1"); line++; } } BigDecimal otherAdjustments = cart.getOrderOtherAdjustmentTotal(); if (otherAdjustments.compareTo(BigDecimal.ZERO) != 0) { encoder.add("L_NUMBER" + line, "N/A"); encoder.add("L_NAME" + line, "Order Adjustments"); encoder.add( "L_AMT" + line, otherAdjustments.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()); encoder.add("L_QTY" + line, "1"); line++; } encoder.add("ITEMAMT", cart.getSubTotal().add(otherAdjustments).setScale(2).toPlainString()); encoder.add("SHIPPINGAMT", "0.00"); encoder.add("TAXAMT", "0.00"); encoder.add("AMT", cart.getSubTotal().add(otherAdjustments).setScale(2).toPlainString()); // NOTE: The docs say this is optional but then won't work without it encoder.add("MAXAMT", cart.getSubTotal().add(otherAdjustments).setScale(2).toPlainString()); }
public static String addBulkFromCart( Delegator delegator, LocalDispatcher dispatcher, ShoppingCart cart, GenericValue userLogin, String shoppingListId, String shoppingListTypeId, String[] items, boolean allowPromo, boolean append) throws IllegalArgumentException { String errMsg = null; if (items == null || items.length == 0) { errMsg = UtilProperties.getMessage( resource_error, "shoppinglistevents.select_items_to_add_to_list", cart.getLocale()); throw new IllegalArgumentException(errMsg); } if (UtilValidate.isEmpty(shoppingListId)) { // create a new shopping list Map<String, Object> newListResult = null; try { newListResult = dispatcher.runSync( "createShoppingList", UtilMisc.<String, Object>toMap( "userLogin", userLogin, "productStoreId", cart.getProductStoreId(), "partyId", cart.getOrderPartyId(), "shoppingListTypeId", shoppingListTypeId, "currencyUom", cart.getCurrency())); } catch (GenericServiceException e) { Debug.logError(e, "Problems creating new ShoppingList", module); errMsg = UtilProperties.getMessage( resource_error, "shoppinglistevents.cannot_create_new_shopping_list", cart.getLocale()); throw new IllegalArgumentException(errMsg); } // check for errors if (ServiceUtil.isError(newListResult)) { throw new IllegalArgumentException(ServiceUtil.getErrorMessage(newListResult)); } // get the new list id if (newListResult != null) { shoppingListId = (String) newListResult.get("shoppingListId"); } // if no list was created throw an error if (shoppingListId == null || shoppingListId.equals("")) { errMsg = UtilProperties.getMessage( resource_error, "shoppinglistevents.shoppingListId_is_required_parameter", cart.getLocale()); throw new IllegalArgumentException(errMsg); } } else if (!append) { try { clearListInfo(delegator, shoppingListId); } catch (GenericEntityException e) { Debug.logError(e, module); throw new IllegalArgumentException( "Could not clear current shopping list: " + e.toString()); } } for (int i = 0; i < items.length; i++) { Integer cartIdInt = null; try { cartIdInt = Integer.valueOf(items[i]); } catch (Exception e) { Debug.logWarning( e, UtilProperties.getMessage( resource_error, "OrderIllegalCharacterInSelectedItemField", cart.getLocale()), module); } if (cartIdInt != null) { ShoppingCartItem item = cart.findCartItem(cartIdInt.intValue()); if (allowPromo || !item.getIsPromo()) { Debug.logInfo( "Adding cart item to shopping list [" + shoppingListId + "], allowPromo=" + allowPromo + ", item.getIsPromo()=" + item.getIsPromo() + ", item.getProductId()=" + item.getProductId() + ", item.getQuantity()=" + item.getQuantity(), module); Map<String, Object> serviceResult = null; try { Map<String, Object> ctx = UtilMisc.<String, Object>toMap( "userLogin", userLogin, "shoppingListId", shoppingListId, "productId", item.getProductId(), "quantity", item.getQuantity()); ctx.put("reservStart", item.getReservStart()); ctx.put("reservLength", item.getReservLength()); ctx.put("reservPersons", item.getReservPersons()); // ctx.put("accommodationMapId", item.getAccommodationMapId()); // ctx.put("accommodationSpotId", item.getAccommodationSpotId()); if (item.getConfigWrapper() != null) { ctx.put("configId", item.getConfigWrapper().getConfigId()); } serviceResult = dispatcher.runSync("createShoppingListItem", ctx); } catch (GenericServiceException e) { Debug.logError(e, "Problems creating ShoppingList item entity", module); errMsg = UtilProperties.getMessage( resource_error, "shoppinglistevents.error_adding_item_to_shopping_list", cart.getLocale()); throw new IllegalArgumentException(errMsg); } // check for errors if (ServiceUtil.isError(serviceResult)) { throw new IllegalArgumentException(ServiceUtil.getErrorMessage(serviceResult)); } } } } // return the shoppinglist id return shoppingListId; }