diff --git a/accounting/utils.py b/accounting/utils.py index f39b235..3a9e3e5 100644 --- a/accounting/utils.py +++ b/accounting/utils.py @@ -21,10 +21,11 @@ import re from django.conf import settings +from django.core.exceptions import ValidationError from django.db.models import Q, Sum, Case, When, F, Count, Max, Min from django.urls import reverse from django.utils import timezone -from django.utils.translation import pgettext +from django.utils.translation import pgettext, gettext_noop from accounting.models import Account, Transaction, Record from mia_core.period import Period @@ -406,3 +407,30 @@ def sort_form_transaction_records(form): del form[x] for key in new_form.keys(): form[key] = new_form[key] + + +def validate_account_code(record): + """Validates the account code. + + Args: + record (Record): The accounting record. + + Exceptions: + ValidationError: Thrown when validation fails. + """ + if record.account.code is None: + raise ValidationError(gettext_noop( + "Please select the account.")) + if record.account.code == "": + raise ValidationError(gettext_noop( + "Please select the account.")) + try: + record.account = Account.objects.get(code=record.account.code) + except Account.DoesNotExist: + raise ValidationError(gettext_noop( + "This account does not exist.")) + child_account = Account.objects.filter( + code__startswith=record.account.code).first() + if child_account is not None: + raise ValidationError(gettext_noop( + "You cannot choose a parent account.")) diff --git a/accounting/views.py b/accounting/views.py index 0fff88b..5b92699 100644 --- a/accounting/views.py +++ b/accounting/views.py @@ -38,7 +38,8 @@ from mia_core.utils import Pagination, get_multi_lingual_search, UrlBuilder from .models import Record, Transaction, Account, RecordSummary from .utils import ReportUrl, get_cash_accounts, get_ledger_accounts, \ find_imbalanced, find_order_holes, fill_transaction_from_form, \ - sort_form_transaction_records, fill_transaction_from_previous_form + sort_form_transaction_records, fill_transaction_from_previous_form, \ + validate_account_code @method_decorator(require_GET, name="dispatch") @@ -870,24 +871,10 @@ def transaction_store(request, txn_type, transaction=None): for key in e.message_dict: errors[F"{record_type}-{no}-{key}"] = e.message_dict[key] # Validates the account - if x.account.code is None: - errors[F"{record_type}-{no}-account"] = gettext_noop( - "Please select the account.") - elif x.account.code == "": - errors[F"{record_type}-{no}-account"] = gettext_noop( - "Please select the account.") - else: - try: - x.account = Account.objects.get(code=x.account.code) - except Account.DoesNotExist: - errors[F"{record_type}-{no}-account"] = gettext_noop( - "This account does not exist.") - else: - child_account = Account.objects.filter( - code__startswith=x.account.code).first() - if child_account is not None: - errors[F"{record_type}-{no}-account"] = gettext_noop( - "You cannot choose a parent account.") + try: + validate_account_code(x) + except ValidationError as e: + errors[F"{record_type}-{no}-account"] = e.message # Validates the transaction if x.transaction is None: x.transaction = transaction