2016-04-02 03:03:25 +00:00
|
|
|
import collections
|
2016-01-22 05:01:30 +00:00
|
|
|
import datetime
|
2016-03-25 07:59:19 +00:00
|
|
|
import discount
|
2016-04-25 07:13:11 +00:00
|
|
|
import functools
|
2016-04-02 23:45:39 +00:00
|
|
|
import itertools
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
|
|
from django.core.exceptions import ValidationError
|
2016-04-02 02:39:42 +00:00
|
|
|
from django.db import transaction
|
2016-04-03 03:21:57 +00:00
|
|
|
from django.db.models import Max
|
2016-01-22 05:01:30 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
|
2016-04-03 05:25:39 +00:00
|
|
|
from registrasion.exceptions import CartValidationError
|
2016-04-22 05:06:24 +00:00
|
|
|
from registrasion.models import commerce
|
|
|
|
from registrasion.models import conditions
|
|
|
|
from registrasion.models import inventory
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-02 03:03:25 +00:00
|
|
|
from category import CategoryController
|
2016-01-22 05:01:30 +00:00
|
|
|
from conditions import ConditionController
|
2016-01-22 05:21:23 +00:00
|
|
|
from product import ProductController
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
|
2016-04-25 07:13:11 +00:00
|
|
|
def _modifies_cart(func):
|
|
|
|
''' Decorator that makes the wrapped function raise ValidationError
|
|
|
|
if we're doing something that could modify the cart. '''
|
|
|
|
|
|
|
|
@functools.wraps(func)
|
|
|
|
def inner(self, *a, **k):
|
|
|
|
self._fail_if_cart_is_not_active()
|
|
|
|
return func(self, *a, **k)
|
|
|
|
|
|
|
|
return inner
|
|
|
|
|
|
|
|
|
2016-01-22 05:01:30 +00:00
|
|
|
class CartController(object):
|
|
|
|
|
|
|
|
def __init__(self, cart):
|
|
|
|
self.cart = cart
|
|
|
|
|
2016-04-03 00:06:35 +00:00
|
|
|
@classmethod
|
|
|
|
def for_user(cls, user):
|
2016-01-22 05:01:30 +00:00
|
|
|
''' Returns the user's current cart, or creates a new cart
|
|
|
|
if there isn't one ready yet. '''
|
|
|
|
|
|
|
|
try:
|
2016-04-25 04:31:25 +00:00
|
|
|
existing = commerce.Cart.objects.get(
|
|
|
|
user=user,
|
|
|
|
status=commerce.Cart.STATUS_ACTIVE,
|
|
|
|
)
|
2016-01-22 05:01:30 +00:00
|
|
|
except ObjectDoesNotExist:
|
2016-04-22 05:06:24 +00:00
|
|
|
existing = commerce.Cart.objects.create(
|
2016-01-22 05:01:30 +00:00
|
|
|
user=user,
|
|
|
|
time_last_updated=timezone.now(),
|
|
|
|
reservation_duration=datetime.timedelta(),
|
2016-04-25 04:31:25 +00:00
|
|
|
)
|
2016-04-03 00:06:35 +00:00
|
|
|
return cls(existing)
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-25 07:13:11 +00:00
|
|
|
def _fail_if_cart_is_not_active(self):
|
|
|
|
self.cart.refresh_from_db()
|
|
|
|
if self.cart.status != commerce.Cart.STATUS_ACTIVE:
|
|
|
|
raise ValidationError("You can only amend active carts.")
|
|
|
|
|
|
|
|
@_modifies_cart
|
2016-01-22 05:01:30 +00:00
|
|
|
def extend_reservation(self):
|
|
|
|
''' Updates the cart's time last updated value, which is used to
|
|
|
|
determine whether the cart has reserved the items and discounts it
|
|
|
|
holds. '''
|
|
|
|
|
|
|
|
reservations = [datetime.timedelta()]
|
|
|
|
|
|
|
|
# If we have vouchers, we're entitled to an hour at minimum.
|
|
|
|
if len(self.cart.vouchers.all()) >= 1:
|
2016-04-22 05:06:24 +00:00
|
|
|
reservations.append(inventory.Voucher.RESERVATION_DURATION)
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
# Else, it's the maximum of the included products
|
2016-04-22 05:06:24 +00:00
|
|
|
items = commerce.ProductItem.objects.filter(cart=self.cart)
|
2016-01-22 05:01:30 +00:00
|
|
|
agg = items.aggregate(Max("product__reservation_duration"))
|
|
|
|
product_max = agg["product__reservation_duration__max"]
|
|
|
|
|
|
|
|
if product_max is not None:
|
|
|
|
reservations.append(product_max)
|
|
|
|
|
|
|
|
self.cart.time_last_updated = timezone.now()
|
|
|
|
self.cart.reservation_duration = max(reservations)
|
|
|
|
|
2016-04-25 07:13:11 +00:00
|
|
|
@_modifies_cart
|
2016-03-04 02:18:58 +00:00
|
|
|
def end_batch(self):
|
|
|
|
''' Performs operations that occur occur at the end of a batch of
|
2016-04-02 02:39:42 +00:00
|
|
|
product changes/voucher applications etc.
|
|
|
|
THIS SHOULD BE PRIVATE
|
|
|
|
'''
|
|
|
|
|
2016-03-04 02:18:58 +00:00
|
|
|
self.recalculate_discounts()
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-03-04 02:18:58 +00:00
|
|
|
self.extend_reservation()
|
|
|
|
self.cart.revision += 1
|
|
|
|
self.cart.save()
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-25 07:13:11 +00:00
|
|
|
@_modifies_cart
|
2016-04-02 02:39:42 +00:00
|
|
|
@transaction.atomic
|
|
|
|
def set_quantities(self, product_quantities):
|
2016-04-02 23:45:39 +00:00
|
|
|
''' Sets the quantities on each of the products on each of the
|
|
|
|
products specified. Raises an exception (ValidationError) if a limit
|
|
|
|
is violated. `product_quantities` is an iterable of (product, quantity)
|
|
|
|
pairs. '''
|
2016-04-02 02:39:42 +00:00
|
|
|
|
2016-04-22 05:06:24 +00:00
|
|
|
items_in_cart = commerce.ProductItem.objects.filter(cart=self.cart)
|
2016-04-06 08:28:33 +00:00
|
|
|
items_in_cart = items_in_cart.select_related(
|
|
|
|
"product",
|
|
|
|
"product__category",
|
|
|
|
)
|
|
|
|
|
2016-04-02 23:45:39 +00:00
|
|
|
product_quantities = list(product_quantities)
|
2016-04-02 03:03:25 +00:00
|
|
|
|
2016-04-02 23:45:39 +00:00
|
|
|
# n.b need to add have the existing items first so that the new
|
|
|
|
# items override the old ones.
|
|
|
|
all_product_quantities = dict(itertools.chain(
|
|
|
|
((i.product, i.quantity) for i in items_in_cart.all()),
|
|
|
|
product_quantities,
|
|
|
|
)).items()
|
2016-04-02 02:39:42 +00:00
|
|
|
|
2016-04-02 23:45:39 +00:00
|
|
|
# Validate that the limits we're adding are OK
|
2016-04-02 07:10:33 +00:00
|
|
|
self._test_limits(all_product_quantities)
|
|
|
|
|
|
|
|
for product, quantity in product_quantities:
|
2016-04-02 23:45:39 +00:00
|
|
|
try:
|
2016-04-22 05:06:24 +00:00
|
|
|
product_item = commerce.ProductItem.objects.get(
|
2016-04-02 23:45:39 +00:00
|
|
|
cart=self.cart,
|
|
|
|
product=product,
|
|
|
|
)
|
|
|
|
product_item.quantity = quantity
|
|
|
|
product_item.save()
|
|
|
|
except ObjectDoesNotExist:
|
2016-04-22 05:06:24 +00:00
|
|
|
commerce.ProductItem.objects.create(
|
2016-04-02 23:45:39 +00:00
|
|
|
cart=self.cart,
|
|
|
|
product=product,
|
|
|
|
quantity=quantity,
|
|
|
|
)
|
|
|
|
|
|
|
|
items_in_cart.filter(quantity=0).delete()
|
2016-04-02 07:10:33 +00:00
|
|
|
|
|
|
|
self.end_batch()
|
|
|
|
|
|
|
|
def _test_limits(self, product_quantities):
|
2016-04-02 23:45:39 +00:00
|
|
|
''' Tests that the quantity changes we intend to make do not violate
|
2016-04-11 22:30:33 +00:00
|
|
|
the limits and flag conditions imposed on the products. '''
|
2016-04-02 23:45:39 +00:00
|
|
|
|
2016-04-03 05:25:39 +00:00
|
|
|
errors = []
|
|
|
|
|
2016-04-02 23:45:39 +00:00
|
|
|
# Test each product limit here
|
|
|
|
for product, quantity in product_quantities:
|
|
|
|
if quantity < 0:
|
2016-04-03 05:25:39 +00:00
|
|
|
errors.append((product, "Value must be zero or greater."))
|
2016-04-02 23:45:39 +00:00
|
|
|
|
|
|
|
prod = ProductController(product)
|
|
|
|
limit = prod.user_quantity_remaining(self.cart.user)
|
|
|
|
|
|
|
|
if quantity > limit:
|
2016-04-03 05:25:39 +00:00
|
|
|
errors.append((
|
|
|
|
product,
|
2016-04-02 23:45:39 +00:00
|
|
|
"You may only have %d of product: %s" % (
|
2016-04-03 05:25:39 +00:00
|
|
|
limit, product,
|
2016-04-02 23:45:39 +00:00
|
|
|
)
|
2016-04-03 05:25:39 +00:00
|
|
|
))
|
2016-04-02 23:45:39 +00:00
|
|
|
|
2016-04-02 03:03:25 +00:00
|
|
|
# Collect by category
|
|
|
|
by_cat = collections.defaultdict(list)
|
|
|
|
for product, quantity in product_quantities:
|
|
|
|
by_cat[product.category].append((product, quantity))
|
|
|
|
|
|
|
|
# Test each category limit here
|
2016-04-03 05:25:39 +00:00
|
|
|
for category in by_cat:
|
|
|
|
ctrl = CategoryController(category)
|
2016-04-02 03:03:25 +00:00
|
|
|
limit = ctrl.user_quantity_remaining(self.cart.user)
|
|
|
|
|
|
|
|
# Get the amount so far in the cart
|
2016-04-03 05:25:39 +00:00
|
|
|
to_add = sum(i[1] for i in by_cat[category])
|
2016-04-02 03:03:25 +00:00
|
|
|
|
2016-04-02 07:10:33 +00:00
|
|
|
if to_add > limit:
|
2016-04-03 05:25:39 +00:00
|
|
|
errors.append((
|
|
|
|
category,
|
2016-04-02 03:03:25 +00:00
|
|
|
"You may only have %d items in category: %s" % (
|
2016-04-03 05:25:39 +00:00
|
|
|
limit, category.name,
|
2016-04-02 03:03:25 +00:00
|
|
|
)
|
2016-04-03 05:25:39 +00:00
|
|
|
))
|
2016-04-02 03:03:25 +00:00
|
|
|
|
2016-04-11 22:30:33 +00:00
|
|
|
# Test the flag conditions
|
|
|
|
errs = ConditionController.test_flags(
|
2016-04-02 05:26:27 +00:00
|
|
|
self.cart.user,
|
2016-04-02 07:10:33 +00:00
|
|
|
product_quantities=product_quantities,
|
2016-04-02 05:26:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if errs:
|
2016-04-06 05:40:16 +00:00
|
|
|
for error in errs:
|
|
|
|
errors.append(error)
|
2016-04-03 05:25:39 +00:00
|
|
|
|
|
|
|
if errors:
|
|
|
|
raise CartValidationError(errors)
|
2016-04-02 02:39:42 +00:00
|
|
|
|
2016-04-25 07:13:11 +00:00
|
|
|
@_modifies_cart
|
2016-03-23 02:29:18 +00:00
|
|
|
def apply_voucher(self, voucher_code):
|
|
|
|
''' Applies the voucher with the given code to this cart. '''
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-03-23 02:29:18 +00:00
|
|
|
# Try and find the voucher
|
2016-04-22 05:06:24 +00:00
|
|
|
voucher = inventory.Voucher.objects.get(code=voucher_code.upper())
|
2016-03-23 02:29:18 +00:00
|
|
|
|
2016-04-06 02:48:05 +00:00
|
|
|
# Re-applying vouchers should be idempotent
|
|
|
|
if voucher in self.cart.vouchers.all():
|
|
|
|
return
|
|
|
|
|
2016-04-06 02:09:16 +00:00
|
|
|
self._test_voucher(voucher)
|
|
|
|
|
|
|
|
# If successful...
|
|
|
|
self.cart.vouchers.add(voucher)
|
|
|
|
self.end_batch()
|
|
|
|
|
|
|
|
def _test_voucher(self, voucher):
|
|
|
|
''' Tests whether this voucher is allowed to be applied to this cart.
|
|
|
|
Raises ValidationError if not. '''
|
|
|
|
|
|
|
|
# Is voucher exhausted?
|
2016-04-22 05:06:24 +00:00
|
|
|
active_carts = commerce.Cart.reserved_carts()
|
2016-04-06 02:09:16 +00:00
|
|
|
|
2016-03-26 08:47:01 +00:00
|
|
|
# It's invalid for a user to enter a voucher that's exhausted
|
2016-01-22 05:01:30 +00:00
|
|
|
carts_with_voucher = active_carts.filter(vouchers=voucher)
|
2016-04-06 02:48:05 +00:00
|
|
|
carts_with_voucher = carts_with_voucher.exclude(pk=self.cart.id)
|
2016-04-06 03:01:25 +00:00
|
|
|
if carts_with_voucher.count() >= voucher.limit:
|
2016-04-07 03:26:25 +00:00
|
|
|
raise ValidationError(
|
|
|
|
"Voucher %s is no longer available" % voucher.code)
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-03-26 08:47:01 +00:00
|
|
|
# It's not valid for users to re-enter a voucher they already have
|
2016-04-06 02:48:05 +00:00
|
|
|
user_carts_with_voucher = carts_with_voucher.filter(
|
2016-03-26 08:47:01 +00:00
|
|
|
user=self.cart.user,
|
|
|
|
)
|
2016-04-06 02:48:05 +00:00
|
|
|
|
2016-04-06 03:01:25 +00:00
|
|
|
if user_carts_with_voucher.count() > 0:
|
2016-03-26 08:47:01 +00:00
|
|
|
raise ValidationError("You have already entered this voucher.")
|
|
|
|
|
2016-04-06 02:48:05 +00:00
|
|
|
def _test_vouchers(self, vouchers):
|
|
|
|
''' Tests each of the vouchers against self._test_voucher() and raises
|
|
|
|
the collective ValidationError.
|
|
|
|
Future work will refactor _test_voucher in terms of this, and save some
|
|
|
|
queries. '''
|
|
|
|
errors = []
|
|
|
|
for voucher in vouchers:
|
|
|
|
try:
|
|
|
|
self._test_voucher(voucher)
|
|
|
|
except ValidationError as ve:
|
|
|
|
errors.append(ve)
|
|
|
|
|
|
|
|
if errors:
|
|
|
|
raise(ValidationError(ve))
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-25 07:13:47 +00:00
|
|
|
def _test_required_categories(self):
|
|
|
|
''' Makes sure that the owner of this cart has satisfied all of the
|
|
|
|
required category constraints in the inventory (be it in this cart
|
|
|
|
or others). '''
|
|
|
|
|
|
|
|
required = set(inventory.Category.objects.filter(required=True))
|
|
|
|
|
|
|
|
items = commerce.ProductItem.objects.filter(
|
|
|
|
product__category__required=True,
|
|
|
|
cart__user=self.cart.user,
|
|
|
|
).exclude(
|
|
|
|
cart__status=commerce.Cart.STATUS_RELEASED,
|
|
|
|
)
|
|
|
|
|
|
|
|
for item in items:
|
|
|
|
required.remove(item.product.category)
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
for category in required:
|
|
|
|
msg = "You must have at least one item from: %s" % category
|
|
|
|
errors.append((None, msg))
|
|
|
|
|
|
|
|
if errors:
|
|
|
|
raise ValidationError(errors)
|
|
|
|
|
|
|
|
def _append_errors(self, errors, ve):
|
|
|
|
for error in ve.error_list:
|
|
|
|
errors.append(error.message[1])
|
|
|
|
|
2016-01-22 05:01:30 +00:00
|
|
|
def validate_cart(self):
|
|
|
|
''' Determines whether the status of the current cart is valid;
|
|
|
|
this is normally called before generating or paying an invoice '''
|
|
|
|
|
2016-04-06 00:22:44 +00:00
|
|
|
cart = self.cart
|
|
|
|
user = self.cart.user
|
|
|
|
errors = []
|
|
|
|
|
2016-04-06 02:48:05 +00:00
|
|
|
try:
|
|
|
|
self._test_vouchers(self.cart.vouchers.all())
|
|
|
|
except ValidationError as ve:
|
|
|
|
errors.append(ve)
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-22 05:06:24 +00:00
|
|
|
items = commerce.ProductItem.objects.filter(cart=cart)
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-02 07:10:33 +00:00
|
|
|
product_quantities = list((i.product, i.quantity) for i in items)
|
2016-04-06 00:22:44 +00:00
|
|
|
try:
|
|
|
|
self._test_limits(product_quantities)
|
|
|
|
except ValidationError as ve:
|
2016-04-25 07:13:47 +00:00
|
|
|
self._append_errors(errors, ve)
|
|
|
|
|
|
|
|
try:
|
|
|
|
self._test_required_categories()
|
|
|
|
except ValidationError as ve:
|
|
|
|
self._append_errors(errors, ve)
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
# Validate the discounts
|
2016-04-22 05:06:24 +00:00
|
|
|
discount_items = commerce.DiscountItem.objects.filter(cart=cart)
|
2016-01-22 05:01:30 +00:00
|
|
|
seen_discounts = set()
|
|
|
|
|
|
|
|
for discount_item in discount_items:
|
|
|
|
discount = discount_item.discount
|
|
|
|
if discount in seen_discounts:
|
|
|
|
continue
|
|
|
|
seen_discounts.add(discount)
|
2016-04-22 05:06:24 +00:00
|
|
|
real_discount = conditions.DiscountBase.objects.get_subclass(
|
2016-01-22 06:02:07 +00:00
|
|
|
pk=discount.pk)
|
2016-01-22 05:01:30 +00:00
|
|
|
cond = ConditionController.for_condition(real_discount)
|
|
|
|
|
2016-04-06 00:22:44 +00:00
|
|
|
if not cond.is_met(user):
|
|
|
|
errors.append(
|
|
|
|
ValidationError("Discounts are no longer available")
|
|
|
|
)
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-06 00:22:44 +00:00
|
|
|
if errors:
|
|
|
|
raise ValidationError(errors)
|
|
|
|
|
2016-04-25 07:13:11 +00:00
|
|
|
@_modifies_cart
|
2016-04-06 03:01:25 +00:00
|
|
|
@transaction.atomic
|
2016-04-06 01:36:07 +00:00
|
|
|
def fix_simple_errors(self):
|
|
|
|
''' This attempts to fix the easy errors raised by ValidationError.
|
|
|
|
This includes removing items from the cart that are no longer
|
|
|
|
available, recalculating all of the discounts, and removing voucher
|
|
|
|
codes that are no longer available. '''
|
|
|
|
|
2016-04-06 03:01:25 +00:00
|
|
|
# Fix vouchers first (this affects available discounts)
|
|
|
|
to_remove = []
|
|
|
|
for voucher in self.cart.vouchers.all():
|
|
|
|
try:
|
|
|
|
self._test_voucher(voucher)
|
2016-04-07 03:26:25 +00:00
|
|
|
except ValidationError:
|
2016-04-06 03:01:25 +00:00
|
|
|
to_remove.append(voucher)
|
|
|
|
|
|
|
|
for voucher in to_remove:
|
|
|
|
self.cart.vouchers.remove(voucher)
|
2016-04-06 01:36:07 +00:00
|
|
|
|
|
|
|
# Fix products and discounts
|
2016-04-22 05:06:24 +00:00
|
|
|
items = commerce.ProductItem.objects.filter(cart=self.cart)
|
2016-04-06 08:28:33 +00:00
|
|
|
items = items.select_related("product")
|
2016-04-06 01:36:07 +00:00
|
|
|
products = set(i.product for i in items)
|
|
|
|
available = set(ProductController.available_products(
|
|
|
|
self.cart.user,
|
|
|
|
products=products,
|
|
|
|
))
|
|
|
|
|
|
|
|
not_available = products - available
|
|
|
|
zeros = [(product, 0) for product in not_available]
|
|
|
|
|
|
|
|
self.set_quantities(zeros)
|
|
|
|
|
2016-04-25 07:13:11 +00:00
|
|
|
@_modifies_cart
|
2016-04-06 00:22:44 +00:00
|
|
|
@transaction.atomic
|
2016-01-22 05:01:30 +00:00
|
|
|
def recalculate_discounts(self):
|
|
|
|
''' Calculates all of the discounts available for this product.
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Delete the existing entries.
|
2016-04-22 05:06:24 +00:00
|
|
|
commerce.DiscountItem.objects.filter(cart=self.cart).delete()
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-06 08:28:33 +00:00
|
|
|
product_items = self.cart.productitem_set.all().select_related(
|
2016-04-25 03:20:48 +00:00
|
|
|
"product", "product__category", "product__price"
|
2016-04-06 08:28:33 +00:00
|
|
|
)
|
2016-03-25 07:59:19 +00:00
|
|
|
|
|
|
|
products = [i.product for i in product_items]
|
|
|
|
discounts = discount.available_discounts(self.cart.user, [], products)
|
|
|
|
|
2016-03-04 21:07:45 +00:00
|
|
|
# The highest-value discounts will apply to the highest-value
|
|
|
|
# products first.
|
|
|
|
product_items = reversed(product_items)
|
|
|
|
for item in product_items:
|
2016-03-25 07:59:19 +00:00
|
|
|
self._add_discount(item.product, item.quantity, discounts)
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-03-25 07:59:19 +00:00
|
|
|
def _add_discount(self, product, quantity, discounts):
|
|
|
|
''' Applies the best discounts on the given product, from the given
|
|
|
|
discounts.'''
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-03-25 07:59:19 +00:00
|
|
|
def matches(discount):
|
|
|
|
''' Returns True if and only if the given discount apples to
|
|
|
|
our product. '''
|
2016-04-22 05:06:24 +00:00
|
|
|
if isinstance(discount.clause, conditions.DiscountForCategory):
|
2016-03-25 07:59:19 +00:00
|
|
|
return discount.clause.category == product.category
|
|
|
|
else:
|
|
|
|
return discount.clause.product == product
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-03-25 07:59:19 +00:00
|
|
|
def value(discount):
|
|
|
|
''' Returns the value of this discount clause
|
|
|
|
as applied to this product '''
|
|
|
|
if discount.clause.percentage is not None:
|
|
|
|
return discount.clause.percentage * product.price
|
|
|
|
else:
|
|
|
|
return discount.clause.price
|
|
|
|
|
|
|
|
discounts = [i for i in discounts if matches(i)]
|
|
|
|
discounts.sort(key=value)
|
|
|
|
|
|
|
|
for candidate in reversed(discounts):
|
2016-01-22 05:01:30 +00:00
|
|
|
if quantity == 0:
|
|
|
|
break
|
2016-03-25 07:59:19 +00:00
|
|
|
elif candidate.quantity == 0:
|
|
|
|
# This discount clause has been exhausted by this cart
|
2016-01-22 05:01:30 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Get a provisional instance for this DiscountItem
|
|
|
|
# with the quantity set to as much as we have in the cart
|
2016-04-22 05:06:24 +00:00
|
|
|
discount_item = commerce.DiscountItem.objects.create(
|
2016-01-22 05:01:30 +00:00
|
|
|
product=product,
|
|
|
|
cart=self.cart,
|
2016-03-25 07:59:19 +00:00
|
|
|
discount=candidate.discount,
|
2016-01-22 05:01:30 +00:00
|
|
|
quantity=quantity,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Truncate the quantity for this DiscountItem if we exceed quantity
|
|
|
|
ours = discount_item.quantity
|
2016-03-25 07:59:19 +00:00
|
|
|
allowed = candidate.quantity
|
2016-01-22 05:01:30 +00:00
|
|
|
if ours > allowed:
|
|
|
|
discount_item.quantity = allowed
|
2016-04-25 04:31:25 +00:00
|
|
|
discount_item.save()
|
2016-01-22 05:01:30 +00:00
|
|
|
# Update the remaining quantity.
|
|
|
|
quantity = ours - allowed
|
|
|
|
else:
|
|
|
|
quantity = 0
|
2016-01-22 05:21:23 +00:00
|
|
|
|
2016-03-25 07:59:19 +00:00
|
|
|
candidate.quantity -= discount_item.quantity
|