From da3e7912dece0816af63a4aee25e96fce9c85c7f 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:13:04 +0800 Subject: [PATCH] Moved the account code of cash from hard-coded in the codes to a class constant in the Account data model in the accounting application. --- accounting/models.py | 5 +++-- accounting/utils.py | 9 +++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/accounting/models.py b/accounting/models.py index 8f50b04..3380fe3 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -51,6 +51,7 @@ class Account(DirtyFieldsMixin, models.Model): settings.AUTH_USER_MODEL, on_delete=models.PROTECT, db_column="updatedby", related_name="updated_accounting_accounts") + CASH = "1111" def __init__(self, *args, **kwargs): super(Account, self).__init__(*args, **kwargs) @@ -225,7 +226,7 @@ class Transaction(DirtyFieldsMixin, models.Model): """Whether this transaction is a cash income transaction.""" debit_records = self.debit_records return (len(debit_records) == 1 - and debit_records[0].account.code == "1111" + and debit_records[0].account.code == Account.CASH and debit_records[0].summary is None) @property @@ -233,7 +234,7 @@ class Transaction(DirtyFieldsMixin, models.Model): """Whether this transaction is a cash expense transaction.""" credit_records = self.credit_records return (len(credit_records) == 1 - and credit_records[0].account.code == "1111" + and credit_records[0].account.code == Account.CASH and credit_records[0].summary is None) @property diff --git a/accounting/utils.py b/accounting/utils.py index 2c26339..5b3e5cd 100644 --- a/accounting/utils.py +++ b/accounting/utils.py @@ -185,7 +185,8 @@ class Populator: the credit records. """ amount = sum([x[2] for x in credit]) - self.add_transfer_transaction(date, (("1111", None, amount),), credit) + self.add_transfer_transaction( + date, ((Account.CASH, None, amount),), credit) def add_expense_transaction(self, date, debit): """Adds a cash income transaction. @@ -197,7 +198,8 @@ class Populator: the debit records. """ amount = sum([x[2] for x in debit]) - self.add_transfer_transaction(date, debit, (("1111", None, amount),)) + self.add_transfer_transaction( + date, debit, ((Account.CASH, None, amount),)) def get_cash_accounts(): @@ -325,8 +327,7 @@ def fill_txn_from_post(txn_type, txn, post): else: record = txn.debit_records[0] record.ord = 1 - # TODO: Store 1111 in the settings file - record.account = Account.objects.get(code="1111") + record.account = Account.objects.get(code=Account.CASH) record.summary = None record.amount = sum([x.amount for x in records]) records.append(record)