Skip to content
Open
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
Empty file.
Empty file.
Binary file not shown.
Empty file.
46 changes: 46 additions & 0 deletions febraban/cnab240/bradesco/multipag/file/file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from datetime import datetime
from febraban.cnab240.libs.fileUtils import FileUtils
from febraban.cnab240.bradesco.multipag.file.header import Header
from febraban.cnab240.bradesco.multipag.file.trailer import Trailer


class File:

def __init__(self, sequenceNumber=1):
self.header = Header(sequenceNumber)
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)
self.header.setBankAgreement(sender.bank.bankAgreement)

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
57 changes: 57 additions & 0 deletions febraban/cnab240/bradesco/multipag/file/header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from febraban.cnab240.row import Row
from febraban.cnab240.characterType import alphaNumeric, numeric


class Header:
def __init__(self, sequenceNumber=1):
self.content = " " * 240
self.defaultValues()
self.setFileSequenceNumber(sequenceNumber)

def defaultValues(self):
structs = [
( 3, 7, 4, numeric, "0000"), # Service lot
( 7, 8, 1, numeric, "0"), # Record type
( 163, 166, 3, numeric, "089"), # Layout version
( 142, 143, 1, numeric, "1"), # 1 - Remittance / 2 - Return
( 166, 171, 5, numeric, "1600"), # File recording density
( 157, 163, 6, numeric, "000000"),
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setFileSequenceNumber(self, number):
structs = [
(157, 163, 6, numeric, number),
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setGeneratedFileDate(self, datetime):
structs = [
(143, 151, 8, numeric, datetime.strftime("%d%m%Y")), # File generation date
(151, 157, 6, numeric, datetime.strftime("%H%M%S")), # File generation time
]
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),
(58, 70, 12, numeric, bank.accountNumber),
(70, 71, 1, alphaNumeric, bank.accountVerifier[:1]),
(102, 132, 30, alphaNumeric, bank.bankName),
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setBankAgreement(self, agreement):
structs = [
(32, 52, 20, alphaNumeric, agreement),
]
self.content = Row.setStructs(structs=structs, content=self.content)
73 changes: 73 additions & 0 deletions febraban/cnab240/bradesco/multipag/file/headerLot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from febraban.cnab240.row import Row
from febraban.cnab240.characterType import alphaNumeric, numeric

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, "C"),
( 9, 11, 2, numeric, "01"),
( 13, 16, 3, numeric, "012"),
( 222, 224, 2, numeric, "01"),
]
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 setGeneratedFileDate(self, datetime):
structs = [
(191, 199, 8, numeric, datetime.strftime("%d%m%Y")), # Recording date
(199, 207, 8, numeric, datetime.strftime("%d%m%Y")), # Credit date
]
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),
(58, 70, 12, numeric, bank.accountNumber),
(70, 71, 1, numeric, bank.accountVerifier[:1]),
(71, 72, 1, alphaNumeric, ""),
(102, 132, 30, alphaNumeric, bank.bankName),
]
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 setBankAgreement(self, agreement):
structs = [
(32, 52, 20, alphaNumeric, agreement),
]
self.content = Row.setStructs(structs=structs, content=self.content)
99 changes: 99 additions & 0 deletions febraban/cnab240/bradesco/multipag/file/lot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from febraban.cnab240.libs.paymentKind import PaymentKind
from febraban.cnab240.libs.paymentMethod import PaymentMethod
from febraban.cnab240.bradesco.multipag.file.headerLot import HeaderLot
from febraban.cnab240.bradesco.multipag.file.trailerLot import TrailerLot
from febraban.cnab240.bradesco.multipag.payment.nonBarCodePayment import NonBarCodePayment


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 _isNonBarCodeTax(self):
return self.kind == PaymentKind.tribute and self.method in PaymentMethod.nonBarcodeTaxes()

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.headerLot.setBankAgreement(sender.bank.bankAgreement)
self.trailerLot.setSenderBank(sender.bank)

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

if self._isNonBarCodeTax():
self.trailerLot.setSumOfValuesNonBarCodeTax(
sum=self.amount,
otherSum=self.otherAmount,
totalSum=self.totalAmount,
)
elif self._isBoletoPayment():
self.trailerLot.setSumOfValues(sum=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 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)
29 changes: 29 additions & 0 deletions febraban/cnab240/bradesco/multipag/file/trailer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from febraban.cnab240.row import Row
from febraban.cnab240.characterType import numeric


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

def defaultValues(self):
structs = [
(3, 7, 4, numeric, "9999"),
(7, 8, 1, numeric, "9"),
(29, 35, 6, numeric, "000000"),
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setSenderBank(self, bank):
structs = [
(0, 3, 3, numeric, bank.bankId), # Debit bank code
]
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)
50 changes: 50 additions & 0 deletions febraban/cnab240/bradesco/multipag/file/trailerLot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from febraban.cnab240.row import Row
from febraban.cnab240.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, "000000000000000000"),
(59, 65, 6, numeric, "000000"),
]
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), # Sum of values of lots
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setSumOfValuesNonBarCodeTax(self, sum, otherSum, totalSum):
structs = [
(23, 41, 18, numeric, sum), # Sum of main values of lots
(41, 59, 13, numeric, otherSum), # Sum of other entities values of lots
(59, 65, 0, numeric, totalSum), # Sum of total values of lots
]
self.content = Row.setStructs(structs=structs, content=self.content)

def setSenderBank(self, bank):
structs = [
(0, 3, 3, numeric, bank.bankId), # Debit bank code
]
self.content = Row.setStructs(structs=structs, content=self.content)

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