Zeroed & paid invoices that are voided now release the cart.

Fixes #95.
This commit is contained in:
Christopher Neugebauer 2016-10-13 11:23:41 -07:00
parent c9c9d2a2b2
commit 3f192c2626
2 changed files with 23 additions and 6 deletions

View file

@ -331,10 +331,7 @@ class InvoiceController(ForId, object):
def _mark_refunded(self): def _mark_refunded(self):
''' Marks the invoice as refunded, and updates the attached cart if ''' Marks the invoice as refunded, and updates the attached cart if
necessary. ''' necessary. '''
cart = self.invoice.cart self._release_cart()
if cart:
cart.status = commerce.Cart.STATUS_RELEASED
cart.save()
self.invoice.status = commerce.Invoice.STATUS_REFUNDED self.invoice.status = commerce.Invoice.STATUS_REFUNDED
self.invoice.save() self.invoice.save()
@ -356,6 +353,12 @@ class InvoiceController(ForId, object):
return cart.revision == self.invoice.cart_revision return cart.revision == self.invoice.cart_revision
def _release_cart(self):
cart = self.invoice.cart
if cart:
cart.status = commerce.Cart.STATUS_RELEASED
cart.save()
def update_validity(self): def update_validity(self):
''' Voids this invoice if the attached cart is no longer valid because ''' Voids this invoice if the attached cart is no longer valid because
the cart revision has changed, or the reservations have expired. ''' the cart revision has changed, or the reservations have expired. '''
@ -381,6 +384,9 @@ class InvoiceController(ForId, object):
raise ValidationError("Invoices with payments must be refunded.") raise ValidationError("Invoices with payments must be refunded.")
elif self.invoice.is_refunded: elif self.invoice.is_refunded:
raise ValidationError("Refunded invoices may not be voided.") raise ValidationError("Refunded invoices may not be voided.")
if self.invoice.is_paid:
self._release_cart()
self._mark_void() self._mark_void()
@transaction.atomic @transaction.atomic

View file

@ -142,7 +142,7 @@ class InvoiceTestCase(TestHelperMixin, RegistrationCartTestCase):
self.PROD_1.price * Decimal("0.5"), self.PROD_1.price * Decimal("0.5"),
invoice_1.invoice.value) invoice_1.invoice.value)
def test_zero_value_invoice_is_automatically_paid(self): def _make_zero_value_invoice(self):
voucher = inventory.Voucher.objects.create( voucher = inventory.Voucher.objects.create(
recipient="Voucher recipient", recipient="Voucher recipient",
code="VOUCHER", code="VOUCHER",
@ -164,10 +164,21 @@ class InvoiceTestCase(TestHelperMixin, RegistrationCartTestCase):
# Should be able to create an invoice after the product is added # Should be able to create an invoice after the product is added
current_cart.add_to_cart(self.PROD_1, 1) current_cart.add_to_cart(self.PROD_1, 1)
invoice_1 = TestingInvoiceController.for_cart(current_cart.cart) return TestingInvoiceController.for_cart(current_cart.cart)
def test_zero_value_invoice_is_automatically_paid(self):
invoice_1 = self._make_zero_value_invoice()
self.assertTrue(invoice_1.invoice.is_paid) self.assertTrue(invoice_1.invoice.is_paid)
def test_refunding_zero_value_invoice_releases_cart(self):
invoice_1 = self._make_zero_value_invoice()
cart = invoice_1.invoice.cart
invoice_1.refund()
cart.refresh_from_db()
self.assertEquals(commerce.Cart.STATUS_RELEASED, cart.status)
def test_invoice_voids_self_if_cart_changes(self): def test_invoice_voids_self_if_cart_changes(self):
current_cart = TestingCartController.for_user(self.USER_1) current_cart = TestingCartController.for_user(self.USER_1)