diff --git a/nummi/main/migrations/0018_snapshot_diff_alter_snapshot_account.py b/nummi/main/migrations/0018_snapshot_diff_alter_snapshot_account.py
new file mode 100644
index 0000000..2f40d1e
--- /dev/null
+++ b/nummi/main/migrations/0018_snapshot_diff_alter_snapshot_account.py
@@ -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",
+ ),
+ ),
+ ]
diff --git a/nummi/main/migrations/0019_snapshot_sum.py b/nummi/main/migrations/0019_snapshot_sum.py
new file mode 100644
index 0000000..6a6cf87
--- /dev/null
+++ b/nummi/main/migrations/0019_snapshot_sum.py
@@ -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",
+ ),
+ ),
+ ]
diff --git a/nummi/main/models.py b/nummi/main/models.py
index 3694261..82d972b 100644
--- a/nummi/main/models.py
+++ b/nummi/main/models.py
@@ -4,7 +4,7 @@ import uuid
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"),
)
@@ -117,6 +115,20 @@ class Snapshot(AccountModel):
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"])],
@@ -135,8 +147,18 @@ class Snapshot(AccountModel):
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()
@@ -147,24 +169,6 @@ class Snapshot(AccountModel):
def get_delete_url(self):
return reverse("del_snapshot", kwargs={"pk": self.pk})
- @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 = _("Statement")
@@ -209,6 +213,7 @@ class Transaction(CustomModel):
def save(self, *args, **kwargs):
self.account = self.snapshot.account
+ self.snapshot.update_sum()
super().save(*args, **kwargs)
def __str__(self):
diff --git a/nummi/main/templates/main/index.html b/nummi/main/templates/main/index.html
index c4e5c84..499a59f 100644
--- a/nummi/main/templates/main/index.html
+++ b/nummi/main/templates/main/index.html
@@ -66,25 +66,19 @@
{{ snap.date|date:"Y-m-d" }}
- {% if trans.account %}
-
- {{ trans.account }}
- {% else %}
- –
- {% endif %}
+
+ {{ snap.account }}
{{ snap.value|value }}
{{ snap.diff|pmvalue }}
{% with sum=snap.sum %}
{{ sum|pmvalue }}
- {% if snap.previous is not None %}
{% if sum == snap.diff %}
{% else %}
{% endif %}
- {% endif %}
{% endwith %}