Simplified the query to get the brought-forward balance in the ledger in the accounting transaction.

This commit is contained in:
依瑪貓 2020-07-18 08:45:41 +08:00
parent dae002387f
commit 85b8515a0a

View File

@ -413,16 +413,12 @@ def ledger(request, subject_code, period_spec):
transaction__date__lte=period.end, transaction__date__lte=period.end,
subject__code__startswith=current_subject.code)) subject__code__startswith=current_subject.code))
if re.match("^[1-3]", current_subject.code) is not None: if re.match("^[1-3]", current_subject.code) is not None:
debit = Record.objects.filter( balance = Record.objects.filter(
transaction__date__lt=period.start, transaction__date__lt=period.start,
subject__code__startswith=current_subject.code, subject__code__startswith=current_subject.code)\
is_credit=False).aggregate(sum=Sum("amount")) .aggregate(balance=Sum(Case(When(
credit = Record.objects.filter( is_credit=True, then=-1),
transaction__date__lt=period.start, default=1) * F("amount")))["balance"]
subject__code__startswith=current_subject.code,
is_credit=True).aggregate(sum=Sum("amount"))
balance = (0 if debit["sum"] is None else debit["sum"]) \
- (0 if credit["sum"] is None else credit["sum"])
record_brought_forward = Record( record_brought_forward = Record(
transaction=Transaction( transaction=Transaction(
date=records[-1].transaction.date), date=records[-1].transaction.date),