Update snapshots with sum db columns
This commit is contained in:
parent
02eb781492
commit
c083fff07e
4 changed files with 87 additions and 30 deletions
|
@ -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",
|
||||
),
|
||||
),
|
||||
]
|
24
nummi/main/migrations/0019_snapshot_sum.py
Normal file
24
nummi/main/migrations/0019_snapshot_sum.py
Normal 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",
|
||||
),
|
||||
),
|
||||
]
|
|
@ -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):
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Reference in a new issue