2024-07-15 00:19:37 +02:00
|
|
|
"""Models for the accounting app."""
|
|
|
|
|
2018-06-23 21:08:56 +02:00
|
|
|
from hashlib import md5
|
2024-07-15 00:19:37 +02:00
|
|
|
from typing import Self
|
2018-06-23 21:08:56 +02:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.db import models
|
|
|
|
from django.db.models.aggregates import Sum
|
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
from django.utils.translation import pgettext_lazy
|
|
|
|
from djmoney.models.fields import MoneyField
|
2024-07-15 00:19:37 +02:00
|
|
|
from djmoney.money import Money
|
2018-06-23 21:08:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CreatedModifiedAbstract(models.Model):
|
2024-07-15 00:19:37 +02:00
|
|
|
"""Abstract model to track creation and modification of objects."""
|
|
|
|
|
2019-09-01 00:27:36 +02:00
|
|
|
modified = models.DateTimeField(auto_now=True, verbose_name=_("modified"))
|
|
|
|
created = models.DateTimeField(auto_now_add=True, verbose_name=_("created"))
|
2018-06-23 21:08:56 +02:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
|
|
|
|
class Account(CreatedModifiedAbstract):
|
2024-07-15 00:19:37 +02:00
|
|
|
"""An account for a user.
|
|
|
|
|
|
|
|
This is the model where we can give access to several users, such that they
|
2018-06-23 21:08:56 +02:00
|
|
|
can decide which account to use to pay for something.
|
|
|
|
"""
|
|
|
|
|
2021-02-27 21:07:48 +01:00
|
|
|
owner = models.ForeignKey("auth.User", on_delete=models.PROTECT)
|
2018-06-23 21:08:56 +02:00
|
|
|
|
2024-07-15 00:19:37 +02:00
|
|
|
def __str__(self) -> str:
|
|
|
|
return f"Account of {self.owner.get_full_name()}"
|
|
|
|
|
2018-06-24 01:05:02 +02:00
|
|
|
@property
|
2024-07-15 00:19:37 +02:00
|
|
|
def balance(self) -> Money:
|
|
|
|
"""Return the balance of the account."""
|
2019-09-01 00:27:36 +02:00
|
|
|
return self.transactions.all().aggregate(Sum("amount")).get("amount", 0)
|
2018-06-23 21:08:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Transaction(CreatedModifiedAbstract):
|
2024-07-15 00:19:37 +02:00
|
|
|
"""A transaction.
|
|
|
|
|
|
|
|
Tracks in and outgoing events of an account. When an order is received, an
|
2018-06-23 21:08:56 +02:00
|
|
|
amount is subtracted, when a payment is received, an amount is added.
|
|
|
|
"""
|
|
|
|
|
|
|
|
account = models.ForeignKey(
|
2023-01-11 21:55:58 +01:00
|
|
|
Account,
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name="transactions",
|
2018-06-23 21:08:56 +02:00
|
|
|
)
|
|
|
|
amount = MoneyField(
|
|
|
|
verbose_name=_("amount"),
|
|
|
|
max_digits=16,
|
|
|
|
decimal_places=2,
|
2019-09-01 00:27:36 +02:00
|
|
|
help_text=_("This will include VAT"),
|
2018-06-23 21:08:56 +02:00
|
|
|
)
|
2019-09-01 00:27:36 +02:00
|
|
|
description = models.CharField(max_length=1024, verbose_name=_("description"))
|
2018-06-23 21:08:56 +02:00
|
|
|
|
2024-07-15 00:19:37 +02:00
|
|
|
def __str__(self) -> str:
|
|
|
|
return f"Transaction of {self.amount} for {self.account}"
|
|
|
|
|
2018-06-23 21:08:56 +02:00
|
|
|
|
|
|
|
class Order(CreatedModifiedAbstract):
|
2024-07-15 00:19:37 +02:00
|
|
|
"""An order.
|
|
|
|
|
|
|
|
Scoped out: Contents of invoices will have to be tracked either here or in
|
2018-06-23 21:08:56 +02:00
|
|
|
a separate Invoice model. This is undecided because we are not generating
|
|
|
|
invoices at the moment.
|
|
|
|
"""
|
|
|
|
|
2021-02-27 21:07:48 +01:00
|
|
|
user = models.ForeignKey("auth.User", on_delete=models.PROTECT)
|
2018-06-23 21:08:56 +02:00
|
|
|
account = models.ForeignKey(Account, on_delete=models.PROTECT)
|
|
|
|
|
2019-09-01 00:27:36 +02:00
|
|
|
description = models.CharField(max_length=1024, verbose_name=_("description"))
|
2018-06-23 21:08:56 +02:00
|
|
|
|
|
|
|
price = MoneyField(
|
2023-01-11 21:55:58 +01:00
|
|
|
verbose_name=_("price (excl. VAT)"),
|
|
|
|
max_digits=16,
|
|
|
|
decimal_places=2,
|
2018-06-23 21:08:56 +02:00
|
|
|
)
|
2019-09-01 00:27:36 +02:00
|
|
|
vat = MoneyField(verbose_name=_("VAT"), max_digits=16, decimal_places=2)
|
2018-06-23 21:08:56 +02:00
|
|
|
|
2019-09-01 00:27:36 +02:00
|
|
|
is_paid = models.BooleanField(default=False, verbose_name=_("is paid"))
|
2018-06-23 21:08:56 +02:00
|
|
|
|
2024-07-15 00:19:37 +02:00
|
|
|
class Meta:
|
|
|
|
verbose_name = pgettext_lazy("accounting term", "Order")
|
|
|
|
verbose_name_plural = pgettext_lazy("accounting term", "Orders")
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return f"Order ID {self.display_id}"
|
|
|
|
|
2018-06-23 21:08:56 +02:00
|
|
|
@property
|
2024-07-15 00:19:37 +02:00
|
|
|
def total(self) -> Money:
|
|
|
|
"""Return the total price of the order."""
|
2018-06-23 21:08:56 +02:00
|
|
|
return self.price + self.vat
|
|
|
|
|
|
|
|
@property
|
2024-07-15 00:19:37 +02:00
|
|
|
def display_id(self) -> str:
|
|
|
|
"""Return an id for the order."""
|
2018-06-23 21:08:56 +02:00
|
|
|
return str(self.id).zfill(6)
|
|
|
|
|
|
|
|
@property
|
2024-07-15 00:19:37 +02:00
|
|
|
def payment_token(self) -> str:
|
|
|
|
"""Return a token for the payment."""
|
2018-06-23 21:08:56 +02:00
|
|
|
pk = str(self.pk).encode("utf-8")
|
2024-07-15 00:19:37 +02:00
|
|
|
x = md5() # noqa: S324
|
2018-06-23 21:08:56 +02:00
|
|
|
x.update(pk)
|
2019-09-01 00:27:36 +02:00
|
|
|
extra_hash = (settings.SECRET_KEY + "blah").encode("utf-8")
|
2018-06-23 21:08:56 +02:00
|
|
|
x.update(extra_hash)
|
|
|
|
return x.hexdigest()
|
|
|
|
|
|
|
|
|
|
|
|
class Payment(CreatedModifiedAbstract):
|
2024-07-15 00:19:37 +02:00
|
|
|
"""A payment is a transaction that is made to pay for an order."""
|
|
|
|
|
2019-09-01 00:27:36 +02:00
|
|
|
amount = MoneyField(max_digits=16, decimal_places=2)
|
2018-06-23 21:08:56 +02:00
|
|
|
order = models.ForeignKey(Order, on_delete=models.PROTECT)
|
|
|
|
|
2019-09-01 00:27:36 +02:00
|
|
|
description = models.CharField(max_length=1024, verbose_name=_("description"))
|
2018-06-23 21:08:56 +02:00
|
|
|
|
2024-07-15 00:19:37 +02:00
|
|
|
stripe_charge_id = models.CharField(max_length=255, blank=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("payment")
|
|
|
|
verbose_name_plural = _("payments")
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return f"Payment ID {self.display_id}"
|
2018-06-23 21:08:56 +02:00
|
|
|
|
|
|
|
@property
|
2024-07-15 00:19:37 +02:00
|
|
|
def display_id(self) -> str:
|
|
|
|
"""Return an id for the payment."""
|
2018-06-23 21:08:56 +02:00
|
|
|
return str(self.id).zfill(6)
|
|
|
|
|
|
|
|
@classmethod
|
2024-07-15 00:19:37 +02:00
|
|
|
def from_order(cls, order: Order) -> Self:
|
|
|
|
"""Create a payment from an order."""
|
2018-06-23 21:08:56 +02:00
|
|
|
return cls.objects.create(
|
|
|
|
order=order,
|
|
|
|
user=order.user,
|
|
|
|
amount=order.total,
|
|
|
|
description=order.description,
|
|
|
|
)
|