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.

40 lines
1.5 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_mobile_partner_id = fields.Many2one(
  8. "res.partner",
  9. compute="_compute_same_mobile_partner_id",
  10. string="Partner with same mobile",
  11. compute_sudo=True,
  12. )
  13. @api.depends("mobile", "company_id")
  14. def _compute_same_mobile_partner_id(self):
  15. # With phone_validation, the "mobile" field should be
  16. # clean in E.164 format, without any start/ending spaces
  17. # So we search on the 'mobile' field with '=' !
  18. for partner in self:
  19. same_mobile_partner_id = False
  20. if partner.mobile:
  21. domain = [("mobile", "=", partner.mobile)]
  22. if partner.company_id:
  23. domain += [
  24. "|",
  25. ("company_id", "=", False),
  26. ("company_id", "=", partner.company_id.id),
  27. ]
  28. partner_id = partner._origin.id
  29. if partner_id:
  30. domain.append(("id", "!=", partner_id))
  31. same_mobile_partner = self.with_context(active_test=False).search(
  32. domain, limit=1
  33. )
  34. same_mobile_partner_id = same_mobile_partner.id or False
  35. partner.same_mobile_partner_id = same_mobile_partner_id