Fix date range for snapshot transactions
This commit is contained in:
parent
5c01b8cd61
commit
d96450aae2
3 changed files with 11 additions and 13 deletions
|
@ -162,11 +162,17 @@ class Snapshot(models.Model):
|
||||||
def sum(self):
|
def sum(self):
|
||||||
if self.previous is None:
|
if self.previous is None:
|
||||||
return None
|
return None
|
||||||
trans = Transaction.objects.filter(
|
trans = self.transactions.aggregate(sum=models.Sum("value"))
|
||||||
date__lt=self.date, date__gte=self.previous.date
|
|
||||||
).aggregate(sum=models.Sum("value"))
|
|
||||||
return trans["sum"] or 0
|
return trans["sum"] or 0
|
||||||
|
|
||||||
|
@property
|
||||||
|
def transactions(self):
|
||||||
|
if self.previous is None:
|
||||||
|
return Transaction.objects.none()
|
||||||
|
return Transaction.objects.filter(
|
||||||
|
date__lte=self.date, date__gt=self.previous.date
|
||||||
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
ordering = ["-date"]
|
ordering = ["-date"]
|
||||||
|
|
||||||
|
|
|
@ -35,10 +35,10 @@
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
{% if transactions %}
|
{% if snapshot.transactions %}
|
||||||
<h2>Transactions ({% pmvalue sum %} / {% pmvalue snapshot.diff %})</h2>
|
<h2>Transactions ({% pmvalue sum %} / {% pmvalue snapshot.diff %})</h2>
|
||||||
|
|
||||||
{% transaction_table transactions %}
|
{% transaction_table snapshot.transactions %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endwith %}
|
{% endwith %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -127,22 +127,14 @@ def update_category(request, uuid):
|
||||||
def snapshot(request, date=None):
|
def snapshot(request, date=None):
|
||||||
if date is None:
|
if date is None:
|
||||||
_snapshot = Snapshot()
|
_snapshot = Snapshot()
|
||||||
_transactions = None
|
|
||||||
else:
|
else:
|
||||||
_snapshot = get_object_or_404(Snapshot, date=date)
|
_snapshot = get_object_or_404(Snapshot, date=date)
|
||||||
if _snapshot.previous is None:
|
|
||||||
_transactions = None
|
|
||||||
else:
|
|
||||||
_transactions = Transaction.objects.filter(
|
|
||||||
date__lt=_snapshot.date, date__gte=_snapshot.previous.date
|
|
||||||
)
|
|
||||||
return render(
|
return render(
|
||||||
request,
|
request,
|
||||||
"main/snapshot.html",
|
"main/snapshot.html",
|
||||||
{
|
{
|
||||||
"snapshot": _snapshot,
|
"snapshot": _snapshot,
|
||||||
"form": SnapshotForm(instance=_snapshot),
|
"form": SnapshotForm(instance=_snapshot),
|
||||||
"transactions": _transactions,
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue