Changeset - e946af0f0487
[Not reviewed]
0 1 0
Christopher Neugebauer - 8 years ago 2016-08-21 06:56:05
chrisjrn@gmail.com
Adds functions for mailing invoices when certain events occur.
1 file changed with 34 insertions and 0 deletions:
0 comments (0 inline, 0 general)
registrasion/controllers/invoice.py
Show inline comments
 
from decimal import Decimal
 
from django.core.exceptions import ObjectDoesNotExist
 
from django.core.exceptions import ValidationError
 
from django.db import transaction
 
from django.db.models import Sum
 
from django.utils import timezone
 

	
 
from registrasion.contrib.mail import send_email
 

	
 
from registrasion.models import commerce
 
from registrasion.models import conditions
 
from registrasion.models import people
 

	
 
from cart import CartController
 
from credit_note import CreditNoteController
 
from for_id import ForId
 

	
 

	
 
class InvoiceController(ForId, object):
 

	
 
    __MODEL__ = commerce.Invoice
...
 
@@ -140,24 +142,26 @@ class InvoiceController(ForId, object):
 
                price=cls.resolve_discount_value(item) * -1,
 
                product=item.product,
 
            )
 
            line_items.append(line_item)
 
            invoice_value += line_item.quantity * line_item.price
 

	
 
        commerce.LineItem.objects.bulk_create(line_items)
 

	
 
        invoice.value = invoice_value
 

	
 
        invoice.save()
 

	
 
        cls.email_on_invoice_creation(invoice)
 

	
 
        return invoice
 

	
 
    def can_view(self, user=None, access_code=None):
 
        ''' Returns true if the accessing user is allowed to view this invoice,
 
        or if the given access code matches this invoice's user's access code.
 
        '''
 

	
 
        if user == self.invoice.user:
 
            return True
 

	
 
        if user.is_staff:
 
            return True
...
 
@@ -305,12 +309,42 @@ class InvoiceController(ForId, object):
 
        if self.invoice.is_void:
 
            raise ValidationError("Void invoices cannot be refunded")
 

	
 
        # Raises a credit note fot the value of the invoice.
 
        amount = self.total_payments()
 

	
 
        if amount == 0:
 
            self.void()
 
            return
 

	
 
        CreditNoteController.generate_from_invoice(self.invoice, amount)
 
        self.update_status()
 

	
 
    @classmethod
 
    def email(cls, invoice, kind):
 
        ''' Sends out an e-mail notifying the user about something to do
 
        with that invoice. '''
 

	
 
        context = {
 
            "invoice": invoice,
 
        }
 

	
 
        send_email(invoice.user.email, kind, context=context)
 

	
 
    @classmethod
 
    def email_on_invoice_creation(cls, invoice):
 
        ''' Sends out an e-mail notifying the user that an invoice has been
 
        created. '''
 

	
 
        cls.email(invoice, "invoice_created")
 

	
 
    @classmethod
 
    def email_on_invoice_change(cls, invoice, old_status, new_status):
 
        ''' Sends out all of the necessary notifications that the status of the
 
        invoice has changed to:
 

	
 
        - Invoice is now paid
 
        - Invoice is now refunded
 

	
 
        '''
 

	
 
        cls.email(invoice, "invoice_updated")
0 comments (0 inline, 0 general)