diff --git a/pms/models/pms_checkin_partner.py b/pms/models/pms_checkin_partner.py index b0743f28e..5d9cc58b0 100644 --- a/pms/models/pms_checkin_partner.py +++ b/pms/models/pms_checkin_partner.py @@ -237,6 +237,7 @@ class PmsCheckinPartner(models.Model): index=True, comodel_name="res.partner.id_category", compute="_compute_document_type", + domain="['|', ('country_ids', '=', False), ('country_ids', 'in', document_country_id)]", ) document_expedition_date = fields.Date( string="Expedition Date", @@ -257,6 +258,15 @@ class PmsCheckinPartner(models.Model): 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( string="partner_incongruences", help="indicates that some partner fields \ @@ -279,29 +289,46 @@ class PmsCheckinPartner(models.Model): @api.depends("partner_id") def _compute_document_number(self): for record in self: - if not record.document_number: - if record.partner_id.id_numbers: - record.document_number = record.partner_id.id_numbers[0].name + if not record.document_number 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].name: + record.document_number = last_update_document[0].name @api.depends("partner_id") def _compute_document_type(self): for record in self: - if record.partner_id and record.partner_id.id_numbers: - if not record.document_type: - if record.partner_id.id_numbers: - record.document_type = record.partner_id.id_numbers[ - 0 - ].category_id + if not record.document_type 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].category_id: + record.document_type = last_update_document[0].category_id @api.depends("partner_id") def _compute_document_expedition_date(self): for record in self: - if not record.document_expedition_date: - record.document_expedition_date = False - if record.partner_id and record.partner_id.id_numbers: - record.document_expedition_date = record.partner_id.id_numbers[ - 0 - ].valid_from + if not record.document_expedition_date 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].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") def _compute_firstname(self): @@ -391,10 +418,6 @@ class PmsCheckinPartner(models.Model): and 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") def _compute_residence_state_id(self): @@ -420,7 +443,7 @@ class PmsCheckinPartner(models.Model): elif any( not getattr(record, field) for field in record._checkin_mandatory_fields( - country=record.nationality_id + country=record.document_country_id ) ): record.state = "draft" @@ -694,6 +717,19 @@ class PmsCheckinPartner(models.Model): % (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 def create(self, vals): # The checkin records are created automatically from adult depends diff --git a/pms/models/res_partner_id_number.py b/pms/models/res_partner_id_number.py index 2818038ee..f2f6b183e 100644 --- a/pms/models/res_partner_id_number.py +++ b/pms/models/res_partner_id_number.py @@ -6,7 +6,6 @@ # Antonio Espinosa # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - from odoo import _, api, fields, models from odoo.exceptions import ValidationError @@ -32,6 +31,15 @@ class ResPartnerIdNumber(models.Model): 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") def _compute_name(self): 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: 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") def _check_category_id_unique(self): for record in self: @@ -106,3 +132,15 @@ class ResPartnerIdNumber(models.Model): ) if len(id_number) > 1: 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") + )