2 Commits

Author SHA1 Message Date
clementthomas
6dac419d5e [IMP] partner_phone_country_validation:
bug fix on partner creation from lead
2024-06-24 09:27:36 +02:00
clementthomas
6c5411a2eb [IMP] partner_phone_country_validation:
fix partner creation with parent
2024-06-20 10:01:19 +02:00

View File

@@ -11,8 +11,25 @@ class Partner(models.Model):
@api.constrains('country_id','phone','mobile') @api.constrains('country_id','phone','mobile')
def _check_country_id(self): def _check_country_id(self):
if not self.country_id and (self.phone or self.mobile):
raise ValidationError(_('You must set a country for the phone number')) #if come back quote creation from lead, update partner
country = False
if self._context.get("default_type") == 'opportunity' and self._context.get("params").get("model") == "crm.lead":
lead = self.env["crm.lead"].browse(self._context.get("params").get("id"))
country = lead.country_id
for partner in self:
if not country and (partner.phone or partner.mobile):
if self.phone:
partner.set_country_from_phone_number(partner.phone)
if self.mobile:
partner.set_country_from_phone_number(partner.mobile)
elif country:
partner.country_id = country
#check country and raise error
for partner in self:
if not partner.country_id and not partner.parent_id.country_id and (partner.phone or partner.mobile):
raise ValidationError(_('You must set a country for the phone number of %s'%(partner.name,)))
@api.onchange('country_id') @api.onchange('country_id')
def _onchange_country(self): def _onchange_country(self):