Invoices now automatically void themselves if their cart is out of date
This commit is contained in:
parent
77ab00bc67
commit
8080d7851b
2 changed files with 40 additions and 9 deletions
|
@ -11,6 +11,7 @@ class InvoiceController(object):
|
||||||
|
|
||||||
def __init__(self, invoice):
|
def __init__(self, invoice):
|
||||||
self.invoice = invoice
|
self.invoice = invoice
|
||||||
|
self.update_validity() # Make sure this invoice is up-to-date
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def for_cart(cls, cart):
|
def for_cart(cls, cart):
|
||||||
|
@ -24,6 +25,10 @@ class InvoiceController(object):
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
cart_controller = CartController(cart)
|
cart_controller = CartController(cart)
|
||||||
cart_controller.validate_cart() # Raises ValidationError on fail.
|
cart_controller.validate_cart() # Raises ValidationError on fail.
|
||||||
|
|
||||||
|
# Void past invoices for this cart
|
||||||
|
invoices = rego.Invoice.objects.filter(cart=cart).update(void=True)
|
||||||
|
|
||||||
invoice = cls._generate(cart)
|
invoice = cls._generate(cart)
|
||||||
|
|
||||||
return InvoiceController(invoice)
|
return InvoiceController(invoice)
|
||||||
|
@ -91,19 +96,17 @@ class InvoiceController(object):
|
||||||
|
|
||||||
return invoice
|
return invoice
|
||||||
|
|
||||||
def is_valid(self):
|
def update_validity(self):
|
||||||
''' Returns true if the attached invoice is not void and it represents
|
''' Updates the validity of this invoice if the cart it is attached to
|
||||||
a valid cart. '''
|
has updated. '''
|
||||||
if self.invoice.void:
|
|
||||||
return False
|
|
||||||
if self.invoice.cart is not None:
|
if self.invoice.cart is not None:
|
||||||
if self.invoice.cart.revision != self.invoice.cart_revision:
|
if self.invoice.cart.revision != self.invoice.cart_revision:
|
||||||
return False
|
self.void()
|
||||||
return True
|
|
||||||
|
|
||||||
def void(self):
|
def void(self):
|
||||||
''' Voids the invoice. '''
|
''' Voids the invoice. '''
|
||||||
self.invoice.void = True
|
self.invoice.void = True
|
||||||
|
self.invoice.save()
|
||||||
|
|
||||||
def pay(self, reference, amount):
|
def pay(self, reference, amount):
|
||||||
''' Pays the invoice by the given amount. If the payment
|
''' Pays the invoice by the given amount. If the payment
|
||||||
|
|
|
@ -27,11 +27,17 @@ class InvoiceTestCase(RegistrationCartTestCase):
|
||||||
# That invoice should have a value equal to cost of PROD_1
|
# That invoice should have a value equal to cost of PROD_1
|
||||||
self.assertEqual(self.PROD_1.price, invoice_1.invoice.value)
|
self.assertEqual(self.PROD_1.price, invoice_1.invoice.value)
|
||||||
|
|
||||||
# Adding item to cart should void all active invoices and produce
|
# Adding item to cart should produce a new invoice
|
||||||
# a new invoice
|
|
||||||
current_cart.add_to_cart(self.PROD_2, 1)
|
current_cart.add_to_cart(self.PROD_2, 1)
|
||||||
invoice_2 = InvoiceController.for_cart(current_cart.cart)
|
invoice_2 = InvoiceController.for_cart(current_cart.cart)
|
||||||
self.assertNotEqual(invoice_1.invoice, invoice_2.invoice)
|
self.assertNotEqual(invoice_1.invoice, invoice_2.invoice)
|
||||||
|
|
||||||
|
# The old invoice should automatically be voided
|
||||||
|
invoice_1_new = rego.Invoice.objects.get(pk=invoice_1.invoice.id)
|
||||||
|
invoice_2_new = rego.Invoice.objects.get(pk=invoice_2.invoice.id)
|
||||||
|
self.assertTrue(invoice_1_new.void)
|
||||||
|
self.assertFalse(invoice_2_new.void)
|
||||||
|
|
||||||
# Invoice should have two line items
|
# Invoice should have two line items
|
||||||
line_items = rego.LineItem.objects.filter(invoice=invoice_2.invoice)
|
line_items = rego.LineItem.objects.filter(invoice=invoice_2.invoice)
|
||||||
self.assertEqual(2, len(line_items))
|
self.assertEqual(2, len(line_items))
|
||||||
|
@ -104,3 +110,25 @@ class InvoiceTestCase(RegistrationCartTestCase):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
self.PROD_1.price * Decimal("0.5"),
|
self.PROD_1.price * Decimal("0.5"),
|
||||||
invoice_1.invoice.value)
|
invoice_1.invoice.value)
|
||||||
|
|
||||||
|
def test_invoice_voids_self_if_cart_is_invalid(self):
|
||||||
|
current_cart = CartController.for_user(self.USER_1)
|
||||||
|
|
||||||
|
# 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.assertFalse(invoice_1.invoice.void)
|
||||||
|
|
||||||
|
# Adding item to cart should produce a new invoice
|
||||||
|
current_cart.add_to_cart(self.PROD_2, 1)
|
||||||
|
invoice_2 = InvoiceController.for_cart(current_cart.cart)
|
||||||
|
self.assertNotEqual(invoice_1.invoice, invoice_2.invoice)
|
||||||
|
|
||||||
|
# Viewing invoice_1's invoice should show it as void
|
||||||
|
invoice_1_new = InvoiceController(invoice_1.invoice)
|
||||||
|
self.assertTrue(invoice_1.invoice.void)
|
||||||
|
|
||||||
|
# Viewing invoice_2's invoice should *not* show it as void
|
||||||
|
invoice_2_new = InvoiceController(invoice_2.invoice)
|
||||||
|
self.assertFalse(invoice_2.invoice.void)
|
||||||
|
|
Loading…
Reference in a new issue