2016-01-22 05:01:30 +00:00
|
|
|
from decimal import Decimal
|
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2016-03-27 03:41:43 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2016-03-27 08:25:24 +00:00
|
|
|
from django.db import transaction
|
2016-01-22 06:02:07 +00:00
|
|
|
from django.db.models import Sum
|
2016-04-06 22:28:43 +00:00
|
|
|
from django.utils import timezone
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-08-21 06:56:05 +00:00
|
|
|
from registrasion.contrib.mail import send_email
|
|
|
|
|
2016-04-22 05:06:24 +00:00
|
|
|
from registrasion.models import commerce
|
|
|
|
from registrasion.models import conditions
|
|
|
|
from registrasion.models import people
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2017-04-17 12:55:48 +00:00
|
|
|
from registrasion.controllers.cart import CartController
|
|
|
|
from registrasion.controllers.credit_note import CreditNoteController
|
|
|
|
from registrasion.controllers.for_id import ForId
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-24 22:13:44 +00:00
|
|
|
|
2016-04-24 21:13:37 +00:00
|
|
|
class InvoiceController(ForId, object):
|
2016-01-22 06:02:07 +00:00
|
|
|
|
2016-04-24 21:13:37 +00:00
|
|
|
__MODEL__ = commerce.Invoice
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
def __init__(self, invoice):
|
|
|
|
self.invoice = invoice
|
2016-04-06 22:28:43 +00:00
|
|
|
self.update_status()
|
2016-03-27 03:41:43 +00:00
|
|
|
self.update_validity() # Make sure this invoice is up-to-date
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def for_cart(cls, cart):
|
|
|
|
''' Returns an invoice object for a given cart at its current revision.
|
|
|
|
If such an invoice does not exist, the cart is validated, and if valid,
|
|
|
|
an invoice is generated.'''
|
|
|
|
|
2016-04-28 04:01:36 +00:00
|
|
|
cart.refresh_from_db()
|
2016-01-22 05:01:30 +00:00
|
|
|
try:
|
2016-04-22 05:06:24 +00:00
|
|
|
invoice = commerce.Invoice.objects.exclude(
|
|
|
|
status=commerce.Invoice.STATUS_VOID,
|
2016-04-06 22:28:43 +00:00
|
|
|
).get(
|
2016-03-27 03:41:43 +00:00
|
|
|
cart=cart,
|
|
|
|
cart_revision=cart.revision,
|
|
|
|
)
|
2016-01-22 05:01:30 +00:00
|
|
|
except ObjectDoesNotExist:
|
|
|
|
cart_controller = CartController(cart)
|
2016-01-22 06:02:07 +00:00
|
|
|
cart_controller.validate_cart() # Raises ValidationError on fail.
|
2016-03-27 03:04:47 +00:00
|
|
|
|
2016-09-03 01:46:24 +00:00
|
|
|
cls.update_old_invoices(cart)
|
2016-09-14 22:38:33 +00:00
|
|
|
invoice = cls._generate_from_cart(cart)
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-06 22:28:43 +00:00
|
|
|
return cls(invoice)
|
|
|
|
|
|
|
|
@classmethod
|
2016-09-03 01:46:24 +00:00
|
|
|
def update_old_invoices(cls, cart):
|
2016-04-22 05:06:24 +00:00
|
|
|
invoices = commerce.Invoice.objects.filter(cart=cart).all()
|
2016-04-06 22:28:43 +00:00
|
|
|
for invoice in invoices:
|
2016-09-03 01:46:24 +00:00
|
|
|
cls(invoice).update_status()
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def resolve_discount_value(cls, item):
|
|
|
|
try:
|
2016-04-22 05:06:24 +00:00
|
|
|
condition = conditions.DiscountForProduct.objects.get(
|
2016-01-22 05:01:30 +00:00
|
|
|
discount=item.discount,
|
|
|
|
product=item.product
|
|
|
|
)
|
|
|
|
except ObjectDoesNotExist:
|
2016-04-22 05:06:24 +00:00
|
|
|
condition = conditions.DiscountForCategory.objects.get(
|
2016-01-22 05:01:30 +00:00
|
|
|
discount=item.discount,
|
|
|
|
category=item.product.category
|
|
|
|
)
|
|
|
|
if condition.percentage is not None:
|
|
|
|
value = item.product.price * (condition.percentage / 100)
|
|
|
|
else:
|
|
|
|
value = condition.price
|
|
|
|
return value
|
|
|
|
|
2016-09-14 22:50:56 +00:00
|
|
|
@classmethod
|
|
|
|
@transaction.atomic
|
|
|
|
def manual_invoice(cls, user, due_delta, description_price_pairs):
|
|
|
|
''' Generates an invoice for arbitrary items, not held in a user's
|
|
|
|
cart.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
user (User): The user the invoice is being generated for.
|
|
|
|
due_delta (datetime.timedelta): The length until the invoice is
|
|
|
|
due.
|
|
|
|
description_price_pairs ([(str, long or Decimal), ...]): A list of
|
|
|
|
pairs. Each pair consists of the description for each line item
|
|
|
|
and the price for that line item. The price will be cast to
|
|
|
|
Decimal.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
an Invoice.
|
|
|
|
'''
|
|
|
|
|
|
|
|
line_items = []
|
|
|
|
for description, price in description_price_pairs:
|
|
|
|
line_item = commerce.LineItem(
|
|
|
|
description=description,
|
|
|
|
quantity=1,
|
|
|
|
price=Decimal(price),
|
|
|
|
product=None,
|
|
|
|
)
|
|
|
|
line_items.append(line_item)
|
|
|
|
|
|
|
|
min_due_time = timezone.now() + due_delta
|
|
|
|
return cls._generate(user, None, min_due_time, line_items)
|
|
|
|
|
2016-01-22 05:01:30 +00:00
|
|
|
@classmethod
|
2016-04-06 07:19:09 +00:00
|
|
|
@transaction.atomic
|
2016-09-14 22:38:33 +00:00
|
|
|
def _generate_from_cart(cls, cart):
|
2016-01-22 05:01:30 +00:00
|
|
|
''' Generates an invoice for the given cart. '''
|
2016-04-06 22:28:43 +00:00
|
|
|
|
2016-04-28 04:01:36 +00:00
|
|
|
cart.refresh_from_db()
|
|
|
|
|
2016-09-14 09:44:36 +00:00
|
|
|
# Generate the line items from the cart.
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-22 05:06:24 +00:00
|
|
|
product_items = commerce.ProductItem.objects.filter(cart=cart)
|
2016-04-28 09:58:09 +00:00
|
|
|
product_items = product_items.select_related(
|
|
|
|
"product",
|
|
|
|
"product__category",
|
|
|
|
)
|
2016-03-04 20:22:01 +00:00
|
|
|
product_items = product_items.order_by(
|
|
|
|
"product__category__order", "product__order"
|
|
|
|
)
|
2016-04-28 09:58:09 +00:00
|
|
|
|
2016-09-14 22:38:33 +00:00
|
|
|
if len(product_items) == 0:
|
|
|
|
raise ValidationError("Your cart is empty.")
|
|
|
|
|
2016-04-22 05:06:24 +00:00
|
|
|
discount_items = commerce.DiscountItem.objects.filter(cart=cart)
|
2016-04-28 09:58:09 +00:00
|
|
|
discount_items = discount_items.select_related(
|
|
|
|
"discount",
|
|
|
|
"product",
|
|
|
|
"product__category",
|
|
|
|
)
|
|
|
|
|
2016-08-24 01:46:15 +00:00
|
|
|
def format_product(product):
|
|
|
|
return "%s - %s" % (product.category.name, product.name)
|
|
|
|
|
|
|
|
def format_discount(discount, product):
|
|
|
|
description = discount.description
|
|
|
|
return "%s (%s)" % (description, format_product(product))
|
|
|
|
|
2016-09-14 22:38:33 +00:00
|
|
|
line_items = []
|
|
|
|
|
2016-01-22 05:01:30 +00:00
|
|
|
for item in product_items:
|
2016-03-04 20:22:01 +00:00
|
|
|
product = item.product
|
2016-04-28 09:58:09 +00:00
|
|
|
line_item = commerce.LineItem(
|
2016-08-24 01:46:15 +00:00
|
|
|
description=format_product(product),
|
2016-01-22 05:01:30 +00:00
|
|
|
quantity=item.quantity,
|
2016-03-04 20:22:01 +00:00
|
|
|
price=product.price,
|
2016-04-06 22:28:43 +00:00
|
|
|
product=product,
|
2016-01-22 05:01:30 +00:00
|
|
|
)
|
2016-04-28 09:58:09 +00:00
|
|
|
line_items.append(line_item)
|
2016-01-22 05:01:30 +00:00
|
|
|
for item in discount_items:
|
2016-04-28 09:58:09 +00:00
|
|
|
line_item = commerce.LineItem(
|
2016-08-24 01:46:15 +00:00
|
|
|
description=format_discount(item.discount, item.product),
|
2016-01-22 05:01:30 +00:00
|
|
|
quantity=item.quantity,
|
|
|
|
price=cls.resolve_discount_value(item) * -1,
|
2016-04-06 22:28:43 +00:00
|
|
|
product=item.product,
|
2016-01-22 05:01:30 +00:00
|
|
|
)
|
2016-04-28 09:58:09 +00:00
|
|
|
line_items.append(line_item)
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-09-14 09:44:36 +00:00
|
|
|
# Generate the invoice
|
2016-04-28 09:58:09 +00:00
|
|
|
|
2016-09-14 22:38:33 +00:00
|
|
|
min_due_time = cart.reservation_duration + cart.time_last_updated
|
|
|
|
|
|
|
|
return cls._generate(cart.user, cart, min_due_time, line_items)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@transaction.atomic
|
|
|
|
def _generate(cls, user, cart, min_due_time, line_items):
|
|
|
|
|
2016-09-14 09:44:36 +00:00
|
|
|
# Never generate a due time that is before the issue time
|
|
|
|
issued = timezone.now()
|
2016-09-14 22:38:33 +00:00
|
|
|
due = max(issued, min_due_time)
|
2016-09-14 09:44:36 +00:00
|
|
|
|
|
|
|
# Get the invoice recipient
|
|
|
|
profile = people.AttendeeProfileBase.objects.get_subclass(
|
|
|
|
id=user.attendee.attendeeprofilebase.id,
|
|
|
|
)
|
|
|
|
recipient = profile.invoice_recipient()
|
|
|
|
|
|
|
|
invoice_value = sum(item.quantity * item.price for item in line_items)
|
2016-04-06 07:19:09 +00:00
|
|
|
|
2016-09-14 09:44:36 +00:00
|
|
|
invoice = commerce.Invoice.objects.create(
|
|
|
|
user=user,
|
|
|
|
cart=cart,
|
2016-09-14 23:08:29 +00:00
|
|
|
cart_revision=cart.revision if cart else None,
|
2016-09-14 09:44:36 +00:00
|
|
|
status=commerce.Invoice.STATUS_UNPAID,
|
|
|
|
value=invoice_value,
|
|
|
|
issue_time=issued,
|
|
|
|
due_time=due,
|
|
|
|
recipient=recipient,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Associate the line items with the invoice
|
|
|
|
for line_item in line_items:
|
|
|
|
line_item.invoice = invoice
|
|
|
|
|
|
|
|
commerce.LineItem.objects.bulk_create(line_items)
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-09-15 00:22:12 +00:00
|
|
|
cls._apply_credit_notes(invoice)
|
2016-08-21 06:56:05 +00:00
|
|
|
cls.email_on_invoice_creation(invoice)
|
|
|
|
|
2016-01-22 05:01:30 +00:00
|
|
|
return invoice
|
|
|
|
|
2016-09-15 00:22:12 +00:00
|
|
|
@classmethod
|
|
|
|
def _apply_credit_notes(cls, invoice):
|
|
|
|
''' Applies the user's credit notes to the given invoice on creation.
|
|
|
|
'''
|
|
|
|
|
2016-09-15 00:32:22 +00:00
|
|
|
# We only automatically apply credit notes if this is the *only*
|
|
|
|
# unpaid invoice for this user.
|
|
|
|
invoices = commerce.Invoice.objects.filter(
|
|
|
|
user=invoice.user,
|
|
|
|
status=commerce.Invoice.STATUS_UNPAID,
|
|
|
|
)
|
|
|
|
if invoices.count() > 1:
|
2016-09-15 00:22:12 +00:00
|
|
|
return
|
|
|
|
|
2016-09-15 01:41:50 +00:00
|
|
|
notes = commerce.CreditNote.unclaimed().filter(
|
|
|
|
invoice__user=invoice.user
|
|
|
|
)
|
2016-09-15 00:22:12 +00:00
|
|
|
for note in notes:
|
|
|
|
try:
|
|
|
|
CreditNoteController(note).apply_to_invoice(invoice)
|
|
|
|
except ValidationError:
|
|
|
|
# ValidationError will get raised once we're overpaying.
|
|
|
|
break
|
|
|
|
|
|
|
|
invoice.refresh_from_db()
|
|
|
|
|
2016-04-08 03:15:24 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
if self.invoice.user.attendee.access_code == access_code:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2016-04-07 08:26:31 +00:00
|
|
|
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()
|
|
|
|
|
2016-04-06 22:28:43 +00:00
|
|
|
def update_status(self):
|
|
|
|
''' Updates the status of this invoice based upon the total
|
|
|
|
payments.'''
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-06 22:28:43 +00:00
|
|
|
old_status = self.invoice.status
|
2016-09-15 23:35:12 +00:00
|
|
|
total_paid = self.invoice.total_payments()
|
2016-04-22 05:06:24 +00:00
|
|
|
num_payments = commerce.PaymentBase.objects.filter(
|
2016-04-06 22:28:43 +00:00
|
|
|
invoice=self.invoice,
|
|
|
|
).count()
|
|
|
|
remainder = self.invoice.value - total_paid
|
|
|
|
|
2016-04-22 05:06:24 +00:00
|
|
|
if old_status == commerce.Invoice.STATUS_UNPAID:
|
2016-04-06 22:28:43 +00:00
|
|
|
# Invoice had an amount owing
|
|
|
|
if remainder <= 0:
|
|
|
|
# Invoice no longer has amount owing
|
|
|
|
self._mark_paid()
|
2016-04-10 04:41:43 +00:00
|
|
|
|
2016-04-06 22:28:43 +00:00
|
|
|
elif total_paid == 0 and num_payments > 0:
|
|
|
|
# Invoice has multiple payments totalling zero
|
|
|
|
self._mark_void()
|
2016-04-22 05:06:24 +00:00
|
|
|
elif old_status == commerce.Invoice.STATUS_PAID:
|
2016-04-06 22:28:43 +00:00
|
|
|
if remainder > 0:
|
|
|
|
# Invoice went from having a remainder of zero or less
|
|
|
|
# to having a positive remainder -- must be a refund
|
|
|
|
self._mark_refunded()
|
2016-04-22 05:06:24 +00:00
|
|
|
elif old_status == commerce.Invoice.STATUS_REFUNDED:
|
2016-04-06 22:28:43 +00:00
|
|
|
# Should not ever change from here
|
|
|
|
pass
|
2016-04-22 05:06:24 +00:00
|
|
|
elif old_status == commerce.Invoice.STATUS_VOID:
|
2016-04-06 22:28:43 +00:00
|
|
|
# Should not ever change from here
|
|
|
|
pass
|
|
|
|
|
2016-04-24 22:13:44 +00:00
|
|
|
# Generate credit notes from residual payments
|
|
|
|
residual = 0
|
|
|
|
if self.invoice.is_paid:
|
|
|
|
if remainder < 0:
|
|
|
|
residual = 0 - remainder
|
|
|
|
elif self.invoice.is_void or self.invoice.is_refunded:
|
|
|
|
residual = total_paid
|
|
|
|
|
|
|
|
if residual != 0:
|
|
|
|
CreditNoteController.generate_from_invoice(self.invoice, residual)
|
|
|
|
|
2016-08-21 07:14:19 +00:00
|
|
|
self.email_on_invoice_change(
|
|
|
|
self.invoice,
|
|
|
|
old_status,
|
|
|
|
self.invoice.status,
|
|
|
|
)
|
|
|
|
|
2016-04-06 22:28:43 +00:00
|
|
|
def _mark_paid(self):
|
|
|
|
''' Marks the invoice as paid, and updates the attached cart if
|
|
|
|
necessary. '''
|
|
|
|
cart = self.invoice.cart
|
|
|
|
if cart:
|
2016-04-25 04:31:25 +00:00
|
|
|
cart.status = commerce.Cart.STATUS_PAID
|
2016-04-06 22:28:43 +00:00
|
|
|
cart.save()
|
2016-04-22 05:06:24 +00:00
|
|
|
self.invoice.status = commerce.Invoice.STATUS_PAID
|
2016-04-06 22:28:43 +00:00
|
|
|
self.invoice.save()
|
2016-03-27 03:41:43 +00:00
|
|
|
|
2016-04-06 22:28:43 +00:00
|
|
|
def _mark_refunded(self):
|
|
|
|
''' Marks the invoice as refunded, and updates the attached cart if
|
|
|
|
necessary. '''
|
2016-10-13 18:23:41 +00:00
|
|
|
self._release_cart()
|
2016-04-22 05:06:24 +00:00
|
|
|
self.invoice.status = commerce.Invoice.STATUS_REFUNDED
|
2016-04-06 22:28:43 +00:00
|
|
|
self.invoice.save()
|
2016-03-27 03:41:43 +00:00
|
|
|
|
2016-04-06 22:28:43 +00:00
|
|
|
def _mark_void(self):
|
|
|
|
''' Marks the invoice as refunded, and updates the attached cart if
|
|
|
|
necessary. '''
|
2016-04-22 05:06:24 +00:00
|
|
|
self.invoice.status = commerce.Invoice.STATUS_VOID
|
2016-04-06 22:28:43 +00:00
|
|
|
self.invoice.save()
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-06 22:28:43 +00:00
|
|
|
def _invoice_matches_cart(self):
|
|
|
|
''' Returns true if there is no cart, or if the revision of this
|
|
|
|
invoice matches the current revision of the cart. '''
|
2016-04-28 04:01:36 +00:00
|
|
|
|
|
|
|
self._refresh()
|
|
|
|
|
2016-04-06 22:28:43 +00:00
|
|
|
cart = self.invoice.cart
|
|
|
|
if not cart:
|
|
|
|
return True
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-06 22:28:43 +00:00
|
|
|
return cart.revision == self.invoice.cart_revision
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-10-13 18:23:41 +00:00
|
|
|
def _release_cart(self):
|
|
|
|
cart = self.invoice.cart
|
|
|
|
if cart:
|
|
|
|
cart.status = commerce.Cart.STATUS_RELEASED
|
|
|
|
cart.save()
|
|
|
|
|
2016-04-06 22:28:43 +00:00
|
|
|
def update_validity(self):
|
2016-10-13 16:19:18 +00:00
|
|
|
''' Voids this invoice if the attached cart is no longer valid because
|
|
|
|
the cart revision has changed, or the reservations have expired. '''
|
|
|
|
|
|
|
|
is_valid = self._invoice_matches_cart()
|
|
|
|
cart = self.invoice.cart
|
|
|
|
if self.invoice.is_unpaid and is_valid and cart:
|
|
|
|
try:
|
|
|
|
CartController(cart).validate_cart()
|
|
|
|
except ValidationError:
|
|
|
|
is_valid = False
|
|
|
|
|
|
|
|
if not is_valid:
|
2016-09-15 23:35:12 +00:00
|
|
|
if self.invoice.total_payments() > 0:
|
2016-09-03 01:46:24 +00:00
|
|
|
# Free up the payments made to this invoice
|
|
|
|
self.refund()
|
|
|
|
else:
|
|
|
|
self.void()
|
2016-03-27 08:25:24 +00:00
|
|
|
|
2016-04-06 22:28:43 +00:00
|
|
|
def void(self):
|
|
|
|
''' Voids the invoice if it is valid to do so. '''
|
2016-09-15 23:35:12 +00:00
|
|
|
if self.invoice.total_payments() > 0:
|
2016-04-10 04:41:43 +00:00
|
|
|
raise ValidationError("Invoices with payments must be refunded.")
|
|
|
|
elif self.invoice.is_refunded:
|
|
|
|
raise ValidationError("Refunded invoices may not be voided.")
|
2016-10-13 18:23:41 +00:00
|
|
|
if self.invoice.is_paid:
|
|
|
|
self._release_cart()
|
|
|
|
|
2016-04-06 22:28:43 +00:00
|
|
|
self._mark_void()
|
2016-03-27 08:25:24 +00:00
|
|
|
|
|
|
|
@transaction.atomic
|
2016-04-10 04:41:43 +00:00
|
|
|
def refund(self):
|
|
|
|
''' Refunds the invoice by generating a CreditNote for the value of
|
|
|
|
all of the payments against the cart.
|
2016-04-06 22:28:43 +00:00
|
|
|
|
|
|
|
The invoice is marked as refunded, and the underlying cart is marked
|
|
|
|
as released.
|
|
|
|
|
2016-03-27 08:25:24 +00:00
|
|
|
'''
|
|
|
|
|
2016-04-06 22:28:43 +00:00
|
|
|
if self.invoice.is_void:
|
2016-03-27 08:25:24 +00:00
|
|
|
raise ValidationError("Void invoices cannot be refunded")
|
|
|
|
|
2016-04-10 04:41:43 +00:00
|
|
|
# Raises a credit note fot the value of the invoice.
|
2016-09-15 23:35:12 +00:00
|
|
|
amount = self.invoice.total_payments()
|
2016-04-10 04:41:43 +00:00
|
|
|
|
|
|
|
if amount == 0:
|
|
|
|
self.void()
|
|
|
|
return
|
2016-03-27 08:25:24 +00:00
|
|
|
|
2016-04-10 04:41:43 +00:00
|
|
|
CreditNoteController.generate_from_invoice(self.invoice, amount)
|
2016-04-06 22:28:43 +00:00
|
|
|
self.update_status()
|
2016-08-21 06:56:05 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def email(cls, invoice, kind):
|
|
|
|
''' Sends out an e-mail notifying the user about something to do
|
|
|
|
with that invoice. '''
|
|
|
|
|
|
|
|
context = {
|
|
|
|
"invoice": invoice,
|
|
|
|
}
|
|
|
|
|
2016-08-21 08:28:16 +00:00
|
|
|
send_email([invoice.user.email], kind, context=context)
|
2016-08-21 06:56:05 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
2016-08-21 07:14:19 +00:00
|
|
|
# The statuses that we don't care about.
|
|
|
|
silent_status = [
|
|
|
|
commerce.Invoice.STATUS_VOID,
|
|
|
|
commerce.Invoice.STATUS_UNPAID,
|
|
|
|
]
|
|
|
|
|
|
|
|
if old_status == new_status:
|
|
|
|
return
|
|
|
|
if False and new_status in silent_status:
|
|
|
|
pass
|
|
|
|
|
2016-08-21 06:56:05 +00:00
|
|
|
cls.email(invoice, "invoice_updated")
|