bemade_partner_email_domain: fix issue where qc.ca emails matched too many partners

This commit is contained in:
Marc Durepos 2024-11-19 13:32:42 -05:00
parent 4163232371
commit f542d8fbf9
2 changed files with 19 additions and 16 deletions

View file

@ -1,6 +1,6 @@
{ {
'name': 'Automated Partner Association by Email Domain', 'name': 'Automated Partner Association by Email Domain',
'version': '17.0.0.0.0', 'version': '17.0.0.0.1',
'category': 'Extra Tools', 'category': 'Extra Tools',
'summary': 'Automatically associates partners with companies using matching email domains', 'summary': 'Automatically associates partners with companies using matching email domains',
'description': """ 'description': """

View file

@ -1,4 +1,5 @@
from odoo import api, fields, models, Command from odoo import api, fields, models, Command
from odoo.tools.sql import SQL
import uuid import uuid
from odoo.addons.website.controllers.main import Website from odoo.addons.website.controllers.main import Website
@ -37,7 +38,6 @@ class Partner(models.Model):
) )
template.with_context(links=links).send_mail(self.id, force_send=True) template.with_context(links=links).send_mail(self.id, force_send=True)
# @api.onchange('email')
def _check_parent_from_email_domain(self): def _check_parent_from_email_domain(self):
for rec in self: for rec in self:
if rec.parent_id: if rec.parent_id:
@ -52,21 +52,24 @@ class Partner(models.Model):
# If there's no '@' symbol, the email address is invalid # If there's no '@' symbol, the email address is invalid
return False return False
# Loop while there's more than one part in the domain (e.g., subdomain.domain.tld) sql = """
while "." in email_domain: SELECT id
# If the current email domain matches the main domain, return True FROM res_partner
company_domain = self.env["res.partner"].search( WHERE email_domain IS NOT NULL
[("email_domain", "ilike", email_domain)] AND email_domain = RIGHT(%s, LENGTH(email_domain))
"""
self.env.cr.execute(SQL(sql, email_domain))
res = self.env.cr.fetchall()
if len(res) > 1:
rec._send_selection_email(
self.env["res.partner"].search(
[("id", "in", [result[0] for result in res])]
)
) )
if company_domain: elif len(res) == 1:
if len(company_domain) > 1: rec.write({"parent_id": res[0][0]})
rec._send_selection_email(company_domain) else:
else: continue
rec.parent_id = company_domain.id
return
# If not, drop the part before the first '.' to check the next level
email_domain = email_domain.split(".", 1)[1]
@api.model_create_multi @api.model_create_multi
def create(self, vals_list): def create(self, vals_list):