Class: Ifmb::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/ifmb/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Generator

Returns a new instance of Generator.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/ifmb/generator.rb', line 6

def initialize(args = {})
  args = args.select do |k,v|
    [:entidade, :sub_entidade, :order_id, :order_value].include? k
  end

  args.each do |attr, value|
    send "#{attr}=", value
    # instance_variable_set "@#{attr}", value
  end

  @erros = Hash.new { |hash, key| hash[key] = [] }
end

Instance Attribute Details

#entidadeObject (readonly)

Returns the value of attribute entidade.



4
5
6
# File 'lib/ifmb/generator.rb', line 4

def entidade
  @entidade
end

#order_idObject (readonly)

Returns the value of attribute order_id.



4
5
6
# File 'lib/ifmb/generator.rb', line 4

def order_id
  @order_id
end

#order_valueObject (readonly)

Returns the value of attribute order_value.



4
5
6
# File 'lib/ifmb/generator.rb', line 4

def order_value
  @order_value
end

#sub_entidadeObject (readonly)

Returns the value of attribute sub_entidade.



4
5
6
# File 'lib/ifmb/generator.rb', line 4

def sub_entidade
  @sub_entidade
end

Instance Method Details

#generateObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ifmb/generator.rb', line 59

def generate
  raise ArgumentError.new "arguments are invalid! #{@errors}"  unless valid?

  check_val = 0
  # order_id  = "0000" + order_id.to_s

  # order_id = order_id.slice((order_id.length - 4)..-1).to_s

  if ((order_value.to_f * 100).round < 1)
    raise RuntimeError.new 'impossivel de realizar pagamentos de 1 euro'
  end

  if ((order_value.to_f * 100).round >= 1_000_000)
    # this is not implemented
    # related with the while loop
    raise RuntimeError.new 'Aviso: Pagamento Fraccionado por exceder o valor limite para pagamentos por multibanco'
  end

  # this is not implemented
  # while (order_value.to_i >= 1_000_000)
  #   order_id += order_id
  #   generate_mb_ref(entidade, sub_entidade, order_id, 999999.99)
  #   order_value = order_value.to_f - 999999.99
  # end


  check_str = sprintf('%05u%03u%04u%08u', entidade, sub_entidade, order_id.to_s, (order_value.to_f * 100).round)

  chk_array = [3, 30, 9, 90, 27, 76, 81, 34, 49, 5, 50, 15, 53, 45, 62, 38, 89, 17, 73, 51]

  20.times { |i|
    check_int = check_str[19 - i, 1]
    check_val += (check_int.to_i%10)*chk_array[i]
  }

  check_val %= 97

  check_digits = sprintf('%02u', 98-check_val);

  @referencia = "#{sub_entidade}#{check_str[8,3]}#{check_str[11,1]}#{check_digits}".to_i
end

#in_htmlObject



55
56
57
# File 'lib/ifmb/generator.rb', line 55

def in_html
  @html_format ||= render_html
end

#referenciaObject



51
52
53
# File 'lib/ifmb/generator.rb', line 51

def referencia
  @referencia ||= generate
end

#valid?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ifmb/generator.rb', line 28

def valid?
  if (entidade.to_s.length < 5)
    @errors[:entidade] << 'nao e valida! Tem que ter 5 digitos'
    return false
  elsif (entidade.to_s.length > 5)
    @errors[:entidade] << 'nao e valida! Tem que ter 5 digitos'
    return false
  end

  if (sub_entidade.to_s.length == 0)
    p 'sub entidade invalida'
    return false
  elsif (sub_entidade.to_s.length == 1)
    sub_entidade = '00' + sub_entidade.to_s
  elsif (sub_entidade.to_s.length == 2)
    sub_entidade = '0' + sub_entidade.to_s
  elsif (sub_entidade.to_s.length > 3)
    p 'sub entidade invalida +'
    return false
  end
  true
end