From 5db3716543664e7adf0cac2953ebba8f0effa242 Mon Sep 17 00:00:00 2001 From: "pedro.firmiano-stark" Date: Tue, 30 Jun 2026 11:14:53 -0300 Subject: [PATCH] Add CNPJ Alphanumeric --- febraban/cnab240/bradesco/multipag/file/header.py | 4 ++-- .../cnab240/bradesco/multipag/file/headerLot.py | 4 ++-- .../bradesco/multipag/payment/darfPayment.py | 2 +- .../cnab240/bradesco/multipag/payment/segmentN.py | 4 ++-- febraban/cnab240/characterType.py | 3 ++- febraban/cnab240/itau/charge/file/header.py | 4 ++-- febraban/cnab240/itau/charge/file/headerLot.py | 4 ++-- febraban/cnab240/itau/charge/slip/segmentQ.py | 6 +++--- febraban/cnab240/itau/sispag/file/header.py | 4 ++-- febraban/cnab240/itau/sispag/file/headerLot.py | 4 ++-- .../cnab240/itau/sispag/payment/darfPayment.py | 2 +- .../cnab240/itau/sispag/payment/fgtsPayment.py | 2 +- .../cnab240/itau/sispag/payment/segmentJ52.py | 6 +++--- febraban/cnab240/itau/sispag/payment/segmentN.py | 4 ++-- febraban/cnab240/libs/middlewares/row.py | 11 ++++++----- febraban/cnab240/row.py | 15 +++++++-------- febraban/cnab240/user.py | 2 +- 17 files changed, 41 insertions(+), 40 deletions(-) diff --git a/febraban/cnab240/bradesco/multipag/file/header.py b/febraban/cnab240/bradesco/multipag/file/header.py index 8b38b20..ba20c84 100644 --- a/febraban/cnab240/bradesco/multipag/file/header.py +++ b/febraban/cnab240/bradesco/multipag/file/header.py @@ -1,5 +1,5 @@ from febraban.cnab240.row import Row -from febraban.cnab240.characterType import alphaNumeric, numeric +from febraban.cnab240.characterType import alphaNumeric, alphaNumericRightAligned, numeric class Header: @@ -35,7 +35,7 @@ def setGeneratedFileDate(self, datetime): def setSender(self, user): structs = [ (17, 18, 1, numeric, "1" if len(user.identifier) == 11 else "2"), - (18, 32, 14, numeric, user.identifier), + (18, 32, 14, alphaNumericRightAligned, user.identifier), (72, 102, 30, alphaNumeric, user.name) ] self.content = Row.setStructs(structs=structs, content=self.content) diff --git a/febraban/cnab240/bradesco/multipag/file/headerLot.py b/febraban/cnab240/bradesco/multipag/file/headerLot.py index 0455059..e01bb5f 100644 --- a/febraban/cnab240/bradesco/multipag/file/headerLot.py +++ b/febraban/cnab240/bradesco/multipag/file/headerLot.py @@ -1,5 +1,5 @@ from febraban.cnab240.row import Row -from febraban.cnab240.characterType import alphaNumeric, numeric +from febraban.cnab240.characterType import alphaNumeric, alphaNumericRightAligned, numeric class HeaderLot: @@ -21,7 +21,7 @@ def defaultValues(self): def setSender(self, user): structs = [ (17, 18, 1, numeric, "1" if len(user.identifier) == 11 else "2"), - (18, 32, 14, numeric, user.identifier), + (18, 32, 14, alphaNumericRightAligned, user.identifier), (72, 102, 30, alphaNumeric, user.name) ] self.content = Row.setStructs(structs=structs, content=self.content) diff --git a/febraban/cnab240/bradesco/multipag/payment/darfPayment.py b/febraban/cnab240/bradesco/multipag/payment/darfPayment.py index c8d8c8d..e8334ea 100644 --- a/febraban/cnab240/bradesco/multipag/payment/darfPayment.py +++ b/febraban/cnab240/bradesco/multipag/payment/darfPayment.py @@ -33,7 +33,7 @@ def setTaxPaymentIdentifier(self, id): def setTaxIdInfo(self, taxId): - taxId = "".join(c for c in taxId if c.isdigit()) + taxId = "".join(c for c in taxId if c.isalnum()).upper() taxIdType = "2" if len(taxId) == 11 else "1" self.segmentN.setTaxIdType(taxIdType) self.segmentN.setTaxId(taxId) diff --git a/febraban/cnab240/bradesco/multipag/payment/segmentN.py b/febraban/cnab240/bradesco/multipag/payment/segmentN.py index 1a05f92..aa7c64b 100644 --- a/febraban/cnab240/bradesco/multipag/payment/segmentN.py +++ b/febraban/cnab240/bradesco/multipag/payment/segmentN.py @@ -1,6 +1,6 @@ # coding: utf-8 from febraban.cnab240.row import Row -from febraban.cnab240.characterType import alphaNumeric, numeric +from febraban.cnab240.characterType import alphaNumeric, alphaNumericRightAligned, numeric class SegmentN: @@ -80,7 +80,7 @@ def setTaxIdType(self, taxIdType): def setTaxId(self, taxId): structs = [ - (118, 132, 14, numeric, taxId), + (118, 132, 14, alphaNumericRightAligned, taxId), ] self.content = Row.setStructs(structs=structs, content=self.content) diff --git a/febraban/cnab240/characterType.py b/febraban/cnab240/characterType.py index a73f887..c2c59e9 100644 --- a/febraban/cnab240/characterType.py +++ b/febraban/cnab240/characterType.py @@ -1,4 +1,5 @@ numeric = "numeric" -alphaNumeric = "alphaNumeric" \ No newline at end of file +alphaNumeric = "alphaNumeric" +alphaNumericRightAligned = "alphaNumericRightAligned" \ No newline at end of file diff --git a/febraban/cnab240/itau/charge/file/header.py b/febraban/cnab240/itau/charge/file/header.py index 53b74ed..e77be5f 100644 --- a/febraban/cnab240/itau/charge/file/header.py +++ b/febraban/cnab240/itau/charge/file/header.py @@ -1,5 +1,5 @@ from ....row import Row -from ....characterType import numeric, alphaNumeric +from ....characterType import numeric, alphaNumeric, alphaNumericRightAligned class Header: @@ -25,7 +25,7 @@ def setGeneratedFileDate(self, datetime): def setSender(self, user): structs = [ (17, 18, 1, numeric, "1" if len(user.identifier) == 11 else "2"), - (18, 32, 14, numeric, user.identifier), + (18, 32, 14, alphaNumericRightAligned, user.identifier), (72, 102, 30, alphaNumeric, user.name) ] self.content = Row.setStructs(structs=structs, content=self.content) diff --git a/febraban/cnab240/itau/charge/file/headerLot.py b/febraban/cnab240/itau/charge/file/headerLot.py index 14f74d3..4eaa43f 100644 --- a/febraban/cnab240/itau/charge/file/headerLot.py +++ b/febraban/cnab240/itau/charge/file/headerLot.py @@ -1,5 +1,5 @@ from ....row import Row -from ....characterType import numeric, alphaNumeric +from ....characterType import numeric, alphaNumeric, alphaNumericRightAligned class HeaderLot: @@ -28,7 +28,7 @@ def setGeneratedFileDate(self, datetime): def setSender(self, user): structs = [ (17, 18, 1, numeric, "1" if len(user.identifier) == 11 else "2"), - (18, 33, 15, numeric, user.identifier), + (18, 33, 15, alphaNumericRightAligned, user.identifier), (73, 103, 30, alphaNumeric, user.name) ] self.content = Row.setStructs(structs=structs, content=self.content) diff --git a/febraban/cnab240/itau/charge/slip/segmentQ.py b/febraban/cnab240/itau/charge/slip/segmentQ.py index e9f6ad7..bacb18e 100644 --- a/febraban/cnab240/itau/charge/slip/segmentQ.py +++ b/febraban/cnab240/itau/charge/slip/segmentQ.py @@ -1,5 +1,5 @@ from ....row import Row -from ....characterType import numeric, alphaNumeric +from ....characterType import numeric, alphaNumeric, alphaNumericRightAligned class SegmentQ: @@ -38,7 +38,7 @@ def setPositionInLot(self, index): def setPayer(self, user): structs = [ (17, 18, 1, numeric, "1" if len(user.identifier) == 11 else "2"), # 1 - CPF/ 2 - CNPJ - (18, 33, 15, numeric, user.identifier), # CPF/CNPJ do Pagador + (18, 33, 15, alphaNumericRightAligned, user.identifier), # CPF/CNPJ do Pagador (33, 63, 30, alphaNumeric, user.name) # Nome do Pagador ] self.content = Row.setStructs(structs=structs, content=self.content) @@ -56,7 +56,7 @@ def setPayerAddress(self, address): def setGuarantor(self, user): structs = [ (153, 154, 1, numeric, "1" if len(user.identifier) == 11 else "2"), # 1 - CPF/ 2 - CNPJ - (154, 169, 15, numeric, user.identifier), # CPF/CNPJ do Sacador Avalista + (154, 169, 15, alphaNumericRightAligned, user.identifier), # CPF/CNPJ do Sacador Avalista (169, 199, 30, alphaNumeric, user.name) # Nome do Sacador Avalista ] self.content = Row.setStructs(structs=structs, content=self.content) diff --git a/febraban/cnab240/itau/sispag/file/header.py b/febraban/cnab240/itau/sispag/file/header.py index 7fa9366..c2c7d11 100644 --- a/febraban/cnab240/itau/sispag/file/header.py +++ b/febraban/cnab240/itau/sispag/file/header.py @@ -1,7 +1,7 @@ # coding: utf-8 from ....row import Row -from ....characterType import numeric, alphaNumeric +from ....characterType import numeric, alphaNumeric, alphaNumericRightAligned class Header: @@ -30,7 +30,7 @@ def setGeneratedFileDate(self, datetime): def setSender(self, user): structs = [ (17, 18, 1, numeric, "1" if len(user.identifier) == 11 else "2"), - (18, 32, 14, numeric, user.identifier), + (18, 32, 14, alphaNumericRightAligned, user.identifier), (72, 102, 30, alphaNumeric, user.name) ] self.content = Row.setStructs(structs=structs, content=self.content) diff --git a/febraban/cnab240/itau/sispag/file/headerLot.py b/febraban/cnab240/itau/sispag/file/headerLot.py index bda474d..218cc68 100644 --- a/febraban/cnab240/itau/sispag/file/headerLot.py +++ b/febraban/cnab240/itau/sispag/file/headerLot.py @@ -1,7 +1,7 @@ # coding: utf-8 from ....row import Row -from ....characterType import numeric, alphaNumeric +from ....characterType import numeric, alphaNumeric, alphaNumericRightAligned class HeaderLot: @@ -22,7 +22,7 @@ def defaultValues(self): def setSender(self, user): structs = [ (17, 18, 1, numeric, "1" if len(user.identifier) == 11 else "2"), - (18, 32, 14, numeric, user.identifier), + (18, 32, 14, alphaNumericRightAligned, user.identifier), (72, 102, 30, alphaNumeric, user.name) ] self.content = Row.setStructs(structs=structs, content=self.content) diff --git a/febraban/cnab240/itau/sispag/payment/darfPayment.py b/febraban/cnab240/itau/sispag/payment/darfPayment.py index 812ce38..60ad9c0 100644 --- a/febraban/cnab240/itau/sispag/payment/darfPayment.py +++ b/febraban/cnab240/itau/sispag/payment/darfPayment.py @@ -30,7 +30,7 @@ def setRevenueCode(self, code): self.segmentN.setRevenueCode(code) def setTaxIdInfo(self, taxId): - taxId = "".join(c for c in taxId if c.isdigit()) + taxId = "".join(c for c in taxId if c.isalnum()).upper() taxIdType = "1" if len(taxId) == 11 else "2" self.segmentN.setTaxIdType(taxIdType) self.segmentN.setTaxId(taxId) diff --git a/febraban/cnab240/itau/sispag/payment/fgtsPayment.py b/febraban/cnab240/itau/sispag/payment/fgtsPayment.py index 12ab815..e62ba38 100644 --- a/febraban/cnab240/itau/sispag/payment/fgtsPayment.py +++ b/febraban/cnab240/itau/sispag/payment/fgtsPayment.py @@ -21,7 +21,7 @@ def setRevenueCode(self, code): self.segmentN.setRevenueCode(code) def setTaxIdInfo(self, taxId): - taxId = "".join(c for c in taxId if c.isdigit()) + taxId = "".join(c for c in taxId if c.isalnum()).upper() taxIdType = "1" if len(taxId) == 14 else "2" self.segmentN.setTaxIdType(taxIdType) self.segmentN.setTaxId(taxId) diff --git a/febraban/cnab240/itau/sispag/payment/segmentJ52.py b/febraban/cnab240/itau/sispag/payment/segmentJ52.py index 6864b5c..d25a8a4 100644 --- a/febraban/cnab240/itau/sispag/payment/segmentJ52.py +++ b/febraban/cnab240/itau/sispag/payment/segmentJ52.py @@ -1,7 +1,7 @@ # coding: utf-8 from ....row import Row -from ....characterType import numeric, alphaNumeric +from ....characterType import numeric, alphaNumeric, alphaNumericRightAligned class SegmentJ52: @@ -31,7 +31,7 @@ def setSenderBank(self, bank): def setSender(self, user): structs = [ (19, 20, 1, numeric, "1" if len(user.identifier) == 11 else "2"), - (20, 35, 15, numeric, user.identifier), + (20, 35, 15, alphaNumericRightAligned, user.identifier), (35, 75, 40, alphaNumeric, user.name) ] self.content = Row.setStructs(structs=structs, content=self.content) @@ -39,7 +39,7 @@ def setSender(self, user): def setReceiverTaxId(self, receiverTaxId): structs = [ (75, 76, 1, numeric, "1" if len(receiverTaxId) == 11 else "2"), - (76, 91, 15, numeric, receiverTaxId), + (76, 91, 15, alphaNumericRightAligned, receiverTaxId), ] self.content = Row.setStructs(structs=structs, content=self.content) diff --git a/febraban/cnab240/itau/sispag/payment/segmentN.py b/febraban/cnab240/itau/sispag/payment/segmentN.py index 7a2db23..4ac9613 100644 --- a/febraban/cnab240/itau/sispag/payment/segmentN.py +++ b/febraban/cnab240/itau/sispag/payment/segmentN.py @@ -1,7 +1,7 @@ # coding: utf-8 # Pagamento de Contas de Concessionárias e Tributos sem código de barras # Página 31 from ....row import Row -from ....characterType import numeric, alphaNumeric +from ....characterType import numeric, alphaNumeric, alphaNumericRightAligned class SegmentN: @@ -57,7 +57,7 @@ def setTaxIdType(self, taxIdType): def setTaxId(self, taxId): structs = [ - (24, 38, 14, alphaNumeric, taxId), + (24, 38, 14, alphaNumericRightAligned, taxId), ] self.content = Row.setStructs(structs=structs, content=self.content) diff --git a/febraban/cnab240/libs/middlewares/row.py b/febraban/cnab240/libs/middlewares/row.py index ba43acc..994fa6a 100644 --- a/febraban/cnab240/libs/middlewares/row.py +++ b/febraban/cnab240/libs/middlewares/row.py @@ -1,5 +1,5 @@ from re import match -from ...characterType import alphaNumeric, numeric +from ...characterType import alphaNumeric, numeric, alphaNumericRightAligned def validateFormatter(func): @@ -7,15 +7,16 @@ def wrapper(self, string, charactersType, numberOfCharacters, defaultCharacter): if type(numberOfCharacters) != int: raise ValueError("numberOfCharacters must be Integer") - if charactersType not in [alphaNumeric, numeric]: - raise ValueError("charactersType must be alphaNumeric or numeric") + if charactersType not in [alphaNumeric, numeric, alphaNumericRightAligned]: + raise ValueError("charactersType must be alphaNumeric, numeric or alphaNumericRightAligned") if not isinstance(string, str): raise ValueError("(%s,%s) is not an allowed value. It must be string" % (str(string), str(type(string)))) regex = { - alphaNumeric: r'^[A-Za-z0-9\s\-]+$', - numeric: r'^[0-9]+$' + alphaNumeric: r'^[A-Za-z0-9\s\-]+$', + numeric: r'^[0-9]+$', + alphaNumericRightAligned: r'^[A-Z0-9]+$', }[charactersType] if string and not match(regex, string): raise ValueError("You add %s that is a non %s value" % (string, charactersType)) diff --git a/febraban/cnab240/row.py b/febraban/cnab240/row.py index bded1c7..0ccd719 100644 --- a/febraban/cnab240/row.py +++ b/febraban/cnab240/row.py @@ -1,5 +1,5 @@ from .libs.middlewares.row import validateFormatter -from .characterType import numeric, alphaNumeric +from .characterType import numeric, alphaNumeric, alphaNumericRightAligned class Row: @@ -11,7 +11,7 @@ def setStructs(cls, structs, content): string=str(value), charactersType=type, numberOfCharacters=len, - defaultCharacter={numeric: "0", alphaNumeric: " "}[type] + defaultCharacter={numeric: "0", alphaNumeric: " ", alphaNumericRightAligned: "0"}[type] ) content = content[:start] + replacement + content[start+len:] return content @@ -25,14 +25,13 @@ def __formatted(cls, string, charactersType, numberOfCharacters, defaultCharacte Args: string: String to be completed - charactersType: Can be .numeric or .alphaNumeric + charactersType: Can be .numeric, .alphaNumeric or .alphaNumericRightAligned numberOfCharacters: Integer that represents the max string len defaultCharacter: Single string with default character to be completed if string is short Returns: String formatted """ - if type(string) != str: return defaultCharacter * numberOfCharacters - if len(string) > numberOfCharacters: return string[:numberOfCharacters] - if charactersType == numeric: return defaultCharacter * (numberOfCharacters - len(string)) + string - if charactersType == alphaNumeric: return string + defaultCharacter * (numberOfCharacters - len(string)) - return string \ No newline at end of file + if type(string) != str: return defaultCharacter * numberOfCharacters + if len(string) > numberOfCharacters: return string[:numberOfCharacters] + if charactersType == alphaNumeric: return string + defaultCharacter * (numberOfCharacters - len(string)) + return defaultCharacter * (numberOfCharacters - len(string)) + string \ No newline at end of file diff --git a/febraban/cnab240/user.py b/febraban/cnab240/user.py index fe970ef..6573f13 100644 --- a/febraban/cnab240/user.py +++ b/febraban/cnab240/user.py @@ -4,7 +4,7 @@ class User: def __init__(self, name, identifier, bank=None, address=None): self.name = name - self.identifier = identifier + self.identifier = identifier.upper() if isinstance(identifier, str) else identifier self.bank = bank self.address = address