From 605277736e9c023c79c5b0f7594e8e0af09b9c23 Mon Sep 17 00:00:00 2001 From: clementthomas Date: Wed, 27 Mar 2024 10:58:22 +0100 Subject: [PATCH] [IMP] partner_phone_country_validation: bugfix --- .../models/res_partner.py | 53 ++++++++++++------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/partner_phone_country_validation/models/res_partner.py b/partner_phone_country_validation/models/res_partner.py index 5a1a8ee..e8562c4 100644 --- a/partner_phone_country_validation/models/res_partner.py +++ b/partner_phone_country_validation/models/res_partner.py @@ -9,31 +9,46 @@ class Partner(models.Model): _name = 'res.partner' _inherit = ['res.partner', 'phone.validation.mixin'] - @api.constrains('country_id') + @api.constrains('country_id','phone','mobile') def _check_country_id(self): - if not self.country_id and self.phone: + if not self.country_id and (self.phone or self.mobile): raise ValidationError(_('You must set a country for the phone number')) + + @api.onchange('country_id') + def _onchange_country(self): + self.format_mobile_from_country() + self.format_phone_from_country() - @api.onchange('phone', 'country_id', 'company_id') + def format_phone_from_country(self): + if self.phone: + if not self.phone.startswith("00") and not self.phone.startswith('+'): + self.phone = self.phone_format(self.phone) + + def format_mobile_from_country(self): + if self.mobile: + if not self.mobile.startswith("00") and not self.mobile.startswith('+'): + self.mobile = self.phone_format(self.mobile) + + def set_country_from_phone_number(self, phone_number): + country = self.env['res.country'].search( + [('code', '=', get_country_from_phone_number(phone_number))], limit=1 + ) + if not country and not self.country_id: + self.country_id = self.env.company.country_id + if country and not self.country_id: + self.country_id = country + + @api.onchange('phone') def _onchange_phone_validation(self): # If no country is found, we define the country based on the beginning of the number - if not self.country_id and self.phone: - self.country_id = self.env['res.country'].search( - [('code', '=', get_country_from_phone_number(self.phone))], limit=1 - ) if self.phone: - if self.phone.startswith("00") or self.phone.startswith('+'): - return - self.phone = self.phone_format(self.phone) + self.set_country_from_phone_number(self.phone) + self.format_phone_from_country() + - @api.onchange('mobile', 'country_id', 'company_id') + @api.onchange('mobile') def _onchange_mobile_validation(self): # If no country is found, we define the country based on the beginning of the number - if not self.country_id and self.mobile: - self.country_id = self.env['res.country'].search( - [('code', '=', get_country_from_phone_number(self.mobile))], limit=1 - ) - if self.mobile: - if self.mobile.startswith("00") or self.mobile.startswith('+'): - return - self.mobile = self.phone_format(self.mobile) + if self.mobile: + self.set_country_from_phone_number(self.mobile) + self.format_mobile_from_country() \ No newline at end of file