Adds validate_allowed_to_pay(), which validates whether you’re allowed to pay for an invoice
This commit is contained in:
		
							parent
							
								
									0e80e0336c
								
							
						
					
					
						commit
						2fbe789090
					
				
					 4 changed files with 44 additions and 12 deletions
				
			
		|  | @ -126,6 +126,30 @@ class InvoiceController(object): | ||||||
| 
 | 
 | ||||||
|         return invoice |         return invoice | ||||||
| 
 | 
 | ||||||
|  |     def _refresh(self): | ||||||
|  |         ''' Refreshes the underlying invoice and cart objects. ''' | ||||||
|  |         self.invoice.refresh_from_db() | ||||||
|  |         if self.invoice.cart: | ||||||
|  |             self.invoice.cart.refresh_from_db() | ||||||
|  | 
 | ||||||
|  |     def validate_allowed_to_pay(self): | ||||||
|  |         ''' Passes cleanly if we're allowed to pay, otherwise raise | ||||||
|  |         a ValidationError. ''' | ||||||
|  | 
 | ||||||
|  |         self._refresh() | ||||||
|  | 
 | ||||||
|  |         if not self.invoice.is_unpaid: | ||||||
|  |             raise ValidationError("You can only pay for unpaid invoices.") | ||||||
|  | 
 | ||||||
|  |         if not self.invoice.cart: | ||||||
|  |             return | ||||||
|  | 
 | ||||||
|  |         if not self._invoice_matches_cart(): | ||||||
|  |             raise ValidationError("The registration has been amended since " | ||||||
|  |                                   "generating this invoice.") | ||||||
|  | 
 | ||||||
|  |         CartController(self.invoice.cart).validate_cart() | ||||||
|  | 
 | ||||||
|     def total_payments(self): |     def total_payments(self): | ||||||
|         ''' Returns the total amount paid towards this invoice. ''' |         ''' Returns the total amount paid towards this invoice. ''' | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -37,17 +37,8 @@ class TestingInvoiceController(InvoiceController): | ||||||
|     def pay(self, reference, amount): |     def pay(self, reference, amount): | ||||||
|         ''' Testing method for simulating an invoice paymenht by the given |         ''' Testing method for simulating an invoice paymenht by the given | ||||||
|         amount. ''' |         amount. ''' | ||||||
|         if self.invoice.cart: |  | ||||||
|             cart = CartController(self.invoice.cart) |  | ||||||
|             cart.validate_cart()  # Raises ValidationError if invalid |  | ||||||
| 
 | 
 | ||||||
|         status = self.invoice.status |         self.validate_allowed_to_pay() | ||||||
|         if status == rego.Invoice.STATUS_VOID: |  | ||||||
|             raise ValidationError("Void invoices cannot be paid") |  | ||||||
|         elif status == rego.Invoice.STATUS_PAID: |  | ||||||
|             raise ValidationError("Paid invoices cannot be paid again") |  | ||||||
|         elif status == rego.Invoice.STATUS_REFUNDED: |  | ||||||
|             raise ValidationError("Refunded invoices cannot be paid") |  | ||||||
| 
 | 
 | ||||||
|         ''' Adds a payment ''' |         ''' Adds a payment ''' | ||||||
|         payment = rego.ManualPayment.objects.create( |         payment = rego.ManualPayment.objects.create( | ||||||
|  |  | ||||||
|  | @ -148,6 +148,10 @@ class RegistrationCartTestCase(SetTimeMixin, TestCase): | ||||||
|         voucher.save() |         voucher.save() | ||||||
|         return voucher |         return voucher | ||||||
| 
 | 
 | ||||||
|  |     @classmethod | ||||||
|  |     def reget(cls, object): | ||||||
|  |         return type(object).objects.get(id=object.id) | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| class BasicCartTests(RegistrationCartTestCase): | class BasicCartTests(RegistrationCartTestCase): | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -180,7 +180,7 @@ class InvoiceTestCase(RegistrationCartTestCase): | ||||||
|         invoice_1.void() |         invoice_1.void() | ||||||
| 
 | 
 | ||||||
|         with self.assertRaises(ValidationError): |         with self.assertRaises(ValidationError): | ||||||
|             invoice_1.pay("Reference", invoice_1.invoice.value) |             invoice_1.validate_allowed_to_pay() | ||||||
| 
 | 
 | ||||||
|     def test_cannot_void_paid_invoice(self): |     def test_cannot_void_paid_invoice(self): | ||||||
|         current_cart = TestingCartController.for_user(self.USER_1) |         current_cart = TestingCartController.for_user(self.USER_1) | ||||||
|  | @ -199,9 +199,22 @@ class InvoiceTestCase(RegistrationCartTestCase): | ||||||
|         with self.assertRaises(ValidationError): |         with self.assertRaises(ValidationError): | ||||||
|             invoice_1 = TestingInvoiceController.for_cart(current_cart.cart) |             invoice_1 = TestingInvoiceController.for_cart(current_cart.cart) | ||||||
| 
 | 
 | ||||||
|  |     def test_cannot_pay_implicitly_void_invoice(self): | ||||||
|  |         cart = TestingCartController.for_user(self.USER_1) | ||||||
|  |         cart.add_to_cart(self.PROD_1, 1) | ||||||
|  |         invoice = TestingInvoiceController.for_cart(self.reget(cart.cart)) | ||||||
|  | 
 | ||||||
|  |         # Implicitly void the invoice | ||||||
|  |         cart.add_to_cart(self.PROD_1, 1) | ||||||
|  | 
 | ||||||
|  |         with self.assertRaises(ValidationError): | ||||||
|  |             invoice.validate_allowed_to_pay() | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|     # TODO: test partially paid invoice cannot be void until payments |     # TODO: test partially paid invoice cannot be void until payments | ||||||
|     # are refunded |     # are refunded | ||||||
| 
 | 
 | ||||||
|     # TODO: test overpaid invoice results in credit note |     # TODO: test overpaid invoice results in credit note | ||||||
| 
 | 
 | ||||||
|     # TODO: test credit note generation more generally  |     # TODO: test credit note generation more generally | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 Christopher Neugebauer
						Christopher Neugebauer