From 8890e60fbbc63c816fc7080467efb2bb8194f629 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Thu, 23 Jul 2020 08:33:53 +0800 Subject: [PATCH] Added CashAccountConverter and LedgerAccountConverter and applied them in the URL patterns of the accounting application. --- accounting/converters.py | 77 +++++++++++- .../templates/accounting/cash-summary.html | 8 +- accounting/templates/accounting/cash.html | 4 +- .../accounting/income-statement.html | 4 +- .../templates/accounting/ledger-summary.html | 6 +- accounting/templates/accounting/ledger.html | 2 +- .../templates/accounting/trial-balance.html | 4 +- accounting/urls.py | 11 +- accounting/utils.py | 36 +++--- accounting/views/reports.py | 118 +++++++----------- 10 files changed, 155 insertions(+), 115 deletions(-) diff --git a/accounting/converters.py b/accounting/converters.py index caf189c..5ee3289 100644 --- a/accounting/converters.py +++ b/accounting/converters.py @@ -18,7 +18,10 @@ """The URL converters. """ -from accounting.models import Transaction + +from django.utils.translation import pgettext + +from accounting.models import Transaction, Record, Account from mia_core.period import Period @@ -67,3 +70,75 @@ class PeriodConverter: if isinstance(value, Period): return value.spec return value + + +class CashAccountConverter: + """The path converter for the cash account.""" + regex = "0|(11|12|21|22)[1-9]{1,3}" + + def to_python(self, value): + """Returns the cash account by the account code. + + Args: + value (str): The account code. + + Returns: + Account: The account. + """ + if value == "0": + return Account( + code="0", + title=pgettext( + "Accounting|", "current assets and liabilities"), + ) + try: + account = Account.objects.get(code=value) + except Account.DoesNotExist: + raise ValueError + if Record.objects.filter(account=account).count() == 0: + raise ValueError + return account + + def to_url(self, value): + """Returns the code of an account. + + Args: + value (Account): The account. + + Returns: + str: The account code. + """ + return value.code + + +class LedgerAccountConverter: + """The path converter for the ledger account.""" + regex = "[1-9]{1,5}" + + def to_python(self, value): + """Returns the ledger accountby the account code. + + Args: + value (str): The account code. + + Returns: + Account: The account. + """ + try: + account = Account.objects.get(code=value) + except Account.DoesNotExist: + raise ValueError + if Record.objects.filter(account__code__startswith=value).count() == 0: + raise ValueError + return account + + def to_url(self, value): + """Returns the code of an account. + + Args: + value (Account): The account. + + Returns: + str: The account code. + """ + return value.code diff --git a/accounting/templates/accounting/cash-summary.html b/accounting/templates/accounting/cash-summary.html index 443d7dc..b925f8b 100644 --- a/accounting/templates/accounting/cash-summary.html +++ b/accounting/templates/accounting/cash-summary.html @@ -72,13 +72,13 @@ First written: 2020/7/15