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.

39 lines
1.4 KiB

  1. # Copyright 2022 Ooops Ashish Hirpara <https://ooops404.com>
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. import logging
  4. from odoo import _, api, models
  5. from odoo.exceptions import UserError
  6. _logger = logging.getLogger(__name__)
  7. class ResPartner(models.Model):
  8. _inherit = "res.partner"
  9. @api.constrains("mobile")
  10. def _check_mobile_unique(self):
  11. if self.env.company.partner_mobile_unique_filter_duplicates:
  12. for partner in self:
  13. if partner.mobile:
  14. domain = [("mobile", "=", partner.mobile)]
  15. if partner.company_id:
  16. domain += [
  17. "|",
  18. ("company_id", "=", False),
  19. ("company_id", "=", partner.company_id.id),
  20. ]
  21. partner_id = partner._origin.id
  22. if partner_id:
  23. domain += [
  24. ("id", "!=", partner.id),
  25. ]
  26. if self.search(domain):
  27. raise UserError(
  28. _(
  29. "The mobile number is already exists for another partner."
  30. " This is not supported when duplicate mobile numbers are "
  31. "not allowed."
  32. )
  33. )