2016-03-25 07:09:24 +00:00
|
|
|
import itertools
|
|
|
|
|
|
|
|
from conditions import ConditionController
|
2016-04-22 05:06:24 +00:00
|
|
|
from registrasion.models import commerce
|
|
|
|
from registrasion.models import conditions
|
2016-03-25 07:09:24 +00:00
|
|
|
|
|
|
|
from django.db.models import Sum
|
|
|
|
|
|
|
|
|
|
|
|
class DiscountAndQuantity(object):
|
|
|
|
def __init__(self, discount, clause, quantity):
|
|
|
|
self.discount = discount
|
|
|
|
self.clause = clause
|
|
|
|
self.quantity = quantity
|
|
|
|
|
2016-03-27 08:25:24 +00:00
|
|
|
def __repr__(self):
|
2016-04-06 08:28:33 +00:00
|
|
|
return "(discount=%s, clause=%s, quantity=%d)" % (
|
2016-03-27 08:25:24 +00:00
|
|
|
self.discount, self.clause, self.quantity,
|
|
|
|
)
|
|
|
|
|
2016-03-25 07:09:24 +00:00
|
|
|
|
|
|
|
def available_discounts(user, categories, products):
|
|
|
|
''' Returns all discounts available to this user for the given categories
|
|
|
|
and products. The discounts also list the available quantity for this user,
|
|
|
|
not including products that are pending purchase. '''
|
|
|
|
|
|
|
|
# discounts that match provided categories
|
2016-04-22 05:06:24 +00:00
|
|
|
category_discounts = conditions.DiscountForCategory.objects.filter(
|
2016-03-25 07:09:24 +00:00
|
|
|
category__in=categories
|
|
|
|
)
|
|
|
|
# discounts that match provided products
|
2016-04-22 05:06:24 +00:00
|
|
|
product_discounts = conditions.DiscountForProduct.objects.filter(
|
2016-03-25 07:09:24 +00:00
|
|
|
product__in=products
|
|
|
|
)
|
|
|
|
# discounts that match categories for provided products
|
2016-04-22 05:06:24 +00:00
|
|
|
product_category_discounts = conditions.DiscountForCategory.objects.filter(
|
2016-03-25 07:09:24 +00:00
|
|
|
category__in=(product.category for product in products)
|
|
|
|
)
|
|
|
|
# (Not relevant: discounts that match products in provided categories)
|
|
|
|
|
2016-04-06 08:28:33 +00:00
|
|
|
product_discounts = product_discounts.select_related(
|
|
|
|
"product",
|
|
|
|
"product__category",
|
|
|
|
)
|
|
|
|
|
|
|
|
all_category_discounts = category_discounts | product_category_discounts
|
|
|
|
all_category_discounts = all_category_discounts.select_related(
|
|
|
|
"category",
|
|
|
|
)
|
|
|
|
|
2016-03-25 07:09:24 +00:00
|
|
|
# The set of all potential discounts
|
|
|
|
potential_discounts = set(itertools.chain(
|
|
|
|
product_discounts,
|
2016-04-06 08:28:33 +00:00
|
|
|
all_category_discounts,
|
2016-03-25 07:09:24 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
discounts = []
|
|
|
|
|
|
|
|
# Markers so that we don't need to evaluate given conditions more than once
|
|
|
|
accepted_discounts = set()
|
|
|
|
failed_discounts = set()
|
|
|
|
|
|
|
|
for discount in potential_discounts:
|
2016-04-22 05:06:24 +00:00
|
|
|
real_discount = conditions.DiscountBase.objects.get_subclass(
|
2016-03-25 07:09:24 +00:00
|
|
|
pk=discount.discount.pk,
|
|
|
|
)
|
|
|
|
cond = ConditionController.for_condition(real_discount)
|
|
|
|
|
|
|
|
# Count the past uses of the given discount item.
|
|
|
|
# If this user has exceeded the limit for the clause, this clause
|
|
|
|
# is not available any more.
|
2016-04-22 05:06:24 +00:00
|
|
|
past_uses = commerce.DiscountItem.objects.filter(
|
2016-03-25 07:09:24 +00:00
|
|
|
cart__user=user,
|
|
|
|
cart__active=False, # Only past carts count
|
2016-03-27 08:25:24 +00:00
|
|
|
cart__released=False, # You can reuse refunded discounts
|
2016-04-06 08:28:33 +00:00
|
|
|
discount=real_discount,
|
2016-03-25 07:09:24 +00:00
|
|
|
)
|
|
|
|
agg = past_uses.aggregate(Sum("quantity"))
|
|
|
|
past_use_count = agg["quantity__sum"]
|
|
|
|
if past_use_count is None:
|
|
|
|
past_use_count = 0
|
|
|
|
|
|
|
|
if past_use_count >= discount.quantity:
|
|
|
|
# This clause has exceeded its use count
|
|
|
|
pass
|
|
|
|
elif real_discount not in failed_discounts:
|
|
|
|
# This clause is still available
|
2016-04-02 05:26:27 +00:00
|
|
|
if real_discount in accepted_discounts or cond.is_met(user):
|
2016-03-25 07:09:24 +00:00
|
|
|
# This clause is valid for this user
|
|
|
|
discounts.append(DiscountAndQuantity(
|
|
|
|
discount=real_discount,
|
|
|
|
clause=discount,
|
|
|
|
quantity=discount.quantity - past_use_count,
|
|
|
|
))
|
|
|
|
accepted_discounts.add(real_discount)
|
|
|
|
else:
|
|
|
|
# This clause is not valid for this user
|
|
|
|
failed_discounts.add(real_discount)
|
|
|
|
return discounts
|