Revised the indents in the views of the accounting application.

This commit is contained in:
依瑪貓 2020-07-21 19:41:53 +08:00
parent 5a48e07ef6
commit e207efc285

View File

@ -96,16 +96,17 @@ def cash(request, account_code, period_spec):
~Q(account__code__startswith="21"), ~Q(account__code__startswith="21"),
~Q(account__code__startswith="22")) ~Q(account__code__startswith="22"))
.order_by("transaction__date", "is_credit", "ord")) .order_by("transaction__date", "is_credit", "ord"))
balance_before = Record.objects.filter( balance_before = Record.objects\
Q(transaction__date__lt=period.start), .filter(
(Q(account__code__startswith="11") | Q(transaction__date__lt=period.start),
Q(account__code__startswith="12") | (Q(account__code__startswith="11") |
Q(account__code__startswith="21") | Q(account__code__startswith="12") |
Q(account__code__startswith="21")))\ Q(account__code__startswith="21") |
Q(account__code__startswith="21")))\
.aggregate( .aggregate(
balance=Coalesce(Sum(Case( balance=Coalesce(Sum(Case(
When(is_credit=True, then=-1), When(is_credit=True, then=-1),
default=1) * F("amount")), 0))["balance"] default=1) * F("amount")), 0))["balance"]
else: else:
records = list( records = list(
Record.objects Record.objects
@ -118,13 +119,14 @@ def cash(request, account_code, period_spec):
~Q(account__code__startswith= ~Q(account__code__startswith=
current_account.code)) current_account.code))
.order_by("transaction__date", "is_credit", "ord")) .order_by("transaction__date", "is_credit", "ord"))
balance_before = Record.objects.filter( balance_before = Record.objects\
transaction__date__lt=period.start, .filter(
account__code__startswith=current_account.code)\ transaction__date__lt=period.start,
account__code__startswith=current_account.code)\
.aggregate( .aggregate(
balance=Coalesce(Sum(Case(When( balance=Coalesce(Sum(Case(When(
is_credit=True, then=-1), is_credit=True, then=-1),
default=1) * F("amount")), 0))["balance"] default=1) * F("amount")), 0))["balance"]
balance = balance_before balance = balance_before
for record in records: for record in records:
sign = 1 if record.is_credit else -1 sign = 1 if record.is_credit else -1
@ -203,47 +205,50 @@ def cash_summary(request, account_code):
raise Http404() raise Http404()
# The month summaries # The month summaries
if current_account.code == "0": if current_account.code == "0":
months = [RecordSummary(**x) for x in Record.objects.filter( months = [RecordSummary(**x) for x in Record.objects
Q(transaction__in=Transaction.objects.filter( .filter(
Q(record__account__code__startswith="11") | Q(transaction__in=Transaction.objects.filter(
Q(record__account__code__startswith="12") | Q(record__account__code__startswith="11") |
Q(record__account__code__startswith="21") | Q(record__account__code__startswith="12") |
Q(record__account__code__startswith="22"))), Q(record__account__code__startswith="21") |
~Q(account__code__startswith="11"), Q(record__account__code__startswith="22"))),
~Q(account__code__startswith="12"), ~Q(account__code__startswith="11"),
~Q(account__code__startswith="21"), ~Q(account__code__startswith="12"),
~Q(account__code__startswith="22")) \ ~Q(account__code__startswith="21"),
~Q(account__code__startswith="22")) \
.annotate(month=TruncMonth("transaction__date")) \ .annotate(month=TruncMonth("transaction__date")) \
.values("month") \ .values("month") \
.order_by("month") \ .order_by("month") \
.annotate( .annotate(
debit=Coalesce( debit=Coalesce(
Sum(Case(When(is_credit=False, then=F("amount")))), Sum(Case(When(is_credit=False, then=F("amount")))),
0), 0),
credit=Coalesce( credit=Coalesce(
Sum(Case(When(is_credit=True, then=F("amount")))), Sum(Case(When(is_credit=True, then=F("amount")))),
0), 0),
balance=Sum(Case( balance=Sum(Case(
When(is_credit=False, then=-F("amount")), When(is_credit=False, then=-F("amount")),
default=F("amount"))))] default=F("amount"))))]
else: else:
months = [RecordSummary(**x) for x in Record.objects.filter( months = [RecordSummary(**x) for x in Record.objects
Q(transaction__in=Transaction.objects.filter( .filter(
record__account__code__startswith=current_account.code)), Q(transaction__in=Transaction.objects.filter(
~Q(account__code__startswith=current_account.code)) \ record__account__code__startswith=
current_account.code)),
~Q(account__code__startswith=current_account.code)) \
.annotate(month=TruncMonth("transaction__date")) \ .annotate(month=TruncMonth("transaction__date")) \
.values("month") \ .values("month") \
.order_by("month") \ .order_by("month") \
.annotate( .annotate(
debit=Coalesce( debit=Coalesce(
Sum(Case(When(is_credit=False, then=F("amount")))), Sum(Case(When(is_credit=False, then=F("amount")))),
0), 0),
credit=Coalesce( credit=Coalesce(
Sum(Case(When(is_credit=True, then=F("amount")))), Sum(Case(When(is_credit=True, then=F("amount")))),
0), 0),
balance=Sum(Case( balance=Sum(Case(
When(is_credit=False, then=-F("amount")), When(is_credit=False, then=-F("amount")),
default=F("amount"))))] default=F("amount"))))]
cumulative_balance = 0 cumulative_balance = 0
for month in months: for month in months:
cumulative_balance = cumulative_balance + month.balance cumulative_balance = cumulative_balance + month.balance
@ -319,13 +324,14 @@ def ledger(request, account_code, period_spec):
account__code__startswith=current_account.code) account__code__startswith=current_account.code)
.order_by("transaction__date", "is_credit", "ord")) .order_by("transaction__date", "is_credit", "ord"))
if re.match("^[1-3]", current_account.code) is not None: if re.match("^[1-3]", current_account.code) is not None:
balance = Record.objects.filter( balance = Record.objects\
transaction__date__lt=period.start, .filter(
account__code__startswith=current_account.code)\ transaction__date__lt=period.start,
account__code__startswith=current_account.code)\
.aggregate( .aggregate(
balance=Coalesce(Sum(Case(When( balance=Coalesce(Sum(Case(When(
is_credit=True, then=-1), is_credit=True, then=-1),
default=1) * F("amount")), 0))["balance"] default=1) * F("amount")), 0))["balance"]
record_brought_forward = Record( record_brought_forward = Record(
transaction=Transaction(date=period.start), transaction=Transaction(date=period.start),
account=current_account, account=current_account,
@ -467,16 +473,17 @@ def journal(request, period_spec):
transaction__date__lte=period.end)\ transaction__date__lte=period.end)\
.order_by("transaction__date", "is_credit", "ord") .order_by("transaction__date", "is_credit", "ord")
# The brought-forward records # The brought-forward records
brought_forward_accounts = Account.objects.filter( brought_forward_accounts = Account.objects\
Q(code__startswith="1") .filter(
| Q(code__startswith="2") Q(code__startswith="1")
| Q(code__startswith="3"))\ | Q(code__startswith="2")
| Q(code__startswith="3"))\
.annotate(balance=Sum( .annotate(balance=Sum(
Case( Case(
When(record__is_credit=True, then=-1), When(record__is_credit=True, then=-1),
default=1 default=1
) * F("record__amount"), ) * F("record__amount"),
filter=Q(record__transaction__date__lt=period.start)))\ filter=Q(record__transaction__date__lt=period.start)))\
.filter(~Q(balance__gt=0)) .filter(~Q(balance__gt=0))
debit_records = [Record( debit_records = [Record(
transaction=Transaction(date=period.start), transaction=Transaction(date=period.start),