
from odoo import _, api, models
from odoo.exceptions import ValidationError


class ResPartner(models.Model):
    _inherit = "res.partner"

    @api.model
    def _get_vat_nrc_constrain_domain(self):
        domain = [
            ("company_id", "=", self.company_id),
            ("parent_id", "=", False),
            ("vat", "=", self.vat),
            "|",
            ("nrc", "=", self.nrc),
            ("nrc", "=", False),
        ]
        return domain

    @api.constrains("vat", "nrc")
    def _check_vat_nrc_unique(self):
        for record in self:
            if record.vat:
                domain = record._get_vat_nrc_constrain_domain()
                found = self.env["res.partner"].search(domain)
                if len(found) > 1:
                    raise ValidationError(
                        _("The VAT and NRC pair (%s, %s) must be unique ids=%s!")
                        % (record.vat, record.nrc, found.ids)
                    )


from odoo import _, api, models
from odoo.exceptions import ValidationError


class ResPartner(models.Model):
    _inherit = "res.partner"

    @api.model
    def _get_vat_nrc_constrain_domain(self):
        domain = [
            ("company_id", "=", self.company_id.id if self.company_id else False),
            ("parent_id", "=", False),
            ("vat", "=", self.vat),
            "|",
            ("nrc", "=", self.nrc),
            ("nrc", "=", False),
        ]
        return domain

    @api.constrains("vat", "nrc")
    def _check_vat_nrc_unique(self):
        for record in self:
            if record.vat:
                domain = record._get_vat_nrc_constrain_domain()
                found = self.env["res.partner"].search(domain)
                if len(found) > 1:
                    raise ValidationError(
                        _("The VAT and NRC pair (%s, %s) must be unique ids=%s!")
                        % (record.vat, record.nrc, found.ids)
                    )

Add vat unique per comapny
Add vat unique per comapny

  
  from odoo import _, api, models
  from odoo.exceptions import ValidationError
  
  
  class ResPartner(models.Model):
      _inherit = "res.partner"
  
      @api.model
      def _get_vat_nrc_constrain_domain(self):
          domain = [
-             ("company_id", "=", self.company_id),
+             ("company_id", "=", self.company_id.id if self.company_id else False),
              ("parent_id", "=", False),
              ("vat", "=", self.vat),
              "|",
              ("nrc", "=", self.nrc),
              ("nrc", "=", False),
          ]
          return domain
  
      @api.constrains("vat", "nrc")
      def _check_vat_nrc_unique(self):
          for record in self:
              if record.vat:
                  domain = record._get_vat_nrc_constrain_domain()
                  found = self.env["res.partner"].search(domain)
                  if len(found) > 1:
                      raise ValidationError(
                          _("The VAT and NRC pair (%s, %s) must be unique ids=%s!")
                          % (record.vat, record.nrc, found.ids)
                      )
  
Add vat unique per comapny
## Code Before:

from odoo import _, api, models
from odoo.exceptions import ValidationError


class ResPartner(models.Model):
    _inherit = "res.partner"

    @api.model
    def _get_vat_nrc_constrain_domain(self):
        domain = [
            ("company_id", "=", self.company_id),
            ("parent_id", "=", False),
            ("vat", "=", self.vat),
            "|",
            ("nrc", "=", self.nrc),
            ("nrc", "=", False),
        ]
        return domain

    @api.constrains("vat", "nrc")
    def _check_vat_nrc_unique(self):
        for record in self:
            if record.vat:
                domain = record._get_vat_nrc_constrain_domain()
                found = self.env["res.partner"].search(domain)
                if len(found) > 1:
                    raise ValidationError(
                        _("The VAT and NRC pair (%s, %s) must be unique ids=%s!")
                        % (record.vat, record.nrc, found.ids)
                    )

## Instruction:
Add vat unique per comapny
## Code After:

from odoo import _, api, models
from odoo.exceptions import ValidationError


class ResPartner(models.Model):
    _inherit = "res.partner"

    @api.model
    def _get_vat_nrc_constrain_domain(self):
        domain = [
            ("company_id", "=", self.company_id.id if self.company_id else False),
            ("parent_id", "=", False),
            ("vat", "=", self.vat),
            "|",
            ("nrc", "=", self.nrc),
            ("nrc", "=", False),
        ]
        return domain

    @api.constrains("vat", "nrc")
    def _check_vat_nrc_unique(self):
        for record in self:
            if record.vat:
                domain = record._get_vat_nrc_constrain_domain()
                found = self.env["res.partner"].search(domain)
                if len(found) > 1:
                    raise ValidationError(
                        _("The VAT and NRC pair (%s, %s) must be unique ids=%s!")
                        % (record.vat, record.nrc, found.ids)
                    )
