Fix updating of snapshot values

This commit is contained in:
Edgar P. Burkhart 2022-12-30 17:37:11 +01:00
parent 0184cba30d
commit 411d258cd7
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
4 changed files with 22 additions and 23 deletions

View file

@ -71,15 +71,5 @@ class SnapshotForm(NummiForm):
id__in=self.cleaned_data["transactions"]
)
to_update = list(
new_transactions.order_by().only("snapshot").distinct("snapshot")
)
instance.account.transaction_set.add(*new_transactions)
instance.transaction_set.add(*new_transactions)
instance.update_sum()
for upd in to_update:
upd.snapshot.update_sum()
instance.transaction_set.add(*new_transactions, bulk=False)
return instance

View file

@ -159,10 +159,15 @@ class Snapshot(AccountModel):
trans.save()
self.diff = self.value - self.start_value
self.sum = (
self.transaction_set.aggregate(sum=models.Sum("value")).get("sum", 0) or 0
)
super().save(*args, **kwargs)
def update_sum(self):
self.sum = self.transaction_set.aggregate(sum=models.Sum("value")).get("sum", 0)
self.sum = (
self.transaction_set.aggregate(sum=models.Sum("value")).get("sum", 0) or 0
)
super().save()
def delete(self, *args, **kwargs):
@ -219,10 +224,14 @@ class Transaction(CustomModel):
)
def save(self, *args, **kwargs):
prev_self = Transaction.objects.get(pk=self.pk)
if Transaction.objects.filter(pk=self.pk):
prev_self = Transaction.objects.get(pk=self.pk)
else:
prev_self = None
self.account = self.snapshot.account
super().save(*args, **kwargs)
prev_self.snapshot.update_sum()
if prev_self is not None:
prev_self.snapshot.update_sum()
self.snapshot.update_sum()
def __str__(self):

View file

@ -66,19 +66,19 @@
<a href="{% url 'snapshot' snap.id %}">{{ snap.date|date:"Y-m-d" }}</a>
</span>
<span class="account text center">
<i class="fa fa-{{ snap.account.icon }}"></i>
<a href="{% url 'account' snap.account.id %}">{{ snap.account }}</a>
<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 sum == snap.diff %}
<i class="fa fa-check green"></i>
{% else %}
<i class="fa fa-xmark red"></i>
{% endif %}
{% if sum == snap.diff %}
<i class="fa fa-check green"></i>
{% else %}
<i class="fa fa-xmark red"></i>
{% endif %}
</span>
{% endwith %}
</div>

View file

@ -71,9 +71,9 @@
{% endfor %}
</div>
{% endif %}
{% if snapshot.transactions %}
{% if snapshot.transaction_set.all %}
<h2>{% translate "Transactions" %} ({{ snapshot.sum|pmvalue }} / {{ snapshot.diff|pmvalue }})</h2>
{% transaction_table snapshot.transactions %}
{% transaction_table snapshot.transaction_set.all %}
{% endif %}
{% endwith %}
{% endblock %}