Revised the coding style according to PEP8.

This commit is contained in:
依瑪貓 2020-08-02 00:08:53 +08:00
parent 9d988f17ca
commit da80816b37
3 changed files with 144 additions and 144 deletions

View File

@ -37,8 +37,8 @@ register_converter(converters.DateConverter, "date")
app_name = "accounting" app_name = "accounting"
urlpatterns = [ urlpatterns = [
path("", require_GET(login_required(RedirectView.as_view( path("", require_GET(login_required(RedirectView.as_view(
query_string = True, query_string=True,
pattern_name = "accounting:cash.home", pattern_name="accounting:cash.home",
))), name="home"), ))), name="home"),
path("cash", path("cash",
views.CashDefaultView.as_view(), name="cash.home"), views.CashDefaultView.as_view(), name="cash.home"),

View File

@ -74,7 +74,7 @@ def cash(request, account, period):
if account.code == "0": if 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),
@ -86,46 +86,47 @@ def cash(request, account, period):
~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", "transaction__ord", .order_by("transaction__date", "transaction__ord",
"is_credit", "ord")) "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),
(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="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
.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=account.code))), Q(record__account__code__startswith=account.code))),
~Q(account__code__startswith=account.code)) ~Q(account__code__startswith=account.code))
.order_by("transaction__date", "transaction__ord", .order_by("transaction__date", "transaction__ord",
"is_credit", "ord")) "is_credit", "ord"))
balance_before = Record.objects \ balance_before = Record.objects \
.filter( .filter(
transaction__date__lt=period.start, transaction__date__lt=period.start,
account__code__startswith=account.code) \ account__code__startswith=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
balance = balance + sign * record.amount balance = balance + sign * record.amount
record.balance = balance record.balance = balance
record_sum = Record( record_sum = Record(
transaction=Transaction(date=records[-1].transaction.date) transaction=(Transaction(date=records[-1].transaction.date)
if len(records)>0 else Transaction(date=timezone.localdate()), if len(records) > 0
else Transaction(date=timezone.localdate())),
account=account, account=account,
summary=pgettext("Accounting|", "Total"), summary=pgettext("Accounting|", "Total"),
balance=balance balance=balance
@ -190,48 +191,48 @@ def cash_summary(request, account):
# The month summaries # The month summaries
if account.code == "0": if 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=account.code)), record__account__code__startswith=account.code)),
~Q(account__code__startswith=account.code)) ~Q(account__code__startswith=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
@ -287,21 +288,21 @@ def ledger(request, account, period):
# 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=account.code) account__code__startswith=account.code)
.order_by("transaction__date", "transaction__ord", "is_credit", .order_by("transaction__date", "transaction__ord", "is_credit",
"ord")) "ord"))
if re.match("^[1-3]", account.code) is not None: if re.match("^[1-3]", account.code) is not None:
balance = Record.objects \ balance = Record.objects \
.filter( .filter(
transaction__date__lt=period.start, transaction__date__lt=period.start,
account__code__startswith=account.code) \ account__code__startswith=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=account, account=account,
@ -360,20 +361,20 @@ def ledger_summary(request, account):
""" """
# 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=account.code) .filter(account__code__startswith=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
@ -422,21 +423,21 @@ def journal(request, period):
# The accounting records # The accounting records
records = Record.objects \ records = Record.objects \
.filter( .filter(
transaction__date__gte=period.start, transaction__date__gte=period.start,
transaction__date__lte=period.end) \ transaction__date__lte=period.end) \
.order_by("transaction__date", "transaction__ord", "is_credit", "ord") .order_by("transaction__date", "transaction__ord", "is_credit", "ord")
# The brought-forward records # The brought-forward records
brought_forward_accounts = Account.objects \ brought_forward_accounts = Account.objects \
.filter( .filter(
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(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=0)) .filter(~Q(balance=0))
debit_records = [Record( debit_records = [Record(
transaction=Transaction(date=period.start), transaction=Transaction(date=period.start),
@ -466,8 +467,7 @@ def journal(request, period):
is_credit=True, is_credit=True,
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,
@ -504,58 +504,58 @@ def trial_balance(request, period):
# 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)
& ~(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(transaction__date__lte=period.end) | (Q(transaction__date__lte=period.end)
& 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")
if balance > 0: if balance > 0:
@ -608,18 +608,18 @@ def income_statement(request, period):
# 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(
@ -688,31 +688,31 @@ def balance_sheet(request, period):
# 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("accounting:ledger", args=(account, period)) account.url = reverse("accounting:ledger", args=(account, period))
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
@ -721,16 +721,16 @@ def balance_sheet(request, period):
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

View File

@ -140,9 +140,9 @@ def get_multi_lingual_search(attr, query):
return Q(**{attr + language.db + "__icontains": query}) return Q(**{attr + language.db + "__icontains": query})
default = Language.default() default = Language.default()
q = (Q(**{attr + language.db + "__isnull": False}) q = (Q(**{attr + language.db + "__isnull": False})
& Q(**{attr + language.db + "__icontains": query}))\ & Q(**{attr + language.db + "__icontains": query}))\
| (Q(**{attr + language.db + "__isnull": True}) | (Q(**{attr + language.db + "__isnull": True})
& Q(**{attr + default.db + "__icontains": query})) & Q(**{attr + default.db + "__icontains": query}))
return q return q