Files
@ 4dbe69574c2a
Branch filter:
Location: symposion_app/registrasion/controllers/invoice.py - annotation
4dbe69574c2a
12.0 KiB
text/x-python
Adds report that tracks the free money in the system
Fixes #52
Fixes #52
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | d9e433659d01 d9e433659d01 3e4e52b165b3 cf85af771983 224878a10ce3 38cdb8aa6330 d9e433659d01 e946af0f0487 e946af0f0487 875f736d67c5 875f736d67c5 875f736d67c5 d9e433659d01 d9e433659d01 6b10a0a7e427 ca8f67c2f396 d9e433659d01 12e04c248fb1 ca8f67c2f396 224878a10ce3 ca8f67c2f396 d9e433659d01 d9e433659d01 d9e433659d01 38cdb8aa6330 3e4e52b165b3 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 587e6e20b284 d9e433659d01 875f736d67c5 875f736d67c5 38cdb8aa6330 3e4e52b165b3 3e4e52b165b3 3e4e52b165b3 d9e433659d01 d9e433659d01 224878a10ce3 8080d7851bda 1e6c90163dd0 d9e433659d01 d9e433659d01 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 1e6c90163dd0 875f736d67c5 38cdb8aa6330 1e6c90163dd0 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 875f736d67c5 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 875f736d67c5 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 8ad265a65a08 d9e433659d01 d9e433659d01 38cdb8aa6330 587e6e20b284 587e6e20b284 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 0e80e0336c20 875f736d67c5 0e80e0336c20 0e80e0336c20 0e80e0336c20 875f736d67c5 d9e433659d01 d9e433659d01 d9e433659d01 875f736d67c5 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 0e80e0336c20 d9e433659d01 d9e433659d01 875f736d67c5 4fb569d9353d 4fb569d9353d 4fb569d9353d 4fb569d9353d c9a62db774c5 c9a62db774c5 c9a62db774c5 c9a62db774c5 a4de15830c18 a4de15830c18 a4de15830c18 4fb569d9353d 875f736d67c5 4fb569d9353d 4fb569d9353d 4fb569d9353d 4fb569d9353d 4fb569d9353d 4fb569d9353d 4fb569d9353d 4fb569d9353d 0b7396c40f09 0b7396c40f09 0b7396c40f09 0b7396c40f09 0b7396c40f09 0b7396c40f09 0b7396c40f09 d9e433659d01 d9e433659d01 a4de15830c18 4fb569d9353d d9e433659d01 0b7396c40f09 d9e433659d01 a4de15830c18 38cdb8aa6330 d9e433659d01 4fb569d9353d d9e433659d01 d9e433659d01 4fb569d9353d d9e433659d01 0b7396c40f09 d9e433659d01 d9e433659d01 38cdb8aa6330 d9e433659d01 4fb569d9353d d9e433659d01 d9e433659d01 4fb569d9353d 4fb569d9353d d9e433659d01 8ad265a65a08 d9e433659d01 d9e433659d01 e946af0f0487 e946af0f0487 d9e433659d01 d9e433659d01 ea1d6f52e693 ea1d6f52e693 ea1d6f52e693 ea1d6f52e693 ea1d6f52e693 ea1d6f52e693 ea1d6f52e693 ea1d6f52e693 ea1d6f52e693 ea1d6f52e693 ea1d6f52e693 ea1d6f52e693 ea1d6f52e693 ea1d6f52e693 ea1d6f52e693 ea1d6f52e693 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 38cdb8aa6330 38cdb8aa6330 d9e433659d01 875f736d67c5 38cdb8aa6330 38cdb8aa6330 d9e433659d01 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 d9e433659d01 38cdb8aa6330 38cdb8aa6330 875f736d67c5 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 875f736d67c5 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 6b10a0a7e427 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 875f736d67c5 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 875f736d67c5 38cdb8aa6330 38cdb8aa6330 875f736d67c5 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 7bf372f92a87 7bf372f92a87 7bf372f92a87 7bf372f92a87 7bf372f92a87 7bf372f92a87 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 63dfd353c16e 38cdb8aa6330 875f736d67c5 38cdb8aa6330 3e4e52b165b3 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 63dfd353c16e 38cdb8aa6330 875f736d67c5 38cdb8aa6330 3e4e52b165b3 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 875f736d67c5 38cdb8aa6330 d9e433659d01 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 587e6e20b284 587e6e20b284 587e6e20b284 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 d9e433659d01 38cdb8aa6330 d9e433659d01 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 1e6c90163dd0 1e6c90163dd0 1e6c90163dd0 1e6c90163dd0 1e6c90163dd0 cf85af771983 38cdb8aa6330 38cdb8aa6330 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 38cdb8aa6330 cf85af771983 cf85af771983 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 38cdb8aa6330 cf85af771983 cf85af771983 38cdb8aa6330 cf85af771983 cf85af771983 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 cf85af771983 6b10a0a7e427 38cdb8aa6330 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 4f16e4b9d0e9 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 e946af0f0487 7bf372f92a87 7bf372f92a87 7bf372f92a87 7bf372f92a87 7bf372f92a87 7bf372f92a87 7bf372f92a87 7bf372f92a87 7bf372f92a87 7bf372f92a87 7bf372f92a87 e946af0f0487 | 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
def __init__(self, invoice):
self.invoice = invoice
self.update_status()
self.update_validity() # Make sure this invoice is up-to-date
@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.'''
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.update_old_invoices(cart)
invoice = cls._generate(cart)
return cls(invoice)
@classmethod
def update_old_invoices(cls, cart):
invoices = commerce.Invoice.objects.filter(cart=cart).all()
for invoice in invoices:
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
)
if condition.percentage is not None:
value = item.product.price * (condition.percentage / 100)
else:
value = condition.price
return value
@classmethod
@transaction.atomic
def _generate(cls, cart):
''' Generates an invoice for the given cart. '''
cart.refresh_from_db()
issued = timezone.now()
reservation_limit = cart.reservation_duration + cart.time_last_updated
# Never generate a due time that is before the issue time
due = max(issued, reservation_limit)
# Get the invoice recipient
profile = people.AttendeeProfileBase.objects.get_subclass(
id=cart.user.attendee.attendeeprofilebase.id,
)
recipient = profile.invoice_recipient()
invoice = commerce.Invoice.objects.create(
user=cart.user,
cart=cart,
cart_revision=cart.revision,
status=commerce.Invoice.STATUS_UNPAID,
value=Decimal(),
issue_time=issued,
due_time=due,
recipient=recipient,
)
product_items = commerce.ProductItem.objects.filter(cart=cart)
product_items = product_items.select_related(
"product",
"product__category",
)
if len(product_items) == 0:
raise ValidationError("Your cart is empty.")
product_items = product_items.order_by(
"product__category__order", "product__order"
)
discount_items = commerce.DiscountItem.objects.filter(cart=cart)
discount_items = discount_items.select_related(
"discount",
"product",
"product__category",
)
line_items = []
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))
invoice_value = Decimal()
for item in product_items:
product = item.product
line_item = commerce.LineItem(
invoice=invoice,
description=format_product(product),
quantity=item.quantity,
price=product.price,
product=product,
)
line_items.append(line_item)
invoice_value += line_item.quantity * line_item.price
for item in discount_items:
line_item = commerce.LineItem(
invoice=invoice,
description=format_discount(item.discount, item.product),
quantity=item.quantity,
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
if self.invoice.user.attendee.access_code == access_code:
return True
return False
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):
''' Returns the total amount paid towards this invoice. '''
payments = commerce.PaymentBase.objects.filter(invoice=self.invoice)
total_paid = payments.aggregate(Sum("amount"))["amount__sum"] or 0
return total_paid
def update_status(self):
''' Updates the status of this invoice based upon the total
payments.'''
old_status = self.invoice.status
total_paid = self.total_payments()
num_payments = commerce.PaymentBase.objects.filter(
invoice=self.invoice,
).count()
remainder = self.invoice.value - total_paid
if old_status == commerce.Invoice.STATUS_UNPAID:
# Invoice had an amount owing
if remainder <= 0:
# Invoice no longer has amount owing
self._mark_paid()
elif total_paid == 0 and num_payments > 0:
# Invoice has multiple payments totalling zero
self._mark_void()
elif old_status == commerce.Invoice.STATUS_PAID:
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()
elif old_status == commerce.Invoice.STATUS_REFUNDED:
# Should not ever change from here
pass
elif old_status == commerce.Invoice.STATUS_VOID:
# Should not ever change from here
pass
# 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)
self.email_on_invoice_change(
self.invoice,
old_status,
self.invoice.status,
)
def _mark_paid(self):
''' Marks the invoice as paid, and updates the attached cart if
necessary. '''
cart = self.invoice.cart
if cart:
cart.status = commerce.Cart.STATUS_PAID
cart.save()
self.invoice.status = commerce.Invoice.STATUS_PAID
self.invoice.save()
def _mark_refunded(self):
''' Marks the invoice as refunded, and updates the attached cart if
necessary. '''
cart = self.invoice.cart
if cart:
cart.status = commerce.Cart.STATUS_RELEASED
cart.save()
self.invoice.status = commerce.Invoice.STATUS_REFUNDED
self.invoice.save()
def _mark_void(self):
''' Marks the invoice as refunded, and updates the attached cart if
necessary. '''
self.invoice.status = commerce.Invoice.STATUS_VOID
self.invoice.save()
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. '''
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():
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
all of the payments against the cart.
The invoice is marked as refunded, and the underlying cart is marked
as released.
'''
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
'''
# 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
cls.email(invoice, "invoice_updated")
|