diff options
| author | oco-odoo <oco@odoo.com> | 2021-03-23 15:29:47 +0000 |
|---|---|---|
| committer | oco-odoo <oco@odoo.com> | 2021-04-02 09:45:24 +0000 |
| commit | 72b7fb15db4df1a3336bc6e90fe60f49877c52f8 (patch) | |
| tree | de67d641390772c696460bb9c493ef9c7e590d7e /addons/base_vat | |
| parent | 72c29b105bdb4da5b306cd3b48a1683c1bf3e620 (diff) | |
[FIX] base_vat: Don't always pass VAT check when validating the VAT of a partner without country_id
Before this, when making a partner without any country_id, any VAT could be set to it, which was inconsistent with the module's purpose.
closes odoo/odoo#68492
X-original-commit: 4c962437bd59a1e9302d8034e8870880994eb713
Related: odoo/enterprise#17358
Signed-off-by: Cedric Snauwaert (csn) <csn@openerp.com>
Signed-off-by: oco-odoo <oco-odoo@users.noreply.github.com>
Diffstat (limited to 'addons/base_vat')
| -rw-r--r-- | addons/base_vat/models/res_partner.py | 7 | ||||
| -rw-r--r-- | addons/base_vat/tests/test_validate_ruc.py | 58 |
2 files changed, 58 insertions, 7 deletions
diff --git a/addons/base_vat/models/res_partner.py b/addons/base_vat/models/res_partner.py index 2dcbee9d8cb..35ce8f0e4d8 100644 --- a/addons/base_vat/models/res_partner.py +++ b/addons/base_vat/models/res_partner.py @@ -159,10 +159,9 @@ class ResPartner(models.Model): 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 country_code: - if not check_func(country_code.lower(), partner.vat): - msg = partner._construct_constraint_msg(country_code.lower()) - raise ValidationError(msg) + 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) 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 c51433da8ed..b7a3be3fbf6 100644 --- a/addons/base_vat/tests/test_validate_ruc.py +++ b/addons/base_vat/tests/test_validate_ruc.py @@ -55,9 +55,61 @@ class TestStructure(SavepointCase): # reactivate it and correct the vat number with patch('odoo.addons.base_vat.models.res_partner.check_vies', type(self)._vies_check_func): self.env.user.company_id.vat_check_vies = True - with self.assertRaises(ValidationError), self.env.cr.savepoint(): - company.vat = "BE0987654321" # VIES refused, don't fallback on other check - company.vat = "BE0477472701" + + def test_vat_validation(self): + """ Tests VAT validation (both successes and failures), with the different country + detection cases possible. + """ + # Disable VIES; syntactic verification is enough for this test case + self.env.user.company_id.vat_check_vies = False + + test_partner =self.env['res.partner'].create({'name': "John Dex"}) + + # VAT starting with country code: use the starting country code + test_partner.write({'vat': 'BE0477472701', 'country_id': self.env.ref('base.fr').id}) + test_partner.write({'vat': 'BE0477472701', 'country_id': None}) + + with self.assertRaises(ValidationError): + test_partner.write({'vat': 'BE42', 'country_id': self.env.ref('base.fr').id}) + + with self.assertRaises(ValidationError): + test_partner.write({'vat': 'BE42', 'country_id': None}) + + # No country code in VAT: use the partner's country + test_partner.write({'vat': '0477472701', 'country_id': self.env.ref('base.be').id}) + + 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}) + + def test_vat_syntactic_validation(self): + """ Tests VAT validation (both successes and failures), with the different country + detection cases possible. + """ + test_partner =self.env['res.partner'].create({'name': "John Dex"}) + + # VAT starting with country code: use the starting country code + test_partner.write({'vat': 'BE0477472701', 'country_id': self.env.ref('base.fr').id}) + test_partner.write({'vat': 'BE0477472701', 'country_id': None}) + + with self.assertRaises(ValidationError): + test_partner.write({'vat': 'BE42', 'country_id': self.env.ref('base.fr').id}) + + with self.assertRaises(ValidationError): + test_partner.write({'vat': 'BE42', 'country_id': None}) + + # No country code in VAT: use the partner's country + test_partner.write({'vat': '0477472701', 'country_id': self.env.ref('base.be').id}) + + 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}) @tagged('-standard', 'external') |
