Class: Cnab240::Lote

Inherits:
Object
  • Object
show all
Defined in:
lib/cnab240/lote.rb,
lib/cnab240/lote/v40/boletos/header.rb,
lib/cnab240/lote/v40/boletos/trailer.rb

Defined Under Namespace

Modules: V40

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Lote

Returns a new instance of Lote.

Yields:

  • (_self)

Yield Parameters:

  • _self (Cnab240::Lote)

    the object that the method was called on



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cnab240/lote.rb', line 10

def initialize(options = {})
  @segmentos = []

  @operacao ||= options[:operacao]
  @tipo ||= options[:tipo]
  @versao ||= options[:versao]
  @fallback ||= options[:fallback]
  @versao ||= 'V86'

  fail "Operacao '#{operacao}' nao suportada na versao #{@versao}" if ESTRUTURA[@versao][operacao].nil?

  estrutura = ESTRUTURA[@versao][operacao]

  @header = estrutura[:header].new
  @trailer = estrutura[:trailer].new

  yield self if block_given?
end

Instance Attribute Details

#arquivoObject

Returns the value of attribute arquivo.



8
9
10
# File 'lib/cnab240/lote.rb', line 8

def arquivo
  @arquivo
end

#headerObject

Returns the value of attribute header.



3
4
5
# File 'lib/cnab240/lote.rb', line 3

def header
  @header
end

#operacaoObject

Returns the value of attribute operacao.



6
7
8
# File 'lib/cnab240/lote.rb', line 6

def operacao
  @operacao
end

#segmentosObject

Returns the value of attribute segmentos.



4
5
6
# File 'lib/cnab240/lote.rb', line 4

def segmentos
  @segmentos
end

#tipoObject

Returns the value of attribute tipo.



7
8
9
# File 'lib/cnab240/lote.rb', line 7

def tipo
  @tipo
end

#trailerObject

Returns the value of attribute trailer.



5
6
7
# File 'lib/cnab240/lote.rb', line 5

def trailer
  @trailer
end

Instance Method Details

#<<(s) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cnab240/lote.rb', line 35

def <<(s)
  versao = arquivo.versao unless arquivo.nil?
  versao ||= @versao
  case s
  when Symbol, String
    segmentos << seg = eval("Cnab240::#{versao}::Segmento#{s.to_s.upcase}.new")
  else
    segmentos << seg = s
  end
  seg.lote = self
  seg
end

#auto_fillObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cnab240/lote.rb', line 63

def auto_fill
  trailer.totais_qtde_registros = (segmentos.length + 2).to_s

  if trailer.respond_to?(:totais_valor)
    _total = 0
    segmentos.each { |seg| _total += seg[:credito_valor_pagamento].to_i }
    trailer.totais_valor = _total.to_s
  end

  # Quando o lote é de boletos
  if trailer.respond_to?(:qtde_cobranca_simples)
    _total = 0
    _qtde = 0
    segmentos.each do |seg|
      next unless seg.class.to_s =~ /SegmentoP/
      _qtde += 1
      _total += seg[:valor].to_i
    end
    trailer.qtde_cobranca_simples = _qtde.to_s
    trailer.total_cobranca_simples = _total.to_s
  end
end

#linhasObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/cnab240/lote.rb', line 48

def linhas
  auto_fill if Cnab240.auto_fill_enabled
  seg_array = []
  seg_array << @header.linha
  segmentos.each do |s|
    seg_array << s.linha
  end
  seg_array << @trailer.linha
  seg_array
end

#read_segmento(s, _line) ⇒ Object



29
30
31
32
33
# File 'lib/cnab240/lote.rb', line 29

def read_segmento(s, _line)
  versao = arquivo.versao unless arquivo.nil?
  versao ||= @versao
  segmentos << seg = eval("Cnab240::#{versao}::Segmento#{s.to_s.upcase}.read(_line)")
end

#stringObject



59
60
61
# File 'lib/cnab240/lote.rb', line 59

def string
  linhas.join("\n")
end