Skip to content

Commit ba95a4e

Browse files
committed
[NEW] Segmento E
1 parent 93ba3c6 commit ba95a4e

File tree

6 files changed

+233
-0
lines changed

6 files changed

+233
-0
lines changed
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .parser import StatementParser
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# coding: utf-8
2+
3+
debit_occurrences = {
4+
'101': 'Cheque Compensado',
5+
'102': 'Encargos',
6+
'103': 'Estornos',
7+
'104': 'Lançamento Avisado',
8+
'105': 'Tarifas',
9+
'106': 'Aplicação',
10+
'107': 'Empréstimo / Financiamento',
11+
'108': 'Câmbio',
12+
'109': 'CPMF',
13+
'110': 'IOF',
14+
'111': 'Imposto de Renda',
15+
'112': 'Pagamento Fornecedores',
16+
'113': 'Pagamentos Salário',
17+
'114': 'Saque Eletrônico',
18+
'115': 'Ações',
19+
'117': 'Transferência entre Contas',
20+
'118': 'Devolução da Compensação',
21+
'119': 'Devolução de Cheque Depositado',
22+
'120': 'Transferência Interbancária (DOC, TED)',
23+
'121': 'Antecipação a Fornecedores',
24+
'122': 'OC / AEROPS',
25+
'123': 'Saque em Espécie',
26+
'124': 'Cheque Pago',
27+
'125': 'Pagamentos Diversos',
28+
'126': 'Pagamento de Tributos',
29+
'127': 'Cartão de crédito - Pagamento de fatura de cartão de crédito da própria IF',
30+
}
31+
32+
credit_occurrences = {
33+
'201': 'Depósito em Cheque',
34+
'202': 'Crédito de Cobrança',
35+
'203': 'Devolução de Cheques',
36+
'204': 'Estornos',
37+
'205': 'Lançamento Avisado',
38+
'206': 'Resgate de Aplicação',
39+
'207': 'Empréstimo / Financiamento',
40+
'208': 'Câmbio',
41+
'209': 'Transferência Interbancária (DOC, TED)',
42+
'210': 'Ações',
43+
'211': 'Dividendos',
44+
'212': 'Seguro',
45+
'213': 'Transferência entre Contas',
46+
'214': 'Depósitos Especiais',
47+
'215': 'Devolução da Compensação',
48+
'216': 'OCT',
49+
'217': 'Pagamentos Fornecedores',
50+
'218': 'Pagamentos Diversos',
51+
'219': 'Recebimento de Salário',
52+
'220': 'Depósito em Espécie',
53+
'221': 'Pagamento de Tributos',
54+
'222': 'Cartão de Crédito - Recebíveis de cartão de crédito',
55+
}

febraban/cnab240/statement/parser.py

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
from .occurrences import debit_occurrences, credit_occurrences
2+
3+
4+
class Statement:
5+
6+
def __init__(
7+
self, content=None, start_date=None, start_amount=None,
8+
start_debit_credit=None, stop_date=False, stop_amount=None,
9+
stop_debit_credit=None, line_quantity=None, debit_sum_in_cents=None,
10+
credit_sum_in_cents=None):
11+
self.content = content or []
12+
self.start_date = start_date
13+
self.start_amount = start_amount
14+
self.start_debit_credit = start_debit_credit
15+
self.stop_date = stop_date
16+
self.stop_amount = stop_amount
17+
self.stop_debit_credit = stop_debit_credit
18+
self.line_quantity = line_quantity
19+
self.debit_sum_in_cents = debit_sum_in_cents
20+
self.credit_sum_in_cents = credit_sum_in_cents
21+
self.lines = []
22+
23+
24+
class StatementLine:
25+
26+
def __init__(self, content=None, occurrence=None, cpmf=None, date_account=None,
27+
date_move=None, amountInCents=None, debit_credit=None,
28+
bank_history_code=None, bank_history_description=None,
29+
document_number=None, errors=None):
30+
self.content = content or []
31+
self.cpmf = cpmf
32+
self.date_account = date_account
33+
self.date_move = date_move
34+
self.amountInCents = amountInCents
35+
self.debit_credit = debit_credit
36+
self.occurrence = occurrence
37+
self.bank_history_code = bank_history_code
38+
self.bank_history_description = bank_history_description
39+
self.document_number = document_number
40+
41+
def occurrenceText(self):
42+
if self.occurrence and self.debit_credit == 'C':
43+
return credit_occurrences[self.occurrence]
44+
elif self.occurrence and self.debit_credit == 'D':
45+
return debit_occurrences[self.occurrence]
46+
47+
48+
def contentText(self, breakLine="\n"):
49+
return breakLine.join(self.content)
50+
51+
52+
class StatementParser:
53+
54+
@classmethod
55+
def parseFile(cls, file):
56+
lines = file.readlines()
57+
return cls.parseLines(lines)
58+
59+
@classmethod
60+
def parseText(cls, text):
61+
lines = text.splitlines()[:-1]
62+
return cls.parseLines(lines)
63+
64+
@classmethod
65+
def parseLines(cls, lines):
66+
statement = None
67+
for line in lines:
68+
if line[7] in ["0", "9"]:
69+
continue
70+
if line[7] == "1" and line[8] == "E":
71+
if not statement:
72+
statement = Statement(
73+
content=[line],
74+
start_date=line[142:150],
75+
start_amount=line[150:168],
76+
start_debit_credit=line[168:169],
77+
)
78+
79+
if line[7] == "5":
80+
statement.content.append(line)
81+
statement.stop_date = line[142:150]
82+
statement.stop_amount = line[150:169]
83+
statement.stop_debit_credit = line[169:170]
84+
statement.line_quantity = line[170:176]
85+
statement.debit_sum_in_cents = int(line[176:194])
86+
statement.credit_sum_in_cents = int(line[194:212])
87+
88+
if line[7] == "3" and line[13] == "E":
89+
statement_line = StatementLine()
90+
statement_line.content.append(line)
91+
statement_line.occurrence = line[15:17]
92+
statement_line.cpmf = line[133:134]
93+
statement_line.date_account = line[134:142]
94+
statement_line.date_move = line[142:150]
95+
statement_line.amountInCents = int(line[150:168])
96+
statement_line.debit_credit = line[168:169]
97+
statement_line.occurrence = line[169:172]
98+
statement_line.bank_history_code = line[172:176]
99+
statement_line.bank_history_description = line[176:201]
100+
statement_line.bank_history_description = line[201:240]
101+
statement.lines.append(statement_line)
102+
return statement

febraban/cnab240/tests/statement/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from unittest.case import TestCase
2+
from febraban.cnab240.statement import StatementParser
3+
4+
returnFile = \
5+
"""
6+
07700000 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA BANCO INTER S.A. 21211202016361800001610100000 000
7+
07700011E0440033 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 17082020000000000000732846CFBRL00016
8+
0770001300001E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S1908202019082020000000000000082240D1127059PAGAMENTO DE TITULO 026135
9+
0770001300002E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2008202020082020000000000000264357D1127045PAGAMENTO DE CONVENIO 000000
10+
0770001300003E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2008202020082020000000000000433675D1127045PAGAMENTO DE CONVENIO 000000
11+
0770001300004E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2008202020082020000000000000084054D1127045PAGAMENTO DE CONVENIO 000000
12+
0770001300005E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2008202020082020000000000000200000C2067211RESGATE 672827
13+
0770001300006E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2108202021082020000000000000144000C2017193DEPOSITO BOLETO 24 HORAS 000000
14+
0770001300007E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2108202021082020000000000000600000C2017193DEPOSITO BOLETO 24 HORAS 000000
15+
0770001300008E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2108202021082020000000000000100000C2017193DEPOSITO BOLETO 24 HORAS 000000
16+
0770001300009E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2108202021082020000000000000131800C2017193DEPOSITO BOLETO 24 HORAS 000000
17+
0770001300010E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2108202021082020000000000000098000C2017193DEPOSITO BOLETO 24 HORAS 000000
18+
0770001300011E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2108202021082020000000000000080000C2017193DEPOSITO BOLETO 24 HORAS 000000
19+
0770001300012E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2408202024082020000000000000300000D1207065TED ENVIADA 025012
20+
0770001300013E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2508202025082020000000000000076900C2097067TED RECEBIDA 671091
21+
07700015 223130935000198 0000190000014054310 00000000000000000000000000000000000000000000000000000025082020000000000000999220CF000015000000000001164326000000000001430700
22+
07799999 000001000017000001
23+
""".strip()
24+
25+
26+
class ParserTest(TestCase):
27+
28+
def testReturnStatementFile(self):
29+
statement = StatementParser.parseText(returnFile)
30+
31+
debit = 0
32+
credit = 0
33+
34+
for line in statement.lines:
35+
if line.debit_credit == 'D':
36+
debit += line.amountInCents
37+
elif line.debit_credit == 'C':
38+
credit += line.amountInCents
39+
40+
self.assertEqual(statement.debit_sum_in_cents, debit)
41+
self.assertEqual(statement.credit_sum_in_cents, credit)

sample-statement-parser.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from febraban.cnab240.statement import StatementParser
2+
3+
file = open("output.RET", "r")
4+
5+
statement = StatementParser.parseFile(file)
6+
7+
debit = 0
8+
credit = 0
9+
10+
for line in statement.lines:
11+
print(line.occurrence)
12+
print(line.cpmf)
13+
print(line.date_account)
14+
print(line.date_move)
15+
print(line.amountInCents)
16+
print(line.debit_credit)
17+
print(line.amountInCents)
18+
print(line.occurrence)
19+
print(line.bank_history_code)
20+
print(line.bank_history_description)
21+
print(line.bank_history_description)
22+
print(line.occurrenceText())
23+
print(line.contentText())
24+
25+
if line.debit_credit == 'D':
26+
debit += line.amountInCents
27+
elif line.debit_credit == 'C':
28+
credit += line.amountInCents
29+
30+
if not statement.debit_sum_in_cents == debit:
31+
raise Exception
32+
33+
if not statement.credit_sum_in_cents == credit:
34+
raise Exception

0 commit comments

Comments
 (0)