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][IMP] Paulistana Status Consulta #3527

Draft
wants to merge 2 commits into
base: 14.0
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions l10n_br_nfse_paulistana/constants/paulistana.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

CONSULTA_LOTE = [
"ConsultaLote",
"ConsultaNFe",
]
43 changes: 25 additions & 18 deletions l10n_br_nfse_paulistana/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ def _eletronic_document_send(self):
def _document_status(self):
status = super()._document_status()
for record in self.filtered(filter_oca_nfse).filtered(filter_paulistana):
vals = dict()
processador = record._processador_erpbrasil_nfse()
processo = processador.consulta_nfse_rps(
numero_rps=record.rps_number,
Expand All @@ -394,25 +395,31 @@ def _document_status(self):
or None,
cnpj_prest=misc.punctuation_rm(record.company_id.partner_id.cnpj_cpf),
)

consulta = processador.analisa_retorno_consulta(processo)
data_emissao = datetime.strptime(
consulta["data_emissao"], "%Y-%m-%dT%H:%M:%S"
)
if isinstance(consulta, dict):
record.write(
{
"verify_code": consulta["codigo_verificacao"],
"document_number": consulta["numero"],
"authorization_date": data_emissao,
}
)
record.authorization_event_id.set_done(
status_code=4,
response=_("Procesado com Sucesso"),
protocol_date=data_emissao,
protocol_number=record.authorization_protocol,
file_response_xml=processo.retorno,
)
retorno = ET.fromstring(processo.retorno)

if processo.webservice in CONSULTA_LOTE:
if processo.resposta.Cabecalho.Sucesso:
nfse = retorno.find(".//NFe")

vals["document_number"] = nfse.find(".//NumeroNFe").text
vals["authorization_date"] = nfse.find(".//DataEmissaoRPS").text
vals["verify_code"] = nfse.find(".//CodigoVerificacao").text
vals["status_name"] = _("Procesado com Sucesso")
vals["status_code"] = 4
vals["edoc_error_message"] = ""

record.authorization_event_id.set_done(
status_code=4,
response=vals["status_name"],
protocol_date=vals["authorization_date"],
protocol_number=nfse.find(".//CodigoVerificacao").text,
file_response_xml=processo.retorno,
)

record._change_state(SITUACAO_EDOC_AUTORIZADA)
record.write(vals)
status = _(consulta)
return status

Expand Down
1 change: 1 addition & 0 deletions l10n_br_nfse_paulistana/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import test_fiscal_document_nfse_paulistana
from . import test_webservices_nfse_paulistana
148 changes: 148 additions & 0 deletions l10n_br_nfse_paulistana/tests/test_webservices_nfse_paulistana.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# Copyright 2024 KMEE INFORMATICA LTDA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from datetime import datetime
from unittest.mock import patch

from odoo.exceptions import UserError
from odoo.tests import common

from odoo.addons.l10n_br_fiscal.constants.fiscal import (
MODELO_FISCAL_NFSE,
PROCESSADOR_OCA,
SITUACAO_EDOC_AUTORIZADA,
SITUACAO_EDOC_REJEITADA,
)


class TestL10nBrNfsePaulistana(common.TransactionCase):
"""Test class for Brazilian NFSe Paulistana integration."""

def setUp(self):
"""Sets up test environment."""
super().setUp()
# Reference to main company
self.company = self.env.ref("base.main_company")
# Setting NFSe provider to paulistana
self.company.provedor_nfse = "paulistana"

self.company.processador_edoc = PROCESSADOR_OCA
self.company.partner_id.inscr_mun = "35174"
self.company.partner_id.inscr_est = ""

self.nfse_demo = self.env.ref(
"l10n_br_fiscal.demo_nfse_same_state"
) # NFSe demo document
self.nfse_demo.document_number = "0001" # Setting document number
self.nfse_demo.rps_number = "0002" # Setting RPS number
self.nfse_demo.document_type_id.code = (
MODELO_FISCAL_NFSE # Setting document type to NFSe
)
# Setting a valid datetime for the document before processing
self.nfse_demo.document_date = datetime.now()
self.nfse_demo.date_in_out = datetime.now()

def test_serialize_nfse_paulistana(self):
"""Tests serialization and processing of NFSe Paulistana."""
# Preparing document for serialization
serialized_nfse = self.nfse_demo.serialize_nfse_paulistana()

self.assertIsNotNone(
serialized_nfse
) # Asserting that serialization is not None
self.assertEqual(
serialized_nfse.Cabecalho.CPFCNPJRemetente.CNPJ,
self.company.cnpj_cpf,
"The serialized CNPJ should match the company's CNPJ",
)

@patch("odoo.addons.l10n_br_nfse_paulistana.models.document.requests.request")
def test_processar_nfse_paulistana(self, mock_request):
"""Tests NFSe Paulistana processing with mocked responses."""
# Configuring mock to simulate different HTTP responses
mock_request.return_value.status_code = (
200
) # Simulating successful POST request
mock_request.return_value.json.return_value = {
"status": "success",
"data": {
"NumeroNFe": "00012345",
"DataEmissaoRPS": "2024-01-01T00:00:00",
"CodigoVerificacao": "123ABC",
},
}

# Testing NFSe processing
self.nfse_demo._eletronic_document_send()
# Sending the electronic document

# Verifying if the document status has changed to authorized
self.assertEqual(
self.nfse_demo.state,
SITUACAO_EDOC_AUTORIZADA,
"The document state should be updated to 'authorized'",
)
self.assertEqual(
self.nfse_demo.document_number,
"00012345",
"The document number should match the returned value from the provider",
)
self.assertEqual(
self.nfse_demo.verify_code,
"123ABC",
"The verification code should match the returned value from the provider",
)

@patch("odoo.addons.l10n_br_nfse_paulistana.models.document.requests.request")
def test_cancela_documento_paulistana(self, mock_delete):
"""Tests edoc cancellation for NFSe Paulistana with mocked DELETE request."""
# Configuring mock to simulate a successful cancellation response
mock_delete.return_value.status_code = 200
mock_delete.return_value.json.return_value = {"status": "cancelado"}

# Setting the document to be in an authorized state
self.nfse_demo.state = SITUACAO_EDOC_AUTORIZADA
self.nfse_demo.document_number = "00012345"
self.nfse_demo.verify_code = "123ABC"

# Attempting to cancel the document
try:
self.nfse_demo.cancel_document_paulistana()
except UserError as e:
self.fail(f"Unexpected UserError raised: {e}")

# Asserting that cancellation was successful
self.assertEqual(
self.nfse_demo.state,
SITUACAO_EDOC_REJEITADA,
"The document state should be updated to 'rejected' after cancellation",
)

@patch("odoo.addons.l10n_br_nfse_paulistana.models.document.requests.request")
def test_document_status_paulistana(self, mock_get):
"""Tests querying edoc status for NFSe Paulistana with mocked GET request."""
# Configuring mock to simulate a successful status query response
mock_get.return_value.status_code = 200
mock_get.return_value.json.return_value = {
"status": "success",
"data": {
"NumeroNFe": "00012345",
"DataEmissaoRPS": "2024-01-01T00:00:00",
"CodigoVerificacao": "123ABC",
},
}

# Querying the status of the NFSe
status = self.nfse_demo._document_status()

# Asserting that the status query was successful
self.assertEqual(
status,
"Procesado com Sucesso",
"The document status should be 'Processed with Success'",
)
self.assertEqual(
self.nfse_demo.state,
SITUACAO_EDOC_AUTORIZADA,
"The document state should be updated to 'authorized'",
)
Loading