[IMP] partner_phone_country_validation:

bugfix
This commit is contained in:
clementthomas
2024-03-27 10:58:22 +01:00
parent d100cf91b6
commit 605277736e

View File

@@ -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()