diff --git a/src/accounting/account/views.py b/src/accounting/account/views.py index 6fae6ac..e7d2463 100644 --- a/src/accounting/account/views.py +++ b/src/accounting/account/views.py @@ -157,6 +157,9 @@ def delete_account(account: Account) -> redirect: :return: The redirection to the account list on success, or the account detail on error. """ + if not account.can_delete: + flash(s(lazy_gettext("The account cannot be deleted.")), "error") + return redirect(inherit_next(__get_detail_uri(account))) account.delete() sort_accounts_in(account.base_code, account.id) db.session.commit() diff --git a/src/accounting/models.py b/src/accounting/models.py index 4fd1236..7c8ddab 100644 --- a/src/accounting/models.py +++ b/src/accounting/models.py @@ -236,6 +236,16 @@ class Account(db.Model): return True return False + @property + def can_delete(self) -> bool: + """Returns whether the account can be deleted. + + :return: True if the account can be deleted, or False otherwise. + """ + if self.code in {"1111-001", "3351-001", "3353-001"}: + return False + return len(self.line_items) == 0 + def delete(self) -> None: """Deletes this account. diff --git a/src/accounting/templates/accounting/account/detail.html b/src/accounting/templates/accounting/account/detail.html index cf6be43..cd28b37 100644 --- a/src/accounting/templates/accounting/account/detail.html +++ b/src/accounting/templates/accounting/account/detail.html @@ -41,10 +41,17 @@ First written: 2023/1/31 {{ A_("Order") }} {% if accounting_can_edit() %} - + {% if obj.can_delete %} + + {% else %} + + {% endif %} {% endif %} @@ -56,7 +63,7 @@ First written: 2023/1/31 {% endif %} -{% if accounting_can_edit() %} +{% if accounting_can_edit() and obj.can_delete %}