Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][RFC] l10n_br_base: refactor vat field value #2718

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion l10n_br_base/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?")

Expand Down Expand Up @@ -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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eu acho que tem que assignar o partner.vat de todos os partners para não ter erros de CacheMiss. Tb, como seria num caso internacional onde a empresa vai usar esse campo vat mesmo?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A principio ainda não trabalhei no caso de uso internacional e sim em uma forma de viabilizar o uso deste campo no método create_company

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lendo o outro PR pelo o que entendi isso está afetando o cadastro de um novo res.partner pelo Portal l10n_br_portal, seria esse o único motivo para preencher o VAT nos casos do Brasil? Se existe essa necessidade talvez o melhor seja usar um onchange ao invés do compute para não afetar os casos internacionais, sobre o Portal parece que o melhor seria ter um campo CNPJ na visão para o usuário informa-lo e assim separar o CNPJ_CPF do contato do CNPJ da empresa ou isso não seria possível?

Copy link
Member Author

@marcelsavegnago marcelsavegnago Sep 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Se tiver um campo CPF separado talvez resolveria. E no caso de pessoa fisica sem parent_id ou company_name o vat e cnpj_cpf seria o CPF.

E sim, a mudança é por conta de cadastros oriundos do portal.

Copy link
Member Author

@marcelsavegnago marcelsavegnago Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eu acho que tem que assignar o partner.vat de todos os partners para não ter erros de CacheMiss. Tb, como seria num caso internacional onde a empresa vai usar esse campo vat mesmo?

@rvalyi tentei garantir que o campo receba um valor em todos os casos massss não tenho certeza se da forma que fiz foi a melhor forma.

@api.constrains("cnpj_cpf", "country_id")
def _check_cnpj_cpf(self):
result = True
Expand Down Expand Up @@ -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
158 changes: 158 additions & 0 deletions l10n_br_base/tests/test_valid_createid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -164,6 +181,147 @@ 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": "93.429.799/0001-17",
}
)
partner = (
self.env["res.partner"]
.with_context(tracking_disable=True)
.create(partner_data)
)
partner._compute_vat_from_cnpj_cpf()
self.assertEqual(
partner.vat,
"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
27 changes: 24 additions & 3 deletions l10n_br_base/views/res_partner_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,18 @@
<field name="priority">99</field>
<field name="inherit_id" ref="base.view_partner_form" />
<field name="arch" type="xml">
<field name="vat" position="replace" />
<field name="vat" position="attributes">
<attribute name="attrs">
{
'readonly':
[
'|', ('cnpj_cpf', 'not in', [False, ""]),
('parent_id', '!=', False),

]
}
</attribute>
</field>
<xpath expr="//h1" position="after">
<group>
<field name="city_id" invisible="1" />
Expand Down Expand Up @@ -80,9 +91,19 @@
attrs="{'invisible': [('is_company','=', True)]}"
/>
<div class="oe_edit_only" name="inscr_est">
<label for="inscr_est" name="inscr_est" string="IE" />
<label
for="inscr_est"
name="inscr_est"
string="IE"
attrs="{'invisible': [('is_company','=', False), ('parent_id', '!=', False)]}"
/>
</div>
<field colspan="4" name="inscr_est" nolabel="1" />
<field
colspan="4"
name="inscr_est"
nolabel="1"
attrs="{'invisible': [('is_company','=', False), ('parent_id', '!=', False)]}"
/>
</group>
</xpath>
<page position="after" name="sales_purchases">
Expand Down