Update snapshots with sum db columns

This commit is contained in:
Edgar P. Burkhart 2022-12-29 21:58:26 +01:00
parent 02eb781492
commit c083fff07e
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
4 changed files with 87 additions and 30 deletions

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

@ -4,7 +4,7 @@ import uuid
from django.conf import settings from django.conf import settings
from django.core.validators import FileExtensionValidator from django.core.validators import FileExtensionValidator
from django.db import models from django.db import models, transaction
from django.forms import ModelForm from django.forms import ModelForm
from django.urls import reverse from django.urls import reverse
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
@ -64,9 +64,7 @@ class Account(CustomModel):
class AccountModel(CustomModel): class AccountModel(CustomModel):
account = models.ForeignKey( account = models.ForeignKey(
Account, Account,
on_delete=models.SET_NULL, on_delete=models.CASCADE,
blank=True,
null=True,
verbose_name=_("Account"), verbose_name=_("Account"),
) )
@ -117,6 +115,20 @@ class Snapshot(AccountModel):
start_value = models.DecimalField( start_value = models.DecimalField(
max_digits=12, decimal_places=2, default=0, verbose_name=_("Start value") 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( file = models.FileField(
upload_to=snapshot_path, upload_to=snapshot_path,
validators=[FileExtensionValidator(["pdf"])], validators=[FileExtensionValidator(["pdf"])],
@ -135,8 +147,18 @@ class Snapshot(AccountModel):
if _prever.file and _prever.file != self.file: if _prever.file and _prever.file != self.file:
pathlib.Path(_prever.file.path).unlink(missing_ok=True) 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) 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): def delete(self, *args, only_super=False, **kwargs):
if self.file: if self.file:
self.file.delete() self.file.delete()
@ -147,24 +169,6 @@ class Snapshot(AccountModel):
def get_delete_url(self): def get_delete_url(self):
return reverse("del_snapshot", kwargs={"pk": self.pk}) 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: class Meta:
ordering = ["-date"] ordering = ["-date"]
verbose_name = _("Statement") verbose_name = _("Statement")
@ -209,6 +213,7 @@ class Transaction(CustomModel):
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
self.account = self.snapshot.account self.account = self.snapshot.account
self.snapshot.update_sum()
super().save(*args, **kwargs) super().save(*args, **kwargs)
def __str__(self): def __str__(self):

View file

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