Skip to content

Commit 3bcebb7

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

File tree

6 files changed

+272
-0
lines changed

6 files changed

+272
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .parser import StatementParser
Lines changed: 55 additions & 0 deletions
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+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
from .occurrences import debit_occurrences, credit_occurrences
2+
3+
4+
class Statement:
5+
6+
def __init__(
7+
self, content=None, bank_code=None, branch_number=None, branch_code=None,
8+
account_number=None, account_code=None, start_date=None,
9+
start_amount_in_cents=None, start_debit_credit=None, stop_date=None,
10+
stop_amount_in_cents=None, stop_debit_credit=None, line_quantity=None,
11+
debit_sum_in_cents=None, credit_sum_in_cents=None, currency=None):
12+
self.content = content or []
13+
self.bank_code = bank_code
14+
self.branch_number = branch_number
15+
self.branch_code = branch_code
16+
self.account_number = account_number
17+
self.account_code = account_code
18+
self.start_date = start_date
19+
self.start_amount_in_cents = start_amount_in_cents
20+
self.start_debit_credit = start_debit_credit
21+
self.stop_date = stop_date
22+
self.stop_amount_in_cents = stop_amount_in_cents
23+
self.stop_debit_credit = stop_debit_credit
24+
self.line_quantity = line_quantity
25+
self.debit_sum_in_cents = debit_sum_in_cents
26+
self.credit_sum_in_cents = credit_sum_in_cents
27+
self.currency = currency
28+
self.amount = 0
29+
self.lines = []
30+
31+
32+
class StatementLine:
33+
34+
def __init__(self, content=None, occurrence=None, cpmf=None, date_account=None,
35+
date_move=None, amountInCents=None, debit_credit=None,
36+
bank_history_code=None, bank_history_description=None,
37+
document_number=None, amount=None):
38+
self.content = content or []
39+
self.cpmf = cpmf
40+
self.date_account = date_account
41+
self.date_move = date_move
42+
self.amountInCents = amountInCents
43+
self.debit_credit = debit_credit
44+
self.occurrence = occurrence
45+
self.bank_history_code = bank_history_code
46+
self.bank_history_description = bank_history_description
47+
self.document_number = document_number
48+
self.amount = amount
49+
50+
def occurrenceText(self):
51+
if self.occurrence and self.debit_credit == 'C':
52+
return credit_occurrences[self.occurrence]
53+
elif self.occurrence and self.debit_credit == 'D':
54+
return debit_occurrences[self.occurrence]
55+
56+
def contentText(self, breakLine="\n"):
57+
return breakLine.join(self.content)
58+
59+
60+
class StatementParser:
61+
62+
def __init__(self, lines):
63+
self._lines = lines
64+
self.statement = None
65+
66+
@classmethod
67+
def parseFile(cls, file):
68+
lines = file.readlines()
69+
statement = StatementParser(lines)
70+
return statement.parseLines()
71+
72+
@classmethod
73+
def parseText(cls, text):
74+
lines = text.splitlines()[:-1]
75+
statement = StatementParser(lines)
76+
return statement.parseLines()
77+
78+
def _prepare_statement_header(self, line):
79+
self.statement = Statement(
80+
content=[line],
81+
bank_code=line[0:3],
82+
branch_number=line[52:57],
83+
branch_code=line[57:58],
84+
account_number=line[58:70],
85+
account_code=line[57:58],
86+
start_date=line[142:150],
87+
start_amount_in_cents=int(line[150:168]),
88+
start_debit_credit=line[168:169],
89+
currency=line[170:173]
90+
)
91+
92+
def _prepare_statement_footer(self, line):
93+
self.statement.content.append(line)
94+
self.statement.stop_date = line[142:150]
95+
self.statement.stop_amount_in_cents = int(line[150:168])
96+
self.statement.stop_debit_credit = line[168:169]
97+
self.statement.line_quantity = int(line[170:176])
98+
self.statement.debit_sum_in_cents = int(line[176:194])
99+
self.statement.credit_sum_in_cents = int(line[194:212])
100+
101+
def _prepare_statement_line(self, line):
102+
statement_line = StatementLine()
103+
statement_line.content.append(line)
104+
statement_line.occurrence = line[15:17]
105+
statement_line.cpmf = line[133:134]
106+
statement_line.date_account = line[134:142]
107+
statement_line.date_move = line[142:150]
108+
statement_line.amountInCents = int(line[150:168])
109+
statement_line.debit_credit = line[168:169]
110+
statement_line.occurrence = line[169:172]
111+
statement_line.bank_history_code = line[172:176]
112+
statement_line.bank_history_description = line[176:201]
113+
statement_line.document_number = line[201:240]
114+
115+
if statement_line.debit_credit == 'C':
116+
statement_line.amount = statement_line.amountInCents / 100
117+
elif statement_line.debit_credit == 'D':
118+
statement_line.amount = - statement_line.amountInCents / 100
119+
self.statement.amount += statement_line.amount
120+
self.statement.lines.append(statement_line)
121+
122+
def parseLines(self):
123+
for line in self._lines:
124+
125+
if line[7] in ["0", "9"]:
126+
continue
127+
if line[7] == "1" and line[8] == "E":
128+
if not self.statement:
129+
self._prepare_statement_header(line)
130+
131+
if line[7] == "5":
132+
self._prepare_statement_footer(line)
133+
134+
if line[7] == "3" and line[13] == "E":
135+
self._prepare_statement_line(line)
136+
137+
return self.statement

febraban/cnab240/tests/statement/__init__.py

Whitespace-only changes.
Lines changed: 41 additions & 0 deletions
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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
print(statement.bank_code)
11+
print(statement.branch_number, '-', statement.branch_code)
12+
print(statement.account_number, '-', statement.account_code)
13+
14+
for line in statement.lines:
15+
print(line.occurrence)
16+
print(line.cpmf)
17+
print(line.date_account)
18+
print(line.date_move)
19+
print(line.amountInCents)
20+
print(line.debit_credit)
21+
print(line.amountInCents)
22+
print(line.occurrence)
23+
print(line.bank_history_code)
24+
print(line.bank_history_description)
25+
print(line.bank_history_description)
26+
print(line.occurrenceText())
27+
print(line.contentText())
28+
29+
if line.debit_credit == 'D':
30+
debit += line.amountInCents
31+
elif line.debit_credit == 'C':
32+
credit += line.amountInCents
33+
34+
if not statement.debit_sum_in_cents == debit:
35+
raise Exception
36+
37+
if not statement.credit_sum_in_cents == credit:
38+
raise Exception

0 commit comments

Comments
 (0)