[IMP]pms: validate number doc in checkin partner model

This commit is contained in:
Dario Lodeiros
2021-07-04 11:52:06 +02:00
parent d277736ef4
commit e043d83582
2 changed files with 41 additions and 25 deletions

View File

@@ -6,7 +6,8 @@ import json
import re
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.exceptions import UserError, ValidationError
from odoo.tools.safe_eval import safe_eval
class PmsCheckinPartner(models.Model):
@@ -470,6 +471,45 @@ class PmsCheckinPartner(models.Model):
if record.document_number != number.name:
raise ValidationError(_("Document_type has already exists"))
def _validation_eval_context(self, id_number):
self.ensure_one()
return {"self": self, "id_number": id_number}
@api.constrains("document_number", "document_type")
def validate_id_number(self):
"""Validate the given ID number
The method raises an odoo.exceptions.ValidationError if the eval of
python validation code fails
"""
for record in self:
if record.document_number and record.document_type:
if (
self.env.context.get("id_no_validate")
or not record.document_type.validation_code
):
return
eval_context = record._validation_eval_context(record.document_number)
try:
safe_eval(
record.document_type.validation_code,
eval_context,
mode="exec",
nocopy=True,
)
except Exception as e:
raise UserError(
_(
"Error when evaluating the id_category validation code:"
":\n %s \n(%s)"
)
% (self.name, e)
)
if eval_context.get("failed", False):
raise ValidationError(
_("%s is not a valid %s identifier")
% (record.document_number, record.document_type.name)
)
@api.model
def create(self, vals):
# The checkin records are created automatically from adult depends

View File

@@ -319,30 +319,6 @@ class ResPartner(models.Model):
]
)
@api.model
def name_search(self, name, args=None, operator="ilike", limit=100):
if not args:
args = []
domain = [
"|",
("mobile", operator, name),
]
partners = self.search(
domain + args,
limit=limit,
)
res = partners.name_get()
if limit:
limit_rest = limit - len(partners)
else:
limit_rest = limit
if limit_rest or not limit:
args += [("id", "not in", partners.ids)]
res += super(ResPartner, self).name_search(
name, args=args, operator=operator, limit=limit_rest
)
return res
@api.constrains("is_agency", "sale_channel_id")
def _check_is_agency(self):
for record in self: