Skip to content

Commit

Permalink
Merge pull request #38 from kmee/formatacao-ie-sp-rn-pe
Browse files Browse the repository at this point in the history
[ADD] Formatação de IE
  • Loading branch information
mileo authored Apr 19, 2023
2 parents 0f0b680 + 3285c3f commit 3a5edbf
Show file tree
Hide file tree
Showing 4 changed files with 658 additions and 38 deletions.
1 change: 0 additions & 1 deletion src/erpbrasil/base/fiscal/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
__version__ = "0.0.0"
276 changes: 256 additions & 20 deletions src/erpbrasil/base/fiscal/ie.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,104 @@
# coding=utf-8
# Copyright (C) 2013 Renato Lima - Akretion
# Copyright (C) 2023 Gabriel Krauss - KMEE
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

import re

PARAMETERS = {
"ac": {"tam": 13, "val_tam": 11, "starts_with": "01"},
"al": {"tam": 9, "starts_with": "24"},
"am": {"tam": 9},
"ce": {"tam": 9},
"df": {"tam": 13, "val_tam": 11},
"es": {"tam": 9},
"ma": {"tam": 9, "starts_with": "12"},
"mt": {"tam": 11, "prod": [3, 2, 9, 8, 7, 6, 5, 4, 3, 2]},
"ms": {"tam": 9, "starts_with": "28"},
"pa": {"tam": 9, "starts_with": "15"},
"pb": {"tam": 9},
"pr": {"tam": 10, "val_tam": 8, "prod": [3, 2, 7, 6, 5, 4, 3, 2]},
"pi": {"tam": 9},
"rj": {"tam": 8, "prod": [2, 7, 6, 5, 4, 3, 2]},
"ac": {
"tam": 13,
"val_tam": 11,
"starts_with": "01",
"format": lambda x: "{0}.{1}.{2}/{3}-{4}".format(
x[:2], x[2:5], x[5:8], x[8:11], x[11:13]
),
},
"al": {
"tam": 9,
"starts_with": "24",
"format": lambda x: "{0}.{1}.{2}-{3}".format(x[:2], x[2:5], x[5:8], x[8:9]),
},
"am": {
"tam": 9,
"format": lambda x: "{0}.{1}.{2}-{3}".format(x[:2], x[2:5], x[5:8], x[8:9]),
},
"ce": {"tam": 9, "format": lambda x: "{0}.{1}-{2}".format(x[:2], x[2:8], x[8:9])},
"df": {
"tam": 13,
"val_tam": 11,
"starts_with": "07",
"format": lambda x: "{0}-{1}.{2}/{3}-{4}".format(
x[:2], x[2:5], x[5:8], x[8:11], x[11:13]
),
},
"es": {
"tam": 9,
"format": lambda x: "{0}.{1}.{2}-{3}".format(x[:3], x[3:6], x[6:8], x[8:9]),
},
"ma": {
"tam": 9,
"starts_with": "12",
"format": lambda x: "{0}.{1}.{2}-{3}".format(x[:2], x[2:5], x[5:8], x[8:9]),
},
"mt": {
"tam": 11,
"prod": [3, 2, 9, 8, 7, 6, 5, 4, 3, 2],
"format": lambda x: "{0}.{1}.{2}.{3}-{4}".format(
x[:2], x[2:4], x[4:6], x[6:10], x[10:11]
),
},
"ms": {
"tam": 9,
"starts_with": "28",
"format": lambda x: "{0}.{1}.{2}-{3}".format(x[:2], x[2:5], x[5:8], x[8:9]),
},
"pa": {
"tam": 9,
"starts_with": "15",
"format": lambda x: "{0}.{1}.{2}-{3}".format(x[:2], x[2:5], x[5:8], x[8:9]),
},
"pb": {
"tam": 9,
"format": lambda x: "{0}.{1}.{2}-{3}".format(x[:2], x[2:5], x[5:8], x[8:9]),
},
"pr": {
"tam": 10,
"val_tam": 8,
"prod": [3, 2, 7, 6, 5, 4, 3, 2],
"format": lambda x: "{0}.{1}-{2}".format(x[:3], x[3:7], x[7:9]),
},
"pi": {
"tam": 9,
"format": lambda x: "{0}.{1}.{2}-{3}".format(x[:2], x[2:5], x[5:8], x[8:9]),
},
"rj": {
"tam": 8,
"prod": [2, 7, 6, 5, 4, 3, 2],
"format": lambda x: "{0}.{1}.{2}-{3}".format(x[:2], x[2:5], x[5:7], x[7:8]),
},
"rn": {"tam": 10, "val_tam": 9, "prod": [10, 9, 8, 7, 6, 5, 4, 3, 2]},
"rs": {"tam": 10},
"rr": {"tam": 9, "starts_with": "24", "prod": [1, 2, 3, 4, 5, 6, 7, 8], "div": 9},
"sc": {"tam": 9},
"se": {"tam": 9},
"to": {"tam": 9, "prod": [9, 8, 7, 6, 5, 4, 3, 2]},
"rs": {
"tam": 10,
"format": lambda x: "{0}/{1}.{2}-{3}".format(x[:3], x[3:6], x[6:9], x[9:10]),
},
"rr": {
"tam": 9,
"starts_with": "24",
"prod": [1, 2, 3, 4, 5, 6, 7, 8],
"div": 9,
"format": lambda x: "{0}.{1}.{2}-{3}".format(x[:2], x[2:5], x[5:8], x[8:9]),
},
"sc": {"tam": 9, "format": lambda x: "{0}.{1}.{2}".format(x[:3], x[3:6], x[6:9])},
"se": {
"tam": 9,
"format": lambda x: "{0}.{1}.{2}-{3}".format(x[:2], x[2:5], x[5:8], x[8:9]),
},
"to": {
"tam": 9,
"prod": [9, 8, 7, 6, 5, 4, 3, 2],
"format": lambda x: "{0}.{1}.{2}-{3}".format(x[:2], x[2:5], x[5:8], x[8:9]),
},
}


Expand Down Expand Up @@ -212,6 +286,17 @@ def validar_go(inscr_est):


def validar_mg(inscr_est):
# A validação de produtor rural foi padronizada com a normal em MG

# A partir de 02 de março de 2009, todos os produtores rurais, pessoa física, de Minas Gerais,
# tiveram que se inscrever no cadastro informatizado da Secretaria de Estado de Fazenda
# (SEF-MG), conforme estabelecido pelo Decreto 45.030, de 29 de fevereiro de 2009.
# A inscrição antiga foi substituída pela Inscrição Estadual de Produtor Rural
# com 13 dígitos, padrão para os contribuintes mineiros,
# devendo ser informada normalmente no arquivo eletrônico.

# Fonte: http://www.fazenda.mg.gov.br/empresas/ped/duvidas_frequentes/

inscr_est = re.sub("[^0-9]", "", inscr_est)

# verificando o tamanho da inscrição estadual
Expand Down Expand Up @@ -290,6 +375,36 @@ def validar_pe(inscr_est):
return nova_ie == inscr_est


def validar_rn(inscr_est):
def gera_digito_rn(nova_ie, prod):
r = (sum([x * y for (x, y) in zip(nova_ie, prod)]) * 10) % 11
if r == 10:
r = 0
return r

inscr_est = re.sub("[^0-9]", "", inscr_est)
inscr_est = list(map(int, inscr_est))
aux = [2, 0]
if inscr_est[:2] == aux[:2]:
if len(inscr_est) == 9:
nova_ie = inscr_est[:8]
prod = [9, 8, 7, 6, 5, 4, 3, 2]
f = gera_digito_rn(nova_ie, prod)
nova_ie.append(f)

elif len(inscr_est) == 10:
nova_ie = inscr_est[:9]
prod = [10, 9, 8, 7, 6, 5, 4, 3, 2]
f = gera_digito_rn(nova_ie, prod)
nova_ie.append(f)
else:
return False
else:
return False

return nova_ie == inscr_est


def validar_ro(inscr_est):
def gera_digito_ro(nova_ie, prod):
r = sum([x * y for (x, y) in zip(nova_ie, prod)]) % 11
Expand Down Expand Up @@ -336,6 +451,10 @@ def gera_digito_sp(nova_ie, prod):
else:
return 1

def gera_digito_sp_pr(nova_ie, prod):
r = sum([x * y for (x, y) in zip(nova_ie, prod)]) % 11
return r

# Industriais e comerciais
if inscr_est[0] != "P":
inscr_est = re.sub("[^0-9]", "", inscr_est)
Expand Down Expand Up @@ -377,7 +496,7 @@ def gera_digito_sp(nova_ie, prod):
nova_ie = inscr_est[:8]

prod = [1, 3, 4, 5, 6, 7, 8, 10]
f = gera_digito_sp(nova_ie, prod)
f = gera_digito_sp_pr(nova_ie, prod)
nova_ie.append(f)

nova_ie.extend(inscr_est[9:])
Expand Down Expand Up @@ -410,3 +529,120 @@ def validar_to(inscr_est):
f = 0
nova_ie.append(f)
return nova_ie == inscr_est


def formata(uf, inscr_est):
try:
formata_by_uf = globals()["formata_%s" % uf]
inscr_est = formata_by_uf(inscr_est)
except KeyError:
inscr_est = re.sub("[^0-9]", "", inscr_est)
inscr_est = formata_param(uf, inscr_est)

return inscr_est


def formata_param(uf, inscr_est):
if uf not in PARAMETERS:
return inscr_est

tam = PARAMETERS[uf].get("tam", 0)
inscr_est = inscr_est.strip().rjust(int(tam), "0")
inscr_est = re.sub("[^0-9]", "", inscr_est)

if len(inscr_est) == tam:
format_lambda = PARAMETERS[uf].get("format")
inscr_est = format_lambda(inscr_est)

return inscr_est


def formata_ap(inscr_est):
tam = 9
inscr_est = inscr_est.strip().rjust(int(tam), "0")
inscr_est = re.sub("[^0-9]", "", inscr_est)
if len(inscr_est) == tam:
inscr_est = "{0}.{1}.{2}-{3}".format(
inscr_est[:2], inscr_est[2:5], inscr_est[5:8], inscr_est[8:9]
)
return inscr_est


def formata_ba(inscr_est):
inscr_est = re.sub("[^0-9]", "", inscr_est)
if len(inscr_est) == 8:
inscr_est = inscr_est.rjust(9, "0")
if len(inscr_est) == 9:
inscr_est = "{0}.{1}.{2}".format(inscr_est[:3], inscr_est[3:6], inscr_est[6:9])

return inscr_est


def formata_go(inscr_est):
tam = 9
inscr_est = inscr_est.strip().rjust(int(tam), "0")
inscr_est = re.sub("[^0-9]", "", inscr_est)
if len(inscr_est) == tam:
inscr_est = "{0}.{1}.{2}-{3}".format(
inscr_est[:2], inscr_est[2:5], inscr_est[5:8], inscr_est[8:9]
)

return inscr_est


def formata_mg(inscr_est):
inscr_est = re.sub("[^0-9]", "", inscr_est)
if len(inscr_est) == 13:
inscr_est = "{0}.{1}.{2}/{3}-{4}".format(
inscr_est[:3],
inscr_est[3:6],
inscr_est[6:9],
inscr_est[9:11],
inscr_est[11:13],
)
return inscr_est


def formata_pe(inscr_est):
inscr_est = re.sub("[^0-9]", "", inscr_est)
if len(inscr_est) == 9:
inscr_est = "{0}-{1}".format(inscr_est[:7], inscr_est[7:9])

if len(inscr_est) == 14:
inscr_est = "{0}.{1}.{2}.{3}-{4}".format(
inscr_est[:2], inscr_est[2], inscr_est[3:6], inscr_est[6:13], inscr_est[13]
)
return inscr_est


def formata_rn(inscr_est):
inscr_est = re.sub("[^0-9]", "", inscr_est)
if len(inscr_est) == 9:
inscr_est = "{0}.{1}.{2}-{3}".format(
inscr_est[:2], inscr_est[2:5], inscr_est[5:8], inscr_est[8:9]
)

if len(inscr_est) == 10:
inscr_est = "{0}.{1}.{2}-{3}".format(
inscr_est[:3], inscr_est[3:6], inscr_est[6:9], inscr_est[9:10]
)
return inscr_est


def formata_sp(inscr_est):
# Adicionar a formatação para IEs normal e para produtor rural
if re.match("P", inscr_est) is None:
# Se IE normal
inscr_est = re.sub("[^0-9]", "", inscr_est)
if len(inscr_est) == 12:
inscr_est = "{0}.{1}.{2}.{3}".format(
inscr_est[:3], inscr_est[3:6], inscr_est[6:9], inscr_est[9:12]
)
else:
# Se produtor rural
inscr_est = re.sub("[^0-9]", "", inscr_est)
if len(inscr_est) == 12:
inscr_est = "P-{0}.{1}/{2}".format(
inscr_est[:8], inscr_est[8:9], inscr_est[9:12]
)
return inscr_est
Loading

0 comments on commit 3a5edbf

Please sign in to comment.