From 9d7a91e59ce325c8c51e9821b1e456ec82cb13e8 Mon Sep 17 00:00:00 2001 From: Marcel Savegnago Date: Thu, 28 Sep 2023 13:08:26 -0300 Subject: [PATCH 1/5] [RFC] l10n_br_base: change inscr_est when partner is_company False and parent_id is True --- l10n_br_base/views/res_partner_view.xml | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/l10n_br_base/views/res_partner_view.xml b/l10n_br_base/views/res_partner_view.xml index a335480aac0c..b9261c4d7a6c 100644 --- a/l10n_br_base/views/res_partner_view.xml +++ b/l10n_br_base/views/res_partner_view.xml @@ -80,9 +80,19 @@ attrs="{'invisible': [('is_company','=', True)]}" />
-
- + From 45cec94b6fcac94086ab418d08a27a08a6fa6eb6 Mon Sep 17 00:00:00 2001 From: Marcel Savegnago Date: Thu, 28 Sep 2023 13:05:34 -0300 Subject: [PATCH 2/5] [RFC] l10n_br_base: refactor vat field and add br fields on create_company function --- l10n_br_base/models/res_partner.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/l10n_br_base/models/res_partner.py b/l10n_br_base/models/res_partner.py index 37747bb0ddb1..bc56fdd40082 100644 --- a/l10n_br_base/models/res_partner.py +++ b/l10n_br_base/models/res_partner.py @@ -20,7 +20,7 @@ class Partner(models.Model): _name = "res.partner" _inherit = [_name, "l10n_br_base.party.mixin"] - vat = fields.Char(related="cnpj_cpf") + vat = fields.Char(compute="_compute_vat_from_cnpj_cpf", store=True) is_accountant = fields.Boolean(string="Is accountant?") @@ -96,6 +96,20 @@ def _check_cnpj_inscr_est(self): _("There is already a partner record with this CPF/RG!") ) + @api.depends( + "cnpj_cpf", "is_company", "parent_id", "parent_id.vat", "commercial_partner_id" + ) + def _compute_vat_from_cnpj_cpf(self): + for partner in self: + if partner.company_name and partner.vat: + continue + elif partner.commercial_partner_id.cnpj_cpf: + partner.vat = partner.commercial_partner_id.cnpj_cpf + elif partner.vat: + continue + else: + partner.vat = False + @api.constrains("cnpj_cpf", "country_id") def _check_cnpj_cpf(self): result = True @@ -198,3 +212,17 @@ def _compute_show_l10n_br(self): rec.show_l10n_br = False else: rec.show_l10n_br = True + + def create_company(self): + self.ensure_one() + inscr_est = self.inscr_est + inscr_mun = self.inscr_mun + res = super().create_company() + if res: + parent = self.parent_id + if parent.country_id.code == "BR": + parent.legal_name = parent.name + parent.cnpj_cpf = parent.vat + parent.inscr_est = inscr_est + parent.inscr_mun = inscr_mun + return res From 82caf2ed300a7e85987ca476a3a03ee134667482 Mon Sep 17 00:00:00 2001 From: Marcel Savegnago Date: Fri, 29 Sep 2023 15:07:02 -0300 Subject: [PATCH 3/5] [RFC] l10n_br_base: remove vat field replace from inherit view and change readonly attribute --- l10n_br_base/views/res_partner_view.xml | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/l10n_br_base/views/res_partner_view.xml b/l10n_br_base/views/res_partner_view.xml index b9261c4d7a6c..e81273298fa3 100644 --- a/l10n_br_base/views/res_partner_view.xml +++ b/l10n_br_base/views/res_partner_view.xml @@ -35,7 +35,18 @@ 99 - + + + { + 'readonly': + [ + '|', ('cnpj_cpf', 'not in', [False, ""]), + ('parent_id', '!=', False), + + ] + } + + From 475dede8748de09411ff7da6d47a59607f0f5fbf Mon Sep 17 00:00:00 2001 From: Marcel Savegnago Date: Fri, 29 Sep 2023 12:35:15 -0300 Subject: [PATCH 4/5] [IMP] l10n_br_base: add partner._compute_vat_from_cnpj_cpf tests --- l10n_br_base/tests/test_valid_createid.py | 92 +++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/l10n_br_base/tests/test_valid_createid.py b/l10n_br_base/tests/test_valid_createid.py index 8a2a18bcf19e..3fb9d0dc94c6 100644 --- a/l10n_br_base/tests/test_valid_createid.py +++ b/l10n_br_base/tests/test_valid_createid.py @@ -108,6 +108,23 @@ def setUpClass(cls): "website": "www.partnertest.com.br", } + cls.partner_outside_br = { + "name": "Partner Test 3", + "legal_name": "Partner Tesc 3 Ltda", + "vat": "123456789", + "street": "Street Company", + "street_number": "955", + "street2": "Street2 Company", + "district": "Company District", + "state_id": cls.env.ref("base.state_us_2").id, + "country_id": cls.env.ref("base.us").id, + "city": "Nome", + "zip": "99762", + "phone": "+1 (907) 443-5796", + "email": "contact@companytest.com.br", + "website": "www.companytest.com.br", + } + # Tests on companies def test_comp_valid(self): @@ -164,6 +181,81 @@ def test_part_invalid_cpf(self): self.partner_invalid_cpf ) + def test_vat_computation_with_cnpj(self): + """Test VAT computation for a br partner with CNPJ""" + partner = ( + self.env["res.partner"] + .with_context(tracking_disable=True) + .create(self.partner_valid) + ) + partner._compute_vat_from_cnpj_cpf() + self.assertEqual( + partner.vat, + self.partner_valid["cnpj_cpf"], + "VAT should be equal to CNPJ for a br partner", + ) + + def test_vat_computation_without_cnpj(self): + """Test VAT computation for a br partner without CNPJ""" + partner_data = self.partner_valid.copy() + partner_data.pop("cnpj_cpf") + partner = ( + self.env["res.partner"] + .with_context(tracking_disable=True) + .create(partner_data) + ) + partner._compute_vat_from_cnpj_cpf() + self.assertFalse( + partner.vat, "VAT should be False for a br partner without CNPJ" + ) + + def test_vat_computation_outside_company_with_vat(self): + """Test VAT computation for a outside br partner with VAT""" + partner = ( + self.env["res.partner"] + .with_context(tracking_disable=True) + .create(self.partner_outside_br) + ) + partner._compute_vat_from_cnpj_cpf() + self.assertEqual( + partner.vat, + "123456789", + "The VAT must be the same as what was registered", + ) + + def test_vat_computation_outside_company_without_vat(self): + """Test VAT computation for a outside br partner without VAT""" + partner_data = self.partner_outside_br.copy() + partner_data.pop("vat") + partner = ( + self.env["res.partner"] + .with_context(tracking_disable=True) + .create(partner_data) + ) + partner._compute_vat_from_cnpj_cpf() + self.assertFalse(partner.vat, "VAT should be False as registered") + + def test_vat_computation_with_company_name_and_vat(self): + """Test VAT computation for a br partner with company_name and vat""" + partner_data = self.partner_valid.copy() + partner_data.update( + { + "company_name": "Company Partner", + "vat": "123456789", + } + ) + partner = ( + self.env["res.partner"] + .with_context(tracking_disable=True) + .create(partner_data) + ) + partner._compute_vat_from_cnpj_cpf() + self.assertEqual( + partner.vat, + "123456789", + "The VAT must be the same as what was registered", + ) + # No test on Inscricao Estadual for partners with CPF # because they haven't Inscricao Estadual From 83a82dfe9a03d333cd7d6070bef56ae42dafb5cc Mon Sep 17 00:00:00 2001 From: Marcel Savegnago Date: Fri, 29 Sep 2023 17:06:11 -0300 Subject: [PATCH 5/5] [IMP] l10n_br_base: add partner.create_company tests --- l10n_br_base/tests/test_valid_createid.py | 70 ++++++++++++++++++++++- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/l10n_br_base/tests/test_valid_createid.py b/l10n_br_base/tests/test_valid_createid.py index 3fb9d0dc94c6..5b4a79bba896 100644 --- a/l10n_br_base/tests/test_valid_createid.py +++ b/l10n_br_base/tests/test_valid_createid.py @@ -241,7 +241,7 @@ def test_vat_computation_with_company_name_and_vat(self): partner_data.update( { "company_name": "Company Partner", - "vat": "123456789", + "vat": "93.429.799/0001-17", } ) partner = ( @@ -252,10 +252,76 @@ def test_vat_computation_with_company_name_and_vat(self): partner._compute_vat_from_cnpj_cpf() self.assertEqual( partner.vat, - "123456789", + "93.429.799/0001-17", "The VAT must be the same as what was registered", ) + def test_create_company_in_brazil(self): + """Test the creation of a company in Brazil""" + partner_data = self.partner_valid.copy() + partner_data.update( + { + "company_name": "Company Partner", + "vat": "93.429.799/0001-17", + } + ) + partner = ( + self.env["res.partner"] + .with_context(tracking_disable=True) + .create(partner_data) + ) + partner.create_company() + company = partner.parent_id + self.assertTrue(company, "The company was not created") + self.assertEqual( + company.legal_name, + company.name, + "The legal name must be the same as the company name", + ) + self.assertEqual( + company.cnpj_cpf, + company.vat, + "The company CNPJ_CPF must be the same as the company VAT", + ) + self.assertEqual( + company.cnpj_cpf, + partner.vat, + "The company CNPJ_CPF must be the same as the partner VAT", + ) + self.assertEqual( + company.inscr_est, + partner.inscr_est, + "The company INSCR_EST must be the same as the partner INSCR_EST", + ) + self.assertEqual( + company.inscr_mun, + partner.inscr_mun, + "The company INSCR_MUN must be the same as the partner INSCR_MUN", + ) + + def test_create_company_outside_brazil(self): + """Test the creation of a company outside Brazil""" + partner_data = self.partner_outside_br.copy() + partner_data.update( + { + "company_name": "Company Partner", + } + ) + partner = ( + self.env["res.partner"] + .with_context(tracking_disable=True) + .create(partner_data) + ) + partner.create_company() + company = partner.parent_id + self.assertTrue(company, "The company was not created") + self.assertEqual( + company.vat, + partner.vat, + "The company CNPJ_CPF must be the same as the partner VAT", + ) + self.assertFalse(company.cnpj_cpf, "CNPJ_CPF should be False") + # No test on Inscricao Estadual for partners with CPF # because they haven't Inscricao Estadual