Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.0] [IMP] l10n_it_fiscalcode: add codicefiscale.isvalid() #3724

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 12 additions & 8 deletions l10n_it_fiscalcode/model/res_partner.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# 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')
primes2h marked this conversation as resolved.
Show resolved Hide resolved
def check_fiscalcode(self):
for partner in self:
if not partner.fiscalcode:
Expand All @@ -20,18 +24,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",
}
)
Loading