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.

38 lines
1.4 KiB

  1. # Copyright 2016 Antonio Espinosa
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo import _, api, fields, models
  4. from odoo.exceptions import ValidationError
  5. class ResPartner(models.Model):
  6. _inherit = "res.partner"
  7. # This related is needed in order to trigger the check when changing the
  8. # value on res.company
  9. partner_ref_unique = fields.Selection(
  10. related='company_id.partner_ref_unique', store=True,
  11. )
  12. @api.multi
  13. @api.constrains('ref', 'is_company', 'company_id', 'partner_ref_unique')
  14. def _check_ref(self):
  15. for partner in self:
  16. mode = partner.partner_ref_unique
  17. if (partner.ref and (
  18. mode == 'all' or
  19. (mode == 'companies' and partner.is_company))):
  20. domain = [
  21. ('id', '!=', partner.id),
  22. ('ref', '=', partner.ref),
  23. ]
  24. if mode == 'companies':
  25. domain.append(('is_company', '=', True))
  26. other = self.search(domain)
  27. # active_test is False when called from
  28. # base.partner.merge.automatic.wizard
  29. if other and self.env.context.get("active_test", True):
  30. raise ValidationError(
  31. _("This reference is equal to partner '%s'") %
  32. other[0].display_name)