[IMP] website_sale, website_portal: Re-use partner.check_vat method

Use check_vat function directly.
It already checks both format and centralize the dispatch of methods.

Closes #17644
This commit is contained in:
Moises Lopez - https://www.vauxoo.com/
2017-06-20 17:33:50 +02:00
committed by Martin Trigaux
parent 4ffb61db1d
commit d7f84c273b
2 changed files with 20 additions and 16 deletions
+11 -9
View File
@@ -6,6 +6,7 @@ from odoo.http import request
from odoo import tools
from odoo.tools import pycompat
from odoo.tools.translate import _
from odoo.exceptions import ValidationError
from odoo.fields import Date
@@ -119,15 +120,16 @@ class website_account(http.Controller):
error_message.append(_('Invalid Email! Please enter a valid email address.'))
# vat validation
if data.get("vat") and hasattr(request.env["res.partner"], "check_vat"):
if request.website.company_id.sudo().vat_check_vies:
# force full VIES online check
check_func = request.env["res.partner"].vies_vat_check
else:
# quick and partial off-line checksum validation
check_func = request.env["res.partner"].simple_vat_check
vat_country, vat_number = request.env["res.partner"]._split_vat(data.get("vat"))
if not check_func(vat_country, vat_number): # simple_vat_check
partner = request.env["res.partner"]
if data.get("vat") and hasattr(partner, "check_vat"):
partner_dummy = partner.new({
'vat': data['vat'],
'country_id': (int(data['country_id'])
if data.get('country_id') else False),
})
try:
partner_dummy.check_vat()
except ValidationError:
error["vat"] = 'error'
# error message for empty required fields
+9 -7
View File
@@ -485,13 +485,15 @@ class WebsiteSale(http.Controller):
# vat validation
Partner = request.env['res.partner']
if data.get("vat") and hasattr(Partner, "check_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):
if country.code and check_func(country.code, data["vat"]):
data["vat"] = country.code + data["vat"]
else:
error["vat"] = 'error'
partner_dummy = Partner.new({
'vat': data['vat'],
'country_id': (int(data['country_id'])
if data.get('country_id') else False),
})
try:
partner_dummy.check_vat()
except ValidationError:
error["vat"] = 'error'
if [err for err in pycompat.items(error) if err == 'missing']:
error_message.append(_('Some required fields are empty.'))