Skip to content

Commit

Permalink
[IMP] l10n_it_fiscalcode: add codicefiscale.isvalid()
Browse files Browse the repository at this point in the history
  • Loading branch information
odooNextev authored and matteonext committed Mar 13, 2024
1 parent 5513b40 commit 88bf816
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion l10n_it_fatturapa_in/tests/test_import_fatturapa_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 12 additions & 9 deletions l10n_it_fiscalcode/model/res_partner.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand Down
11 changes: 10 additions & 1 deletion l10n_it_fiscalcode/tests/test_fiscalcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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",
}
)

0 comments on commit 88bf816

Please sign in to comment.