This repository has been archived by the owner on Jan 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Algumas refatorações sobre mod10, mod11 e febraran... (#21)
* refatora ModService * adiciona FebrabranService * faz algumas refatorações
- Loading branch information
1 parent
a4f6d2c
commit 6890af2
Showing
7 changed files
with
127 additions
and
115 deletions.
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
from ._version import get_versions | ||
from .services import ( # noqa | ||
from .services import ( # noqa: F401 | ||
generate_qrcode_b64image, | ||
generate_barcode_b64image, | ||
parse_unicode_to_alphanumeric, | ||
) | ||
from .wrapper import CobrancasBBWrapper # noqa | ||
from .wrapper import CobrancasBBWrapper # noqa: F401 | ||
|
||
__version__ = get_versions()["version"] | ||
del get_versions |
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,27 @@ | ||
from .mod import ModService | ||
|
||
|
||
class FebrabranService: | ||
def dac_10(self, number): | ||
""" | ||
Método para calcular o dac modulo 10 de um número. | ||
Referência: | ||
- página 14 https://github.com/imobanco/bb-wrapper/blob/7643255ac3d6f4ed1d6086cc2ad37c281659ea95/docs/Layout%20-%20C%C3%B3digo%20de%20Barras%20ATUALIZADO.pdf # noqa | ||
""" | ||
dv = ModService().mod_10(number) | ||
if dv == 10: | ||
dv = 0 | ||
return dv | ||
|
||
def dac_11(self, number): | ||
""" | ||
Método para calcular o dac modulo 11 de um número. | ||
Referências: | ||
- página 16 https://github.com/imobanco/bb-wrapper/blob/7643255ac3d6f4ed1d6086cc2ad37c281659ea95/docs/Layout%20-%20C%C3%B3digo%20de%20Barras%20ATUALIZADO.pdf # noqa | ||
""" | ||
dv = ModService().mod_11(number, False) | ||
if dv in [11, 10]: | ||
dv = 0 | ||
return dv |
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 |
---|---|---|
@@ -1,66 +1,52 @@ | ||
class ModService: | ||
def modulo10(self, num): | ||
def mod_10(self, num): | ||
""" | ||
Método para calcular módulo 10. | ||
Método para calcular o DV módulo 10. | ||
Referências: | ||
- https://pt.wikipedia.org/wiki/D%C3%ADgito_verificador | ||
- página 14 https://github.com/imobanco/bb-wrapper/blob/7643255ac3d6f4ed1d6086cc2ad37c281659ea95/docs/Layout%20-%20C%C3%B3digo%20de%20Barras%20ATUALIZADO.pdf # noqa | ||
- https://github.com/eduardocereto/pyboleto/blob/1fed215eac2c974efc6f03a16b94406c2bb55cc2/pyboleto/data.py#L453 # noqa | ||
""" | ||
if not isinstance(num, str): | ||
raise TypeError | ||
soma = 0 | ||
peso = 2 | ||
for c in reversed(num): | ||
parcial = int(c) * peso | ||
if parcial > 9: | ||
s = str(parcial) | ||
parcial = int(s[0]) + int(s[1]) | ||
soma += parcial | ||
if peso == 2: | ||
peso = 1 | ||
else: | ||
peso = 2 | ||
if isinstance(num, int): | ||
num = str(num) | ||
|
||
resto10 = soma % 10 | ||
if not isinstance(num, str): | ||
raise TypeError("O número deve estar no formato de str!") | ||
|
||
if resto10 == 0: | ||
modulo10 = 0 | ||
else: | ||
modulo10 = 10 - resto10 | ||
total = 0 | ||
factor = 2 | ||
for digit in reversed(num): | ||
result = int(digit) * factor | ||
total += sum([int(d) for d in str(result)]) | ||
factor = (factor % 2) + 1 | ||
|
||
return modulo10 | ||
rest = total % 10 | ||
dv = 10 - rest | ||
return dv | ||
|
||
def modulo11(self, num, base=9, r_base=0): | ||
def mod_11(self, num, multiply_by_10_flag=True, base=9): | ||
""" | ||
Método para calcular módulo 11. | ||
Método para calcular o DV módulo 11. | ||
Referências: | ||
- https://pt.wikipedia.org/wiki/D%C3%ADgito_verificador | ||
- página 16 https://github.com/imobanco/bb-wrapper/blob/7643255ac3d6f4ed1d6086cc2ad37c281659ea95/docs/Layout%20-%20C%C3%B3digo%20de%20Barras%20ATUALIZADO.pdf # noqa | ||
- https://github.com/eduardocereto/pyboleto/blob/1fed215eac2c974efc6f03a16b94406c2bb55cc2/pyboleto/data.py#L478 # noqa | ||
""" | ||
if not isinstance(num, str): | ||
raise TypeError | ||
if isinstance(num, int): | ||
num = str(num) | ||
|
||
if r_base != 0 and r_base != 1: | ||
raise ValueError("O r_base precisa ser 1 ou 0") | ||
|
||
soma = 0 | ||
fator = 2 | ||
for c in reversed(num): | ||
soma += int(c) * fator | ||
if fator == base: | ||
fator = 1 | ||
fator += 1 | ||
|
||
if r_base == 0: | ||
soma = soma * 10 | ||
if not isinstance(num, str): | ||
raise TypeError("O número deve estar no formato de str!") | ||
|
||
digito = soma % 11 | ||
total = 0 | ||
factor = 2 | ||
for digit in reversed(num): | ||
total += int(digit) * factor | ||
factor = (factor % base) + 1 + (factor // base) * 1 | ||
|
||
if r_base == 0 and digito == 10: | ||
digito = 0 | ||
if multiply_by_10_flag: | ||
total = total * 10 | ||
|
||
return digito | ||
rest = total % 11 | ||
dv = 11 - rest | ||
return dv |
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
Oops, something went wrong.