svn commit: r1061192 - in /ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart: ShoppingCart.java ShoppingCartEvents.java product/ProductPromoWorker.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1061192 - in /ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart: ShoppingCart.java ShoppingCartEvents.java product/ProductPromoWorker.java

mor-2
Author: mor
Date: Thu Jan 20 10:24:20 2011
New Revision: 1061192

URL: http://svn.apache.org/viewvc?rev=1061192&view=rev
Log:
Bug fix: In a multiple store app now onwards a coupon code belonging to a particular store will not be accepted if used on a different store. If user attempts to do it, the
system 
will notify user that it is an invalid coupon code. Earlier the coupon code was accepted but wasn't applied. Also it was preventing the user to add
a correct coupon code.

Modified:
    ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java
    ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartEvents.java
    ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/product/ProductPromoWorker.java

Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java?rev=1061192&r1=1061191&r2=1061192&view=diff
==============================================================================
--- ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java (original)
+++ ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java Thu Jan 20 10:24:20 2011
@@ -3246,11 +3246,16 @@ public class ShoppingCart implements Ite
         }
     }
 
+    public String addProductPromoCode(String productPromoCodeId, LocalDispatcher dispatcher) {
+        return this.addProductPromoCode(productPromoCodeId, null, dispatcher);
+    }
+
     /** Adds a promotion code to the cart, checking if it is valid. If it is valid this will return null, otherwise it will return a message stating why it was not valid
      * @param productPromoCodeId The promotion code to check and add
+     * @param cart Shopping Cart Object
      * @return String that is null if valid, and added to cart, or an error message of the code was not valid and not added to the cart.
      */
-    public String addProductPromoCode(String productPromoCodeId, LocalDispatcher dispatcher) {
+    public String addProductPromoCode(String productPromoCodeId, ShoppingCart cart, LocalDispatcher dispatcher) {
         if (this.productPromoCodes.contains(productPromoCodeId)) {
             return UtilProperties.getMessage(resource_error, "productpromoworker.promotion_code_already_been_entered", UtilMisc.toMap("productPromoCodeId", productPromoCodeId), locale);
         }
@@ -3259,7 +3264,7 @@ public class ShoppingCart implements Ite
             return null;
         }
         // if the promo code requires it make sure the code is valid
-        String checkResult = ProductPromoWorker.checkCanUsePromoCode(productPromoCodeId, this.getPartyId(), this.getDelegator(), locale);
+        String checkResult = ProductPromoWorker.checkCanUsePromoCode(productPromoCodeId, this.getPartyId(), this.getDelegator(), cart, locale);
         if (checkResult == null) {
             this.productPromoCodes.add(productPromoCodeId);
             // new promo code, re-evaluate promos

Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartEvents.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartEvents.java?rev=1061192&r1=1061191&r2=1061192&view=diff
==============================================================================
--- ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartEvents.java (original)
+++ ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartEvents.java Thu Jan 20 10:24:20 2011
@@ -85,7 +85,7 @@ public class ShoppingCartEvents {
         ShoppingCart cart = getCartObject(request);
         String productPromoCodeId = request.getParameter("productPromoCodeId");
         if (UtilValidate.isNotEmpty(productPromoCodeId)) {
-            String checkResult = cart.addProductPromoCode(productPromoCodeId, dispatcher);
+            String checkResult = cart.addProductPromoCode(productPromoCodeId, cart, dispatcher);
             if (UtilValidate.isNotEmpty(checkResult)) {
                 request.setAttribute("_ERROR_MESSAGE_", checkResult);
                 return "error";

Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/product/ProductPromoWorker.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/product/ProductPromoWorker.java?rev=1061192&r1=1061191&r2=1061192&view=diff
==============================================================================
--- ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/product/ProductPromoWorker.java (original)
+++ ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/product/ProductPromoWorker.java Thu Jan 20 10:24:20 2011
@@ -155,6 +155,43 @@ public class ProductPromoWorker {
         return productPromos;
     }
 
+    public static Set<String> getStoreProductPromoCodes(ShoppingCart cart) {
+        Set<String> promoCodes = new HashSet();
+        Delegator delegator = cart.getDelegator();
+
+        String productStoreId = cart.getProductStoreId();
+        GenericValue productStore = null;
+        try {
+            productStore = delegator.findByPrimaryKeyCache("ProductStore", UtilMisc.toMap("productStoreId", productStoreId));
+        } catch (GenericEntityException e) {
+            Debug.logError(e, "Error looking up store with id " + productStoreId, module);
+        }
+        if (productStore == null) {
+            Debug.logWarning(UtilProperties.getMessage(resource_error,"OrderNoStoreFoundWithIdNotDoingPromotions", UtilMisc.toMap("productStoreId",productStoreId), cart.getLocale()), module);
+            return promoCodes;
+        }
+        try {
+            Iterator<GenericValue> productStorePromoAppls = UtilMisc.toIterator(EntityUtil.filterByDate(productStore.getRelatedCache("ProductStorePromoAppl", UtilMisc.toMap("productStoreId", productStoreId), UtilMisc.toList("sequenceNum")), true));
+            while (productStorePromoAppls != null && productStorePromoAppls.hasNext()) {
+                GenericValue productStorePromoAppl = (GenericValue) productStorePromoAppls.next();
+                if (UtilValidate.isNotEmpty(productStorePromoAppl.getString("manualOnly")) && "Y".equals(productStorePromoAppl.getString("manualOnly"))) {
+                    // manual only promotions are not automatically evaluated (they must be explicitly selected by the user)
+                    if (Debug.verboseOn()) Debug.logVerbose("Skipping promotion with id [" + productStorePromoAppl.getString("productPromoId") + "] because it is applied to the store with ID " + productStoreId + " as a manual only promotion.", module);
+                        continue;
+                }
+                GenericValue productPromo = productStorePromoAppl.getRelatedOneCache("ProductPromo");
+                Iterator<GenericValue> productPromoCodesIter = UtilMisc.toIterator(productPromo.getRelatedCache("ProductPromoCode", null, null));
+                while (productPromoCodesIter != null && productPromoCodesIter.hasNext()) {
+                    GenericValue productPromoCode = (GenericValue) productPromoCodesIter.next();
+                    promoCodes.add(productPromoCode.getString("productPromoCodeId"));
+                }
+            }
+        } catch (GenericEntityException e) {
+            Debug.logError(e, module);
+        }
+        return promoCodes;
+    }
+
     public static List getProductStorePromotions(ShoppingCart cart, Timestamp nowTimestamp, LocalDispatcher dispatcher) {
         List productPromoList = FastList.newInstance();
 
@@ -537,11 +574,21 @@ public class ProductPromoWorker {
     }
 
     public static String checkCanUsePromoCode(String productPromoCodeId, String partyId, Delegator delegator, Locale locale) {
+        return checkCanUsePromoCode(productPromoCodeId, partyId, delegator, null, locale);
+    }
+
+    public static String checkCanUsePromoCode(String productPromoCodeId, String partyId, Delegator delegator, ShoppingCart cart, Locale locale) {
         try {
             GenericValue productPromoCode = delegator.findByPrimaryKey("ProductPromoCode", UtilMisc.toMap("productPromoCodeId", productPromoCodeId));
             if (productPromoCode == null) {
                 return UtilProperties.getMessage(resource_error, "productpromoworker.promotion_code_not_valid", UtilMisc.toMap("productPromoCodeId", productPromoCodeId), locale);
             }
+            if (cart != null) {
+                Set<String> promoCodes = ProductPromoWorker.getStoreProductPromoCodes(cart);
+                if (UtilValidate.isEmpty(promoCodes) || !promoCodes.contains(productPromoCodeId)) {
+                    return UtilProperties.getMessage(resource_error, "productpromoworker.promotion_code_not_valid", UtilMisc.toMap("productPromoCodeId", productPromoCodeId), locale);
+                }
+            }
             Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
             Timestamp thruDate = productPromoCode.getTimestamp("thruDate");
             if (thruDate != null) {