Skip to content

Commit

Permalink
Merge pull request #11 from danimaribeiro/feature/normalize
Browse files Browse the repository at this point in the history
Filtro para remover alguns caracteres especiais de certos campos
  • Loading branch information
danimaribeiro committed Dec 16, 2016
2 parents 153eab4 + 880754b commit f471e73
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 215 deletions.
50 changes: 25 additions & 25 deletions pytrustnfe/nfe/templates/NfeAutorizacao.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
<serie>{{ ide.serie }}</serie>
<nNF>{{ ide.nNF }}</nNF>
<dhEmi>{{ ide.dhEmi }}</dhEmi>
{% if ide.mod != '65' %}
<dhSaiEnt>{{ ide.dhSaiEnt }}</dhSaiEnt>
{% endif %}
{% if ide.mod != '65' %}
<dhSaiEnt>{{ ide.dhSaiEnt }}</dhSaiEnt>
{% endif %}
<tpNF>{{ ide.tpNF }}</tpNF>
<idDest>{{ ide.idDest }}</idDest>
<cMunFG>{{ ide.cMunFG }}</cMunFG>
Expand Down Expand Up @@ -78,14 +78,14 @@
{% if emit.tipo == 'company' -%}
<CNPJ>{{ emit.cnpj_cpf }}</CNPJ>
{% endif %}
<xNome>{{ emit.xNome }}</xNome>
<xFant>{{ emit.xFant }}</xFant>
<xNome>{{ emit.xNome|normalize }}</xNome>
<xFant>{{ emit.xFant|normalize }}</xFant>
<enderEmit>
<xLgr>{{ emit.enderEmit.xLgr }}</xLgr>
<xLgr>{{ emit.enderEmit.xLgr|normalize }}</xLgr>
<nro>{{ emit.enderEmit.nro }}</nro>
<xBairro>{{ emit.enderEmit.xBairro }}</xBairro>
<xBairro>{{ emit.enderEmit.xBairro|normalize }}</xBairro>
<cMun>{{ emit.enderEmit.cMun }}</cMun>
<xMun>{{ emit.enderEmit.xMun }}</xMun>
<xMun>{{ emit.enderEmit.xMun|normalize }}</xMun>
<UF>{{ emit.enderEmit.UF }}</UF>
<CEP>{{ emit.enderEmit.CEP }}</CEP>
<cPais>{{ emit.enderEmit.cPais }}</cPais>
Expand Down Expand Up @@ -113,13 +113,13 @@
<CNPJ>{{ dest.cnpj_cpf }}</CNPJ>
{% endif %}
{% endif %}
<xNome>{{ dest.xNome }}</xNome>
<xNome>{{ dest.xNome|normalize }}</xNome>
<enderDest>
<xLgr>{{ dest.enderDest.xLgr }}</xLgr>
<xLgr>{{ dest.enderDest.xLgr|normalize }}</xLgr>
<nro>{{ dest.enderDest.nro }}</nro>
<xBairro>{{ dest.enderDest.xBairro }}</xBairro>
<xBairro>{{ dest.enderDest.xBairro|normalize }}</xBairro>
<cMun>{{ dest.enderDest.cMun }}</cMun>
<xMun>{{ dest.enderDest.xMun }}</xMun>
<xMun>{{ dest.enderDest.xMun|normalize }}</xMun>
<UF>{{ dest.enderDest.UF }}</UF>
<CEP>{{ dest.enderDest.CEP }}</CEP>
<cPais>{{ dest.enderDest.cPais }}</cPais>
Expand Down Expand Up @@ -175,7 +175,7 @@
{% with prod = det.prod %}
<cProd>{{ prod.cProd }}</cProd>
<cEAN>{{ prod.cEAN }}</cEAN>
<xProd>{{ prod.xProd }}</xProd>
<xProd>{{ prod.xProd|normalize }}</xProd>
<NCM>{{ prod.NCM }}</NCM>
<NVE>{{ prod.NVE }}</NVE>
<CEST>{{ prod.CEST }}</CEST>
Expand Down Expand Up @@ -235,7 +235,7 @@
</exportInd>
{% endfor %}
{% endif %}
</detExport>
</detExport>
{% endfor %}
{% endif %}
<xPed>{{ prod.xPed }}</xPed>
Expand Down Expand Up @@ -465,7 +465,7 @@
</ICMSSN900>
{% endif %}
</ICMS>
{% if NFe.infNFe.ide.mod != '65' %}
{% if NFe.infNFe.ide.mod != '65' %}
<IPI>
<clEnq>{{ imposto.IPI.clEnq }}</clEnq>
<CNPJProd>{{ imposto.IPI.CNPJProd }}</CNPJProd>
Expand All @@ -488,7 +488,7 @@
</IPINT>
{% endif %}
</IPI>
{% endif %}
{% endif %}
{% if imposto.II is defined %}
<II>
<vBC>{{ imposto.II.vBC }}</vBC>
Expand Down Expand Up @@ -623,7 +623,7 @@
</impostoDevol>
{% endif %}
{% if det.infAdProd %}
<infAdProd>{{ det.infAdProd }}<infAdProd>
<infAdProd>{{ det.infAdProd|normalize }}<infAdProd>
{% endif %}
{% set count = count + 1 %}
</det>
Expand Down Expand Up @@ -722,7 +722,7 @@
{% endif %}
</transp>
{% if NFe.infNFe.cobr is defined %}
{% if NFe.infNFe.ide.mod != '65' %}
{% if NFe.infNFe.ide.mod != '65' %}
<cobr>
{% if NFe.infNFe.cobr.fat is defined %}
<fat>
Expand All @@ -742,7 +742,7 @@
{% endfor %}
{% endif %}
</cobr>
{% endif %}
{% endif %}
{% endif %}
{% if NFe.infNFe.pag is defined %}
{% for pag in NFe.infNFe.pag %}
Expand All @@ -760,21 +760,21 @@
{% endfor %}
{% endif %}
<infAdic>
<infAdFisco>{{ NFe.infNFe.infAdic.infAdFisco }}</infAdFisco>
<infCpl>{{ NFe.infNFe.infAdic.infCpl }}</infCpl>
<infAdFisco>{{ NFe.infNFe.infAdic.infAdFisco|normalize }}</infAdFisco>
<infCpl>{{ NFe.infNFe.infAdic.infCpl|normalize }}</infCpl>
{% if NFe.infNFe.infAdic.obsCont is defined %}
{% for obs in NFe.infNFe.infAdic.obsCont %}
<obsCont>
<xCampo>{{ obs.xCampo }}</xCampo>
<xTexto>{{ obs.xTexto }}</xTexto>
<xCampo>{{ obs.xCampo|normalize }}</xCampo>
<xTexto>{{ obs.xTexto|normalize }}</xTexto>
</obsCont>
{% endfor %}
{% endif %}
{% if NFe.infNFe.infAdic.obsFisco is defined %}
{% for obs in NFe.infNFe.infAdic.obsFisco %}
<obsFisco>
<xCampo>{{ obs.xCampo }}</xCampo>
<xTexto>{{ obs.xTexto }}</xTexto>
<xCampo>{{ obs.xCampo|normalize }}</xCampo>
<xTexto>{{ obs.xTexto|normalize }}</xTexto>
</obsFisco>
{% endfor %}
{% endif %}
Expand Down
4 changes: 2 additions & 2 deletions pytrustnfe/nfe/templates/NfeInutilizacao.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
<serie>{{ obj.serie }}</serie>
<nNFIni>{{ obj.numero_inicio}}</nNFIni>
<nNFFin>{{ obj.numero_fim }}</nNFFin>
<xJust>{{ obj.justificativa }}</xJust>
<xJust>{{ obj.justificativa|normalize }}</xJust>
</infInut>
<inutNFe>
<inutNFe>
2 changes: 1 addition & 1 deletion pytrustnfe/nfe/templates/RecepcaoEventoCancelamento.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<detEvento versao="1.00">
<descEvento>Cancelamento</descEvento>
<nProt>{{ evento.nProt }}</nProt>
<xJust>{{ evento.xJust }}</xJust>
<xJust>{{ evento.xJust|normalize }}</xJust>
</detEvento>
</infEvento>
</evento>
Expand Down
2 changes: 1 addition & 1 deletion pytrustnfe/nfe/templates/RecepcaoEventoCarta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<detEvento versao="1.00">
<descEvento>Carta de Correção</descEvento>
<xCorrecao>
{{ xCorrecao }}
{{ xCorrecao|normalize }}
</xCorrecao>
<xCondUso>
A Carta de Correção é disciplinada pelo § 1º-A do art. 7º do Convênio S/N, de 15 de dezembro de 1970 e pode ser utilizada para regularização de erro ocorrido na emissão de documento fiscal, desde que o erro não esteja relacionado com: I - as variáveis que determinam o valor do imposto tais como: base de cálculo, alíquota, diferença de preço, quantidade, valor da operação ou da prestação; II - a correção de dados cadastrais que implique mudança do remetente ou do destinatário; III - a data de emissão ou de saída.
Expand Down
4 changes: 4 additions & 0 deletions pytrustnfe/test/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import unittest
from datetime import datetime
from pytrustnfe.xml.filters import normalize_str
from pytrustnfe.xml.filters import strip_line_feed
from pytrustnfe.xml.filters import format_percent
from pytrustnfe.xml.filters import format_date
from pytrustnfe.xml.filters import format_datetime
Expand All @@ -24,3 +25,6 @@ def test_xmlfilters(self):
dt = datetime(2016, 9, 17, 12, 12, 12)
self.assertEqual('2016-09-17', format_date(dt.date()))
self.assertEqual('2016-09-17T12:12:12', format_datetime(dt))

word = strip_line_feed(u"olá\ncomo vai\r senhor ")
self.assertEqual(word, u"olá como vai senhor")
3 changes: 2 additions & 1 deletion pytrustnfe/xml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def render_xml(path, template_name, remove_empty, **nfe):
env = Environment(
loader=FileSystemLoader(path), extensions=['jinja2.ext.with_'])

env.filters["normalize"] = filters.normalize_str
env.filters["normalize"] = filters.strip_line_feed
env.filters["normalize_str"] = filters.normalize_str
env.filters["format_percent"] = filters.format_percent
env.filters["format_datetime"] = filters.format_datetime
env.filters["format_date"] = filters.format_date
Expand Down
14 changes: 13 additions & 1 deletion pytrustnfe/xml/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

def normalize_str(string):
"""
Remove special characters and return the ascii string
Remove special characters and strip spaces
"""
if string:
if not isinstance(string, unicode):
Expand All @@ -22,6 +22,18 @@ def normalize_str(string):
return ''


def strip_line_feed(string):
if string:
remap = {
ord(u'\t'): u' ',
ord(u'\n'): u' ',
ord(u'\f'): u' ',
ord(u'\r'): None, # Delete
}
return string.translate(remap).strip()
return string


def format_percent(value):
if value:
return Decimal(value) / 100
Expand Down
Loading

0 comments on commit f471e73

Please sign in to comment.