From a9bc6475707c6f6aaea7f512b9140cc67abc54d2 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Thu, 15 Sep 2016 08:38:33 +1000 Subject: [PATCH] Replaces _generate with _generate_from_cart and _generate --- registrasion/controllers/invoice.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/registrasion/controllers/invoice.py b/registrasion/controllers/invoice.py index b88abb09..118b49cb 100644 --- a/registrasion/controllers/invoice.py +++ b/registrasion/controllers/invoice.py @@ -44,7 +44,7 @@ class InvoiceController(ForId, object): cart_controller.validate_cart() # Raises ValidationError on fail. cls.update_old_invoices(cart) - invoice = cls._generate(cart) + invoice = cls._generate_from_cart(cart) return cls(invoice) @@ -74,7 +74,7 @@ class InvoiceController(ForId, object): @classmethod @transaction.atomic - def _generate(cls, cart): + def _generate_from_cart(cls, cart): ''' Generates an invoice for the given cart. ''' cart.refresh_from_db() @@ -86,14 +86,13 @@ class InvoiceController(ForId, object): "product", "product__category", ) - - if len(product_items) == 0: - raise ValidationError("Your cart is empty.") - product_items = product_items.order_by( "product__category__order", "product__order" ) + if len(product_items) == 0: + raise ValidationError("Your cart is empty.") + discount_items = commerce.DiscountItem.objects.filter(cart=cart) discount_items = discount_items.select_related( "discount", @@ -101,8 +100,6 @@ class InvoiceController(ForId, object): "product__category", ) - line_items = [] - def format_product(product): return "%s - %s" % (product.category.name, product.name) @@ -110,6 +107,8 @@ class InvoiceController(ForId, object): description = discount.description return "%s (%s)" % (description, format_product(product)) + line_items = [] + for item in product_items: product = item.product line_item = commerce.LineItem( @@ -131,10 +130,17 @@ class InvoiceController(ForId, object): # Generate the invoice user = cart.user - reservation_limit = cart.reservation_duration + cart.time_last_updated + min_due_time = cart.reservation_duration + cart.time_last_updated + + return cls._generate(cart.user, cart, min_due_time, line_items) + + @classmethod + @transaction.atomic + def _generate(cls, user, cart, min_due_time, line_items): + # Never generate a due time that is before the issue time issued = timezone.now() - due = max(issued, reservation_limit) + due = max(issued, min_due_time) # Get the invoice recipient profile = people.AttendeeProfileBase.objects.get_subclass(