Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] Sisdeb #25

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions febraban/cnab240/itau/sisdeb/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .file.file import File
from .result.parser import PaymentParser
from .payment.debit import Debit
Empty file.
48 changes: 48 additions & 0 deletions febraban/cnab240/itau/sisdeb/file/file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from datetime import datetime
from .header import Header
from .trailer import Trailer
from ....libs.fileUtils import FileUtils


class File:

def __init__(self):
self.header = Header()
self.lots = []
self.trailer = Trailer()

def addLot(self, lot):
lot.setLotNumber(len(self.lots) + 1)
self.lots.append(lot)

def toString(self, currentDatetime=None):
lotsToString = "\r\n".join([lot.toString() for lot in self.lots])
self.header.setGeneratedFileDate(currentDatetime or datetime.now())
self.trailer.setNumberOfLotsAndRegisters(
num=len(self.lots),
sum=2 + self._countRegistersInLots()
)
return "%s\r\n%s\r\n%s\r\n" % (
self.header.content,
lotsToString,
self.trailer.content
)

def setSender(self, sender):
self.header.setSender(sender)
self.header.setSenderBank(sender.bank)
self.trailer.setSenderBank(sender.bank)

def setBankAgreementCode(self, code):
self.header.setBankAgreementCode(code)

def output(self, fileName, path="/../", content=None, currentDatetime=None):
file = FileUtils.create(name=fileName, path=path)
file.write(self.toString(currentDatetime or datetime.now()) if not content else content)
file.close()

def _countRegistersInLots(self):
count = 0
for lot in self.lots:
count += lot.count
return count
53 changes: 53 additions & 0 deletions febraban/cnab240/itau/sisdeb/file/header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# coding: utf-8

from ....row import Row
from ....characterType import numeric, alphaNumeric


class Header:

def __init__(self):
self.content = " " * 240
self.defaultValues()

def defaultValues(self):
structs = [
( 3, 8, 5, numeric, "0"), # Tipo de registro
( 58, 65, 7, numeric, "0000000"),
( 102, 132, 30, alphaNumeric, "BANCO ITAU"),
( 142, 143, 1, numeric, "1"), # 1 - Remessa / 2 - Retorno
( 157, 163, 6, numeric, "0"), # TODO: Número sequencial do arquivo
( 163, 166, 3, numeric, "040"), # NR. DA VERSÃO DO LAYOUT
( 166, 171, 5, numeric, "0"),
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setGeneratedFileDate(self, datetime):
structs = [
(143, 151, 8, numeric, datetime.strftime("%d%m%Y")), # Dia que o arquivo foi gerado
(151, 157, 6, numeric, datetime.strftime("%H%M%S")), # Horario que o arquivo foi gerado
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setSender(self, user):
structs = [
(17, 18, 1, numeric, "1" if len(user.identifier) == 11 else "2"),
(18, 32, 14, numeric, user.identifier),
(72, 102, 30, alphaNumeric, user.name)
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setSenderBank(self, bank):
structs = [
( 0, 3, 3, numeric, bank.bankId),
(52, 57, 5, numeric, bank.branchCode),
(65, 70, 5, numeric, bank.accountNumber),
(71, 72, 1, numeric, bank.accountVerifier),
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setBankAgreementCode(self, code):
structs = [
(32, 45, 13, alphaNumeric, code),
]
self.content = Row.setStructs(structs=structs, content=self.content)
66 changes: 66 additions & 0 deletions febraban/cnab240/itau/sisdeb/file/headerLot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# coding: utf-8

from ....row import Row
from ....characterType import numeric, alphaNumeric


class HeaderLot:

def __init__(self):
self.content = " " * 240
self.defaultValues()

def defaultValues(self):
structs = [
( 3, 7, 4, numeric, "1"),
( 7, 8, 1, numeric, "1"),
( 8, 9, 1, alphaNumeric, "D"),
( 13, 16, 3, numeric, "030"),
( 58, 65, 7, numeric, "0000000"),
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setSender(self, user):
structs = [
(17, 18, 1, numeric, "1" if len(user.identifier) == 11 else "2"),
(18, 32, 14, numeric, user.identifier),
(72, 102, 30, alphaNumeric, user.name)
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setSenderBank(self, bank):
structs = [
( 0, 3, 3, numeric, bank.bankId),
(52, 57, 5, numeric, bank.branchCode),
(65, 70, 5, numeric, bank.accountNumber),
(71, 72, 1, numeric, bank.accountVerifier),
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setSenderAddress(self, address):
structs = [
(142, 192, 50, alphaNumeric, "%s %s %s" % (address.streetLine1, address.streetLine2, address.district)),
(192, 212, 20, alphaNumeric, address.city),
(212, 220, 8, numeric, address.zipCode),
(220, 222, 2, alphaNumeric, address.stateCode),
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setPositionInLot(self, index):
structs = [
(3, 7, 4, numeric, index)
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setInfo(self, kind, method):
structs = [
( 9, 11, 2, numeric, kind),
(11, 13, 2, numeric, method)
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setBankAgreementCode(self, code):
structs = [
(32, 45, 13, alphaNumeric, code),
]
self.content = Row.setStructs(structs=structs, content=self.content)
103 changes: 103 additions & 0 deletions febraban/cnab240/itau/sisdeb/file/lot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
from .headerLot import HeaderLot
from .trailerLot import TrailerLot
from ....itau.sispag import Transfer, ChargePayment, BarCodePayment, NonBarCodePayment
from ....libs.paymentKind import PaymentKind
from ....libs.paymentMethod import PaymentMethod


class Lot:

def __init__(self):
self.headerLot = HeaderLot()
self.registers = []
self.trailerLot = TrailerLot()
self.kind = ""
self.method = ""
self.amount = 0
self.otherAmount = 0
self.additionAmount = 0
self.totalAmount = 0
self.index = 1
self.count = 0

def add(self, register):
register.setPositionInLot(index=self.index)
self.registers.append(register)
self.amount += register.amountInCents()
if self._isNonBarCodeTax():
self.otherAmount += register.otherAmountInCents()
self.additionAmount += register.additionAmountInCents()
self.totalAmount += register.totalAmountInCents()
self.index += 1

def setLotNumber(self, index):
self.headerLot.setPositionInLot(index)
self.trailerLot.setPositionInLot(index)
for register in self.registers:
register.setLot(index)

def setSender(self, sender):
self.headerLot.setSender(sender)
self.headerLot.setSenderBank(sender.bank)
self.headerLot.setSenderAddress(sender.address)
self.trailerLot.setSenderBank(sender.bank)

def setBankAgreementCode(self, code):
self.headerLot.setBankAgreementCode(code)

def setHeaderLotType(self, kind=PaymentKind.vendor, method=PaymentMethod.tedOther):
"""
Trasfers:
kind: String - Kind of payment - 20 Fornecedores, read: NOTES 4
method: String - Payment method - 41 TED Outro titular, 43 TED Mesmo titular, 01 ITAU account. read: NOTES 5

Charge-payments:
kind: String - Kind of payment - 98 Diversos, read: NOTES 4
method: String - Payment method - 30 Pagamento Boleto Itau, 31 Pagamento Boleto outros Bancos. read: NOTES 5

Utilities:
kind: String - Kind of payment - 98 Diversos, read: NOTES 4
method: String - Payment method - 13 Concessionarias. read: NOTES 5

Tax-payments:
kind: String - Kind of payment - 22 Tributos, read: NOTES 4
method: String - Payment method - 91 GNRE e Tributos com Codigo de Barras,
19 IPTU/ISS/Outros Tributos Municipais. read: NOTES 5,
16 DARF (No barcode)

"""
self.kind = kind
self.method = method
self.headerLot.setInfo(kind, method)

def toString(self):
self.count = (2 + self._count(Transfer) + 2 * self._count(ChargePayment)
+ self._count(BarCodePayment) + self._count(NonBarCodePayment))
self.trailerLot.setLotNumberOfRegisters(
num=self.count
)

if self._isNonBarCodeTax():
self.trailerLot.setSumOfValuesNonBarCodeTax(
sum=self.amount,
otherSum=self.otherAmount,
additionSum=self.additionAmount,
totalSum=self.totalAmount,
)
else:
self.trailerLot.setSumOfValues(sum=self.amount)

registersToString = "\r\n".join([register.toString() for register in self.registers])
return "%s\r\n%s\r\n%s" % (
self.headerLot.content,
registersToString,
self.trailerLot.content,
)

def _count(self, cls):
return len([register for register in self.registers if isinstance(register, cls)])

def _isNonBarCodeTax(self):
return self.kind == PaymentKind.tribute and self.method in PaymentMethod.nonBarcodeTaxes()


30 changes: 30 additions & 0 deletions febraban/cnab240/itau/sisdeb/file/trailer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# coding: utf-8

from ....row import Row
from ....characterType import numeric


class Trailer:

def __init__(self):
self.content = " " * 240
self.defaultValues()

def defaultValues(self):
structs = [
( 3, 8, 5, numeric, "99999"),
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setNumberOfLotsAndRegisters(self, sum, num):
structs = [
(17, 23, 6, numeric, num),
(23, 29, 6, numeric, sum),
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setSenderBank(self, bank):
structs = [
(0, 3, 3, numeric, bank.bankId), # Código do banco debitado
]
self.content = Row.setStructs(structs=structs, content=self.content)
52 changes: 52 additions & 0 deletions febraban/cnab240/itau/sisdeb/file/trailerLot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# coding: utf-8

from ....row import Row
from ....characterType import numeric


class TrailerLot:

def __init__(self):
self.content = " " * 240
self.defaultValues()

def defaultValues(self):
structs = [
( 3, 7, 4, numeric, "1"),
( 7, 8, 1, numeric, "5"),
(41, 59, 18, numeric, "0"),
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setLotNumberOfRegisters(self, num):
structs = [
(17, 23, 6, numeric, num),
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setSumOfValues(self, sum):
structs = [
(23, 41, 18, numeric, sum), # Soma dos valores dos lotes
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setSumOfValuesNonBarCodeTax(self, sum, otherSum, additionSum, totalSum):
structs = [
(23, 37, 14, numeric, sum), # Soma dos valores principais dos lotes
(37, 51, 14, numeric, otherSum), # Soma dos valores outras entidades dos lotes
(51, 65, 14, numeric, additionSum), # Soma dos valores acréscimos dos lotes
(65, 79, 14, numeric, totalSum), # Soma dos valores totais dos lotes
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setSenderBank(self, bank):
structs = [
(0, 3, 3, numeric, bank.bankId), # Código do banco debitado
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setPositionInLot(self, index):
structs = [
(3, 7, 4, numeric, index) # Indica index do lote
]
self.content = Row.setStructs(structs=structs, content=self.content)
Empty file.
Loading