[IMP]pms:improve country document compute

This commit is contained in:
Darío Lodeiros
2024-04-17 10:52:18 +02:00
parent 29eb5bcd92
commit 53e4b23f76
2 changed files with 95 additions and 21 deletions

View File

@@ -237,6 +237,7 @@ class PmsCheckinPartner(models.Model):
index=True, index=True,
comodel_name="res.partner.id_category", comodel_name="res.partner.id_category",
compute="_compute_document_type", compute="_compute_document_type",
domain="['|', ('country_ids', '=', False), ('country_ids', 'in', document_country_id)]",
) )
document_expedition_date = fields.Date( document_expedition_date = fields.Date(
string="Expedition Date", string="Expedition Date",
@@ -257,6 +258,15 @@ class PmsCheckinPartner(models.Model):
ondelete="restrict", ondelete="restrict",
) )
document_country_id = fields.Many2one(
string="Document Country",
help="Country of the document",
comodel_name="res.country",
compute="_compute_document_country_id",
store=True,
readonly=False,
)
partner_incongruences = fields.Char( partner_incongruences = fields.Char(
string="partner_incongruences", string="partner_incongruences",
help="indicates that some partner fields \ help="indicates that some partner fields \
@@ -279,29 +289,46 @@ class PmsCheckinPartner(models.Model):
@api.depends("partner_id") @api.depends("partner_id")
def _compute_document_number(self): def _compute_document_number(self):
for record in self: for record in self:
if not record.document_number: if not record.document_number and record.partner_id.id_numbers:
if record.partner_id.id_numbers: last_update_document = record.partner_id.id_numbers.filtered(
record.document_number = record.partner_id.id_numbers[0].name lambda x: x.write_date
== max(record.partner_id.id_numbers.mapped("write_date"))
)
if last_update_document and last_update_document[0].name:
record.document_number = last_update_document[0].name
@api.depends("partner_id") @api.depends("partner_id")
def _compute_document_type(self): def _compute_document_type(self):
for record in self: for record in self:
if record.partner_id and record.partner_id.id_numbers: if not record.document_type and record.partner_id.id_numbers:
if not record.document_type: last_update_document = record.partner_id.id_numbers.filtered(
if record.partner_id.id_numbers: lambda x: x.write_date
record.document_type = record.partner_id.id_numbers[ == max(record.partner_id.id_numbers.mapped("write_date"))
0 )
].category_id if last_update_document and last_update_document[0].category_id:
record.document_type = last_update_document[0].category_id
@api.depends("partner_id") @api.depends("partner_id")
def _compute_document_expedition_date(self): def _compute_document_expedition_date(self):
for record in self: for record in self:
if not record.document_expedition_date: if not record.document_expedition_date and record.partner_id.id_numbers:
record.document_expedition_date = False last_update_document = record.partner_id.id_numbers.filtered(
if record.partner_id and record.partner_id.id_numbers: lambda x: x.write_date
record.document_expedition_date = record.partner_id.id_numbers[ == max(record.partner_id.id_numbers.mapped("write_date"))
0 )
].valid_from if last_update_document and last_update_document[0].valid_from:
record.document_expedition_date = last_update_document[0].valid_from
@api.depends("partner_id")
def _compute_document_country_id(self):
for record in self:
if not record.document_country_id and record.partner_id.id_numbers:
last_update_document = record.partner_id.id_numbers.filtered(
lambda x: x.write_date
== max(record.partner_id.id_numbers.mapped("write_date"))
)
if last_update_document and last_update_document[0].country_id:
record.document_country_id = last_update_document[0].country_id
@api.depends("partner_id") @api.depends("partner_id")
def _compute_firstname(self): def _compute_firstname(self):
@@ -391,10 +418,6 @@ class PmsCheckinPartner(models.Model):
and record.partner_id.residence_country_id and record.partner_id.residence_country_id
): ):
record.residence_country_id = record.partner_id.residence_country_id record.residence_country_id = record.partner_id.residence_country_id
elif not record.residence_country_id and record.nationality_id:
record.residence_country_id = record.nationality_id
elif not record.residence_country_id:
record.residence_country_id = False
@api.depends("partner_id") @api.depends("partner_id")
def _compute_residence_state_id(self): def _compute_residence_state_id(self):
@@ -420,7 +443,7 @@ class PmsCheckinPartner(models.Model):
elif any( elif any(
not getattr(record, field) not getattr(record, field)
for field in record._checkin_mandatory_fields( for field in record._checkin_mandatory_fields(
country=record.nationality_id country=record.document_country_id
) )
): ):
record.state = "draft" record.state = "draft"
@@ -694,6 +717,19 @@ class PmsCheckinPartner(models.Model):
% (record.document_number, record.document_type.name) % (record.document_number, record.document_type.name)
) )
@api.constrains("document_country_id", "document_type")
def _check_document_country_id_document_type_consistence(self):
for record in self:
if record.document_country_id and record.document_type:
if (
record.document_type.country_ids
and record.document_country_id
not in record.document_type.country_ids
):
raise ValidationError(
_("Document type and country of document do not match")
)
@api.model @api.model
def create(self, vals): def create(self, vals):
# The checkin records are created automatically from adult depends # The checkin records are created automatically from adult depends

View File

@@ -6,7 +6,6 @@
# Antonio Espinosa <antonioea@antiun.com> # Antonio Espinosa <antonioea@antiun.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import _, api, fields, models from odoo import _, api, fields, models
from odoo.exceptions import ValidationError from odoo.exceptions import ValidationError
@@ -32,6 +31,15 @@ class ResPartnerIdNumber(models.Model):
compute="_compute_valid_from", compute="_compute_valid_from",
) )
country_id = fields.Many2one(
string="Country",
comodel_name="res.country",
help="Country of the document",
compute="_compute_country_id",
store=True,
readonly=False,
)
@api.depends("partner_id", "partner_id.pms_checkin_partner_ids.document_number") @api.depends("partner_id", "partner_id.pms_checkin_partner_ids.document_number")
def _compute_name(self): def _compute_name(self):
if hasattr(super(), "_compute_name"): if hasattr(super(), "_compute_name"):
@@ -95,6 +103,24 @@ class ResPartnerIdNumber(models.Model):
if last_update_category_id and last_update_category_id[0].document_type: if last_update_category_id and last_update_category_id[0].document_type:
record.category_id = last_update_category_id[0].document_type record.category_id = last_update_category_id[0].document_type
@api.depends("partner_id", "partner_id.pms_checkin_partner_ids.document_country_id")
def _compute_country_id(self):
for record in self:
if record.partner_id.pms_checkin_partner_ids:
last_update_document = (
record.partner_id.pms_checkin_partner_ids.filtered(
lambda x: x.document_id == record
and x.write_date
== max(
record.partner_id.pms_checkin_partner_ids.mapped(
"write_date"
)
)
)
)
if last_update_document and last_update_document[0].document_country_id:
record.country_id = last_update_document[0].document_country_id
@api.constrains("partner_id", "category_id") @api.constrains("partner_id", "category_id")
def _check_category_id_unique(self): def _check_category_id_unique(self):
for record in self: for record in self:
@@ -106,3 +132,15 @@ class ResPartnerIdNumber(models.Model):
) )
if len(id_number) > 1: if len(id_number) > 1:
raise ValidationError(_("Partner already has this document type")) raise ValidationError(_("Partner already has this document type"))
@api.constrains("country_id", "category_id")
def _check_document_country_id_category_id_consistence(self):
for record in self:
if record.category_id and record.country_id:
if (
record.category_id.country_ids
and record.country_id not in record.category_id.country_ids
):
raise ValidationError(
_("Country is not allowed for this document type")
)