diff --git a/addons/base_vat/models/res_partner.py b/addons/base_vat/models/res_partner.py index 12129945b8c..69eb983cc62 100644 --- a/addons/base_vat/models/res_partner.py +++ b/addons/base_vat/models/res_partner.py @@ -18,6 +18,12 @@ from odoo import api, models, _ from odoo.tools.misc import ustr from odoo.exceptions import ValidationError +_eu_country_vat = { + 'GR': 'EL' +} + +_eu_country_vat_inverse = {v: k for k, v in _eu_country_vat.items()} + _ref_vat = { 'at': 'ATU12345675', 'be': 'BE0477472701', @@ -79,6 +85,7 @@ class ResPartner(models.Model): # Foreign companies that trade with non-enterprises in the EU # may have a VATIN starting with "EU" instead of a country code. return True + country_code = _eu_country_vat_inverse.get(country_code, country_code) return bool(self.env['res.country'].search([('code', '=ilike', country_code)])) return check_func(vat_number) @@ -96,6 +103,19 @@ class ResPartner(models.Model): # country code or empty VAT number), so we fall back to the simple check. return self.simple_vat_check(country_code, vat_number) + @api.model + def fix_eu_vat_number(self, country_id, vat): + europe = self.env.ref('base.europe') + country = self.env["res.country"].browse(country_id) + if not europe: + europe = self.env["res.country.group"].search([('name', '=', 'Europe')], limit=1) + if europe and country and country.id in europe.country_ids.ids: + vat = re.sub('[^A-Za-z0-9]', '', vat).upper() + country_code = _eu_country_vat.get(country.code, country.code).upper() + if vat[:2] != country_code: + vat = country_code + vat + return vat + @api.constrains("vat") def check_vat(self): if self.env.context.get('company_id'): diff --git a/addons/website_sale/controllers/main.py b/addons/website_sale/controllers/main.py index ec9b52b9246..ab5f0021931 100644 --- a/addons/website_sale/controllers/main.py +++ b/addons/website_sale/controllers/main.py @@ -483,6 +483,8 @@ class WebsiteSale(http.Controller): # vat validation Partner = request.env['res.partner'] if data.get("vat") and hasattr(Partner, "check_vat"): + if data.get("country_id"): + data["vat"] = Partner.fix_eu_vat_number(data.get("country_id"), data.get("vat")) check_func = request.website.company_id.vat_check_vies and Partner.vies_vat_check or Partner.simple_vat_check vat_country, vat_number = Partner._split_vat(data.get("vat")) if not check_func(vat_country, vat_number):