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.1 KiB

  1. # Copyright 2021 Camptocamp SA
  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 ResPartnerIdCategory(models.Model):
  6. _inherit = "res.partner.id_category"
  7. has_unique_numbers = fields.Boolean(
  8. string="Enforce unicity",
  9. help="When set, duplicate numbers will not be allowed for this category.",
  10. )
  11. @api.constrains("has_unique_numbers")
  12. def validate_must_be_unique(self):
  13. for rec in self:
  14. if not rec.has_unique_numbers:
  15. continue
  16. ids = self.env["res.partner.id_number"].search(
  17. [("category_id", "in", rec.ids)]
  18. )
  19. unique_numbers = set(ids.mapped("name"))
  20. if len(ids) != len(unique_numbers):
  21. raise ValidationError(
  22. _(
  23. "The category {} can not be set to use unique numbers, "
  24. "because it already contains duplicates."
  25. ).format(rec.name)
  26. )