From f6ecc778a056a0d4aff4da4d4c3a93cc5a263063 Mon Sep 17 00:00:00 2001 From: clementthomas Date: Fri, 29 Mar 2024 12:04:13 +0100 Subject: [PATCH] [IMP] partner_phone_country_validation: quick fix --- .../models/res_partner.py | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/partner_phone_country_validation/models/res_partner.py b/partner_phone_country_validation/models/res_partner.py index e8562c4..7a35660 100644 --- a/partner_phone_country_validation/models/res_partner.py +++ b/partner_phone_country_validation/models/res_partner.py @@ -19,22 +19,39 @@ class Partner(models.Model): self.format_mobile_from_country() self.format_phone_from_country() + def format_number_zerozero(self, number): + if number.startswith("00"): + number = "+"+number[2:] + return number + 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) + if self.phone: + country = None + if self.phone.startswith('+'): + country = self.env['res.country'].search( + [('code', '=', get_country_from_phone_number(self.phone))], limit=1 + ) + if self.country_id: + country = self.country_id + if country: + self.phone = self.phone_format(self.phone, country=country) 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) + if self.mobile: + country = None + if self.mobile.startswith('+'): + country = self.env['res.country'].search( + [('code', '=', get_country_from_phone_number(self.mobile))], limit=1 + ) + if self.country_id: + country = self.country_id + if country: + self.mobile = self.phone_format(self.mobile, country=country) 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 @@ -42,6 +59,7 @@ class Partner(models.Model): def _onchange_phone_validation(self): # If no country is found, we define the country based on the beginning of the number if self.phone: + self.phone = self.format_number_zerozero(self.phone) self.set_country_from_phone_number(self.phone) self.format_phone_from_country() @@ -50,5 +68,6 @@ class Partner(models.Model): def _onchange_mobile_validation(self): # If no country is found, we define the country based on the beginning of the number if self.mobile: + self.mobile = self.format_number_zerozero(self.mobile) self.set_country_from_phone_number(self.mobile) self.format_mobile_from_country() \ No newline at end of file