From b652d090d3df320bbd011e45eb3d6fcd095154ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Sun, 2 Aug 2020 19:42:48 +0800 Subject: [PATCH] Added the mount, debit_amount, and credit_amount properties to replace the run-time balance, total, debit, and credit properties in the Account data model in the accounting application. --- accounting/models.py | 3 + .../templates/accounting/balance-sheet.html | 50 ++++++------- .../accounting/income-statement.html | 20 ++--- .../templates/accounting/trial-balance.html | 20 ++--- accounting/views.py | 73 +++++++++---------- 5 files changed, 83 insertions(+), 83 deletions(-) diff --git a/accounting/models.py b/accounting/models.py index c701fd6..828995d 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -58,6 +58,9 @@ class Account(DirtyFieldsMixin, models.Model): def __init__(self, *args, **kwargs): super(Account, self).__init__(*args, **kwargs) self.url = None + self.debit_amount = None + self.credit_amount = None + self.amount = None def __str__(self): """Returns the string representation of this account.""" diff --git a/accounting/templates/accounting/balance-sheet.html b/accounting/templates/accounting/balance-sheet.html index 0e9fe1b..9413f4f 100644 --- a/accounting/templates/accounting/balance-sheet.html +++ b/accounting/templates/accounting/balance-sheet.html @@ -95,7 +95,7 @@ First written: 2020/7/20 {% for item in group.details %}
{{ item.title|title }}
- {{ item.balance|accounting_amount }} + {{ item.amount|accounting_amount }} @@ -126,7 +126,7 @@ First written: 2020/7/20 {% for item in group.details %} - {{ item.balance|accounting_amount }} + {{ item.amount|accounting_amount }} @@ -140,8 +140,8 @@ First written: 2020/7/20 {% trans "Total" context "Accounting|" as text %}{{ text|force_escape }} - - {{ liabilities.balance|accounting_amount }} + + {{ liabilities.amount|accounting_amount }} @@ -163,7 +163,7 @@ First written: 2020/7/20 {% for item in group.details %} - {{ item.balance|accounting_amount }} + {{ item.amount|accounting_amount }} @@ -177,8 +177,8 @@ First written: 2020/7/20 {% trans "Total" context "Accounting|" as text %}{{ text|force_escape }} - - {{ owners_equity.balance|accounting_amount }} + + {{ owners_equity.amount|accounting_amount }} @@ -192,8 +192,8 @@ First written: 2020/7/20 {% trans "Total" context "Accounting|" as text %}{{ text|force_escape }} - - {{ assets.balance|accounting_amount }} + + {{ assets.amount|accounting_amount }} @@ -205,8 +205,8 @@ First written: 2020/7/20 {% trans "Total" context "Accounting|" as text %}{{ text|force_escape }} - - {{ liabilities.balance|add:owners_equity.balance|accounting_amount }} + + {{ liabilities.amount|add:owners_equity.amount|accounting_amount }} @@ -236,8 +236,8 @@ First written: 2020/7/20 {{ item.title|title }}
- - {{ item.balance|accounting_amount }} + + {{ item.amount|accounting_amount }}
@@ -246,8 +246,8 @@ First written: 2020/7/20 {% endfor %}
  • {% trans "Total" context "Accounting|" as text %}{{ text|force_escape }} - - {{ assets.balance|accounting_amount }} + + {{ assets.amount|accounting_amount }}
  • @@ -267,8 +267,8 @@ First written: 2020/7/20 {{ item.title|title }}
    - - {{ item.balance|accounting_amount }} + + {{ item.amount|accounting_amount }}
    @@ -277,8 +277,8 @@ First written: 2020/7/20 {% endfor %}
  • {% trans "Total" context "Accounting|" as text %}{{ text|force_escape }} - - {{ liabilities.balance|accounting_amount }} + + {{ liabilities.amount|accounting_amount }}
  • @@ -297,8 +297,8 @@ First written: 2020/7/20 {{ item.title|title }}
    - - {{ item.balance|accounting_amount }} + + {{ item.amount|accounting_amount }}
    @@ -307,8 +307,8 @@ First written: 2020/7/20 {% endfor %}
  • {% trans "Total" context "Accounting|" as text %}{{ text|force_escape }} - - {{ owners_equity.balance|accounting_amount }} + + {{ owners_equity.amount|accounting_amount }}
  • @@ -316,8 +316,8 @@ First written: 2020/7/20 diff --git a/accounting/templates/accounting/income-statement.html b/accounting/templates/accounting/income-statement.html index b2316b7..ac7233d 100644 --- a/accounting/templates/accounting/income-statement.html +++ b/accounting/templates/accounting/income-statement.html @@ -106,7 +106,7 @@ First written: 2020/7/19 {% for item in group.details %}
    {{ item.title|title }}
    - {{ item.balance|accounting_amount }} + {{ item.amount|accounting_amount }} @@ -119,7 +119,7 @@ First written: 2020/7/19
    {% trans "Total" context "Accounting|" as text %}{{ text|force_escape }}
    - {{ group.total|accounting_amount }} + {{ group.amount|accounting_amount }} {% endfor %} @@ -135,7 +135,7 @@ First written: 2020/7/19
    {{ section.cumulative_total.title|title }}
    - {{ section.cumulative_total.total|accounting_amount }} + {{ section.cumulative_total.amount|accounting_amount }} {% endif %} @@ -172,8 +172,8 @@ First written: 2020/7/19
    {{ item.title|title }}
    - - {{ item.balance|accounting_amount }} + + {{ item.amount|accounting_amount }}
    @@ -182,8 +182,8 @@ First written: 2020/7/19
  • {% trans "Total" context "Accounting|" as text %}{{ text|force_escape }}
    - - {{ group.total|accounting_amount }} + + {{ group.amount|accounting_amount }}
  • @@ -192,7 +192,7 @@ First written: 2020/7/19
  • {% trans "Total" context "Accounting|" as text %}{{ text|force_escape }}
    - - + -
  • {% endif %} @@ -200,8 +200,8 @@ First written: 2020/7/19
  • {{ section.cumulative_total.title|title }}
    - - {{ section.cumulative_total.total|accounting_amount }} + + {{ section.cumulative_total.amount|accounting_amount }}
  • diff --git a/accounting/templates/accounting/trial-balance.html b/accounting/templates/accounting/trial-balance.html index a74c39e..555561a 100644 --- a/accounting/templates/accounting/trial-balance.html +++ b/accounting/templates/accounting/trial-balance.html @@ -95,8 +95,8 @@ First written: 2020/7/19 {% for item in item_list %} {{ item.title|title }} - {{ item.debit|accounting_amount }} - {{ item.credit|accounting_amount }} + {{ item.debit_amount|accounting_amount }} + {{ item.credit_amount|accounting_amount }} @@ -109,8 +109,8 @@ First written: 2020/7/19 {% trans "Total" context "Accounting|" as text %}{{ text|force_escape }} - {{ total_item.debit|accounting_amount }} - {{ total_item.credit|accounting_amount }} + {{ total_item.debit_amount|accounting_amount }} + {{ total_item.credit_amount|accounting_amount }} @@ -133,14 +133,14 @@ First written: 2020/7/19 {{ item.title|title }}
    - {% if item.debit is not None %} + {% if item.debit_amount is not None %} - {{ item.debit|intcomma:False }} + {{ item.debit_amount|intcomma:False }} {% endif %} - {% if item.credit is not None %} + {% if item.credit_amount is not None %} - {{ item.credit|intcomma:False }} + {{ item.credit_amount|intcomma:False }} {% endif %}
    @@ -151,10 +151,10 @@ First written: 2020/7/19 {% trans "Total" context "Accounting|" as text %}{{ text|force_escape }}
    - {{ total_item.debit|intcomma:False }} + {{ total_item.debit_amount|intcomma:False }} - {{ total_item.credit|intcomma:False }} + {{ total_item.credit_amount|intcomma:False }}
    diff --git a/accounting/views.py b/accounting/views.py index 6f4401e..eea0263 100644 --- a/accounting/views.py +++ b/accounting/views.py @@ -514,16 +514,16 @@ def trial_balance(request, period): | Q(code__startswith="2") | Q(code__startswith="3"))) .annotate( - balance=Sum(Case( + amount=Sum(Case( When(record__is_credit=True, then=-1), default=1) * F("record__amount"))) - .filter(balance__isnull=False) + .filter(amount__isnull=False) .annotate( - debit=Case( - When(balance__gt=0, then=F("balance")), + debit_amount=Case( + When(amount__gt=0, then=F("amount")), default=None), - credit=Case( - When(balance__lt=0, then=-F("balance")), + credit_amount=Case( + When(amount__lt=0, then=-F("amount")), default=None)) .order_by("code")) real = list( @@ -535,16 +535,16 @@ def trial_balance(request, period): | Q(code__startswith="3")), ~Q(code=Account.ACCUMULATED_BALANCE)) .annotate( - balance=Sum(Case( + amount=Sum(Case( When(record__is_credit=True, then=-1), default=1) * F("record__amount"))) - .filter(balance__isnull=False) + .filter(amount__isnull=False) .annotate( - debit=Case( - When(balance__gt=0, then=F("balance")), + debit_amount=Case( + When(amount__gt=0, then=F("amount")), default=None), - credit=Case( - When(balance__lt=0, then=-F("balance")), + credit_amount=Case( + When(amount__lt=0, then=-F("amount")), default=None)) .order_by("code")) balance = Record.objects \ @@ -563,20 +563,20 @@ def trial_balance(request, period): brought_forward = Account.objects.get( code=Account.ACCUMULATED_BALANCE) if balance > 0: - brought_forward.debit = balance - brought_forward.credit = 0 + brought_forward.debit_amount = balance + brought_forward.credit_amount = None else: - brought_forward.debit = None - brought_forward.credit = -balance + brought_forward.debit_amount = None + brought_forward.credit_amount = -balance real.append(brought_forward) accounts = nominal + real accounts.sort(key=lambda x: x.code) total_account = Account() total_account.title = pgettext("Accounting|", "Total") - total_account.debit = sum([x.debit for x in accounts - if x.debit is not None]) - total_account.credit = sum([x.credit for x in accounts - if x.credit is not None]) + total_account.debit_amount = sum([x.debit_amount for x in accounts + if x.debit_amount is not None]) + total_account.credit_amount = sum([x.credit_amount for x in accounts + if x.credit_amount is not None]) return render(request, "accounting/trial-balance.html", { "item_list": accounts, "total_item": total_account, @@ -619,10 +619,10 @@ def income_statement(request, period): | Q(code__startswith="2") | Q(code__startswith="3"))) .annotate( - balance=Sum(Case( + amount=Sum(Case( When(record__is_credit=True, then=1), default=-1) * F("record__amount"))) - .filter(balance__isnull=False) + .filter(amount__isnull=False) .order_by("code")) groups = list(Account.objects.filter( code__in=[x.code[:2] for x in accounts])) @@ -643,17 +643,14 @@ def income_statement(request, period): for group in section.groups: group.details = [x for x in accounts if x.code[:2] == group.code] - group.balance = None - group.total = sum([x.balance - for x in group.details]) - section.balance = None - section.total = sum([x.total for x in section.groups]) - cumulative_total = cumulative_total + section.total + group.amount = sum([x.amount + for x in group.details]) + section.amount = sum([x.amount for x in section.groups]) + cumulative_total = cumulative_total + section.amount if section.code in cumulative_accounts: section.cumulative_total \ = cumulative_accounts[section.code] - section.cumulative_total.balance = None - section.cumulative_total.total = cumulative_total + section.cumulative_total.amount = cumulative_total else: section.cumulative_total = None section.has_next = True @@ -699,10 +696,10 @@ def balance_sheet(request, period): | Q(code__startswith="3")), ~Q(code=Account.ACCUMULATED_BALANCE)) .annotate( - balance=Sum(Case( + amount=Sum(Case( When(record__is_credit=True, then=-1), default=1) * F("record__amount"))) - .filter(balance__isnull=False) + .filter(amount__isnull=False) .order_by("code")) for account in accounts: account.url = reverse("accounting:ledger", args=(account, period)) @@ -720,7 +717,7 @@ def balance_sheet(request, period): if balance is not None and balance != 0: brought_forward = Account.objects.get( code=Account.ACCUMULATED_BALANCE) - brought_forward.balance = balance + brought_forward.amount = balance brought_forward.url = reverse( "accounting:income-statement", args=(period,)) accounts.append(brought_forward) @@ -738,12 +735,12 @@ def balance_sheet(request, period): default=1) * F("amount")))["balance"] if balance is not None and balance != 0: net_change = Account.objects.get(code=Account.NET_CHANGE) - net_change.balance = balance + net_change.amount = balance net_change.url = reverse( "accounting:income-statement", args=(period,)) accounts.append(net_change) for account in [x for x in accounts if x.code[0] in "23"]: - account.balance = -account.balance + account.amount = -account.amount groups = list(Account.objects.filter( code__in=[x.code[:2] for x in accounts])) sections = list(Account.objects.filter( @@ -754,9 +751,9 @@ def balance_sheet(request, period): for group in section.groups: group.details = [x for x in accounts if x.code[:2] == group.code] - group.balance = sum([x.balance - for x in group.details]) - section.balance = sum([x.balance for x in section.groups]) + group.amount = sum([x.amount + for x in group.details]) + section.amount = sum([x.amount for x in section.groups]) by_code = {x.code: x for x in sections} return render(request, "accounting/balance-sheet.html", { "assets": by_code["1"],