Skip to content

Commit aae4a45

Browse files
committed
Serializacao: Adequecao de campos de quantidade ao TDec_1104v
1 parent 0713cae commit aae4a45

2 files changed

Lines changed: 91 additions & 2 deletions

File tree

pynfe/processamento/serializacao.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import warnings
66

77
from datetime import datetime
8+
from decimal import Decimal
89

910
import pynfe.utils.xml_writer as xmlw
1011
from pynfe.entidades import Manifesto, NotaFiscal
@@ -268,6 +269,13 @@ def _serializar_autorizados_baixar_xml(
268269
return etree.tostring(raiz, encoding="unicode", pretty_print=True)
269270
else:
270271
return raiz
272+
273+
def _formatarQuantidade(self, quantidade: Decimal) -> str:
274+
return (
275+
str(quantidade.quantize(Decimal("1.0000")).normalize())
276+
if quantidade % 1 != 0
277+
else str(int(quantidade))
278+
)
271279

272280
def _serializar_produto_servico(
273281
self, produto_servico, modelo, tag_raiz="det", retorna_string=True
@@ -290,7 +298,9 @@ def _serializar_produto_servico(
290298
etree.SubElement(prod, "cBenef").text = produto_servico.cbenef
291299
etree.SubElement(prod, "CFOP").text = produto_servico.cfop
292300
etree.SubElement(prod, "uCom").text = produto_servico.unidade_comercial
293-
etree.SubElement(prod, "qCom").text = str(produto_servico.quantidade_comercial or 0)
301+
etree.SubElement(prod, "qCom").text = self._formatarQuantidade(
302+
produto_servico.quantidade_comercial or 0
303+
)
294304
etree.SubElement(prod, "vUnCom").text = str("{:.10f}").format(
295305
produto_servico.valor_unitario_comercial or 0
296306
)
@@ -308,7 +318,9 @@ def _serializar_produto_servico(
308318
)
309319
etree.SubElement(prod, "cEANTrib").text = produto_servico.ean_tributavel
310320
etree.SubElement(prod, "uTrib").text = produto_servico.unidade_tributavel
311-
etree.SubElement(prod, "qTrib").text = str(produto_servico.quantidade_tributavel)
321+
etree.SubElement(prod, "qTrib").text = self._formatarQuantidade(
322+
produto_servico.quantidade_tributavel
323+
)
312324
etree.SubElement(prod, "vUnTrib").text = "{:.10f}".format(
313325
produto_servico.valor_unitario_tributavel or 0
314326
)

tests/test_nfce_serializacao.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,83 @@ def test_codigo_numerico_aleatorio(self):
484484
self.assertEqual(antigo_codigo, self.notafiscal.codigo_numerico_aleatorio)
485485
self.assertEqual(chave_nfce, self.xml[0].attrib["Id"])
486486

487+
488+
# Preenche as classes do pynfe
489+
self.emitente = self.preenche_emitente()
490+
self.cliente = self.preenche_destinatario()
491+
self.preenche_notafiscal_produto()
492+
493+
def test_notafiscal_formatador_de_quantidade(self):
494+
emitente = self.preenche_emitente()
495+
cliente = self.preenche_destinatario()
496+
497+
utc = datetime.timezone.utc
498+
data_emissao = datetime.datetime(2021, 1, 14, 12, 0, 0, tzinfo=utc)
499+
data_saida_entrada = datetime.datetime(2021, 1, 14, 13, 10, 20, tzinfo=utc)
500+
501+
notafiscal = NotaFiscal(
502+
emitente,
503+
cliente,
504+
uf="PR",
505+
natureza_operacao="VENDA",
506+
modelo=55,
507+
serie="1",
508+
numero_nf="222",
509+
data_emissao=data_emissao,
510+
data_saida_entrada=data_saida_entrada,
511+
tipo_documento=1,
512+
municipio="4118402",
513+
tipo_impressao_danfe=1,
514+
forma_emissao="1",
515+
cliente_final=1,
516+
indicador_destino=1,
517+
indicador_presencial=1,
518+
finalidade_emissao="1",
519+
processo_emissao="0",
520+
transporte_modalidade_frete=1,
521+
informacoes_adicionais_interesse_fisco="Teste quantidade decimal",
522+
totais_tributos_aproximado=Decimal("1.23"),
523+
valor_troco=Decimal("0.00"),
524+
)
525+
526+
notafiscal.adicionar_produto_servico(
527+
codigo="001",
528+
descricao="Produto Decimal",
529+
ncm="12345678",
530+
cfop="5102",
531+
unidade_comercial="UN",
532+
quantidade_comercial=Decimal("1.123456"),
533+
valor_unitario_comercial=Decimal("10.00"),
534+
valor_total_bruto=Decimal("11.23"),
535+
unidade_tributavel="UN",
536+
quantidade_tributavel=Decimal("1.123456"),
537+
valor_unitario_tributavel=Decimal("10.00"),
538+
ean="SEM GTIN",
539+
ean_tributavel="SEM GTIN",
540+
ind_total=1,
541+
icms_modalidade="00",
542+
icms_origem=0,
543+
pis_modalidade="99",
544+
cofins_modalidade="99",
545+
pis_valor_base_calculo=Decimal("0.00"),
546+
pis_aliquota_percentual=Decimal("0.00"),
547+
pis_valor=Decimal("0.00"),
548+
cofins_valor_base_calculo=Decimal("0.00"),
549+
cofins_aliquota_percentual=Decimal("0.00"),
550+
cofins_valor=Decimal("0.00"),
551+
valor_tributos_aprox="1.23",
552+
informacoes_adicionais="Teste de casas decimais",
553+
)
554+
555+
notafiscal.adicionar_pagamento(t_pag="01", x_pag="Dinheiro", v_pag=11.23, ind_pag=0)
556+
557+
xml = self.serializa_nfe()
558+
559+
qCom = xml.xpath("//ns:det/ns:prod/ns:qCom", namespaces=self.ns)[0].text
560+
qTrib = xml.xpath("//ns:det/ns:prod/ns:qTrib", namespaces=self.ns)[0].text
561+
562+
self.assertEqual(qCom, "1.1235")
563+
self.assertEqual(qTrib, "1.1235")
487564

488565
if __name__ == "__main__":
489566
unittest.main()

0 commit comments

Comments
 (0)