Changeset - e1281796294a
[Not reviewed]
Merge
0 2 0
Christopher Neugebauer - 8 years ago 2016-09-03 01:46:51
chrisjrn@gmail.com
Merge branch 'chrisjrn/fix_64'
2 files changed with 54 insertions and 4 deletions:
0 comments (0 inline, 0 general)
registrasion/controllers/invoice.py
Show inline comments
...
 
@@ -34,34 +34,34 @@ class InvoiceController(ForId, object):
 
        cart.refresh_from_db()
 
        try:
 
            invoice = commerce.Invoice.objects.exclude(
 
                status=commerce.Invoice.STATUS_VOID,
 
            ).get(
 
                cart=cart,
 
                cart_revision=cart.revision,
 
            )
 
        except ObjectDoesNotExist:
 
            cart_controller = CartController(cart)
 
            cart_controller.validate_cart()  # Raises ValidationError on fail.
 

	
 
            cls.void_all_invoices(cart)
 
            cls.update_old_invoices(cart)
 
            invoice = cls._generate(cart)
 

	
 
        return cls(invoice)
 

	
 
    @classmethod
 
    def void_all_invoices(cls, cart):
 
    def update_old_invoices(cls, cart):
 
        invoices = commerce.Invoice.objects.filter(cart=cart).all()
 
        for invoice in invoices:
 
            cls(invoice).void()
 
            cls(invoice).update_status()
 

	
 
    @classmethod
 
    def resolve_discount_value(cls, item):
 
        try:
 
            condition = conditions.DiscountForProduct.objects.get(
 
                discount=item.discount,
 
                product=item.product
 
            )
 
        except ObjectDoesNotExist:
 
            condition = conditions.DiscountForCategory.objects.get(
 
                discount=item.discount,
 
                category=item.product.category
...
 
@@ -290,25 +290,29 @@ class InvoiceController(ForId, object):
 

	
 
        self._refresh()
 

	
 
        cart = self.invoice.cart
 
        if not cart:
 
            return True
 

	
 
        return cart.revision == self.invoice.cart_revision
 

	
 
    def update_validity(self):
 
        ''' Voids this invoice if the cart it is attached to has updated. '''
 
        if not self._invoice_matches_cart():
 
            self.void()
 
            if self.total_payments() > 0:
 
                # Free up the payments made to this invoice
 
                self.refund()
 
            else:
 
                self.void()
 

	
 
    def void(self):
 
        ''' Voids the invoice if it is valid to do so. '''
 
        if self.total_payments() > 0:
 
            raise ValidationError("Invoices with payments must be refunded.")
 
        elif self.invoice.is_refunded:
 
            raise ValidationError("Refunded invoices may not be voided.")
 
        self._mark_void()
 

	
 
    @transaction.atomic
 
    def refund(self):
 
        ''' Refunds the invoice by generating a CreditNote for the value of
registrasion/tests/test_invoice.py
Show inline comments
...
 
@@ -525,24 +525,70 @@ class InvoiceTestCase(RegistrationCartTestCase):
 

	
 
        invoice2 = TestingInvoiceController.for_cart(cart.cart)
 

	
 
        # Void invoice2, and release the first cart
 
        # now we don't have any CAT_1
 
        invoice2.void()
 
        invoice.refund()
 

	
 
        # Now that we don't have CAT_1, we can't checkout this cart
 
        with self.assertRaises(ValidationError):
 
            invoice = TestingInvoiceController.for_cart(cart.cart)
 

	
 
    def test_invoice_with_credit_note_applied_is_refunded(self):
 
        ''' Invoices with partial payments should void when cart is updated.
 

	
 
        Test for issue #64 -- applying a credit note to an invoice
 
        means that invoice cannot be voided, and new invoices cannot be
 
        created. '''
 

	
 
        cart = TestingCartController.for_user(self.USER_1)
 

	
 
        cart.add_to_cart(self.PROD_1, 1)
 
        invoice = TestingInvoiceController.for_cart(cart.cart)
 

	
 
        # Now get a credit note
 
        invoice.pay("Lol", invoice.invoice.value)
 
        invoice.refund()
 
        cn = self._credit_note_for_invoice(invoice.invoice)
 

	
 
        # Create a cart of higher value than the credit note
 
        cart = TestingCartController.for_user(self.USER_1)
 
        cart.add_to_cart(self.PROD_1, 2)
 

	
 
        # Create a current invoice, and apply partial payments
 
        invoice = TestingInvoiceController.for_cart(cart.cart)
 
        cn.apply_to_invoice(invoice.invoice)
 

	
 
        # Adding to cart will mean that the old invoice for this cart
 
        # will be invalidated. A new invoice should be generated.
 
        cart.add_to_cart(self.PROD_1, 1)
 
        invoice = TestingInvoiceController.for_id(invoice.invoice.id)
 
        invoice2 = TestingInvoiceController.for_cart(cart.cart)
 
        cn2 = self._credit_note_for_invoice(invoice.invoice)
 

	
 
        invoice._refresh()
 

	
 
        # The first invoice should be refunded
 
        self.assertEquals(
 
            commerce.Invoice.STATUS_VOID,
 
            invoice.invoice.status,
 
        )
 

	
 
        # Both credit notes should be for the same amount
 
        self.assertEquals(
 
            cn.credit_note.value,
 
            cn2.credit_note.value,
 
        )
 

	
 
    def test_sends_email_on_invoice_creation(self):
 
        invoice = self._invoice_containing_prod_1(1)
 
        self.assertEquals(1, len(self.emails))
 
        email = self.emails[0]
 
        self.assertEquals([self.USER_1.email], email["to"])
 
        self.assertEquals("invoice_created", email["kind"])
 
        self.assertEquals(invoice.invoice, email["context"]["invoice"])
 

	
 
    def test_sends_first_change_email_on_invoice_fully_paid(self):
 
        invoice = self._invoice_containing_prod_1(1)
 

	
 
        self.assertEquals(1, len(self.emails))
0 comments (0 inline, 0 general)