2022-05-20 18:27:55 +02:00
|
|
|
from datetime import date
|
2022-05-19 16:09:28 +02:00
|
|
|
import uuid
|
|
|
|
from django.db import models
|
2022-05-19 20:33:36 +02:00
|
|
|
from django.forms import ModelForm
|
2022-05-22 13:57:53 +02:00
|
|
|
from django.core.validators import FileExtensionValidator
|
2022-05-19 16:09:28 +02:00
|
|
|
|
|
|
|
|
2022-05-20 13:44:46 +02:00
|
|
|
class Category(models.Model):
|
|
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
2022-05-22 13:57:53 +02:00
|
|
|
name = models.CharField(max_length=64, default="New Category")
|
2022-05-20 22:19:13 +02:00
|
|
|
icon = models.CharField(max_length=64, default="folder")
|
2022-05-20 13:44:46 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
2022-05-20 21:33:06 +02:00
|
|
|
return self.name
|
2022-05-20 18:35:14 +02:00
|
|
|
|
2022-05-20 16:20:42 +02:00
|
|
|
class Meta:
|
2022-05-20 21:33:06 +02:00
|
|
|
ordering = ["name"]
|
2022-05-20 13:44:46 +02:00
|
|
|
|
|
|
|
|
2022-05-20 19:17:42 +02:00
|
|
|
class CategoryForm(ModelForm):
|
2022-05-22 18:14:11 +02:00
|
|
|
template_name = "main/form.html"
|
|
|
|
|
2022-05-20 19:17:42 +02:00
|
|
|
class Meta:
|
|
|
|
model = Category
|
2022-05-20 22:19:13 +02:00
|
|
|
fields = ["name", "icon"]
|
2022-05-20 19:17:42 +02:00
|
|
|
|
|
|
|
|
2022-05-19 16:09:28 +02:00
|
|
|
class Transaction(models.Model):
|
|
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
2022-05-20 19:44:32 +02:00
|
|
|
name = models.CharField(max_length=256, default="New Transaction")
|
2022-05-20 18:27:55 +02:00
|
|
|
description = models.TextField(null=True, blank=True)
|
|
|
|
value = models.DecimalField(max_digits=12, decimal_places=2, default=0)
|
|
|
|
date = models.DateField(default=date.today)
|
2022-05-22 13:36:43 +02:00
|
|
|
real_date = models.DateField(blank=True, null=True)
|
2022-05-20 18:27:55 +02:00
|
|
|
trader = models.CharField(max_length=128, blank=True, null=True)
|
2022-05-22 13:52:09 +02:00
|
|
|
payment = models.CharField(max_length=128, blank=True, null=True)
|
2022-05-20 13:44:46 +02:00
|
|
|
category = models.ForeignKey(
|
|
|
|
Category, on_delete=models.SET_NULL, blank=True, null=True
|
|
|
|
)
|
2022-05-19 16:09:28 +02:00
|
|
|
|
2022-05-19 16:14:23 +02:00
|
|
|
def __str__(self):
|
2022-05-20 13:44:46 +02:00
|
|
|
res = f"{self.date} {self.name}: {self.value}€"
|
|
|
|
if self.category:
|
|
|
|
return f"{res} ({self.category})"
|
|
|
|
return res
|
2022-05-19 16:14:23 +02:00
|
|
|
|
2022-05-20 19:17:42 +02:00
|
|
|
class Meta:
|
|
|
|
ordering = ["-date"]
|
|
|
|
|
2022-05-19 16:09:28 +02:00
|
|
|
|
2022-05-19 20:33:36 +02:00
|
|
|
class TransactionForm(ModelForm):
|
2022-05-22 18:14:11 +02:00
|
|
|
template_name = "main/form.html"
|
|
|
|
|
2022-05-19 20:33:36 +02:00
|
|
|
class Meta:
|
|
|
|
model = Transaction
|
2022-05-22 13:52:09 +02:00
|
|
|
fields = [
|
|
|
|
"date",
|
|
|
|
"name",
|
|
|
|
"value",
|
|
|
|
"trader",
|
|
|
|
"category",
|
|
|
|
"real_date",
|
|
|
|
"payment",
|
|
|
|
"description",
|
|
|
|
]
|
2022-05-19 20:33:36 +02:00
|
|
|
|
|
|
|
|
2022-05-19 16:09:28 +02:00
|
|
|
class Invoice(models.Model):
|
|
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
2022-05-20 19:44:32 +02:00
|
|
|
name = models.CharField(max_length=256, default="New Invoice")
|
2022-05-20 16:27:18 +02:00
|
|
|
file = models.FileField(
|
|
|
|
upload_to="invoices/", validators=[FileExtensionValidator(["pdf"])]
|
|
|
|
)
|
2022-05-19 16:09:28 +02:00
|
|
|
transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE)
|
2022-05-19 16:14:23 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"{self.name}: {self.transaction}"
|
2022-05-19 21:21:55 +02:00
|
|
|
|
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
self.file.delete()
|
|
|
|
super().delete(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class InvoiceForm(ModelForm):
|
2022-05-22 18:14:11 +02:00
|
|
|
template_name = "main/form.html"
|
2022-05-23 14:07:12 +02:00
|
|
|
prefix = "invoice"
|
2022-05-22 18:14:11 +02:00
|
|
|
|
2022-05-19 21:21:55 +02:00
|
|
|
class Meta:
|
|
|
|
model = Invoice
|
|
|
|
fields = ["name", "file"]
|
2022-05-21 21:00:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Snapshot(models.Model):
|
|
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
2022-05-22 11:10:52 +02:00
|
|
|
date = models.DateField(unique=True)
|
2022-05-21 21:00:26 +02:00
|
|
|
value = models.DecimalField(max_digits=12, decimal_places=2, default=0)
|
|
|
|
previous = models.OneToOneField(
|
|
|
|
"self", on_delete=models.SET_NULL, blank=True, null=True, editable=False
|
|
|
|
)
|
2022-05-22 09:21:30 +02:00
|
|
|
diff = models.DecimalField(
|
2022-05-22 12:32:26 +02:00
|
|
|
max_digits=12, decimal_places=2, editable=False, blank=True, null=True
|
2022-05-22 09:21:30 +02:00
|
|
|
)
|
2022-05-21 21:00:26 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
if self.previous is None:
|
|
|
|
return f"Snapshot {self.date}: {self.value}€"
|
|
|
|
return f"Snapshot {self.date}: {self.value}€ (Previous: {self.previous.date})"
|
|
|
|
|
|
|
|
def save(self, *args, only_super=False, **kwargs):
|
|
|
|
if not only_super:
|
|
|
|
_prev = (
|
|
|
|
self.__class__.objects.order_by("-date")
|
2022-05-22 10:06:44 +02:00
|
|
|
.exclude(id=self.id)
|
2022-05-21 21:00:26 +02:00
|
|
|
.filter(date__lt=self.date)
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
2022-05-22 09:21:30 +02:00
|
|
|
_next = self.__class__.objects.exclude(id=self.id).get(previous=_prev)
|
2022-05-21 21:00:26 +02:00
|
|
|
except self.__class__.DoesNotExist:
|
|
|
|
pass
|
|
|
|
else:
|
2022-05-22 11:10:52 +02:00
|
|
|
try:
|
2022-05-22 12:32:26 +02:00
|
|
|
_prevnext = self.__class__.objects.exclude(id=self.id).get(
|
|
|
|
previous=self
|
|
|
|
)
|
2022-05-22 11:10:52 +02:00
|
|
|
except self.__class__.DoesNotExist:
|
|
|
|
pass
|
|
|
|
else:
|
2022-05-22 12:32:26 +02:00
|
|
|
_prevnext.previous = (
|
|
|
|
self.__class__.objects.order_by("-date")
|
|
|
|
.exclude(id=self.id)
|
|
|
|
.filter(date__lt=_prevnext.date)
|
|
|
|
.first()
|
|
|
|
)
|
2022-05-22 11:10:52 +02:00
|
|
|
_prevnext.save(only_super=True)
|
2022-05-30 14:48:28 +02:00
|
|
|
_next.previous = None
|
|
|
|
super().save(*args, **kwargs)
|
2022-05-21 21:00:26 +02:00
|
|
|
_next.previous = self
|
|
|
|
_next.save(only_super=True)
|
|
|
|
|
|
|
|
self.previous = _prev
|
2022-05-22 09:21:30 +02:00
|
|
|
|
|
|
|
if self.previous is None:
|
2022-05-22 12:32:26 +02:00
|
|
|
self.diff = None
|
2022-05-22 09:21:30 +02:00
|
|
|
else:
|
|
|
|
self.diff = self.value - self.previous.value
|
2022-05-21 21:00:26 +02:00
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2022-05-22 11:14:05 +02:00
|
|
|
try:
|
|
|
|
_next = self.__class__.objects.get(previous=self)
|
|
|
|
except self.__class__.DoesNotExist:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
_next.save(only_super=True)
|
|
|
|
|
2022-05-21 21:00:26 +02:00
|
|
|
def delete(self, *args, only_super=False, **kwargs):
|
|
|
|
if not only_super:
|
|
|
|
try:
|
|
|
|
_next = self.__class__.objects.get(previous=self)
|
|
|
|
except self.__class__.DoesNotExist:
|
2022-05-22 10:39:29 +02:00
|
|
|
super().delete(*args, **kwargs)
|
2022-05-21 21:00:26 +02:00
|
|
|
else:
|
|
|
|
_next.previous = self.previous
|
|
|
|
super().delete(*args, **kwargs)
|
|
|
|
_next.save(only_super=True)
|
|
|
|
else:
|
|
|
|
super().delete(*args, **kwargs)
|
|
|
|
|
2022-05-22 09:21:30 +02:00
|
|
|
@property
|
|
|
|
def sum(self):
|
|
|
|
if self.previous is None:
|
|
|
|
return None
|
2022-05-22 14:41:15 +02:00
|
|
|
trans = self.transactions.aggregate(sum=models.Sum("value"))
|
2022-05-22 09:21:30 +02:00
|
|
|
return trans["sum"] or 0
|
|
|
|
|
2022-05-22 14:41:15 +02:00
|
|
|
@property
|
|
|
|
def transactions(self):
|
|
|
|
if self.previous is None:
|
|
|
|
return Transaction.objects.none()
|
|
|
|
return Transaction.objects.filter(
|
|
|
|
date__lte=self.date, date__gt=self.previous.date
|
|
|
|
)
|
|
|
|
|
2022-05-21 21:00:26 +02:00
|
|
|
class Meta:
|
|
|
|
ordering = ["-date"]
|
2022-05-22 10:06:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SnapshotForm(ModelForm):
|
2022-05-22 18:14:11 +02:00
|
|
|
template_name = "main/form.html"
|
|
|
|
|
2022-05-22 10:06:44 +02:00
|
|
|
class Meta:
|
|
|
|
model = Snapshot
|
|
|
|
fields = ["date", "value"]
|