summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoroco-odoo <oco@odoo.com>2021-04-07 12:44:15 +0000
committeroco-odoo <oco@odoo.com>2021-04-09 12:08:01 +0000
commit7faa11ca87cb5308df1d1921a674e2e2edc3f065 (patch)
treeede3bb12227e2aa2d412c3f07a8bb2eb27a8c975
parentd5075308131f82020702c6650a1869ffd93fb85e (diff)
[FIX] base_vat: patch orm limitation
https://github.com/odoo/odoo/pull/68253 fixed a bug in check_vat that caused it not to run any check when called on a partner with no country. However, doing this caused another issue because of an ORM limitation: when writing vat and country_id with a single write() on the res.company, two distinct write are triggered on the related res.partner, one for each field. Both those write trigger the check_vat constraint. Depending on the order in which the keys of the dictionnary passed to res.company's write were ordered, country_id could or could not be written before vat. If vat was written first and did not start with a country code, the write() on res.partner failed the constraint, because country_id wasn't set yet. This was wrong, but used to pass as there was no country_id on the partner, before https://github.com/odoo/odoo/pull/68253 fixed that. To circumvent the issue, we now allow entering any vat number without performing any check if it does not start with a country code and no country_id is set on the partner. closes odoo/odoo#69034 X-original-commit: 3b73ea994a0eedc08ee0e7c43610ca5e78984b54 Signed-off-by: Laurent Smet <smetl@users.noreply.github.com> Signed-off-by: oco-odoo <oco-odoo@users.noreply.github.com>
-rw-r--r--addons/base_vat/models/res_partner.py31
-rw-r--r--addons/base_vat/tests/test_validate_ruc.py6
2 files changed, 26 insertions, 11 deletions
diff --git a/addons/base_vat/models/res_partner.py b/addons/base_vat/models/res_partner.py
index 35ce8f0e4d8..f2dcb4bcad4 100644
--- a/addons/base_vat/models/res_partner.py
+++ b/addons/base_vat/models/res_partner.py
@@ -148,20 +148,35 @@ class ResPartner(models.Model):
for partner in self:
if not partner.vat:
continue
- #check with country code as prefix of the TIN
- vat_country, vat_number = self._split_vat(partner.vat)
+
if company.vat_check_vies and partner.commercial_partner_id.country_id in eu_countries:
# force full VIES online check
check_func = self.vies_vat_check
else:
# quick and partial off-line checksum validation
check_func = self.simple_vat_check
- if not check_func(vat_country, vat_number):
- #if fails, check with country code from country
- country_code = partner.commercial_partner_id.country_id.code
- if not country_code or not check_func(country_code.lower(), partner.vat):
- msg = partner._construct_constraint_msg(country_code.lower() if country_code else None)
- raise ValidationError(msg)
+
+ failed_check = False
+ #check with country code as prefix of the TIN
+ vat_country_code, vat_number = self._split_vat(partner.vat)
+ vat_guessed_country = self.env['res.country'].search([('code', '=', vat_country_code.upper())])
+ if vat_guessed_country:
+ failed_check = not check_func(vat_country_code, vat_number)
+
+ #if fails, check with country code from country
+ partner_country_code = partner.commercial_partner_id.country_id.code
+ if (not vat_guessed_country or failed_check) and partner_country_code:
+ failed_check = not check_func(partner_country_code.lower(), partner.vat)
+
+ # We allow any number if it doesn't start with a country code and the partner has no country.
+ # This is necessary to support an ORM limitation: setting vat and country_id together on a company
+ # triggers two distinct write on res.partner, one for each field, both triggering this constraint.
+ # If vat is set before country_id, the constraint must not break.
+
+ if failed_check:
+ country_code = partner_country_code or vat_country_code
+ msg = partner._construct_constraint_msg(country_code.lower() if country_code else None)
+ raise ValidationError(msg)
def _construct_constraint_msg(self, country_code):
self.ensure_one()
diff --git a/addons/base_vat/tests/test_validate_ruc.py b/addons/base_vat/tests/test_validate_ruc.py
index ed189bff784..55845fdd61d 100644
--- a/addons/base_vat/tests/test_validate_ruc.py
+++ b/addons/base_vat/tests/test_validate_ruc.py
@@ -78,9 +78,9 @@ class TestStructure(SavepointCase):
with self.assertRaises(ValidationError):
test_partner.write({'vat': '42', 'country_id': self.env.ref('base.be').id})
- # If no country can be guessed: VAT number cannot be validated
- with self.assertRaises(ValidationError):
- test_partner.write({'vat': '0477472701', 'country_id': None})
+ # If no country can be guessed: VAT number should always be considered valid
+ # (for technical reasons due to ORM and res.company making related fields towards res.partner for country_id and vat)
+ test_partner.write({'vat': '0477472701', 'country_id': None})
@tagged('-standard', 'external')