Skip to content

Commit 3e9776f

Browse files
committed
[ADD] Sisdeb
1 parent 93ba3c6 commit 3e9776f

17 files changed

Lines changed: 858 additions & 0 deletions
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .file.file import File
2+
from .result.parser import PaymentParser
3+
from .payment.debit import Debit

febraban/cnab240/itau/sisdeb/file/__init__.py

Whitespace-only changes.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from datetime import datetime
2+
from .header import Header
3+
from .trailer import Trailer
4+
from ....libs.fileUtils import FileUtils
5+
6+
7+
class File:
8+
9+
def __init__(self):
10+
self.header = Header()
11+
self.lots = []
12+
self.trailer = Trailer()
13+
14+
def addLot(self, lot):
15+
lot.setLotNumber(len(self.lots) + 1)
16+
self.lots.append(lot)
17+
18+
def toString(self, currentDatetime=None):
19+
lotsToString = "\r\n".join([lot.toString() for lot in self.lots])
20+
self.header.setGeneratedFileDate(currentDatetime or datetime.now())
21+
self.trailer.setNumberOfLotsAndRegisters(
22+
num=len(self.lots),
23+
sum=2 + self._countRegistersInLots()
24+
)
25+
return "%s\r\n%s\r\n%s\r\n" % (
26+
self.header.content,
27+
lotsToString,
28+
self.trailer.content
29+
)
30+
31+
def setSender(self, sender):
32+
self.header.setSender(sender)
33+
self.header.setSenderBank(sender.bank)
34+
self.trailer.setSenderBank(sender.bank)
35+
36+
def setBankAgreementCode(self, code):
37+
self.header.setBankAgreementCode(code)
38+
39+
def output(self, fileName, path="/../", content=None, currentDatetime=None):
40+
file = FileUtils.create(name=fileName, path=path)
41+
file.write(self.toString(currentDatetime or datetime.now()) if not content else content)
42+
file.close()
43+
44+
def _countRegistersInLots(self):
45+
count = 0
46+
for lot in self.lots:
47+
count += lot.count
48+
return count
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# coding: utf-8
2+
3+
from ....row import Row
4+
from ....characterType import numeric, alphaNumeric
5+
6+
7+
class Header:
8+
9+
def __init__(self):
10+
self.content = " " * 240
11+
self.defaultValues()
12+
13+
def defaultValues(self):
14+
structs = [
15+
( 3, 8, 5, numeric, "0"), # Tipo de registro
16+
( 58, 65, 7, numeric, "0000000"),
17+
( 102, 132, 30, alphaNumeric, "BANCO ITAU"),
18+
( 142, 143, 1, numeric, "1"), # 1 - Remessa / 2 - Retorno
19+
( 157, 163, 6, numeric, "0"), # TODO: Número sequencial do arquivo
20+
( 163, 166, 3, numeric, "040"), # NR. DA VERSÃO DO LAYOUT
21+
( 166, 171, 5, numeric, "0"),
22+
]
23+
self.content = Row.setStructs(structs=structs, content=self.content)
24+
25+
def setGeneratedFileDate(self, datetime):
26+
structs = [
27+
(143, 151, 8, numeric, datetime.strftime("%d%m%Y")), # Dia que o arquivo foi gerado
28+
(151, 157, 6, numeric, datetime.strftime("%H%M%S")), # Horario que o arquivo foi gerado
29+
]
30+
self.content = Row.setStructs(structs=structs, content=self.content)
31+
32+
def setSender(self, user):
33+
structs = [
34+
(17, 18, 1, numeric, "1" if len(user.identifier) == 11 else "2"),
35+
(18, 32, 14, numeric, user.identifier),
36+
(72, 102, 30, alphaNumeric, user.name)
37+
]
38+
self.content = Row.setStructs(structs=structs, content=self.content)
39+
40+
def setSenderBank(self, bank):
41+
structs = [
42+
( 0, 3, 3, numeric, bank.bankId),
43+
(52, 57, 5, numeric, bank.branchCode),
44+
(65, 70, 5, numeric, bank.accountNumber),
45+
(71, 72, 1, numeric, bank.accountVerifier),
46+
]
47+
self.content = Row.setStructs(structs=structs, content=self.content)
48+
49+
def setBankAgreementCode(self, code):
50+
structs = [
51+
(32, 45, 13, alphaNumeric, code),
52+
]
53+
self.content = Row.setStructs(structs=structs, content=self.content)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# coding: utf-8
2+
3+
from ....row import Row
4+
from ....characterType import numeric, alphaNumeric
5+
6+
7+
class HeaderLot:
8+
9+
def __init__(self):
10+
self.content = " " * 240
11+
self.defaultValues()
12+
13+
def defaultValues(self):
14+
structs = [
15+
( 3, 7, 4, numeric, "1"),
16+
( 7, 8, 1, numeric, "1"),
17+
( 8, 9, 1, alphaNumeric, "D"),
18+
( 13, 16, 3, numeric, "030"),
19+
( 58, 65, 7, numeric, "0000000"),
20+
]
21+
self.content = Row.setStructs(structs=structs, content=self.content)
22+
23+
def setSender(self, user):
24+
structs = [
25+
(17, 18, 1, numeric, "1" if len(user.identifier) == 11 else "2"),
26+
(18, 32, 14, numeric, user.identifier),
27+
(72, 102, 30, alphaNumeric, user.name)
28+
]
29+
self.content = Row.setStructs(structs=structs, content=self.content)
30+
31+
def setSenderBank(self, bank):
32+
structs = [
33+
( 0, 3, 3, numeric, bank.bankId),
34+
(52, 57, 5, numeric, bank.branchCode),
35+
(65, 70, 5, numeric, bank.accountNumber),
36+
(71, 72, 1, numeric, bank.accountVerifier),
37+
]
38+
self.content = Row.setStructs(structs=structs, content=self.content)
39+
40+
def setSenderAddress(self, address):
41+
structs = [
42+
(142, 192, 50, alphaNumeric, "%s %s %s" % (address.streetLine1, address.streetLine2, address.district)),
43+
(192, 212, 20, alphaNumeric, address.city),
44+
(212, 220, 8, numeric, address.zipCode),
45+
(220, 222, 2, alphaNumeric, address.stateCode),
46+
]
47+
self.content = Row.setStructs(structs=structs, content=self.content)
48+
49+
def setPositionInLot(self, index):
50+
structs = [
51+
(3, 7, 4, numeric, index)
52+
]
53+
self.content = Row.setStructs(structs=structs, content=self.content)
54+
55+
def setInfo(self, kind, method):
56+
structs = [
57+
( 9, 11, 2, numeric, kind),
58+
(11, 13, 2, numeric, method)
59+
]
60+
self.content = Row.setStructs(structs=structs, content=self.content)
61+
62+
def setBankAgreementCode(self, code):
63+
structs = [
64+
(32, 45, 13, alphaNumeric, code),
65+
]
66+
self.content = Row.setStructs(structs=structs, content=self.content)
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
from .headerLot import HeaderLot
2+
from .trailerLot import TrailerLot
3+
from ....itau.sispag import Transfer, ChargePayment, BarCodePayment, NonBarCodePayment
4+
from ....libs.paymentKind import PaymentKind
5+
from ....libs.paymentMethod import PaymentMethod
6+
7+
8+
class Lot:
9+
10+
def __init__(self):
11+
self.headerLot = HeaderLot()
12+
self.registers = []
13+
self.trailerLot = TrailerLot()
14+
self.kind = ""
15+
self.method = ""
16+
self.amount = 0
17+
self.otherAmount = 0
18+
self.additionAmount = 0
19+
self.totalAmount = 0
20+
self.index = 1
21+
self.count = 0
22+
23+
def add(self, register):
24+
register.setPositionInLot(index=self.index)
25+
self.registers.append(register)
26+
self.amount += register.amountInCents()
27+
if self._isNonBarCodeTax():
28+
self.otherAmount += register.otherAmountInCents()
29+
self.additionAmount += register.additionAmountInCents()
30+
self.totalAmount += register.totalAmountInCents()
31+
self.index += 1
32+
33+
def setLotNumber(self, index):
34+
self.headerLot.setPositionInLot(index)
35+
self.trailerLot.setPositionInLot(index)
36+
for register in self.registers:
37+
register.setLot(index)
38+
39+
def setSender(self, sender):
40+
self.headerLot.setSender(sender)
41+
self.headerLot.setSenderBank(sender.bank)
42+
self.headerLot.setSenderAddress(sender.address)
43+
self.trailerLot.setSenderBank(sender.bank)
44+
45+
def setBankAgreementCode(self, code):
46+
self.headerLot.setBankAgreementCode(code)
47+
48+
def setHeaderLotType(self, kind=PaymentKind.vendor, method=PaymentMethod.tedOther):
49+
"""
50+
Trasfers:
51+
kind: String - Kind of payment - 20 Fornecedores, read: NOTES 4
52+
method: String - Payment method - 41 TED Outro titular, 43 TED Mesmo titular, 01 ITAU account. read: NOTES 5
53+
54+
Charge-payments:
55+
kind: String - Kind of payment - 98 Diversos, read: NOTES 4
56+
method: String - Payment method - 30 Pagamento Boleto Itau, 31 Pagamento Boleto outros Bancos. read: NOTES 5
57+
58+
Utilities:
59+
kind: String - Kind of payment - 98 Diversos, read: NOTES 4
60+
method: String - Payment method - 13 Concessionarias. read: NOTES 5
61+
62+
Tax-payments:
63+
kind: String - Kind of payment - 22 Tributos, read: NOTES 4
64+
method: String - Payment method - 91 GNRE e Tributos com Codigo de Barras,
65+
19 IPTU/ISS/Outros Tributos Municipais. read: NOTES 5,
66+
16 DARF (No barcode)
67+
68+
"""
69+
self.kind = kind
70+
self.method = method
71+
self.headerLot.setInfo(kind, method)
72+
73+
def toString(self):
74+
self.count = (2 + self._count(Transfer) + 2 * self._count(ChargePayment)
75+
+ self._count(BarCodePayment) + self._count(NonBarCodePayment))
76+
self.trailerLot.setLotNumberOfRegisters(
77+
num=self.count
78+
)
79+
80+
if self._isNonBarCodeTax():
81+
self.trailerLot.setSumOfValuesNonBarCodeTax(
82+
sum=self.amount,
83+
otherSum=self.otherAmount,
84+
additionSum=self.additionAmount,
85+
totalSum=self.totalAmount,
86+
)
87+
else:
88+
self.trailerLot.setSumOfValues(sum=self.amount)
89+
90+
registersToString = "\r\n".join([register.toString() for register in self.registers])
91+
return "%s\r\n%s\r\n%s" % (
92+
self.headerLot.content,
93+
registersToString,
94+
self.trailerLot.content,
95+
)
96+
97+
def _count(self, cls):
98+
return len([register for register in self.registers if isinstance(register, cls)])
99+
100+
def _isNonBarCodeTax(self):
101+
return self.kind == PaymentKind.tribute and self.method in PaymentMethod.nonBarcodeTaxes()
102+
103+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# coding: utf-8
2+
3+
from ....row import Row
4+
from ....characterType import numeric
5+
6+
7+
class Trailer:
8+
9+
def __init__(self):
10+
self.content = " " * 240
11+
self.defaultValues()
12+
13+
def defaultValues(self):
14+
structs = [
15+
( 3, 8, 5, numeric, "99999"),
16+
]
17+
self.content = Row.setStructs(structs=structs, content=self.content)
18+
19+
def setNumberOfLotsAndRegisters(self, sum, num):
20+
structs = [
21+
(17, 23, 6, numeric, num),
22+
(23, 29, 6, numeric, sum),
23+
]
24+
self.content = Row.setStructs(structs=structs, content=self.content)
25+
26+
def setSenderBank(self, bank):
27+
structs = [
28+
(0, 3, 3, numeric, bank.bankId), # Código do banco debitado
29+
]
30+
self.content = Row.setStructs(structs=structs, content=self.content)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# coding: utf-8
2+
3+
from ....row import Row
4+
from ....characterType import numeric
5+
6+
7+
class TrailerLot:
8+
9+
def __init__(self):
10+
self.content = " " * 240
11+
self.defaultValues()
12+
13+
def defaultValues(self):
14+
structs = [
15+
( 3, 7, 4, numeric, "1"),
16+
( 7, 8, 1, numeric, "5"),
17+
(41, 59, 18, numeric, "0"),
18+
]
19+
self.content = Row.setStructs(structs=structs, content=self.content)
20+
21+
def setLotNumberOfRegisters(self, num):
22+
structs = [
23+
(17, 23, 6, numeric, num),
24+
]
25+
self.content = Row.setStructs(structs=structs, content=self.content)
26+
27+
def setSumOfValues(self, sum):
28+
structs = [
29+
(23, 41, 18, numeric, sum), # Soma dos valores dos lotes
30+
]
31+
self.content = Row.setStructs(structs=structs, content=self.content)
32+
33+
def setSumOfValuesNonBarCodeTax(self, sum, otherSum, additionSum, totalSum):
34+
structs = [
35+
(23, 37, 14, numeric, sum), # Soma dos valores principais dos lotes
36+
(37, 51, 14, numeric, otherSum), # Soma dos valores outras entidades dos lotes
37+
(51, 65, 14, numeric, additionSum), # Soma dos valores acréscimos dos lotes
38+
(65, 79, 14, numeric, totalSum), # Soma dos valores totais dos lotes
39+
]
40+
self.content = Row.setStructs(structs=structs, content=self.content)
41+
42+
def setSenderBank(self, bank):
43+
structs = [
44+
(0, 3, 3, numeric, bank.bankId), # Código do banco debitado
45+
]
46+
self.content = Row.setStructs(structs=structs, content=self.content)
47+
48+
def setPositionInLot(self, index):
49+
structs = [
50+
(3, 7, 4, numeric, index) # Indica index do lote
51+
]
52+
self.content = Row.setStructs(structs=structs, content=self.content)

febraban/cnab240/itau/sisdeb/payment/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)