-
Notifications
You must be signed in to change notification settings - Fork 171
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
Serialização do grupo de pagamentos #318
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
defbf8d
ADD: metodos de formatação de Decimais
7a7e218
Update .gitignore
a95e0a1
Update test_nfce_serializacao.py
9369ce7
Update __init__.py
87e652a
ADD: adicionado flags de FORMAS_PAGAMENTO e BANDEIRA_CARTAO
0a376a6
FIX: `valor_troco`corrigido para permitir valor zerado
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,3 +77,5 @@ target/ | |
db.* | ||
|
||
TODO* | ||
|
||
venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from lxml import etree | ||
from decimal import Decimal | ||
from . import ajustar_valor, is_empty | ||
from typing import Literal, Any | ||
|
||
__MIN_LEN_ERROR = 'Tamanho do campo {} é menor que o mímino permitido "{}"' | ||
__MAX_LEN_ERROR = 'Tamanho do campo {} é maior que o mímino permitido "{}"' | ||
|
||
def write_txt(root: etree.ElementTree, tag_name, value: str, required: bool, min_len=0, max_len=0): | ||
""" | ||
:param root: XML root | ||
:param tag_name: Nome da TAG a ser escrita (str) | ||
:param value: Valor a ser escrito na TAG (str) | ||
:param min_len: comprimento minimo | ||
:param max-len: comprimento maximo | ||
:param required: Se `True` e esta vazio, escreve a TAG vazia, do contrario não precisa gerar a TAG | ||
""" | ||
tag_value = str(value).strip() | ||
|
||
# retorna sem gerar nada se a TAG não é obrigatoria | ||
if is_empty(tag_value) and not required: | ||
return | ||
|
||
if len(tag_value) < min_len: | ||
raise Exception(__MIN_LEN_ERROR.format(len(tag_value), min_len)) | ||
if max_len > 0 and len(tag_value) > max_len: | ||
raise Exception(__MAX_LEN_ERROR.format(len(tag_value), max_len)) | ||
|
||
etree.SubElement(root, tag_name).text = tag_value | ||
|
||
def write_float(root: etree.ElementTree, tag_name: str, value: Decimal, required: bool, decimal_places = 2, min_decimals = 0, iat: Literal["ROUND", "TRUNC"]="ROUND"): | ||
""" | ||
:param root: XML root | ||
:param tag_name: Nome da TAG a ser escrita (str) | ||
:param value: Valor a ser escrito na TAG (str) | ||
:param decimal_places: casas decimais | ||
:param min_decimals: numero minimo de casas decimais | ||
:param required: Se `True` e esta vazio, escreve a TAG vazia, do contrario não precisa gerar a TAG (considera 0 como vazio) | ||
:param iat: indice de arredondamento/truncamento (default: ROUND) | ||
""" | ||
|
||
# retorna sem gerar nada se a TAG não é obrigatoria | ||
if is_empty(Decimal(value or '0')): | ||
if not required: | ||
return | ||
raise Exception(f"{tag_name} - Valor requerido e não informado") | ||
|
||
tag_value = ajustar_valor( | ||
value=value.__float__(), | ||
decimal_places=decimal_places, | ||
min_decimal_places=min_decimals, | ||
tipo=iat | ||
) | ||
|
||
etree.SubElement(root, tag_name).text = tag_value | ||
|
||
def write_int(root: etree.ElementTree, tag_name: str, value: int, required: bool): | ||
""" | ||
:param root: XML root | ||
:param tag_name: Nome da TAG a ser escrita (str) | ||
:param value: Valor a ser escrito na TAG (str) | ||
:param required: Se `True` e esta vazio, escreve a TAG vazia, do contrario não precisa gerar a TAG (considera 0 como vazio) | ||
:param zero_is_empty: se `True` e o valor for zero será tratado como vazio | ||
""" | ||
|
||
# retorna sem gerar nada se a TAG não é obrigatoria | ||
if is_empty(value): | ||
if not required: | ||
return | ||
raise Exception(f"{tag_name} - Valor requerido e não informado") | ||
|
||
etree.SubElement(root, tag_name).text = int(value) | ||
|
||
|
||
def write_tag(root: etree.ElementTree, tag_name: str, value: Any, required: bool): | ||
# retorna sem gerar nada se a TAG não é obrigatoria | ||
if is_empty(value): | ||
if not required: | ||
return | ||
raise Exception(f"{tag_name} - Valor requerido e não informado") | ||
|
||
etree.SubElement(root, tag_name).text = str(value) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Teria como fazer essa alteração não ser uma breaking change? Como verificar se existe o
tipo_pagamento
preenchido, garantindo que todos que utilizam atualmente a PyNFE possam migrar gradativamente.De preferencia, pode também botar um warning no console avisando que usar o
tipo_pagamento
ficará depreciado e que usaremos somente oadicionar_pagamento
.Dessa forma também nos testes devemos ter por enquanto uma validação que garanta que a forma antiga e nova funcionam.