You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.9 KiB

  1. # Copyright 2021 Akretion France (http://www.akretion.com/)
  2. # @author: Alexis de Lattre <alexis.delattre@akretion.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo import api, fields, models
  5. class ResPartner(models.Model):
  6. _inherit = "res.partner"
  7. same_email_partner_id = fields.Many2one(
  8. "res.partner",
  9. compute="_compute_same_email_partner_id",
  10. string="Partner with same e-mail",
  11. compute_sudo=True,
  12. )
  13. @api.depends("email", "company_id")
  14. def _compute_same_email_partner_id(self):
  15. for partner in self:
  16. same_email_partner_id = False
  17. if partner.email and partner.email.strip():
  18. partner_email = partner.email.strip().lower()
  19. domain = [("email", "=ilike", "%" + partner_email + "%")]
  20. if partner.company_id:
  21. domain += [
  22. "|",
  23. ("company_id", "=", False),
  24. ("company_id", "=", partner.company_id.id),
  25. ]
  26. partner_id = partner._origin.id
  27. if partner_id:
  28. domain += [
  29. ("id", "!=", partner_id),
  30. "!",
  31. ("id", "child_of", partner_id),
  32. "!",
  33. ("id", "parent_of", partner_id),
  34. ]
  35. search_partners = self.with_context(active_test=False).search(domain)
  36. for search_partner in search_partners:
  37. if (
  38. search_partner.email
  39. and search_partner.email.strip().lower() == partner_email
  40. ):
  41. same_email_partner_id = search_partner
  42. break
  43. partner.same_email_partner_id = same_email_partner_id