Revised the code of the views of the account application with PEP8.

This commit is contained in:
依瑪貓 2020-07-21 20:57:59 +08:00
parent b353a3fba8
commit 7a6879951a

View File

@ -83,19 +83,19 @@ def cash(request, account_code, period_spec):
if current_account.code == "0": if current_account.code == "0":
records = list( records = list(
Record.objects Record.objects
.filter( .filter(
Q(transaction__in=Transaction.objects.filter( Q(transaction__in=Transaction.objects.filter(
Q(date__gte=period.start), Q(date__gte=period.start),
Q(date__lte=period.end), Q(date__lte=period.end),
(Q(record__account__code__startswith="11") | (Q(record__account__code__startswith="11") |
Q(record__account__code__startswith="12") | Q(record__account__code__startswith="12") |
Q(record__account__code__startswith="21") | Q(record__account__code__startswith="21") |
Q(record__account__code__startswith="22")))), Q(record__account__code__startswith="22")))),
~Q(account__code__startswith="11"), ~Q(account__code__startswith="11"),
~Q(account__code__startswith="12"), ~Q(account__code__startswith="12"),
~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\ balance_before = Record.objects\
.filter( .filter(
Q(transaction__date__lt=period.start), Q(transaction__date__lt=period.start),
@ -110,15 +110,13 @@ def cash(request, account_code, period_spec):
else: else:
records = list( records = list(
Record.objects Record.objects
.filter( .filter(
Q(transaction__in=Transaction.objects.filter( Q(transaction__in=Transaction.objects.filter(
Q(date__gte=period.start), Q(date__gte=period.start),
Q(date__lte=period.end), Q(date__lte=period.end),
Q(record__account__code__startswith= Q(record__account__code__startswith=account_code))),
current_account.code))), ~Q(account__code__startswith=account_code))
~Q(account__code__startswith= .order_by("transaction__date", "is_credit", "ord"))
current_account.code))
.order_by("transaction__date", "is_credit", "ord"))
balance_before = Record.objects\ balance_before = Record.objects\
.filter( .filter(
transaction__date__lt=period.start, transaction__date__lt=period.start,
@ -206,49 +204,48 @@ def cash_summary(request, account_code):
# The month summaries # The month summaries
if current_account.code == "0": if current_account.code == "0":
months = [RecordSummary(**x) for x in Record.objects months = [RecordSummary(**x) for x in Record.objects
.filter( .filter(
Q(transaction__in=Transaction.objects.filter( Q(transaction__in=Transaction.objects.filter(
Q(record__account__code__startswith="11") | Q(record__account__code__startswith="11") |
Q(record__account__code__startswith="12") | Q(record__account__code__startswith="12") |
Q(record__account__code__startswith="21") | Q(record__account__code__startswith="21") |
Q(record__account__code__startswith="22"))), Q(record__account__code__startswith="22"))),
~Q(account__code__startswith="11"), ~Q(account__code__startswith="11"),
~Q(account__code__startswith="12"), ~Q(account__code__startswith="12"),
~Q(account__code__startswith="21"), ~Q(account__code__startswith="21"),
~Q(account__code__startswith="22")) \ ~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 months = [RecordSummary(**x) for x in Record.objects
.filter( .filter(
Q(transaction__in=Transaction.objects.filter( Q(transaction__in=Transaction.objects.filter(
record__account__code__startswith= record__account__code__startswith=account_code)),
current_account.code)), ~Q(account__code__startswith=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
@ -289,7 +286,8 @@ def ledger_default(request):
account_code = settings.ACCOUNTING["DEFAULT_LEDGER_ACCOUNT"] account_code = settings.ACCOUNTING["DEFAULT_LEDGER_ACCOUNT"]
period_spec = dateformat.format(timezone.localdate(), "Y-m") period_spec = dateformat.format(timezone.localdate(), "Y-m")
return HttpResponseRedirect( return HttpResponseRedirect(
reverse("accounting:ledger", args=(account_code, period_spec))) reverse("accounting:ledger",
args=(account_code, period_spec)))
@require_GET @require_GET
@ -318,11 +316,11 @@ def ledger(request, account_code, period_spec):
# The accounting records # The accounting records
records = list( records = list(
Record.objects Record.objects
.filter( .filter(
transaction__date__gte=period.start, transaction__date__gte=period.start,
transaction__date__lte=period.end, transaction__date__lte=period.end,
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\ balance = Record.objects\
.filter( .filter(
@ -400,21 +398,21 @@ def ledger_summary(request, account_code):
if current_account is None: if current_account is None:
raise Http404() raise Http404()
# The month summaries # The month summaries
months = [RecordSummary(**x) for x in Record.objects\ months = [RecordSummary(**x) for x in Record.objects
.filter(account__code__startswith=current_account.code)\ .filter(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
@ -514,7 +512,7 @@ def journal(request, period_spec):
amount=sum_debits - sum_credits amount=sum_debits - sum_credits
)) ))
records = list(debit_records) + list(credit_records)\ records = list(debit_records) + list(credit_records)\
+ list(records) + list(records)
pagination = Pagination(request, records, True) pagination = Pagination(request, records, True)
return render(request, "accounting/journal.html", { return render(request, "accounting/journal.html", {
"item_list": pagination.items, "item_list": pagination.items,
@ -557,46 +555,46 @@ def trial_balance(request, period_spec):
# The accounts # The accounts
nominal = list( nominal = list(
Account.objects Account.objects
.filter( .filter(
Q(record__transaction__date__gte=period.start), Q(record__transaction__date__gte=period.start),
Q(record__transaction__date__lte=period.end), Q(record__transaction__date__lte=period.end),
~(Q(code__startswith="1") ~(Q(code__startswith="1")
| Q(code__startswith="2") | Q(code__startswith="2")
| Q(code__startswith="3"))) | Q(code__startswith="3")))
.annotate( .annotate(
balance=Sum(Case( balance=Sum(Case(
When(record__is_credit=True, then=-1), When(record__is_credit=True, then=-1),
default=1) * F("record__amount"))) default=1) * F("record__amount")))
.filter(balance__isnull=False) .filter(balance__isnull=False)
.annotate( .annotate(
debit=Case( debit=Case(
When(balance__gt=0, then=F("balance")), When(balance__gt=0, then=F("balance")),
default=None), default=None),
credit=Case( credit=Case(
When(balance__lt=0, then=-F("balance")), When(balance__lt=0, then=-F("balance")),
default=None)) default=None))
.order_by("code")) .order_by("code"))
real = list( real = list(
Account.objects Account.objects
.filter( .filter(
Q(record__transaction__date__lte=period.end), Q(record__transaction__date__lte=period.end),
(Q(code__startswith="1") (Q(code__startswith="1")
| Q(code__startswith="2") | Q(code__startswith="2")
| Q(code__startswith="3")), | Q(code__startswith="3")),
~Q(code="3351")) ~Q(code="3351"))
.annotate( .annotate(
balance=Sum(Case( balance=Sum(Case(
When(record__is_credit=True, then=-1), When(record__is_credit=True, then=-1),
default=1) * F("record__amount"))) default=1) * F("record__amount")))
.filter(balance__isnull=False) .filter(balance__isnull=False)
.annotate( .annotate(
debit=Case( debit=Case(
When(balance__gt=0, then=F("balance")), When(balance__gt=0, then=F("balance")),
default=None), default=None),
credit=Case( credit=Case(
When(balance__lt=0, then=-F("balance")), When(balance__lt=0, then=-F("balance")),
default=None)) default=None))
.order_by("code")) .order_by("code"))
balance = Record.objects\ balance = Record.objects\
.filter( .filter(
(Q(transaction__date__lt=period.start) (Q(transaction__date__lt=period.start)
@ -667,18 +665,18 @@ def income_statement(request, period_spec):
# The accounts # The accounts
accounts = list( accounts = list(
Account.objects Account.objects
.filter( .filter(
Q(record__transaction__date__gte=period.start), Q(record__transaction__date__gte=period.start),
Q(record__transaction__date__lte=period.end), Q(record__transaction__date__lte=period.end),
~(Q(code__startswith="1") ~(Q(code__startswith="1")
| Q(code__startswith="2") | Q(code__startswith="2")
| Q(code__startswith="3"))) | Q(code__startswith="3")))
.annotate( .annotate(
balance=Sum(Case( balance=Sum(Case(
When(record__is_credit=True, then=1), When(record__is_credit=True, then=1),
default=-1) * F("record__amount"))) default=-1) * F("record__amount")))
.filter(balance__isnull=False) .filter(balance__isnull=False)
.order_by("code")) .order_by("code"))
groups = list(Account.objects.filter( groups = list(Account.objects.filter(
code__in=[x.code[:2] for x in accounts])) code__in=[x.code[:2] for x in accounts]))
sections = list(Account.objects.filter( sections = list(Account.objects.filter(
@ -753,32 +751,32 @@ def balance_sheet(request, period_spec):
# The accounts # The accounts
accounts = list( accounts = list(
Account.objects Account.objects
.filter( .filter(
Q(record__transaction__date__lte=period.end), Q(record__transaction__date__lte=period.end),
(Q(code__startswith="1") (Q(code__startswith="1")
| Q(code__startswith="2") | Q(code__startswith="2")
| Q(code__startswith="3")), | Q(code__startswith="3")),
~Q(code="3351")) ~Q(code="3351"))
.annotate( .annotate(
balance=Sum(Case( balance=Sum(Case(
When(record__is_credit=True, then=-1), When(record__is_credit=True, then=-1),
default=1) * F("record__amount"))) default=1) * F("record__amount")))
.filter(balance__isnull=False) .filter(balance__isnull=False)
.order_by("code")) .order_by("code"))
for account in accounts: for account in accounts:
account.url = reverse( account.url = reverse(
"accounting:ledger", args=[account.code, period.spec]) "accounting:ledger", args=[account.code, period.spec])
balance = Record.objects\ balance = Record.objects\
.filter( .filter(
Q(transaction__date__lt=period.start) Q(transaction__date__lt=period.start)
& ~((Q(account__code__startswith="1") & ~((Q(account__code__startswith="1")
| Q(account__code__startswith="2") | Q(account__code__startswith="2")
| Q(account__code__startswith="3")) | Q(account__code__startswith="3"))
& ~Q(account__code="3351")))\ & ~Q(account__code="3351")))\
.aggregate( .aggregate(
balance=Sum(Case( balance=Sum(Case(
When(is_credit=True, then=-1), When(is_credit=True, then=-1),
default=1) * F("amount")))["balance"] default=1) * F("amount")))["balance"]
if balance is not None and balance != 0: if balance is not None and balance != 0:
brought_forward = Account.objects.get(code="3351") brought_forward = Account.objects.get(code="3351")
brought_forward.balance = -balance brought_forward.balance = -balance
@ -787,16 +785,16 @@ def balance_sheet(request, period_spec):
accounts.append(brought_forward) accounts.append(brought_forward)
balance = Record.objects\ balance = Record.objects\
.filter( .filter(
Q(transaction__date__gte=period.start) Q(transaction__date__gte=period.start)
& Q(transaction__date__lte=period.end) & Q(transaction__date__lte=period.end)
& ~((Q(account__code__startswith="1") & ~((Q(account__code__startswith="1")
| Q(account__code__startswith="2") | Q(account__code__startswith="2")
| Q(account__code__startswith="3")) | Q(account__code__startswith="3"))
& ~Q(account__code="3351")))\ & ~Q(account__code="3351")))\
.aggregate( .aggregate(
balance=Sum(Case( balance=Sum(Case(
When(is_credit=True, then=-1), When(is_credit=True, then=-1),
default=1) * F("amount")))["balance"] default=1) * F("amount")))["balance"]
if balance is not None and balance != 0: if balance is not None and balance != 0:
net_income = Account.objects.get(code="3353") net_income = Account.objects.get(code="3353")
net_income.balance = -balance net_income.balance = -balance
@ -878,15 +876,15 @@ def _cash_accounts():
""" """
accounts = list( accounts = list(
Account.objects Account.objects
.filter(
code__in=Record.objects
.filter( .filter(
code__in=Record.objects Q(account__code__startswith="11")
.filter( | 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") .values("account__code"))
| Q(account__code__startswith="22")) .order_by("code"))
.values("account__code"))
.order_by("code"))
accounts.insert(0, Account( accounts.insert(0, Account(
code="0", code="0",
title=pgettext( title=pgettext(
@ -923,11 +921,11 @@ def _find_imbalanced(records):
records (list[Record]): The accounting records. records (list[Record]): The accounting records.
""" """
imbalanced = [x.sn for x in Transaction.objects imbalanced = [x.sn for x in Transaction.objects
.annotate( .annotate(
balance=Sum(Case( balance=Sum(Case(
When(record__is_credit=True, then=-1), When(record__is_credit=True, then=-1),
default=1) * F("record__amount"))) default=1) * F("record__amount")))
.filter(~Q(balance=0))] .filter(~Q(balance=0))]
for record in records: for record in records:
record.is_balanced = record.transaction.sn not in imbalanced record.is_balanced = record.transaction.sn not in imbalanced
@ -941,12 +939,14 @@ def _find_order_holes(records):
records (list[Record]): The accounting records. records (list[Record]): The accounting records.
""" """
holes = [x["date"] for x in Transaction.objects holes = [x["date"] for x in Transaction.objects
.values("date") .values("date")
.annotate(count=Count("ord"), max=Max("ord"), min=Min("ord")) .annotate(count=Count("ord"),
.filter(~(Q(max=F("count")) & Q(min=1)))]\ max=Max("ord"),
+ [x["date"] for x in Transaction.objects min=Min("ord"))
.values("date", "ord") .filter(~(Q(max=F("count")) & Q(min=1)))] +\
.annotate(count=Count("sn")) [x["date"] for x in Transaction.objects
.filter(~Q(count=1))] .values("date", "ord")
.annotate(count=Count("sn"))
.filter(~Q(count=1))]
for record in records: for record in records:
record.has_order_hole = record.transaction.date in holes record.has_order_hole = record.transaction.date in holes