|
|
|
@ -4,6 +4,7 @@ |
|
|
|
|
|
|
|
|
|
|
|
import os |
|
|
|
import hashlib |
|
|
|
from lxml import etree |
|
|
|
from .comunicacao import executar_consulta |
|
|
|
from .assinatura import Assinatura |
|
|
|
@ -67,26 +68,51 @@ def _add_qrCode(xml, **kwargs): |
|
|
|
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 |
|
|
|
versao = '100' |
|
|
|
ambiente = kwargs['ambiente'] |
|
|
|
valor_total = inf_nfe['total']['vNF'] |
|
|
|
dest_cpf = 'Inexistente' |
|
|
|
dest = nfe.find(".//{http://www.portalfiscal.inf.br/nfe}dest") |
|
|
|
dest_parent = dest.getparent() |
|
|
|
dest_parent.remove(dest) |
|
|
|
if inf_nfe.get('dest', False): |
|
|
|
dest_cpf = inf_nfe['dest'].get('CPF', False) |
|
|
|
if inf_nfe['dest'].get('CPF', False): |
|
|
|
dest_cpf = inf_nfe['dest']['CPF'] |
|
|
|
dest = etree.Element('dest') |
|
|
|
cpf = etree.Element('CPF') |
|
|
|
cpf.text = dest_cpf |
|
|
|
dest.append(cpf) |
|
|
|
dest_parent.append(dest) |
|
|
|
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 |
|
|
|
dig_val = xml.find( |
|
|
|
".//{http://www.w3.org/2000/09/xmldsig#}DigestValue").text.encode('hex') |
|
|
|
cid_token = kwargs['NFes'][0]['infNFe']['codigo_seguranca']['cid_token'] |
|
|
|
csc = kwargs['NFes'][0]['infNFe']['codigo_seguranca']['csc'] |
|
|
|
|
|
|
|
c_hash_QR_code = "chNFe={0}&nVersao={1}&tpAmb={2}&cDest={3}&dhEmi={4}&vNF={5}&vICMS={6}&digVal={7}&cIdToken={8}{9}".\ |
|
|
|
format(chave_nfe, versao, ambiente, dest_cpf, dh_emissao, |
|
|
|
valor_total, icms_total, dig_val, cid_token, csc) |
|
|
|
c_hash_QR_code = hashlib.sha1(c_hash_QR_code).hexdigest() |
|
|
|
|
|
|
|
QR_code_url = "?chNFe={0}&nVersao={1}&tpAmb={2}&{3}dhEmi={4}&vNF={5}&vICMS={6}&digVal={7}&cIdToken={8}&cHashQRCode={9}".\ |
|
|
|
format(chave_nfe, versao, ambiente, |
|
|
|
'cDest={}&'.format(dest_cpf) if dest_cpf != 'Inexistente' else '', |
|
|
|
dh_emissao, valor_total, icms_total, dig_val, cid_token, c_hash_QR_code) |
|
|
|
qr_code_server = localizar_qrcode(kwargs['estado'], ambiente) |
|
|
|
qrcode_text = qr_code_server + QR_code_url |
|
|
|
qrcode.text = qrcode_text |
|
|
|
infnfesupl.append(qrcode) |
|
|
|
nfe.insert(1, infnfesupl) |
|
|
|
return etree.tostring(xml) |
|
|
|
|
|
|
|
|
|
|
|
def _send(certificado, method, sign, **kwargs): |
|
|
|
path = os.path.join(os.path.dirname(__file__), 'templates') |
|
|
|
modelo = '55' |
|
|
|
xmlElem_send = render_xml(path, '%s.xml' % method, True, **kwargs) |
|
|
|
modelo = xmlElem_send.find(".//{http://www.portalfiscal.inf.br/nfe}mod") |
|
|
|
modelo = modelo.text if modelo is not None else '55' |
|
|
|
if sign: |
|
|
|
# Caso for autorização temos que adicionar algumas tags tipo |
|
|
|
# cEan, cEANTrib porque o governo sempre complica e não segue padrão |
|
|
|
@ -102,7 +128,7 @@ def _send(certificado, method, sign, **kwargs): |
|
|
|
xmlElem_send, kwargs['eventos'][0]['Id']) |
|
|
|
|
|
|
|
if modelo == '65': |
|
|
|
_add_qrCode(xml_send, **kwargs) |
|
|
|
xml_send = _add_qrCode(xml_send, **kwargs) |
|
|
|
|
|
|
|
else: |
|
|
|
xml_send = etree.tostring(xmlElem_send) |
|
|
|
|