Skip to content

Commit

Permalink
[REF] Move EDOC KEY to erpbrasil.base
Browse files Browse the repository at this point in the history
  • Loading branch information
mileo committed Apr 6, 2021
1 parent ce27b9d commit ea6d33e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 59 deletions.
77 changes: 19 additions & 58 deletions l10n_br_fiscal/models/document_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@
MODELO_FISCAL_CTE,
)


_logger = logging.getLogger(__name__)

try:
from erpbrasil.base import misc
from erpbrasil.base.fiscal.edoc import ChaveEdoc
except ImportError:
_logger.error("Biblioteca erpbrasil.base não instalada")

Expand Down Expand Up @@ -262,74 +261,36 @@ def _generate_key(self):
MODELO_FISCAL_NFE,
MODELO_FISCAL_NFCE,
MODELO_FISCAL_CTE):
company = record.company_id.partner_id
chave = str(company.state_id
and company.state_id.ibge_code or "").zfill(2)

chave += record.date.strftime("%y%m").zfill(4)

chave += str(misc.punctuation_rm(
record.company_id.partner_id.cnpj_cpf)).zfill(14)
chave += str(record.document_type_id.code or "").zfill(2)
chave += str(record.document_serie or "").zfill(3)
chave += str(record.number or "").zfill(9)

#
# A inclusão do tipo de emissão na chave já torna a chave válida
# também para a versão 2.00 da NF-e
#
chave += str(1).zfill(1)

#
# O código numério é um número aleatório
#
# chave += str(random.randint(0, 99999999)).strip().rjust(8, '0')

#
# Mas, por segurança, é preferível que esse número não seja
# aleatório
#
soma = 0
for c in chave:
soma += int(c) ** 3 ** 2

codigo = str(soma)
if len(codigo) > 8:
codigo = codigo[-8:]
else:
codigo = codigo.rjust(8, "0")

chave += codigo

soma = 0
m = 2
for i in range(len(chave) - 1, -1, -1):
c = chave[i]
soma += int(c) * m
m += 1
if m > 9:
m = 2

digito = 11 - (soma % 11)
if digito > 9:
digito = 0

chave += str(digito)
key = record.document_type_id.prefix + chave
record.key = key
chave_edoc = ChaveEdoc(
ano_mes=record.date.strftime("%y%m").zfill(4),
cnpj_emitente=record.company_cnpj_cpf,
codigo_uf=(
record.company_state_id and
record.company_state_id.ibge_code or ""
),
forma_emissao=1, # TODO: Implementar campo no Odoo
modelo_documento=record.document_type_id.code or "",
numero_documento=record.number or "",
numero_serie=record.document_serie or "",
validar=True,
)
# TODO: Implementar campos no Odoo
# record.key_number = chave_edoc.campos
# record.key_formated = ' '.joint(chave_edoc.partes())
record.key = chave_edoc.chave

def document_number(self):
if self.issuer == DOCUMENT_ISSUER_COMPANY:
if not self.number and self.document_serie_id:
self.number = self.document_serie_id.next_seq_number()
self.document_serie = self.document_serie_id.code

if not self.operation_name:
self.operation_name = ', '.join(
[l.name for l in self.line_ids.mapped(
'fiscal_operation_id')])

if self.document_electronic and not self.key:
self.document_serie = self.document_serie_id.code
self._generate_key()

def _document_confirm(self):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
num2words==0.5.4
pycep-correios==5.0.0
workalendar==7.1.1
erpbrasil.base==2.0.0
erpbrasil.base==2.1.0
erpbrasil.assinatura==1.0.2
erpbrasil.transmissao==1.0.0
erpbrasil.edoc==1.1.0
Expand Down

0 comments on commit ea6d33e

Please sign in to comment.