Moved the account validation rule to from the transaction_store() view to the validate_account_code() utility function in the accounting application.

This commit is contained in:
依瑪貓 2020-07-30 08:04:18 +08:00
parent 6e08093464
commit 69e8eed33f
2 changed files with 35 additions and 20 deletions

View File

@ -21,10 +21,11 @@
import re import re
from django.conf import settings 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.db.models import Q, Sum, Case, When, F, Count, Max, Min
from django.urls import reverse from django.urls import reverse
from django.utils import timezone 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 accounting.models import Account, Transaction, Record
from mia_core.period import Period from mia_core.period import Period
@ -406,3 +407,30 @@ def sort_form_transaction_records(form):
del form[x] del form[x]
for key in new_form.keys(): for key in new_form.keys():
form[key] = new_form[key] 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."))

View File

@ -38,7 +38,8 @@ from mia_core.utils import Pagination, get_multi_lingual_search, UrlBuilder
from .models import Record, Transaction, Account, RecordSummary from .models import Record, Transaction, Account, RecordSummary
from .utils import ReportUrl, get_cash_accounts, get_ledger_accounts, \ from .utils import ReportUrl, get_cash_accounts, get_ledger_accounts, \
find_imbalanced, find_order_holes, fill_transaction_from_form, \ 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") @method_decorator(require_GET, name="dispatch")
@ -870,24 +871,10 @@ def transaction_store(request, txn_type, transaction=None):
for key in e.message_dict: for key in e.message_dict:
errors[F"{record_type}-{no}-{key}"] = e.message_dict[key] errors[F"{record_type}-{no}-{key}"] = e.message_dict[key]
# Validates the account # 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: try:
x.account = Account.objects.get(code=x.account.code) validate_account_code(x)
except Account.DoesNotExist: except ValidationError as e:
errors[F"{record_type}-{no}-account"] = gettext_noop( errors[F"{record_type}-{no}-account"] = e.message
"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.")
# Validates the transaction # Validates the transaction
if x.transaction is None: if x.transaction is None:
x.transaction = transaction x.transaction = transaction