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"] id__in=self.cleaned_data["transactions"]
) )
to_update = list( instance.transaction_set.add(*new_transactions, bulk=False)
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()
return instance return instance

View file

@ -159,10 +159,15 @@ class Snapshot(AccountModel):
trans.save() trans.save()
self.diff = self.value - self.start_value 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) super().save(*args, **kwargs)
def update_sum(self): 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() super().save()
def delete(self, *args, **kwargs): def delete(self, *args, **kwargs):
@ -219,10 +224,14 @@ class Transaction(CustomModel):
) )
def save(self, *args, **kwargs): 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 self.account = self.snapshot.account
super().save(*args, **kwargs) super().save(*args, **kwargs)
prev_self.snapshot.update_sum() if prev_self is not None:
prev_self.snapshot.update_sum()
self.snapshot.update_sum() self.snapshot.update_sum()
def __str__(self): def __str__(self):

View file

@ -66,19 +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">
<i class="fa fa-{{ snap.account.icon }}"></i> <i class="fa fa-{{ snap.account.icon }}"></i>
<a href="{% url 'account' snap.account.id %}">{{ snap.account }}</a> <a href="{% url 'account' snap.account.id %}">{{ snap.account }}</a>
</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 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 %}
</span> </span>
{% endwith %} {% endwith %}
</div> </div>

View file

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