From 2b9034cfe878584d1d87707c5cd9fcc519613dda 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 18:20:36 +0800 Subject: [PATCH] Moved the hard-coded account code 3351 and 3353 to constants in the Account data model in the accounting application. --- accounting/models.py | 2 ++ accounting/views.py | 32 +++++++++++++++++--------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/accounting/models.py b/accounting/models.py index 3380fe3..c701fd6 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -52,6 +52,8 @@ class Account(DirtyFieldsMixin, models.Model): db_column="updatedby", related_name="updated_accounting_accounts") CASH = "1111" + ACCUMULATED_BALANCE = "3351" + NET_CHANGE = "3353" def __init__(self, *args, **kwargs): super(Account, self).__init__(*args, **kwargs) diff --git a/accounting/views.py b/accounting/views.py index 816c548..6f4401e 100644 --- a/accounting/views.py +++ b/accounting/views.py @@ -138,7 +138,7 @@ def cash(request, account, period): x.amount for x in records if not x.is_credit]) record_balance_before = Record( transaction=Transaction(date=period.start), - account=Account.objects.get(code="3351"), + account=Account.objects.get(code=Account.ACCUMULATED_BALANCE), is_credit=balance_before >= 0, amount=abs(balance_before), ) @@ -459,14 +459,14 @@ def journal(request, period): if sum_debits < sum_credits: debit_records.append(Record( transaction=Transaction(date=period.start), - account=Account.objects.get(code="3351"), + account=Account.objects.get(code=Account.ACCUMULATED_BALANCE), is_credit=False, amount=sum_credits - sum_debits )) elif sum_debits > sum_credits: credit_records.append(Record( transaction=Transaction(date=period.start), - account=Account.objects.get(code="3351"), + account=Account.objects.get(code=Account.ACCUMULATED_BALANCE), is_credit=True, amount=sum_debits - sum_credits )) @@ -533,7 +533,7 @@ def trial_balance(request, period): (Q(code__startswith="1") | Q(code__startswith="2") | Q(code__startswith="3")), - ~Q(code="3351")) + ~Q(code=Account.ACCUMULATED_BALANCE)) .annotate( balance=Sum(Case( When(record__is_credit=True, then=-1), @@ -554,13 +554,14 @@ def trial_balance(request, period): | Q(account__code__startswith="2") | Q(account__code__startswith="3"))) | (Q(transaction__date__lte=period.end) - & Q(account__code="3351"))) \ + & Q(account__code=Account.ACCUMULATED_BALANCE))) \ .aggregate( balance=Sum(Case( When(is_credit=True, then=-1), default=1) * F("amount")))["balance"] if balance is not None and balance != 0: - brought_forward = Account.objects.get(code="3351") + brought_forward = Account.objects.get( + code=Account.ACCUMULATED_BALANCE) if balance > 0: brought_forward.debit = balance brought_forward.credit = 0 @@ -633,7 +634,7 @@ def income_statement(request, period): "6": Account(title=pgettext("Accounting|", "Operating Income")), "7": Account(title=pgettext("Accounting|", "Before Tax Income")), "8": Account(title=pgettext("Accounting|", "After Tax Income")), - "9": Account.objects.get(code="3353"), + "9": Account.objects.get(code=Account.NET_CHANGE), } cumulative_total = 0 for section in sections: @@ -696,7 +697,7 @@ def balance_sheet(request, period): (Q(code__startswith="1") | Q(code__startswith="2") | Q(code__startswith="3")), - ~Q(code="3351")) + ~Q(code=Account.ACCUMULATED_BALANCE)) .annotate( balance=Sum(Case( When(record__is_credit=True, then=-1), @@ -711,13 +712,14 @@ def balance_sheet(request, period): & ~((Q(account__code__startswith="1") | Q(account__code__startswith="2") | Q(account__code__startswith="3")) - & ~Q(account__code="3351"))) \ + & ~Q(account__code=Account.ACCUMULATED_BALANCE))) \ .aggregate( balance=Sum(Case( When(is_credit=True, then=-1), default=1) * F("amount")))["balance"] if balance is not None and balance != 0: - brought_forward = Account.objects.get(code="3351") + brought_forward = Account.objects.get( + code=Account.ACCUMULATED_BALANCE) brought_forward.balance = balance brought_forward.url = reverse( "accounting:income-statement", args=(period,)) @@ -729,17 +731,17 @@ def balance_sheet(request, period): & ~((Q(account__code__startswith="1") | Q(account__code__startswith="2") | Q(account__code__startswith="3")) - & ~Q(account__code="3351"))) \ + & ~Q(account__code=Account.ACCUMULATED_BALANCE))) \ .aggregate( balance=Sum(Case( When(is_credit=True, then=-1), default=1) * F("amount")))["balance"] if balance is not None and balance != 0: - net_income = Account.objects.get(code="3353") - net_income.balance = balance - net_income.url = reverse( + net_change = Account.objects.get(code=Account.NET_CHANGE) + net_change.balance = balance + net_change.url = reverse( "accounting:income-statement", args=(period,)) - accounts.append(net_income) + accounts.append(net_change) for account in [x for x in accounts if x.code[0] in "23"]: account.balance = -account.balance groups = list(Account.objects.filter(