summaryrefslogtreecommitdiff
path: root/addons/l10n_it_edi/models/res_partner.py
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/l10n_it_edi/models/res_partner.py
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/l10n_it_edi/models/res_partner.py')
-rw-r--r--addons/l10n_it_edi/models/res_partner.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/addons/l10n_it_edi/models/res_partner.py b/addons/l10n_it_edi/models/res_partner.py
new file mode 100644
index 00000000..3a1d8d71
--- /dev/null
+++ b/addons/l10n_it_edi/models/res_partner.py
@@ -0,0 +1,49 @@
+# -*- coding:utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, fields, models, _
+from odoo.exceptions import UserError, ValidationError
+
+import re
+
+
+class ResPartner(models.Model):
+ _name = 'res.partner'
+ _inherit = 'res.partner'
+
+ l10n_it_pec_email = fields.Char(string="PEC e-mail")
+ l10n_it_codice_fiscale = fields.Char(string="Codice Fiscale", size=16)
+ l10n_it_pa_index = fields.Char(string="PA index",
+ size=7,
+ help="Must contain the 6-character (or 7) code, present in the PA\
+ Index in the information relative to the electronic invoicing service,\
+ associated with the office which, within the addressee administration, deals\
+ with receiving (and processing) the invoice.")
+
+ _sql_constraints = [
+ ('l10n_it_codice_fiscale',
+ "CHECK(l10n_it_codice_fiscale IS NULL OR l10n_it_codice_fiscale = '' OR LENGTH(l10n_it_codice_fiscale) >= 11)",
+ "Codice fiscale must have between 11 and 16 characters."),
+
+ ('l10n_it_pa_index',
+ "CHECK(l10n_it_pa_index IS NULL OR l10n_it_pa_index = '' OR LENGTH(l10n_it_pa_index) >= 6)",
+ "PA index must have between 6 and 7 characters."),
+ ]
+
+ @api.model
+ def _l10n_it_normalize_codice_fiscale(self, codice):
+ if codice and re.match(r'^IT[0-9]{11}$', codice):
+ return codice[2:13]
+ return codice
+
+ @api.onchange('vat')
+ def _l10n_it_onchange_vat(self):
+ if not self.l10n_it_codice_fiscale:
+ self.l10n_it_codice_fiscale = self._l10n_it_normalize_codice_fiscale(self.vat)
+
+ @api.constrains('l10n_it_codice_fiscale')
+ def validate_codice_fiscale(self):
+ p = re.compile(r'^([A-Za-z]{6}[0-9]{2}[A-Za-z]{1}[0-9]{2}[A-Za-z]{1}[0-9]{3}[A-Za-z]{1}$)|([0-9]{11})|(IT[0-9]{11})$')
+ for record in self:
+ if record.l10n_it_codice_fiscale and not p.match(record.l10n_it_codice_fiscale):
+ raise UserError(_("Invalid Codice Fiscale '%s': should be like 'MRTMTT91D08F205J' for physical person and '12345678901' or 'IT12345678901' for businesses.", record.l10n_it_codice_fiscale))