class Rocknab::Layouts::CNAB240::CNAB240

This is the class that builds a CNAB240 file.

Parameters:

- params: A hash containing the following values:
  :cnpj, :branch, :account, :digit, :name, :bank, :date,
  :zipcode, :state
- payments: An array of hashes, each with the following:
  :bank, :branch, :account, :digit, :name, :date, :value

Usage:

cnab = CNAB240.new(params, payments)
cnab.build
=> "... CNAB content ..."

Constants

HEADER_PARAMS
TRAILER_PARAMS

Public Instance Methods

build() click to toggle source

Builds the CNAB 240 v81 file according to the contents

Usage:

cnab.build
=> "... CNAB content ..."
# File lib/rocknab/layouts/cnab_240.rb, line 34
def build
  [ header, batches, trailer, nil ].join("\r\n")
end

Private Instance Methods

batch_groups() click to toggle source
# File lib/rocknab/layouts/cnab_240.rb, line 54
def batch_groups
  payments.group_by do |payment|
    is_same_bank = payment.dig(:bank) === params.dig(:bank_code)
    is_ted = payment.dig(:is_ted)
    is_savings = payment.dig(:is_savings)

    {
      is_same_bank: is_same_bank,
      is_ted: !is_same_bank && is_ted,
      is_savings: is_savings,
    }
  end
end
batches() click to toggle source
# File lib/rocknab/layouts/cnab_240.rb, line 44
def batches
  batch_groups.each_with_index.map do |group, index|
    config, batch_payments = group
    is_same_bank = config[:is_same_bank]
    is_ted = config[:is_ted]
    is_savings = config[:is_savings]
    Batch.new(params, batch_payments, is_same_bank, is_ted, is_savings, index + 1).build
  end
end
header() click to toggle source
# File lib/rocknab/layouts/cnab_240.rb, line 40
def header
  Header.new(params.slice(*HEADER_PARAMS)).build
end
trailer() click to toggle source
# File lib/rocknab/layouts/cnab_240.rb, line 68
def trailer
  default_params = {
    register_count: batches.flatten.count + 2,
    batch_count: batches.length
  }
  line_params = params.slice(*TRAILER_PARAMS).merge(default_params)

  Trailer.new(line_params).build
end