Danimar Ribeiro 9 years ago
parent
commit
0f404a1f22
  1. 98
      pytrustnfe/Servidores.py
  2. 33
      pytrustnfe/nfe/__init__.py
  3. 6
      pytrustnfe/nfe/templates/NfeAutorizacao.xml

98
pytrustnfe/Servidores.py

@ -10,6 +10,17 @@ WS_NFE_CONSULTA = 'NfeConsultaProtocolo'
WS_NFE_SITUACAO = 'NfeStatusServico' WS_NFE_SITUACAO = 'NfeStatusServico'
WS_NFE_CADASTRO = 'NfeConsultaCadastro' WS_NFE_CADASTRO = 'NfeConsultaCadastro'
WS_NFCE_AUTORIZACAO = 'NfeAutorizacao'
WS_NFCE_RET_AUTORIZACAO = 'NfeRetAutorizacao'
WS_NFCE_CANCELAMENTO = 'RecepcaoEventoCancelamento'
WS_NFCE_INUTILIZACAO = 'NfeInutilizacao'
WS_NFCE_CONSULTA = 'NfeConsultaProtocolo'
WS_NFCE_SITUACAO = 'NfeStatusServico'
WS_NFCE_CADASTRO = 'NfeConsultaCadastro'
WS_NFCE_RECEPCAO_EVENTO = 'RecepcaoEventoCarta'
WS_NFCE_QR_CODE = 'NfeQRCode'
WS_NFE_CADASTRO = 'NfeConsultaCadastro'
WS_DPEC_RECEPCAO = 'RecepcaoEventoEPEC' WS_DPEC_RECEPCAO = 'RecepcaoEventoEPEC'
WS_DPEC_CONSULTA = 8 WS_DPEC_CONSULTA = 8
@ -20,6 +31,11 @@ WS_DFE_DISTRIBUICAO = 12
NFE_AMBIENTE_PRODUCAO = 1 NFE_AMBIENTE_PRODUCAO = 1
NFE_AMBIENTE_HOMOLOGACAO = 2 NFE_AMBIENTE_HOMOLOGACAO = 2
NFCE_AMBIENTE_PRODUCAO = 1
NFCE_AMBIENTE_HOMOLOGACAO = 2
NFE_MODELO = u'55'
NFCE_MODELO = u'65'
SIGLA_ESTADO = { SIGLA_ESTADO = {
'12': 'AC', '12': 'AC',
@ -54,49 +70,9 @@ SIGLA_ESTADO = {
def localizar_url(servico, estado, mod=55, ambiente=2): def localizar_url(servico, estado, mod=55, ambiente=2):
sigla = SIGLA_ESTADO[estado] sigla = SIGLA_ESTADO[estado]
dominio = ESTADO_WS[sigla][ambiente]['servidor']
complemento = ESTADO_WS[sigla][ambiente][servico]
if mod == 65:
if sigla == 'SP':
if servico == WS_NFE_AUTORIZACAO:
dominio = 'https://homologacao.nfce.fazenda.sp.gov.br/ws/\
nfeautorizacao.asmx'
if servico == WS_NFE_RET_AUTORIZACAO:
dominio = 'https://homologacao.nfce.fazenda.sp.gov.br/ws/\
nferetautorizacao.asmx'
if servico == WS_NFE_INUTILIZACAO:
dominio = 'https://homologacao.nfce.fazenda.sp.gov.br/ws/\
nfeinutilizacao2.asmx'
if servico == WS_NFE_CONSULTA:
dominio = 'https://homologacao.nfce.fazenda.sp.gov.br/ws/\
nfeconsulta2.asmx'
if servico == WS_NFE_SITUACAO:
dominio = 'https://homologacao.nfce.fazenda.sp.gov.br/ws/\
nfestatusservico2.asmx'
if servico == WS_NFE_RECEPCAO_EVENTO:
dominio = 'https://homologacao.nfce.fazenda.sp.gov.br/ws/\
recepcaoevento.asmx'
if sigla == 'RS':
if servico == WS_NFE_AUTORIZACAO:
dominio = 'https://nfce-homologacao.sefazrs.rs.gov.br/ws/\
NfeAutorizacao/NFeAutorizacao.asmx'
if servico == WS_NFE_RET_AUTORIZACAO:
dominio = 'https://nfce-homologacao.sefazrs.rs.gov.br/ws/\
NfeRetAutorizacao/NFeRetAutorizacao.asmx'
if servico == WS_NFE_INUTILIZACAO:
dominio = 'https://nfce-homologacao.sefazrs.rs.gov.br/ws/\
nfeinutilizacao/nfeinutilizacao2.asmx'
if servico == WS_NFE_CONSULTA:
dominio = 'https://nfce-homologacao.sefazrs.rs.gov.br/ws/\
NfeConsulta/NfeConsulta2.asmx'
if servico == WS_NFE_SITUACAO:
dominio = 'https://nfce-homologacao.sefazrs.rs.gov.br/ws/\
NfeStatusServico/NfeStatusServico2.asmx'
if servico == WS_NFE_RECEPCAO_EVENTO:
dominio = 'https://nfce-homologacao.sefazrs.rs.gov.br/ws/\
recepcaoevento/recepcaoevento.asmx'
else:
dominio = ESTADO_WS[sigla][mod][ambiente]['servidor']
complemento = ESTADO_WS[sigla][mod][ambiente][servico]
if sigla == 'RS' and servico == WS_NFE_CADASTRO: if sigla == 'RS' and servico == WS_NFE_CADASTRO:
dominio = 'cad.sefazrs.rs.gov.br' dominio = 'cad.sefazrs.rs.gov.br'
if sigla in ('AC', 'RN', 'PB', 'SC') and \ if sigla in ('AC', 'RN', 'PB', 'SC') and \
@ -106,6 +82,14 @@ recepcaoevento/recepcaoevento.asmx'
return "https://%s/%s" % (dominio, complemento) return "https://%s/%s" % (dominio, complemento)
def localizar_qrcode(estado, ambiente=2):
sigla = SIGLA_ESTADO[estado]
dominio = ESTADO_WS[sigla]['65'][ambiente]['servidor']
complemento = ESTADO_WS[sigla]['65'][ambiente][WS_NFCE_QR_CODE]
return "https://%s/%s" % (dominio, complemento)
METODO_WS = { METODO_WS = {
WS_NFE_AUTORIZACAO: { WS_NFE_AUTORIZACAO: {
'webservice': 'NfeAutorizacao', 'webservice': 'NfeAutorizacao',
@ -529,6 +513,7 @@ UFRS = {
UFSP = { UFSP = {
NFE_MODELO: {
NFE_AMBIENTE_PRODUCAO: { NFE_AMBIENTE_PRODUCAO: {
'servidor': 'nfe.fazenda.sp.gov.br', 'servidor': 'nfe.fazenda.sp.gov.br',
WS_NFE_AUTORIZACAO: 'ws/nfeautorizacao.asmx', WS_NFE_AUTORIZACAO: 'ws/nfeautorizacao.asmx',
@ -538,7 +523,6 @@ UFSP = {
WS_NFE_SITUACAO: 'ws/nfestatusservico2.asmx', WS_NFE_SITUACAO: 'ws/nfestatusservico2.asmx',
WS_NFE_CADASTRO: 'ws/cadconsultacadastro2.asmx', WS_NFE_CADASTRO: 'ws/cadconsultacadastro2.asmx',
WS_NFE_RECEPCAO_EVENTO: 'ws/recepcaoevento.asmx', WS_NFE_RECEPCAO_EVENTO: 'ws/recepcaoevento.asmx',
WS_NFE_CANCELAMENTO: 'ws/recepcaoevento.asmx',
}, },
NFE_AMBIENTE_HOMOLOGACAO: { NFE_AMBIENTE_HOMOLOGACAO: {
'servidor': 'homologacao.nfe.fazenda.sp.gov.br', 'servidor': 'homologacao.nfe.fazenda.sp.gov.br',
@ -549,7 +533,31 @@ UFSP = {
WS_NFE_SITUACAO: 'ws/nfestatusservico2.asmx', WS_NFE_SITUACAO: 'ws/nfestatusservico2.asmx',
WS_NFE_CADASTRO: 'ws/cadconsultacadastro2.asmx', WS_NFE_CADASTRO: 'ws/cadconsultacadastro2.asmx',
WS_NFE_RECEPCAO_EVENTO: 'ws/recepcaoevento.asmx', WS_NFE_RECEPCAO_EVENTO: 'ws/recepcaoevento.asmx',
WS_NFE_CANCELAMENTO: 'ws/recepcaoevento.asmx',
}
},
NFCE_MODELO: {
NFCE_AMBIENTE_PRODUCAO: {
'servidor': 'nfce.fazenda.sp.gov.br',
WS_NFCE_AUTORIZACAO: 'ws/nfeautorizacao.asmx',
WS_NFCE_RET_AUTORIZACAO: 'ws/nferetautorizacao.asmx',
WS_NFCE_INUTILIZACAO: 'ws/nfeinutilizacao2.asmx',
WS_NFCE_CONSULTA: 'ws/nfeconsulta2.asmx',
WS_NFCE_SITUACAO: 'ws/nfestatusservico2.asmx',
WS_NFCE_CADASTRO: 'ws/cadconsultacadastro2.asmx',
WS_NFCE_RECEPCAO_EVENTO: 'ws/recepcaoevento.asmx',
WS_NFCE_QR_CODE: '',
},
NFCE_AMBIENTE_HOMOLOGACAO: {
'servidor': 'homologacao.nfce.fazenda.sp.gov.br',
WS_NFCE_AUTORIZACAO: 'ws/nfeautorizacao.asmx',
WS_NFCE_RET_AUTORIZACAO: 'ws/nferetautorizacao.asmx',
WS_NFCE_INUTILIZACAO: 'ws/nfeinutilizacao2.asmx',
WS_NFCE_CONSULTA: 'ws/nfeconsulta2.asmx',
WS_NFCE_SITUACAO: 'ws/nfestatusservico2.asmx',
WS_NFCE_CADASTRO: 'ws/cadconsultacadastro2.asmx',
WS_NFCE_RECEPCAO_EVENTO: 'ws/recepcaoevento.asmx',
WS_NFCE_QR_CODE: 'NFCEConsultaPublica/Paginas/ConstultaQRCode.aspx',
}
} }
} }

33
pytrustnfe/nfe/__init__.py

@ -10,7 +10,7 @@ from .assinatura import Assinatura
from pytrustnfe.xml import render_xml from pytrustnfe.xml import render_xml
from pytrustnfe.utils import CabecalhoSoap from pytrustnfe.utils import CabecalhoSoap
from pytrustnfe.utils import gerar_chave, ChaveNFe from pytrustnfe.utils import gerar_chave, ChaveNFe
from pytrustnfe.Servidores import localizar_url
from pytrustnfe.Servidores import localizar_url, localizar_qrcode
def _build_header(method, **kwargs): def _build_header(method, **kwargs):
@ -61,9 +61,31 @@ def _add_required_node(elemTree):
return elemTree return elemTree
def _add_qrCode(xml, **kwargs):
xml = etree.fromstring(xml)
inf_nfe = kwargs['NFes'][0]['infNFe']
nfe = xml.find(".//{http://www.portalfiscal.inf.br/nfe}NFe")
infnfesupl = etree.Element('infNFeSupl')
qrcode = etree.Element('qrCode')
qrcode_url = localizar_qrcode(kwargs['estado'], kwargs['ambiente'])
chave_nfe = inf_nfe['Id'][3:]
dh_emissao = inf_nfe['ide']['dhEmi'].encode('hex')
versao = 100
ambiente = kwargs['ambiente']
valor_total = inf_nfe['total']['vNF']
if inf_nfe.get('dest', False):
dest_cpf = inf_nfe['dest'].get('CPF', False)
icms_total = inf_nfe['total']['vICMS']
dig_val_tag = xml.find(
".//{http://www.portalfiscal.inf.br/nfe}Signature/SignedInfo/Reference/DigestValue")
dig_val = dig_val_tag.text.encode('hex')
qrcode_text = qrcode_url
def _send(certificado, method, sign, **kwargs): def _send(certificado, method, sign, **kwargs):
path = os.path.join(os.path.dirname(__file__), 'templates') path = os.path.join(os.path.dirname(__file__), 'templates')
modelo = kwargs['NFes'][0]['infNFe']['ide']['mod']
xmlElem_send = render_xml(path, '%s.xml' % method, True, **kwargs) xmlElem_send = render_xml(path, '%s.xml' % method, True, **kwargs)
if sign: if sign:
# Caso for autorização temos que adicionar algumas tags tipo # Caso for autorização temos que adicionar algumas tags tipo
@ -78,10 +100,15 @@ def _send(certificado, method, sign, **kwargs):
elif method == 'RecepcaoEventoCancelamento': elif method == 'RecepcaoEventoCancelamento':
xml_send = signer.assina_xml( xml_send = signer.assina_xml(
xmlElem_send, kwargs['eventos'][0]['Id']) xmlElem_send, kwargs['eventos'][0]['Id'])
if modelo == '65':
_add_qrCode(xml_send, **kwargs)
else: else:
xml_send = etree.tostring(xmlElem_send) xml_send = etree.tostring(xmlElem_send)
url = localizar_url(method, kwargs['estado'], kwargs['ambiente'])
url = localizar_url(method, kwargs['estado'], modelo,
kwargs['ambiente'])
cabecalho = _build_header(method, **kwargs) cabecalho = _build_header(method, **kwargs)
response, obj = executar_consulta(certificado, url, cabecalho, xml_send) response, obj = executar_consulta(certificado, url, cabecalho, xml_send)

6
pytrustnfe/nfe/templates/NfeAutorizacao.xml

@ -14,7 +14,9 @@
<serie>{{ ide.serie }}</serie> <serie>{{ ide.serie }}</serie>
<nNF>{{ ide.nNF }}</nNF> <nNF>{{ ide.nNF }}</nNF>
<dhEmi>{{ ide.dhEmi }}</dhEmi> <dhEmi>{{ ide.dhEmi }}</dhEmi>
{% if ide.mod != '65' %}
<dhSaiEnt>{{ ide.dhSaiEnt }}</dhSaiEnt> <dhSaiEnt>{{ ide.dhSaiEnt }}</dhSaiEnt>
{% endif %}
<tpNF>{{ ide.tpNF }}</tpNF> <tpNF>{{ ide.tpNF }}</tpNF>
<idDest>{{ ide.idDest }}</idDest> <idDest>{{ ide.idDest }}</idDest>
<cMunFG>{{ ide.cMunFG }}</cMunFG> <cMunFG>{{ ide.cMunFG }}</cMunFG>
@ -462,6 +464,7 @@
</ICMSSN900> </ICMSSN900>
{% endif %} {% endif %}
</ICMS> </ICMS>
{% if NFe.infNFe.ide.mod != '65' %}
<IPI> <IPI>
<clEnq>{{ imposto.IPI.clEnq }}</clEnq> <clEnq>{{ imposto.IPI.clEnq }}</clEnq>
<CNPJProd>{{ imposto.IPI.CNPJProd }}</CNPJProd> <CNPJProd>{{ imposto.IPI.CNPJProd }}</CNPJProd>
@ -484,6 +487,7 @@
</IPINT> </IPINT>
{% endif %} {% endif %}
</IPI> </IPI>
{% endif %}
{% if imposto.II is defined %} {% if imposto.II is defined %}
<II> <II>
<vBC>{{ imposto.II.vBC }}</vBC> <vBC>{{ imposto.II.vBC }}</vBC>
@ -717,6 +721,7 @@
{% endif %} {% endif %}
</transp> </transp>
{% if NFe.infNFe.cobr is defined %} {% if NFe.infNFe.cobr is defined %}
{% if NFe.infNFe.ide.mod != '65' %}
<cobr> <cobr>
{% if NFe.infNFe.cobr.fat is defined %} {% if NFe.infNFe.cobr.fat is defined %}
<fat> <fat>
@ -737,6 +742,7 @@
{% endif %} {% endif %}
</cobr> </cobr>
{% endif %} {% endif %}
{% endif %}
{% if NFe.infNFe.pag is defined %} {% if NFe.infNFe.pag is defined %}
{% for pag in NFe.infNFe.pag %} {% for pag in NFe.infNFe.pag %}
<pag> <pag>

Loading…
Cancel
Save