Giter Site home page Giter Site logo

py_remessas's Introduction

Python Factory for CNAB 400

Generate CNAB 400 files

Support

  • Contracts
  • Data for liquitation
  • Receivable Units

Development and Test

to either develop or test the library please install pipenv and run

pipenv run shell
pipenv install

to run tests in Linux\WSL\OSX run the following command in the root folder

make test

Validation

You can use the following links to test files:

https://wspf.bradesco.com.br/wsValidadorTeleBanco/ValidadorRemessa.aspx

Basic usage

from io import SringIO

from cnab.cnab import CnabReturnHeader, CnabReturnTypeThree, CnabReturnTrail, CnabName
from cnab.enums import EArrangement
from cnab.exporters import FileExporter
from cnab.types import CnabDate, Document, Guid, Money

# Create properties
unique_id = Guid("12345678-1234-1234-1234-123456789abc")
contract_key = Guid("12345678-1234-1234-1234-123456789abc")
receivable_unit_key = Guid("12345678-1234-1234-1234-123456789abc")
customer_document = Document("11111111111")
transferror_document = Document("11111111111")
accreditor_document = Document("11111111111")
holder_document = Document("11111111111")
arrengement_code = EArrangement.AMEX_CREDIT_CARD
date_liquidation = CnabDate(1, 12, 2022)  # 1/12/2022
receivable_unit_value = Money(1000)  # R$ 10,00
commited_contract_value = Money(1000)  # R$ 10,00
contract_priority = 1

# instance of name factory
# used mostly for file exporter
last_used_file = "CC121000.RET"
service_prefix = "TRT"
extension = "RET"
name = CnabName(last_used_file, service_prefix, extension)

# instance of Header factory
header = CnabReturnHeader(customer_document, unique_id)

# instance of Trail Factory
trail = CnabReturnTrail(1)

# instance of Record Factory with properties
contract_one = CnabReturnTypeThree(
    contract_key=contract_key,
    receivable_unit_key=receivable_unit_key,
    transferror_document=transferror_document,
    accreditor_document=accreditor_document,
    holder_document=holder_document,
    arrengement_code=arrengement_code,
    date_liquidation=date_liquidation,
    receivable_unit_value=receivable_unit_value,
    commited_contract_value=commited_contract_value,
    contract_priority=contract_priority,
)

# You can export the content using one of the exporters
# provided by the library or create your own

# Export to file
FileExporter.export(name, header, trail, [contract_one], "./")

# Return a Generator for further usage
GeneratorExporter.export(header, trail, [contract_one])

# Attach content to a pre declared StringIO
iostream = StringIO()
StreamSyncExporter.export(header, trail, [contract_one], iostream)

# Return a fresh StringIO instance
StreamSyncExporter.export(header, trail, [contract_one])

Building you own Exporter

from socket import socket, AF_INET, SOCK_STREAM

from cnab.cnab import CnabReturnHeader, CnabReturnTypeThree, CnabReturnTrail
from cnab.interfaces import ICnabFactory

# Create your own exporter by using the ICnabFactory interface
# in this (untested) example we can generate the CNAB content
# and send to a socket server

class SocketExporter(ICnabFactory):

    @staticmethod
    def export(
        header_factory: ICnabLine,
        trail_factory: ICnabLine,
        contracts: list[ICnabLine],
        host: str,
        port: int
    ):
        with socket(AF_INET, SOCK_STREAM) as sock:
            sock.connect((host, port))
            sock.send(header_factory.make_line())
            
            for contract in contracts:
                sock.send(contract.make_line())
            
            sock.send(trail_factory.make_line())


# Example of exporter usage
header = CnabReturnHeader(...)
trail = CnabReturnTrail(...)
contract_one = CnabReturnTypeThree(...)
SocketExporter.export(header, trail, [contract_one], "127.0.0.1", 8080)

py_remessas's People

Contributors

frreinert avatar

Stargazers

Salomão Prestupa Garbim avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.