diff --git a/registrasion/controllers/invoice.py b/registrasion/controllers/invoice.py index dafb134b..264049c8 100644 --- a/registrasion/controllers/invoice.py +++ b/registrasion/controllers/invoice.py @@ -57,6 +57,7 @@ class InvoiceController(object): return value @classmethod + @transaction.atomic def _generate(cls, cart): ''' Generates an invoice for the given cart. ''' invoice = rego.Invoice.objects.create( @@ -65,7 +66,6 @@ class InvoiceController(object): cart_revision=cart.revision, value=Decimal() ) - invoice.save() product_items = rego.ProductItem.objects.filter(cart=cart) @@ -85,7 +85,6 @@ class InvoiceController(object): quantity=item.quantity, price=product.price, ) - line_item.save() invoice_value += line_item.quantity * line_item.price for item in discount_items: @@ -95,11 +94,13 @@ class InvoiceController(object): quantity=item.quantity, price=cls.resolve_discount_value(item) * -1, ) - line_item.save() invoice_value += line_item.quantity * line_item.price - # TODO: calculate line items from discounts invoice.value = invoice_value + + if invoice.value == 0: + invoice.paid = True + invoice.save() return invoice diff --git a/registrasion/tests/test_discount.py b/registrasion/tests/test_discount.py index 1fb4225d..860a90a3 100644 --- a/registrasion/tests/test_discount.py +++ b/registrasion/tests/test_discount.py @@ -346,9 +346,8 @@ class DiscountTestCase(RegistrationCartTestCase): discounts = discount.available_discounts(self.USER_1, [self.CAT_2], []) self.assertEqual(2, discounts[0].quantity) - inv = InvoiceController.for_cart(cart.cart) - inv.pay("Dummy reference", inv.invoice.value) - self.assertTrue(inv.invoice.paid) + cart.cart.active = False + cart.cart.save() def test_discount_quantity_is_correct_after_first_purchase(self): self.test_discount_quantity_is_correct_before_first_purchase() @@ -358,9 +357,8 @@ class DiscountTestCase(RegistrationCartTestCase): discounts = discount.available_discounts(self.USER_1, [self.CAT_2], []) self.assertEqual(1, discounts[0].quantity) - inv = InvoiceController.for_cart(cart.cart) - inv.pay("Dummy reference", inv.invoice.value) - self.assertTrue(inv.invoice.paid) + cart.cart.active = False + cart.cart.save() def test_discount_is_gone_after_quantity_exhausted(self): self.test_discount_quantity_is_correct_after_first_purchase() diff --git a/registrasion/tests/test_invoice.py b/registrasion/tests/test_invoice.py index fd6c9cfb..5dc1559b 100644 --- a/registrasion/tests/test_invoice.py +++ b/registrasion/tests/test_invoice.py @@ -83,18 +83,16 @@ class InvoiceTestCase(RegistrationCartTestCase): code="VOUCHER", limit=1 ) - voucher.save() discount = rego.VoucherDiscount.objects.create( description="VOUCHER RECIPIENT", voucher=voucher, ) - discount.save() rego.DiscountForProduct.objects.create( discount=discount, product=self.PROD_1, percentage=Decimal(50), quantity=1 - ).save() + ) current_cart = TestingCartController.for_user(self.USER_1) current_cart.apply_voucher(voucher.code) @@ -111,6 +109,32 @@ class InvoiceTestCase(RegistrationCartTestCase): self.PROD_1.price * Decimal("0.5"), invoice_1.invoice.value) + def test_zero_value_invoice_is_automatically_paid(self): + voucher = rego.Voucher.objects.create( + recipient="Voucher recipient", + code="VOUCHER", + limit=1 + ) + discount = rego.VoucherDiscount.objects.create( + description="VOUCHER RECIPIENT", + voucher=voucher, + ) + rego.DiscountForProduct.objects.create( + discount=discount, + product=self.PROD_1, + percentage=Decimal(100), + quantity=1 + ) + + current_cart = TestingCartController.for_user(self.USER_1) + current_cart.apply_voucher(voucher.code) + + # Should be able to create an invoice after the product is added + current_cart.add_to_cart(self.PROD_1, 1) + invoice_1 = InvoiceController.for_cart(current_cart.cart) + + self.assertTrue(invoice_1.invoice.paid) + def test_invoice_voids_self_if_cart_is_invalid(self): current_cart = TestingCartController.for_user(self.USER_1) @@ -169,3 +193,8 @@ class InvoiceTestCase(RegistrationCartTestCase): with self.assertRaises(ValidationError): invoice_1.void() + + def test_cannot_generate_blank_invoice(self): + current_cart = TestingCartController.for_user(self.USER_1) + with self.assertRaises(ValidationError): + invoice_1 = InvoiceController.for_cart(current_cart.cart) diff --git a/registrasion/tests/test_voucher.py b/registrasion/tests/test_voucher.py index e3ac0d0d..1502c2fc 100644 --- a/registrasion/tests/test_voucher.py +++ b/registrasion/tests/test_voucher.py @@ -125,8 +125,8 @@ class VoucherTestCases(RegistrationCartTestCase): current_cart = TestingCartController.for_user(self.USER_1) current_cart.apply_voucher(voucher.code) - inv = InvoiceController.for_cart(current_cart.cart) - inv.pay("Hello!", inv.invoice.value) + current_cart.cart.active = False + current_cart.cart.save() current_cart = TestingCartController.for_user(self.USER_1) @@ -139,9 +139,11 @@ class VoucherTestCases(RegistrationCartTestCase): voucher = self.new_voucher(limit=2) current_cart = TestingCartController.for_user(self.USER_1) current_cart.apply_voucher(voucher.code) + current_cart.add_to_cart(self.PROD_1, 1) inv = InvoiceController.for_cart(current_cart.cart) - inv.pay("Hello!", inv.invoice.value) + if not inv.invoice.paid: + inv.pay("Hello!", inv.invoice.value) current_cart = TestingCartController.for_user(self.USER_1) with self.assertRaises(ValidationError):