Compare commits

...

2 commits

7 changed files with 288 additions and 156 deletions

View file

@ -0,0 +1,71 @@
# Generated by Django 4.1.4 on 2022-12-29 20:26
import datetime
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("main", "0014_alter_account_icon"),
]
operations = [
migrations.AlterModelOptions(
name="snapshot",
options={
"ordering": ["-date"],
"verbose_name": "Statement",
"verbose_name_plural": "Statements",
},
),
migrations.RemoveField(
model_name="snapshot",
name="diff",
),
migrations.RemoveField(
model_name="snapshot",
name="previous",
),
migrations.AddField(
model_name="snapshot",
name="start_date",
field=models.DateField(
default=datetime.date.today, verbose_name="Start date"
),
),
migrations.AddField(
model_name="snapshot",
name="start_value",
field=models.DecimalField(
decimal_places=2, default=0, max_digits=12, verbose_name="Start value"
),
),
migrations.AddField(
model_name="transaction",
name="snapshot",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="main.snapshot",
verbose_name="Statement",
),
),
migrations.AlterField(
model_name="snapshot",
name="date",
field=models.DateField(
default=datetime.date.today, verbose_name="End date"
),
),
migrations.AlterField(
model_name="snapshot",
name="value",
field=models.DecimalField(
decimal_places=2, default=0, max_digits=12, verbose_name="End value"
),
),
]

View file

@ -0,0 +1,35 @@
# Generated by Django 4.1.4 on 2022-12-29 20:32
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("main", "0015_alter_snapshot_options_remove_snapshot_diff_and_more"),
]
operations = [
migrations.AlterField(
model_name="transaction",
name="account",
field=models.ForeignKey(
blank=True,
editable=False,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="main.account",
verbose_name="Account",
),
),
migrations.AlterField(
model_name="transaction",
name="snapshot",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="main.snapshot",
verbose_name="Statement",
),
),
]

View file

@ -0,0 +1,24 @@
# Generated by Django 4.1.4 on 2022-12-29 20:34
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("main", "0016_alter_transaction_account_alter_transaction_snapshot"),
]
operations = [
migrations.AlterField(
model_name="transaction",
name="account",
field=models.ForeignKey(
editable=False,
on_delete=django.db.models.deletion.CASCADE,
to="main.account",
verbose_name="Account",
),
),
]

View file

@ -0,0 +1,34 @@
# Generated by Django 4.1.4 on 2022-12-29 20:56
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("main", "0017_alter_transaction_account"),
]
operations = [
migrations.AddField(
model_name="snapshot",
name="diff",
field=models.DecimalField(
decimal_places=2,
default=0,
editable=False,
max_digits=12,
verbose_name="Différence",
),
),
migrations.AlterField(
model_name="snapshot",
name="account",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="main.account",
verbose_name="Account",
),
),
]

View file

@ -0,0 +1,24 @@
# Generated by Django 4.1.4 on 2022-12-29 20:57
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("main", "0018_snapshot_diff_alter_snapshot_account"),
]
operations = [
migrations.AddField(
model_name="snapshot",
name="sum",
field=models.DecimalField(
decimal_places=2,
default=0,
editable=False,
max_digits=12,
verbose_name="Transaction difference",
),
),
]

View file

@ -1,10 +1,10 @@
import datetime
import pathlib
import uuid
from datetime import date
from django.conf import settings
from django.core.validators import FileExtensionValidator
from django.db import models
from django.db import models, transaction
from django.forms import ModelForm
from django.urls import reverse
from django.utils.translation import gettext as _
@ -64,9 +64,7 @@ class Account(CustomModel):
class AccountModel(CustomModel):
account = models.ForeignKey(
Account,
on_delete=models.SET_NULL,
blank=True,
null=True,
on_delete=models.CASCADE,
verbose_name=_("Account"),
)
@ -101,7 +99,83 @@ class Category(CustomModel):
verbose_name_plural = _("Categories")
class Transaction(AccountModel):
def snapshot_path(instance, filename):
return pathlib.Path("snapshots", str(instance.id)).with_suffix(".pdf")
class Snapshot(AccountModel):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
date = models.DateField(default=datetime.date.today, verbose_name=_("End date"))
start_date = models.DateField(
default=datetime.date.today, verbose_name=_("Start date")
)
value = models.DecimalField(
max_digits=12, decimal_places=2, default=0, verbose_name=_("End value")
)
start_value = models.DecimalField(
max_digits=12, decimal_places=2, default=0, verbose_name=_("Start value")
)
diff = models.DecimalField(
max_digits=12,
decimal_places=2,
default=0,
verbose_name=_("Difference"),
editable=False,
)
sum = models.DecimalField(
max_digits=12,
decimal_places=2,
default=0,
verbose_name=_("Transaction difference"),
editable=False,
)
file = models.FileField(
upload_to=snapshot_path,
validators=[FileExtensionValidator(["pdf"])],
verbose_name=_("File"),
max_length=256,
blank=True,
default="",
)
def __str__(self):
return _("%(date)s statement") % {"date": self.date}
def save(self, *args, **kwargs):
if Snapshot.objects.filter(id=self.id).exists():
_prever = Snapshot.objects.get(id=self.id)
if _prever.file and _prever.file != self.file:
pathlib.Path(_prever.file.path).unlink(missing_ok=True)
with transaction.atomic():
for trans in self.transaction_set.all():
trans.save()
self.diff = self.value - self.start_value
self.sum = self.transaction_set.aggregate(sum=models.Sum("value")).get("sum", 0)
super().save(*args, **kwargs)
def update_sum(self):
self.sum = self.transaction_set.aggregate(sum=models.Sum("value")).get("sum", 0)
super().save()
def delete(self, *args, only_super=False, **kwargs):
if self.file:
self.file.delete()
def get_absolute_url(self):
return reverse("snapshot", kwargs={"pk": self.pk})
def get_delete_url(self):
return reverse("del_snapshot", kwargs={"pk": self.pk})
class Meta:
ordering = ["-date"]
verbose_name = _("Statement")
verbose_name_plural = _("Statements")
class Transaction(CustomModel):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(
max_length=256, default=_("Transaction"), verbose_name=_("Name")
@ -110,7 +184,7 @@ class Transaction(AccountModel):
value = models.DecimalField(
max_digits=12, decimal_places=2, default=0, verbose_name=_("Value")
)
date = models.DateField(default=date.today, verbose_name=_("Date"))
date = models.DateField(default=datetime.date.today, verbose_name=_("Date"))
real_date = models.DateField(blank=True, null=True, verbose_name=_("Real date"))
trader = models.CharField(
max_length=128, blank=True, null=True, verbose_name=_("Trader")
@ -125,6 +199,22 @@ class Transaction(AccountModel):
null=True,
verbose_name=_("Category"),
)
snapshot = models.ForeignKey(
Snapshot,
on_delete=models.CASCADE,
verbose_name=_("Statement"),
)
account = models.ForeignKey(
Account,
on_delete=models.CASCADE,
verbose_name=_("Account"),
editable=False,
)
def save(self, *args, **kwargs):
self.account = self.snapshot.account
self.snapshot.update_sum()
super().save(*args, **kwargs)
def __str__(self):
return f"{self.date} {self.name}"
@ -192,146 +282,6 @@ class Invoice(CustomModel):
verbose_name_plural = _("Invoices")
def snapshot_path(instance, filename):
return pathlib.Path("snapshots", str(instance.id)).with_suffix(".pdf")
class Snapshot(AccountModel):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
date = models.DateField(default=date.today, verbose_name=_("Date"))
value = models.DecimalField(
max_digits=12, decimal_places=2, default=0, verbose_name=_("Value")
)
previous = models.OneToOneField(
"self", on_delete=models.SET_NULL, blank=True, null=True, editable=False
)
diff = models.DecimalField(
max_digits=12, decimal_places=2, editable=False, blank=True, null=True
)
file = models.FileField(
upload_to=snapshot_path,
validators=[FileExtensionValidator(["pdf"])],
verbose_name=_("File"),
max_length=256,
blank=True,
default="",
)
def __str__(self):
return _("%(date)s snapshot") % {"date": self.date}
def save(self, *args, only_super=False, **kwargs):
if not only_super:
if Snapshot.objects.filter(id=self.id).exists():
_prever = Snapshot.objects.get(id=self.id)
if _prever.file and _prever.file != self.file:
pathlib.Path(_prever.file.path).unlink(missing_ok=True)
_prev = (
self.__class__.objects.order_by("-date")
.exclude(id=self.id)
.filter(date__lt=self.date)
.first()
)
try:
_next = self.__class__.objects.exclude(id=self.id).get(previous=_prev)
except self.__class__.DoesNotExist:
pass
else:
try:
_prevnext = self.__class__.objects.exclude(id=self.id).get(
previous=self
)
except self.__class__.DoesNotExist:
pass
else:
_prevnext.previous = (
self.__class__.objects.order_by("-date")
.exclude(id=self.id)
.filter(date__lt=_prevnext.date)
.first()
)
_prevnext.save(only_super=True)
_next.previous = None
super().save(*args, **kwargs)
_next.previous = self
_next.save(only_super=True)
self.previous = _prev
if self.previous is None:
self.diff = None
else:
self.diff = self.value - self.previous.value
super().save(*args, **kwargs)
try:
_next = self.__class__.objects.get(previous=self)
except self.__class__.DoesNotExist:
pass
else:
_next.save(only_super=True)
def delete(self, *args, only_super=False, **kwargs):
self.file.delete()
if not only_super:
try:
_next = self.__class__.objects.get(previous=self)
except self.__class__.DoesNotExist:
super().delete(*args, **kwargs)
else:
_next.previous = self.previous
super().delete(*args, **kwargs)
_next.save(only_super=True)
else:
super().delete(*args, **kwargs)
def get_absolute_url(self):
return reverse("snapshot", kwargs={"pk": self.pk})
def get_delete_url(self):
return reverse("del_snapshot", kwargs={"pk": self.pk})
@property
def sum(self):
if self.previous is None:
return 0
trans = self.transactions.aggregate(sum=models.Sum("value"))
return trans["sum"] or 0
@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
)
@property
def pos(self):
return (
self.transactions.filter(value__gt=0).aggregate(sum=models.Sum("value"))[
"sum"
]
or 0
)
@property
def neg(self):
return (
self.transactions.filter(value__lt=0).aggregate(sum=models.Sum("value"))[
"sum"
]
or 0
)
class Meta:
ordering = ["-date"]
verbose_name = _("Snapshot")
verbose_name_plural = _("Snapshots")
class NummiForm(ModelForm):
template_name = "main/form/base.html"
@ -361,7 +311,7 @@ class TransactionForm(NummiForm):
_user = kwargs.pop("user")
super().__init__(*args, **kwargs)
self.fields["category"].queryset = Category.objects.filter(user=_user)
self.fields["account"].queryset = Account.objects.filter(user=_user)
self.fields["snapshot"].queryset = Snapshot.objects.filter(user=_user)
class InvoiceForm(NummiForm):

View file

@ -66,25 +66,19 @@
<a href="{% url 'snapshot' snap.id %}">{{ snap.date|date:"Y-m-d" }}</a>
</span>
<span class="account text center">
{% if trans.account %}
<i class="fa fa-{{ trans.account.icon }}"></i>
<a href="{% url 'account' trans.account.id %}">{{ trans.account }}</a>
{% else %}
{% endif %}
<i class="fa fa-{{ snap.account.icon }}"></i>
<a href="{% url 'account' snap.account.id %}">{{ snap.account }}</a>
</span>
<span class="value num right">{{ snap.value|value }}</span>
<span class="diff num right">{{ snap.diff|pmvalue }}</span>
{% with sum=snap.sum %}
<span class="sum num right">{{ sum|pmvalue }}</span>
<span class="valid center">
{% if snap.previous is not None %}
{% if sum == snap.diff %}
<i class="fa fa-check green"></i>
{% else %}
<i class="fa fa-xmark red"></i>
{% endif %}
{% endif %}
</span>
{% endwith %}
</div>