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.

31 lines
1.3 KiB

  1. # Copyright 2016 Antonio Espinosa
  2. # Copyright 2020 Tecnativa - João Marques
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import _, api, models
  5. from odoo.exceptions import ValidationError
  6. class ResPartner(models.Model):
  7. _inherit = "res.partner"
  8. @api.constrains("ref", "is_company", "company_id")
  9. def _check_ref(self):
  10. for partner in self.filtered("ref"):
  11. # If the company is not defined in the partner, take current user company
  12. company = partner.company_id or self.env.company
  13. mode = company.partner_ref_unique
  14. if mode == "all" or (mode == "companies" and partner.is_company):
  15. domain = [
  16. ("id", "!=", partner.id),
  17. ("ref", "=", partner.ref),
  18. ]
  19. if mode == "companies":
  20. domain.append(("is_company", "=", True))
  21. other = self.search(domain)
  22. # Don't raise when coming from contact merge wizard or no duplicates
  23. if other and not self.env.context.get("partner_ref_unique_merging"):
  24. raise ValidationError(
  25. _("This reference is equal to partner '%s'")
  26. % other[0].display_name
  27. )