Commits

Josh VanderLinden  committed 9072246

#127 - Only using the one combo discount that will save the user the most money

  • Participants
  • Parent commits cda2ebe

Comments (0)

Files changed (1)

 # HG changeset patch
 # Parent e08070b169a4ae650fcac0490ca9eec33eb16b0a
-Used to be on 1986:38ebd825108e
+Only using the one combo discount that will save the user the most money.
 
 diff --git a/.hgignore b/.hgignore
 --- a/.hgignore
          order.add_status('Shipped', ugettext("Order immediately available for download"))
  
      product_signals.subtype_order_success.connect(create_download_link, sender=None)
+diff --git a/satchmo/apps/satchmo_ext/discounts/combo/__init__.py b/satchmo/apps/satchmo_ext/discounts/combo/__init__.py
+--- a/satchmo/apps/satchmo_ext/discounts/combo/__init__.py
++++ b/satchmo/apps/satchmo_ext/discounts/combo/__init__.py
+@@ -1,7 +1,6 @@
+ import logging
+ 
+ from l10n.utils import moneyfmt
+-from satchmo_ext.discounts.common import DISCOUNT_AMOUNT, DISCOUNT_PERCENT
+ from satchmo_ext.discounts.signals import pre_recalculate_total
+ 
+ from utils import applicable_discounts
+@@ -14,18 +13,33 @@
+     all_products = set([li.product for li in instance.orderitem_set.all()])
+     discounts = applicable_discounts(all_products)
+ 
++    greatest_discount = None
++
+     for discount in discounts:
+         log.debug('Processing combo discount: %s' % discount)
+-        log.debug('Order discount BEFORE processing: %s' % moneyfmt(instance.discount))
+ 
+         lineitems = instance.orderitem_set.filter(product__id__in=[p.id for p in discount.products.all()])
+         combo_total = sum([li.line_item_price for li in lineitems])
+         amount = discount.amount(combo_total)
+-        instance.discount += amount
++
++        if greatest_discount is None or amount > greatest_discount[1]:
++            log.debug('Discount "%s" has a discount greater than the others: %s' % (discount, amount))
++            greatest_discount = (discount, amount)
++        else:
++            log.debug('Discount "%s" does not cut it! %s' % (discount, amount))
++
++    if greatest_discount:
++        discount, amount = greatest_discount
++
++        log.debug('Order discount BEFORE processing: %s' % moneyfmt(instance.discount))
+ 
+         # total is calculated using itemprices. dirty? yes. functional? yes.
++        instance.discount += amount
+         itemprices.append(amount * -1)
++
+         log.debug('Order discount AFTER processing: %s' % moneyfmt(instance.discount))
++    else:
++        log.debug('No valid discounts!')
+ 
+ pre_recalculate_total.connect(apply_combo_discount)
+ 
+diff --git a/satchmo/apps/satchmo_ext/discounts/combo/context_processors.py b/satchmo/apps/satchmo_ext/discounts/combo/context_processors.py
+--- a/satchmo/apps/satchmo_ext/discounts/combo/context_processors.py
++++ b/satchmo/apps/satchmo_ext/discounts/combo/context_processors.py
+@@ -1,18 +1,26 @@
++import logging
++
+ from satchmo_store.shop.models import Cart
+ from utils import applicable_discounts, cart_discount_amounts
+ 
++log = logging.getLogger('satchmo_ext.discounts.combo.context_processors')
++
+ def combo_discount(request):
+     """Injects the valid combo discounts into the context"""
+ 
+     cart = Cart.objects.from_request(request)
+-    combo_discounts = {}
+-
+     all_products = set([i.product for i in cart])
+ 
+     discounts = applicable_discounts(all_products)
+     amounts = cart_discount_amounts(cart, discounts)
+     discount_amounts = zip(discounts, amounts)
+ 
++    # issue 127 - only one discount per order
++    log.debug('The following discounts may apply to this cart: %s' % (discount_amounts,))
++    discount_amounts.sort(key=lambda t: t[1], reverse=True)
++    discount_amounts = discount_amounts[:1]
++    log.debug('Only applying discount because it saves the user the most money: %s' % (discount_amounts,))
++
+     return {
+         'combo_discounts': discount_amounts,
+     }
 diff --git a/satchmo/apps/satchmo_store/shop/admin.py b/satchmo/apps/satchmo_store/shop/admin.py
 --- a/satchmo/apps/satchmo_store/shop/admin.py
 +++ b/satchmo/apps/satchmo_store/shop/admin.py