Revised the transaction_store() view to validate the POSTed form in the accounting application.
This commit is contained in:
parent
5c2f5a0407
commit
63d83e0e4d
@ -21,6 +21,7 @@
|
|||||||
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 Sum, Case, When, F, Q
|
from django.db.models import Sum, Case, When, F, Q
|
||||||
from django.db.models.functions import TruncMonth, Coalesce
|
from django.db.models.functions import TruncMonth, Coalesce
|
||||||
from django.http import HttpResponseRedirect
|
from django.http import HttpResponseRedirect
|
||||||
@ -905,20 +906,77 @@ def transaction_store(request, type, transaction=None):
|
|||||||
form = request.POST.dict()
|
form = request.POST.dict()
|
||||||
sort_form_transaction_records(form)
|
sort_form_transaction_records(form)
|
||||||
fill_transaction_from_form(transaction, form)
|
fill_transaction_from_form(transaction, form)
|
||||||
# TODO: To be done.
|
errors = {}
|
||||||
|
try:
|
||||||
|
transaction.full_clean(exclude=[
|
||||||
|
"sn",
|
||||||
|
"created_by",
|
||||||
|
"updated_by",
|
||||||
|
])
|
||||||
|
except ValidationError as e:
|
||||||
|
errors = e.message_dict
|
||||||
|
records = {
|
||||||
|
"debit": transaction.debit_records,
|
||||||
|
"credit": transaction.credit_records,
|
||||||
|
}
|
||||||
|
for record_type in records.keys():
|
||||||
|
no = 0
|
||||||
|
for x in records[record_type]:
|
||||||
|
no = no + 1
|
||||||
|
try:
|
||||||
|
x.full_clean(exclude=[
|
||||||
|
"sn",
|
||||||
|
"transaction",
|
||||||
|
"account",
|
||||||
|
"created_by",
|
||||||
|
"updated_by",
|
||||||
|
])
|
||||||
|
except ValidationError as e:
|
||||||
|
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.")
|
||||||
|
# Validates the transaction
|
||||||
|
if x.transaction is None:
|
||||||
|
x.transaction = transaction
|
||||||
|
if transaction.pk is None:
|
||||||
|
if x.transaction.pk is not None:
|
||||||
|
errors[F"{record_type}-{no}-transaction"] = gettext_noop(
|
||||||
|
"This record is not of the same transaction.")
|
||||||
|
else:
|
||||||
|
if x.transaction.pk is None:
|
||||||
|
pass
|
||||||
|
elif x.transaction.pk != transaction.pk:
|
||||||
|
errors[F"{record_type}-{no}-transaction"] = gettext_noop(
|
||||||
|
"This record is not of the same transaction.")
|
||||||
|
if len(errors) > 0:
|
||||||
if transaction.pk is None:
|
if transaction.pk is None:
|
||||||
url = reverse("accounting:transactions.create", args=(type,))
|
url = reverse("accounting:transactions.create", args=(type,))
|
||||||
else:
|
else:
|
||||||
url = reverse("accounting:transactions.edit", args=(type, transaction))
|
url = reverse(
|
||||||
|
"accounting:transactions.edit", args=(type, transaction))
|
||||||
return error_redirect(
|
return error_redirect(
|
||||||
request,
|
request,
|
||||||
str(UrlBuilder(url).add_param("r", request.GET.get("r"))),
|
str(UrlBuilder(url).add_param("r", request.GET.get("r"))),
|
||||||
form,
|
form,
|
||||||
{
|
errors)
|
||||||
"date": "The date is error.",
|
|
||||||
"debit-2-amount": "The amount is error.",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
return success_redirect(
|
return success_redirect(
|
||||||
request,
|
request,
|
||||||
str(UrlBuilder(reverse("accounting:transactions.show",
|
str(UrlBuilder(reverse("accounting:transactions.show",
|
||||||
|
Loading…
Reference in New Issue
Block a user