Skip to content

Commit

Permalink
[NEW] Segmento E
Browse files Browse the repository at this point in the history
  • Loading branch information
mileo committed Nov 12, 2020
1 parent 93ba3c6 commit ba95a4e
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 0 deletions.
1 change: 1 addition & 0 deletions febraban/cnab240/statement/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .parser import StatementParser
55 changes: 55 additions & 0 deletions febraban/cnab240/statement/occurrences.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# coding: utf-8

debit_occurrences = {
'101': 'Cheque Compensado',
'102': 'Encargos',
'103': 'Estornos',
'104': 'Lançamento Avisado',
'105': 'Tarifas',
'106': 'Aplicação',
'107': 'Empréstimo / Financiamento',
'108': 'Câmbio',
'109': 'CPMF',
'110': 'IOF',
'111': 'Imposto de Renda',
'112': 'Pagamento Fornecedores',
'113': 'Pagamentos Salário',
'114': 'Saque Eletrônico',
'115': 'Ações',
'117': 'Transferência entre Contas',
'118': 'Devolução da Compensação',
'119': 'Devolução de Cheque Depositado',
'120': 'Transferência Interbancária (DOC, TED)',
'121': 'Antecipação a Fornecedores',
'122': 'OC / AEROPS',
'123': 'Saque em Espécie',
'124': 'Cheque Pago',
'125': 'Pagamentos Diversos',
'126': 'Pagamento de Tributos',
'127': 'Cartão de crédito - Pagamento de fatura de cartão de crédito da própria IF',
}

credit_occurrences = {
'201': 'Depósito em Cheque',
'202': 'Crédito de Cobrança',
'203': 'Devolução de Cheques',
'204': 'Estornos',
'205': 'Lançamento Avisado',
'206': 'Resgate de Aplicação',
'207': 'Empréstimo / Financiamento',
'208': 'Câmbio',
'209': 'Transferência Interbancária (DOC, TED)',
'210': 'Ações',
'211': 'Dividendos',
'212': 'Seguro',
'213': 'Transferência entre Contas',
'214': 'Depósitos Especiais',
'215': 'Devolução da Compensação',
'216': 'OCT',
'217': 'Pagamentos Fornecedores',
'218': 'Pagamentos Diversos',
'219': 'Recebimento de Salário',
'220': 'Depósito em Espécie',
'221': 'Pagamento de Tributos',
'222': 'Cartão de Crédito - Recebíveis de cartão de crédito',
}
102 changes: 102 additions & 0 deletions febraban/cnab240/statement/parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from .occurrences import debit_occurrences, credit_occurrences


class Statement:

def __init__(
self, content=None, start_date=None, start_amount=None,
start_debit_credit=None, stop_date=False, stop_amount=None,
stop_debit_credit=None, line_quantity=None, debit_sum_in_cents=None,
credit_sum_in_cents=None):
self.content = content or []
self.start_date = start_date
self.start_amount = start_amount
self.start_debit_credit = start_debit_credit
self.stop_date = stop_date
self.stop_amount = stop_amount
self.stop_debit_credit = stop_debit_credit
self.line_quantity = line_quantity
self.debit_sum_in_cents = debit_sum_in_cents
self.credit_sum_in_cents = credit_sum_in_cents
self.lines = []


class StatementLine:

def __init__(self, content=None, occurrence=None, cpmf=None, date_account=None,
date_move=None, amountInCents=None, debit_credit=None,
bank_history_code=None, bank_history_description=None,
document_number=None, errors=None):
self.content = content or []
self.cpmf = cpmf
self.date_account = date_account
self.date_move = date_move
self.amountInCents = amountInCents
self.debit_credit = debit_credit
self.occurrence = occurrence
self.bank_history_code = bank_history_code
self.bank_history_description = bank_history_description
self.document_number = document_number

def occurrenceText(self):
if self.occurrence and self.debit_credit == 'C':
return credit_occurrences[self.occurrence]
elif self.occurrence and self.debit_credit == 'D':
return debit_occurrences[self.occurrence]


def contentText(self, breakLine="\n"):
return breakLine.join(self.content)


class StatementParser:

@classmethod
def parseFile(cls, file):
lines = file.readlines()
return cls.parseLines(lines)

@classmethod
def parseText(cls, text):
lines = text.splitlines()[:-1]
return cls.parseLines(lines)

@classmethod
def parseLines(cls, lines):
statement = None
for line in lines:
if line[7] in ["0", "9"]:
continue
if line[7] == "1" and line[8] == "E":
if not statement:
statement = Statement(
content=[line],
start_date=line[142:150],
start_amount=line[150:168],
start_debit_credit=line[168:169],
)

if line[7] == "5":
statement.content.append(line)
statement.stop_date = line[142:150]
statement.stop_amount = line[150:169]
statement.stop_debit_credit = line[169:170]
statement.line_quantity = line[170:176]
statement.debit_sum_in_cents = int(line[176:194])
statement.credit_sum_in_cents = int(line[194:212])

if line[7] == "3" and line[13] == "E":
statement_line = StatementLine()
statement_line.content.append(line)
statement_line.occurrence = line[15:17]
statement_line.cpmf = line[133:134]
statement_line.date_account = line[134:142]
statement_line.date_move = line[142:150]
statement_line.amountInCents = int(line[150:168])
statement_line.debit_credit = line[168:169]
statement_line.occurrence = line[169:172]
statement_line.bank_history_code = line[172:176]
statement_line.bank_history_description = line[176:201]
statement_line.bank_history_description = line[201:240]
statement.lines.append(statement_line)
return statement
Empty file.
41 changes: 41 additions & 0 deletions febraban/cnab240/tests/statement/parserTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from unittest.case import TestCase
from febraban.cnab240.statement import StatementParser

returnFile = \
"""
07700000 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA BANCO INTER S.A. 21211202016361800001610100000 000
07700011E0440033 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 17082020000000000000732846CFBRL00016
0770001300001E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S1908202019082020000000000000082240D1127059PAGAMENTO DE TITULO 026135
0770001300002E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2008202020082020000000000000264357D1127045PAGAMENTO DE CONVENIO 000000
0770001300003E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2008202020082020000000000000433675D1127045PAGAMENTO DE CONVENIO 000000
0770001300004E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2008202020082020000000000000084054D1127045PAGAMENTO DE CONVENIO 000000
0770001300005E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2008202020082020000000000000200000C2067211RESGATE 672827
0770001300006E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2108202021082020000000000000144000C2017193DEPOSITO BOLETO 24 HORAS 000000
0770001300007E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2108202021082020000000000000600000C2017193DEPOSITO BOLETO 24 HORAS 000000
0770001300008E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2108202021082020000000000000100000C2017193DEPOSITO BOLETO 24 HORAS 000000
0770001300009E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2108202021082020000000000000131800C2017193DEPOSITO BOLETO 24 HORAS 000000
0770001300010E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2108202021082020000000000000098000C2017193DEPOSITO BOLETO 24 HORAS 000000
0770001300011E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2108202021082020000000000000080000C2017193DEPOSITO BOLETO 24 HORAS 000000
0770001300012E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2408202024082020000000000000300000D1207065TED ENVIADA 025012
0770001300013E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2508202025082020000000000000076900C2097067TED RECEBIDA 671091
07700015 223130935000198 0000190000014054310 00000000000000000000000000000000000000000000000000000025082020000000000000999220CF000015000000000001164326000000000001430700
07799999 000001000017000001
""".strip()


class ParserTest(TestCase):

def testReturnStatementFile(self):
statement = StatementParser.parseText(returnFile)

debit = 0
credit = 0

for line in statement.lines:
if line.debit_credit == 'D':
debit += line.amountInCents
elif line.debit_credit == 'C':
credit += line.amountInCents

self.assertEqual(statement.debit_sum_in_cents, debit)
self.assertEqual(statement.credit_sum_in_cents, credit)
34 changes: 34 additions & 0 deletions sample-statement-parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from febraban.cnab240.statement import StatementParser

file = open("output.RET", "r")

statement = StatementParser.parseFile(file)

debit = 0
credit = 0

for line in statement.lines:
print(line.occurrence)
print(line.cpmf)
print(line.date_account)
print(line.date_move)
print(line.amountInCents)
print(line.debit_credit)
print(line.amountInCents)
print(line.occurrence)
print(line.bank_history_code)
print(line.bank_history_description)
print(line.bank_history_description)
print(line.occurrenceText())
print(line.contentText())

if line.debit_credit == 'D':
debit += line.amountInCents
elif line.debit_credit == 'C':
credit += line.amountInCents

if not statement.debit_sum_in_cents == debit:
raise Exception

if not statement.credit_sum_in_cents == credit:
raise Exception

0 comments on commit ba95a4e

Please sign in to comment.