diff --git a/l10n_it_fatturapa_in/tests/test_import_fatturapa_xml.py b/l10n_it_fatturapa_in/tests/test_import_fatturapa_xml.py index b99f455519b9..61895c9753b8 100644 --- a/l10n_it_fatturapa_in/tests/test_import_fatturapa_xml.py +++ b/l10n_it_fatturapa_in/tests/test_import_fatturapa_xml.py @@ -1005,7 +1005,7 @@ def test_56_xml_import_vat_group(self): ) existing_partners.update({ 'vat': 'IT12345670017', - 'fiscalcode': '1234567890123456', + 'fiscalcode': 'TSTCOA80R07I829X', }) # Act: Import the XMLs, diff --git a/l10n_it_fiscalcode/model/res_partner.py b/l10n_it_fiscalcode/model/res_partner.py index 7d330397cb4f..86d152f1f80f 100644 --- a/l10n_it_fiscalcode/model/res_partner.py +++ b/l10n_it_fiscalcode/model/res_partner.py @@ -1,12 +1,15 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo import models, fields, api +from codicefiscale import isvalid + +from odoo import _, models, fields, api +from odoo.exceptions import ValidationError class ResPartner(models.Model): _inherit = 'res.partner' - @api.multi + @api.constrains('fiscalcode') def check_fiscalcode(self): for partner in self: if not partner.fiscalcode: @@ -20,18 +23,18 @@ def check_fiscalcode(self): # Perform the same check as Company case continue if len(partner.fiscalcode) != 16: - # Check fiscalcode of a person - return False + # Check fiscalcode length of a person + msg = _("The fiscal code must have 16 characters.") + raise ValidationError(msg) + if not isvalid(partner.fiscalcode): + # Check fiscalcode validity + msg = _("The fiscal code isn't valid.") + raise ValidationError(msg) return True fiscalcode = fields.Char( 'Fiscal Code', size=16, help="Italian Fiscal Code") - _constraints = [ - (check_fiscalcode, - "The fiscal code doesn't seem to be correct.", ["fiscalcode"]) - ] - @api.onchange('fiscalcode') def _fiscalcode_changed(self): if self.fiscalcode: diff --git a/l10n_it_fiscalcode/tests/test_fiscalcode.py b/l10n_it_fiscalcode/tests/test_fiscalcode.py index ffb1fc1c7fe6..c231d19fe6aa 100644 --- a/l10n_it_fiscalcode/tests/test_fiscalcode.py +++ b/l10n_it_fiscalcode/tests/test_fiscalcode.py @@ -27,7 +27,7 @@ def test_fiscalcode_compute(self): self.assertEqual(self.partner.fiscalcode, 'RSSMRA84H04H501X') def test_fiscalcode_check(self): - # Wrong FC + # Wrong FC length with self.assertRaises(ValidationError): self.env['res.partner'].create({ 'name': 'Person', @@ -51,3 +51,12 @@ def test_fiscalcode_check(self): 'is_company': False, 'fiscalcode': '123456789', }) + # Invalid FC + with self.assertRaises(ValidationError): + self.env["res.partner"].create( + { + "name": "Person", + "is_company": False, + "fiscalcode": "AAAMRA00H04H5010", + } + )