Added to validate if an existing accounting record is a correct debit or credit record in the transaction forms in the accounting application.

This commit is contained in:
依瑪貓 2020-08-02 12:01:20 +08:00
parent 3224a1d111
commit 70bc4e9662

View File

@ -86,7 +86,9 @@ class RecordForm(forms.Form):
ValidationError: When the validation fails. ValidationError: When the validation fails.
""" """
errors = [] errors = []
validators = [self._validate_transaction, self._validate_account_type] validators = [self._validate_transaction,
self._validate_account_type,
self._validate_is_credit]
for validator in validators: for validator in validators:
try: try:
validator() validator()
@ -147,6 +149,32 @@ class RecordForm(forms.Form):
self.add_error("account", error) self.add_error("account", error)
raise error raise error
def _validate_is_credit(self):
"""Validates whether debit and credit records are submitted correctly
as corresponding debit and credit records.
Raises:
ValidationError: When the validation fails.
"""
if "id" in self.errors:
return
if "id" not in self.data:
return
record = Record.objects.get(pk=self.data["id"])
if record.is_credit != self.is_credit:
if self.is_credit:
error = forms.ValidationError(
pgettext("Accounting|",
"This record is not a credit record."),
code="not_credit")
else:
error = forms.ValidationError(
pgettext("Accounting|",
"This record is not a debit record."),
code="not_debit")
self.add_error("id", error)
raise error
class TransactionForm(forms.Form): class TransactionForm(forms.Form):
"""A transaction form. """A transaction form.