Skip to content

Commit 5b02d29

Browse files
CristianoMafraJuniorantoniospneto
authored andcommitted
[IMP] l10n_br_ie_search: add assistant to IE
1 parent bb45367 commit 5b02d29

File tree

7 files changed

+136
-52
lines changed

7 files changed

+136
-52
lines changed

l10n_br_ie_search/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
22

33
from . import models
4+
from . import wizard

l10n_br_ie_search/models/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
from . import sefaz_webservice
44
from . import sintegra_webservice
55
from . import res_config_settings
6-
from . import l10n_br_base_party_mixin
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
interactions:
2+
- request:
3+
body: null
4+
headers:
5+
Accept:
6+
- "*/*"
7+
Accept-Encoding:
8+
- gzip, deflate
9+
Authorization:
10+
- Bearer 06aef429-a981-3ec5-a1f8-71d38d86481e
11+
Connection:
12+
- keep-alive
13+
User-Agent:
14+
- python-requests/2.25.1
15+
method: GET
16+
uri: https://www.receitaws.com.br/v1/cnpj/88570377000127
17+
response:
18+
body:
19+
string:
20+
"{\"abertura\": \"24/11/2021\", \"situacao\": \"BAIXADA\", \"tipo\":
21+
\"MATRIZ\", \"nome\": \"Dummy Empresa\", \"porte\": \"MICRO EMPRESA\",
22+
\"natureza_juridica\": \"213-5 - Empres\\u00e1rio (Individual)\",
23+
\"logradouro\": \"RUA DUMMY\", \"numero\": \"250\", \"complemento\": \"BLOCO
24+
E;APT 302\", \"municipio\": \"SAO PAULO\", \"bairro\": \"VILA FELIZ\", \"uf\":
25+
\"SP\", \"cep\": \"01222001\", \"email\": \"kilian.melcher@gmail.com\",
26+
\"telefone\": \"(83) 8665-0905\", \"data_situacao\": \"18/12/2023\",
27+
\"motivo_situacao\": \"EXTIN\\u00c7\\u00c3O POR ENCERRAMENTO
28+
LIQUIDA\\u00c7\\u00c3O VOLUNT\\u00c1RIA\", \"cnpj\": \"88570377000127\",
29+
\"ultima_atualizacao\": \"2024-01-13T23:59:59.000Z\", \"status\": \"OK\",
30+
\"fantasia\": \"\", \"efr\": \"\", \"situacao_especial\": \"\",
31+
\"data_situacao_especial\": \"\", \"atividade_principal\": [{\"code\":
32+
\"4751-2/01\", \"text\": \"********\"}], \"atividades_secundarias\":
33+
[{\"code\": \"00.00-0-00\", \"text\": \"Não informada\"}], \"capital_social\":
34+
\"3000.00\", \"qsa\": [], \"extra\": {}, \"billing\": {\"free\": true,
35+
\"database\": true}}"
36+
headers:
37+
Access-Control-Allow-Headers:
38+
- authorization,Access-Control-Allow-Origin,Content-Type,SOAPAction,apikey,Internal-Key,x-cpf-usuario,signature,x-signature,x-credencial,X-Request-Trace-Id
39+
Access-Control-Allow-Methods:
40+
- GET
41+
Access-Control-Expose-Headers:
42+
- ""
43+
Connection:
44+
- keep-alive
45+
Date:
46+
- Wed, 28 Jun 2023 13:45:28 GMT
47+
Transfer-Encoding:
48+
- chunked
49+
access-control-allow-origin:
50+
- "*"
51+
activityid:
52+
- 0c70c470-9419-4869-aa01-b964c345f5ac
53+
content-type:
54+
- application/json; charset=utf-8
55+
etag:
56+
- W/"86a-p2CeQnt0OF5zs4vPG4Lpgnsxv8k"
57+
strict-transport-security:
58+
- max-age=15768000
59+
x-content-type-options:
60+
- nosniff
61+
x-powered-by:
62+
- Express
63+
status:
64+
code: 200
65+
message: OK
66+
version: 1

l10n_br_ie_search/tests/test_sefaz.py

+32-15
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# Copyright 2023 KMEE
2+
# Copyright (C) 2024-Today - Engenere (<https://engenere.one>).
3+
# @author Cristiano Mafra Junior
24
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
3-
4-
import time
5+
import os
56
from datetime import timedelta
7+
from unittest.mock import patch
68

9+
import vcr
710
from erpbrasil.assinatura import misc
811

912
from odoo import fields
10-
from odoo.tests import tagged
13+
from odoo.tests import Form, tagged
1114
from odoo.tests.common import SavepointCase
1215
from odoo.tools.misc import format_date
1316

@@ -102,16 +105,30 @@ def _switch_user_company(self, user, company):
102105
}
103106
)
104107

108+
@vcr.use_cassette(
109+
os.path.dirname(__file__) + "/fixtures/test_sefaz.yaml",
110+
match_on=["method", "scheme", "host", "port", "path", "query", "body"],
111+
ignore_localhost=True,
112+
)
105113
def test_sefaz(self):
106-
dummy = self.model.create(
107-
{
108-
"name": "Dummy",
109-
"cnpj_cpf": "88.570.377/0001-27",
110-
"certificate_ecnpj_id": self.cert.id,
111-
}
112-
)
113-
time.sleep(1) # to avoid too many requests
114-
dummy._onchange_cnpj_cpf()
115-
dummy.ie_search(self.retorno)
116-
117-
self.assertEqual(dummy.inscr_est, "528388258640")
114+
with patch(
115+
"odoo.addons.l10n_br_ie_search.models.sefaz_webservice.SefazWebservice.sefaz_search"
116+
) as mock_get_partner_ie:
117+
mock_get_partner_ie.return_value = self.retorno
118+
dummy = self.model.create(
119+
{
120+
"name": "Dummy",
121+
"cnpj_cpf": "88.570.377/0001-27",
122+
"certificate_ecnpj_id": self.cert.id,
123+
}
124+
)
125+
dummy._onchange_cnpj_cpf()
126+
action_wizard = dummy.action_open_cnpj_search_wizard()
127+
wizard_context = action_wizard.get("context")
128+
wizard = Form(
129+
self.env["partner.search.wizard"]
130+
.with_context(wizard_context)
131+
.create({})
132+
).save()
133+
wizard.action_update_partner()
134+
self.assertEqual(dummy.inscr_est, "528388258640")

l10n_br_ie_search/tests/test_sintegra.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Copyright 2023 KMEE
2+
# Copyright (C) 2024-Today - Engenere (<https://engenere.one>).
3+
# @author Cristiano Mafra Junior
24
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
35

46
import logging
5-
import time # You can't send multiple requests at the same time in trial version
7+
from unittest.mock import patch
68

7-
from odoo.tests import tagged
9+
from odoo.tests import Form, tagged
810
from odoo.tests.common import SavepointCase
911

1012
_logger = logging.getLogger(__name__)
@@ -74,11 +76,15 @@ def set_param(self, param_name, param_value):
7476
.set_param("l10n_br_ie_search." + param_name, param_value)
7577
)
7678

77-
def test_sintegra(self):
79+
@patch("requests.get")
80+
def test_sintegra(self, mock_get_partner_ie):
81+
mock_get_partner_ie.return_value = self.retorno
7882
dummy = self.model.create({"name": "Dummy", "cnpj_cpf": "06990590000123"})
79-
80-
time.sleep(2) # to avoid too many requests
8183
dummy._onchange_cnpj_cpf()
82-
dummy.ie_search(self.retorno)
83-
84+
action_wizard = dummy.action_open_cnpj_search_wizard()
85+
wizard_context = action_wizard.get("context")
86+
wizard = Form(
87+
self.env["partner.search.wizard"].with_context(wizard_context).create({})
88+
).save()
89+
wizard.action_update_partner()
8490
self.assertEqual(dummy.inscr_est, "149848403115")

l10n_br_ie_search/wizard/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import extend_partner_cnpj_search_wizard

l10n_br_ie_search/models/l10n_br_base_party_mixin.py l10n_br_ie_search/wizard/extend_partner_cnpj_search_wizard.py

+23-29
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,40 @@
11
# Copyright 2023 KMEE - Breno Oliveira Dias
2+
# Copyright (C) 2024-Today - Engenere (<https://engenere.one>).
3+
# @author Cristiano Mafra Junior
24
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
35

6+
import requests
47
from erpbrasil.assinatura import certificado as cert
58
from erpbrasil.edoc.nfe import NFe as edoc_nfe
69
from erpbrasil.transmissao import TransmissaoSOAP
7-
from requests import Session, get
10+
from requests import Session
811

912
from odoo import _, api, models
1013
from odoo.exceptions import UserError
1114

1215
SINTEGRA_URL = "https://www.sintegraws.com.br/api/v1/execute-api.php"
1316

1417

15-
class PartyMixin(models.AbstractModel):
16-
_inherit = "l10n_br_base.party.mixin"
18+
class ExtendPartnerCnpjSearchWizard(models.TransientModel):
19+
_inherit = "partner.search.wizard"
1720

18-
def search_cnpj(self):
19-
"""Search state subscription"""
20-
21-
super().search_cnpj()
22-
self.ie_search()
23-
24-
@api.model
25-
def ie_search(self, mockresponse=False):
21+
def _get_partner_ie(self, state_code, cnpj):
2622
webservice = self.env["l10n_br_cnpj_search.webservice.abstract"]
2723
if self._provider() == "sefaz":
2824
processo = self._processador()
29-
response = (
30-
webservice.sefaz_search(self.state_id.code, self.cnpj_cpf, processo)
31-
if not mockresponse
32-
else mockresponse
33-
)
25+
response = webservice.sefaz_search(state_code, cnpj, processo)
3426
data = webservice.sefaz_validate(response)
3527
values = webservice._sefaz_import_data(data)
36-
self.write(values)
28+
return values
3729
elif self._provider() == "sintegraws":
38-
response = (
39-
get(
40-
SINTEGRA_URL,
41-
data="",
42-
params=webservice._get_query(
43-
self.cnpj_cpf, webservice._get_token()
44-
),
45-
)
46-
if not mockresponse
47-
else mockresponse
30+
response = requests.get(
31+
SINTEGRA_URL,
32+
data="",
33+
params=webservice._get_query(cnpj, webservice._get_token()),
4834
)
49-
5035
data = webservice.sintegra_validate(response)
5136
values = webservice._sintegra_import_data(data)
52-
self.write(values)
37+
return values
5338

5439
@api.model
5540
def _provider(self):
@@ -59,6 +44,15 @@ def _provider(self):
5944
.get_param("l10n_br_ie_search.ie_search")
6045
)
6146

47+
@api.model
48+
def _get_partner_values(self, cnpj_cpf):
49+
values = super()._get_partner_values(cnpj_cpf)
50+
state_id = self.env["res.country.state"].browse(values["state_id"])
51+
ie_values = self._get_partner_ie(state_code=state_id.code, cnpj=cnpj_cpf)
52+
if ie_values:
53+
values.update(ie_values)
54+
return values
55+
6256
@api.model
6357
def _processador(self):
6458
company = self.env.company

0 commit comments

Comments
 (0)