cnab240 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  *.gem
2
2
  *.rbc
3
+ *.test
3
4
  .bundle
4
5
  .config
5
6
  .yardoc
@@ -24,9 +24,38 @@ module Cnab240::Arquivo
24
24
  end
25
25
 
26
26
  def string
27
- a.join("\n")
27
+ linhas.join("\n")
28
28
  end
29
29
 
30
+ def save_to_file(filename)
31
+ File.delete(filename) # LoL
32
+ File.open(filename, 'w') {|f| f.write(string) }
33
+ end
34
+
35
+ def self.load_from_file(filename) # usar IO, blah, tanto faz :D
36
+ arquivos = []
37
+ line_number = 0
38
+ File.open(filename, "r").each_line do |line|
39
+ line.gsub!("\n", "")
40
+ line_number = line_number + 1
41
+ raise "Invalid line length: #{line.length} expected 240 at line number #{line_number}\n\t Line: [#{line}]" unless line.length == 240
42
+ case line[7]
43
+ when '0' # header de arquivo
44
+ arquivos << Arquivo.new
45
+ arquivos.last.header = Header.read(line)
46
+ when '1'
47
+ when '2'
48
+ when '3'
49
+ when '4'
50
+ when '5'
51
+ when '9'
52
+ arquivos.last.header = Trailer.read(line)
53
+ else
54
+ raise 'Invalid tipo de registro: #{line[7]}.'
55
+ end
56
+ end
57
+ arquivos
58
+ end
30
59
  end
31
60
 
32
61
  end
@@ -3,37 +3,37 @@ module Cnab240::Arquivo
3
3
 
4
4
  include Cnab240::DefaultMixin
5
5
 
6
- string :controle_banco, :length => 3
6
+ string :controle_banco, :length => 3, :pad_byte => '0'
7
7
  string :controle_lote, :value => '0000'
8
8
  string :controle_registro, :value => '0'
9
9
 
10
- string :cnab_g004_1, :length => 9
10
+ string :cnab_g004_1, :length => 9, :pad_byte => ' '
11
11
 
12
- string :empresa_tipo, :length => 1
13
- string :empresa_numero, :length => 14
14
- string :empresa_convenio, :length => 20
15
- string :empresa_agencia_codigo, :length => 5
16
- string :empresa_agencia_dv, :length => 1
17
- string :empresa_conta_numero, :length => 12
18
- string :empresa_conta_dv, :length => 1
19
- string :empresa_agencia_conta_dv, :length => 1
20
- string :empresa_nome, :length => 30
12
+ string :empresa_tipo, :length => 1, :pad_byte => '0'
13
+ string :empresa_numero, :length => 14, :pad_byte => '0'
14
+ string :empresa_convenio, :length => 20, :pad_byte => ' '
15
+ string :empresa_agencia_codigo, :length => 5, :pad_byte => '0'
16
+ string :empresa_agencia_dv, :length => 1, :pad_byte => ' '
17
+ string :empresa_conta_numero, :length => 12, :pad_byte => '0'
18
+ string :empresa_conta_dv, :length => 1, :pad_byte => ' '
19
+ string :empresa_agencia_conta_dv, :length => 1, :pad_byte => ' '
20
+ string :empresa_nome, :length => 30, :pad_byte => ' '
21
21
 
22
- string :banco_nome, :length => 30
22
+ string :banco_nome, :length => 30, :pad_byte => ' '
23
23
 
24
- string :cnab_g004_2, :length => 10
24
+ string :cnab_g004_2, :length => 10, :pad_byte => ' '
25
25
 
26
- string :arquivo_codigo, :length => 1
27
- string :arquivo_data_geracao, :length => 8
28
- string :arquivo_hora_geracao,:length => 6
29
- string :arquivo_sequencia, :length => 6
26
+ string :arquivo_codigo, :length => 1, :pad_byte => '0'
27
+ string :arquivo_data_geracao, :length => 8, :pad_byte => '0'
28
+ string :arquivo_hora_geracao,:length => 6, :pad_byte => '0'
29
+ string :arquivo_sequencia, :length => 6, :pad_byte => '0'
30
30
  string :arquivo_layout, :value => '085'
31
- string :arquivo_densidade, :length => 5
31
+ string :arquivo_densidade, :length => 5, :pad_byte => '0'
32
32
 
33
- string :reservado_banco, :length => 20
34
- string :reservado_empresa, :length => 20
33
+ string :reservado_banco, :length => 20, :pad_byte => ' '
34
+ string :reservado_empresa, :length => 20, :pad_byte => ' '
35
35
 
36
- string :cnab_g004_3, :length => 29
36
+ string :cnab_g004_3, :length => 29, :pad_byte => ' '
37
37
 
38
38
  end
39
39
  end
@@ -30,9 +30,11 @@ module Cnab240
30
30
  def linhas
31
31
  seg_array = Array.new
32
32
  estrutura = ESTRUTURA[operacao]
33
+ seg_array << @header.linha
33
34
  estrutura[:segmentos].each do |s|
34
35
  seg_array << @segmentos[s].linha
35
36
  end
37
+ seg_array << @trailer.linha
36
38
  seg_array
37
39
  end
38
40
 
@@ -3,16 +3,16 @@ module Cnab240::Arquivo
3
3
 
4
4
  include Cnab240::DefaultMixin
5
5
 
6
- string :controle_banco, :length => 3
7
- string :controle_lote, :value => '9999'
8
- string :controle_registro, :value => '9'
6
+ string :controle_banco, :length => 3, :pad_byte => '0'
7
+ string :controle_lote, :value => '9999', :pad_byte => '0'
8
+ string :controle_registro, :value => '9', :pad_byte => '0'
9
9
 
10
- string :cnab_g004_1, :length => 9
10
+ string :cnab_g004_1, :length => 9, :pad_byte => ' '
11
11
 
12
- string :totais_qtde_lotes, :length => 6
13
- string :totais_qtde_registros, :length => 6
14
- string :totais_qtde_contas_concil, :length => 6
12
+ string :totais_qtde_lotes, :length => 6, :pad_byte => '0'
13
+ string :totais_qtde_registros, :length => 6, :pad_byte => '0'
14
+ string :totais_qtde_contas_concil, :length => 6, :pad_byte => '0'
15
15
 
16
- string :cnab_g004_2, :length => 205
16
+ string :cnab_g004_2, :length => 205, :pad_byte => ' '
17
17
  end
18
18
  end
@@ -4,6 +4,7 @@ module BinData
4
4
  self.auto_fill if self.respond_to?(:auto_fill)
5
5
  s = StringIO.new
6
6
  self.write(s)
7
+ raise "Invalid line length #{s.string.length}" unless s.string.length == 240
7
8
  s.string
8
9
  end
9
10
  end
@@ -3,40 +3,40 @@ module Cnab240::Pagamentos
3
3
 
4
4
  include Cnab240::DefaultMixin
5
5
 
6
- string :controle_banco, :length => 3
7
- string :controle_lote, :length => 4
8
- string :controle_registro, :length => 1, :initial_value => '1'
9
-
10
- string :servico_operacao, :value => 'C'
11
- string :servico_tipo, :length => 2
12
- string :servico_forma, :length => 2
13
- string :servico_layout, :value => '044'
14
-
15
- string :cnab_g004_1, :length => 1
16
-
17
- string :empresa_tipo, :length => 1
18
- string :empresa_numero, :length => 14
19
- string :empresa_convenio, :length => 20
20
- string :empresa_agencia_codigo, :length => 5
21
- string :empresa_agencia_dv, :length => 1
22
- string :empresa_conta_numero, :length => 12
23
- string :empresa_conta_dv, :length => 1
24
- string :empresa_agencia_conta_dv, :length => 1
25
- string :empresa_nome, :length => 30
26
-
27
- string :informacao_1, :length => 40
28
-
29
- string :endereco_logradouro, :length => 30
30
- string :endereco_numero, :length => 5
31
- string :endereco_complemento, :length => 15
32
- string :endereco_cidade, :length => 20
33
- string :endereco_cep, :length => 5
34
- string :endereco_complemento_cep, :length => 3
35
- string :endereco_estado, :length => 2
36
-
37
- string :indicativo_forma_pagamento, :length => 2
38
- string :cnab_g004_2, :length => 6
39
- string :ocorrencias, :length => 10
6
+ string :controle_banco, :length => 3, :pad_byte => '0'
7
+ string :controle_lote, :length => 4, :pad_byte => '0'
8
+ string :controle_registro, :length => 1, :initial_value => '1', :pad_byte => '0'
9
+
10
+ string :servico_operacao, :value => 'C', :pad_byte => ' '
11
+ string :servico_tipo, :length => 2 , :pad_byte => '0'
12
+ string :servico_forma, :length => 2, :pad_byte => '0'
13
+ string :servico_layout, :value => '044', :pad_byte => '0'
14
+
15
+ string :cnab_g004_1, :length => 1, :pad_byte => ' '
16
+
17
+ string :empresa_tipo, :length => 1, :pad_byte => '0'
18
+ string :empresa_numero, :length => 14, :pad_byte => '0'
19
+ string :empresa_convenio, :length => 20, :pad_byte => ' '
20
+ string :empresa_agencia_codigo, :length => 5, :pad_byte => '0'
21
+ string :empresa_agencia_dv, :length => 1, :pad_byte => ' '
22
+ string :empresa_conta_numero, :length => 12, :pad_byte => '0'
23
+ string :empresa_conta_dv, :length => 1, :pad_byte => ' '
24
+ string :empresa_agencia_conta_dv, :length => 1, :pad_byte => ' '
25
+ string :empresa_nome, :length => 30, :pad_byte => ' '
26
+
27
+ string :informacao_1, :length => 40, :pad_byte => ' '
28
+
29
+ string :endereco_logradouro, :length => 30, :pad_byte => ' '
30
+ string :endereco_numero, :length => 5, :pad_byte => '0'
31
+ string :endereco_complemento, :length => 15, :pad_byte => ' '
32
+ string :endereco_cidade, :length => 20, :pad_byte => ' '
33
+ string :endereco_cep, :length => 5, :pad_byte => '0'
34
+ string :endereco_complemento_cep, :length => 3, :pad_byte => '0'
35
+ string :endereco_estado, :length => 2, :pad_byte => ' '
36
+
37
+ string :indicativo_forma_pagamento, :length => 2, :pad_byte => '0'
38
+ string :cnab_g004_2, :length => 6, :pad_byte => ' '
39
+ string :ocorrencias, :length => 10, :pad_byte => ' '
40
40
 
41
41
  private
42
42
 
@@ -3,42 +3,42 @@ module Cnab240
3
3
 
4
4
  include Cnab240::DefaultMixin
5
5
 
6
- string :controle_banco, :length => 3
7
- string :controle_lote, :length => 4
8
- string :controle_registro, :length => 1, :initial_value => '3'
9
-
10
- string :servico_numero_registro, :length => 5
11
- string :servico_codigo_segmento, :value => 'A'
12
- string :servico_tipo_movimento, :length => 1
13
- string :servico_codigo_movimento, :length => 2
14
-
15
- string :favorecido_camara, :length => 3
16
- string :favorecido_banco, :length => 3
17
- string :favorecido_agencia_codigo, :length => 5
18
- string :favorecido_agencia_dv, :length => 1
19
- string :favorecido_conta_numero, :length => 12
20
- string :favorecido_conta_dv, :length => 1
21
- string :favorecido_agencia_conta_dv, :length => 1
22
- string :favorecido_nome, :length => 30
23
-
24
- string :credito_seu_numero, :length => 20
25
- string :credito_data_pagamento, :length => 8
26
- string :credito_moeda_tipo, :length => 3
27
- string :credito_moeda_quantidade, :length => 15
28
- string :credito_valor_pagamento, :length => 15
29
- string :credito_nosso_numero, :length => 20
30
- string :credito_data_real, :length => 8
31
- string :credito_valor_real, :length => 15
32
-
33
- string :informacao_2, :length => 40
34
-
35
- string :codigo_finalidade_doc, :length => 2
36
- string :codigo_finalidade_ted, :length => 5
37
- string :codigo_finalidade_complementar, :length => 2
38
-
39
- string :cnab_g004_1, :length => 3
40
- string :aviso, :length => 1
41
- string :ocorrencias, :length => 10
6
+ string :controle_banco, :length => 3, :pad_byte => '0'
7
+ string :controle_lote, :length => 4, :pad_byte => '0'
8
+ string :controle_registro, :length => 1, :initial_value => '3', :pad_byte => '0'
9
+
10
+ string :servico_numero_registro, :length => 5, :pad_byte => '0'
11
+ string :servico_codigo_segmento, :value => 'A', :pad_byte => ' '
12
+ string :servico_tipo_movimento, :length => 1, :pad_byte => '0'
13
+ string :servico_codigo_movimento, :length => 2, :pad_byte => '0'
14
+
15
+ string :favorecido_camara, :length => 3, :pad_byte => '0'
16
+ string :favorecido_banco, :length => 3, :pad_byte => '0'
17
+ string :favorecido_agencia_codigo, :length => 5, :pad_byte => '0'
18
+ string :favorecido_agencia_dv, :length => 1, :pad_byte => ' '
19
+ string :favorecido_conta_numero, :length => 12, :pad_byte => '0'
20
+ string :favorecido_conta_dv, :length => 1, :pad_byte => ' '
21
+ string :favorecido_agencia_conta_dv, :length => 1, :pad_byte => ' '
22
+ string :favorecido_nome, :length => 30, :pad_byte => ' '
23
+
24
+ string :credito_seu_numero, :length => 20, :pad_byte => ' '
25
+ string :credito_data_pagamento, :length => 8, :pad_byte => '0'
26
+ string :credito_moeda_tipo, :length => 3, :pad_byte => ' '
27
+ string :credito_moeda_quantidade, :length => 15, :pad_byte => '0'
28
+ string :credito_valor_pagamento, :length => 15, :pad_byte => '0'
29
+ string :credito_nosso_numero, :length => 20, :pad_byte => ' '
30
+ string :credito_data_real, :length => 8, :pad_byte => '0'
31
+ string :credito_valor_real, :length => 15, :pad_byte => '0'
32
+
33
+ string :informacao_2, :length => 40, :pad_byte => ' '
34
+
35
+ string :codigo_finalidade_doc, :length => 2, :pad_byte => ' '
36
+ string :codigo_finalidade_ted, :length => 5, :pad_byte => ' '
37
+ string :codigo_finalidade_complementar, :length => 2, :pad_byte => ' '
38
+
39
+ string :cnab_g004_1, :length => 3, :pad_byte => ' '
40
+ string :aviso, :length => 1, :pad_byte => '0'
41
+ string :ocorrencias, :length => 10, :pad_byte => ' '
42
42
 
43
43
  end
44
44
  end
@@ -3,38 +3,38 @@ module Cnab240
3
3
 
4
4
  include Cnab240::DefaultMixin
5
5
 
6
- string :controle_banco, :length => 3
7
- string :controle_lote, :length => 4
8
- string :controle_registro, :length => 1, :initial_value => '3'
9
-
10
- string :servico_numero_registro, :length => 5
11
- string :servico_codigo_segmento, :value => 'B'
12
-
13
- string :cnab240_g004_1, :length => 3
14
-
15
- string :favorecido_inscricao_tipo, :length => 1
16
- string :favorecido_inscricao_numero, :length => 14
17
- string :favorecido_endereco_logradouro, :length => 30
18
- string :favorecido_endereco_numero, :length => 5
19
- string :favorecido_endereco_complemento, :length => 15
20
- string :favorecido_endereco_bairro, :length => 15
21
- string :favorecido_endereco_cidade, :length => 20
22
- string :favorecido_endereco_cep, :length => 5
23
- string :favorecido_endereco_cep_complemento, :length => 3
24
- string :favorecido_endereco_estado, :length => 2
25
-
26
- string :pagamento_data_vencimento, :length => 8
27
- string :pagamento_valor_documento, :length => 15
28
- string :pagamento_valor_abatimento, :length => 15
29
- string :pagamento_valor_desconto, :length => 15
30
- string :pagamento_valor_mora, :length => 15
31
- string :pagamento_valor_multa, :length => 15
32
-
33
- string :codigo_documento_favorecido, :length => 15
34
- string :aviso, :length => 1
35
- string :codigo_ug, :length => 6
6
+ string :controle_banco, :length => 3, :pad_byte => '0'
7
+ string :controle_lote, :length => 4, :pad_byte => '0'
8
+ string :controle_registro, :length => 1, :initial_value => '3', :pad_byte => '0'
9
+
10
+ string :servico_numero_registro, :length => 5, :pad_byte => '0'
11
+ string :servico_codigo_segmento, :value => 'B', :pad_byte => ' '
12
+
13
+ string :cnab240_g004_1, :length => 3, :pad_byte => ' '
14
+
15
+ string :favorecido_inscricao_tipo, :length => 1, :pad_byte => '0'
16
+ string :favorecido_inscricao_numero, :length => 14, :pad_byte => '0'
17
+ string :favorecido_endereco_logradouro, :length => 30, :pad_byte => ' '
18
+ string :favorecido_endereco_numero, :length => 5, :pad_byte => '0'
19
+ string :favorecido_endereco_complemento, :length => 15, :pad_byte => ' '
20
+ string :favorecido_endereco_bairro, :length => 15, :pad_byte => ' '
21
+ string :favorecido_endereco_cidade, :length => 20, :pad_byte => ' '
22
+ string :favorecido_endereco_cep, :length => 5, :pad_byte => '0'
23
+ string :favorecido_endereco_cep_complemento, :length => 3, :pad_byte => ' '
24
+ string :favorecido_endereco_estado, :length => 2, :pad_byte => ' '
25
+
26
+ string :pagamento_data_vencimento, :length => 8, :pad_byte => '0'
27
+ string :pagamento_valor_documento, :length => 15, :pad_byte => '0'
28
+ string :pagamento_valor_abatimento, :length => 15, :pad_byte => '0'
29
+ string :pagamento_valor_desconto, :length => 15, :pad_byte => '0'
30
+ string :pagamento_valor_mora, :length => 15, :pad_byte => '0'
31
+ string :pagamento_valor_multa, :length => 15, :pad_byte => '0'
32
+
33
+ string :codigo_documento_favorecido, :length => 15, :pad_byte => ' '
34
+ string :aviso, :length => 1, :pad_byte => '0'
35
+ string :codigo_ug, :length => 6, :pad_byte => '0'
36
36
 
37
- string :cnab_g004_2, :length => 8
37
+ string :cnab_g004_2, :length => 8, :pad_byte => ' '
38
38
 
39
39
  end
40
40
  end
@@ -3,30 +3,30 @@ module Cnab240
3
3
 
4
4
  include Cnab240::DefaultMixin
5
5
 
6
- string :controle_banco, :length => 3
7
- string :controle_lote, :length => 4
8
- string :controle_registro, :length => 1, :initial_value => '3'
6
+ string :controle_banco, :length => 3, :pad_byte => '0'
7
+ string :controle_lote, :length => 4, :pad_byte => '0'
8
+ string :controle_registro, :length => 1, :initial_value => '3', :pad_byte => '0'
9
9
 
10
- string :servico_numero_registro, :length => 5
11
- string :servico_codigo_segmento, :value => 'C'
10
+ string :servico_numero_registro, :length => 5, :pad_byte => '0'
11
+ string :servico_codigo_segmento, :value => 'C', :pad_byte => ' '
12
12
 
13
- string :cnab240_g004_1, :length => 3
13
+ string :cnab240_g004_1, :length => 3, :pad_byte => ' '
14
14
 
15
- string :complementar_valor_ir, :length => 15
16
- string :complementar_valor_iss, :length => 15
17
- string :complementar_valor_iof, :length => 15
18
- string :complementar_valor_outras_deducoes, :length => 15
19
- string :complementar_valor_outros_acrescimos, :length => 15
15
+ string :complementar_valor_ir, :length => 15, :pad_byte => '0'
16
+ string :complementar_valor_iss, :length => 15, :pad_byte => '0'
17
+ string :complementar_valor_iof, :length => 15, :pad_byte => '0'
18
+ string :complementar_valor_outras_deducoes, :length => 15, :pad_byte => '0'
19
+ string :complementar_valor_outros_acrescimos, :length => 15, :pad_byte => '0'
20
20
 
21
- string :substituta_agencia, :length => 5
22
- string :substituta_agencia_dv, :length => 1
23
- string :substituta_numero_cc, :length => 12
24
- string :substituta_conta_dv, :length => 1
25
- string :substituta_agencia_conta_dv, :length => 1
21
+ string :substituta_agencia, :length => 5, :pad_byte => '0'
22
+ string :substituta_agencia_dv, :length => 1, :pad_byte => ' '
23
+ string :substituta_numero_cc, :length => 12, :pad_byte => '0'
24
+ string :substituta_conta_dv, :length => 1, :pad_byte => ' '
25
+ string :substituta_agencia_conta_dv, :length => 1, :pad_byte => ' '
26
26
 
27
- string :valor_inss, :length => 15
27
+ string :valor_inss, :length => 15, :pad_byte => '0'
28
28
 
29
- string :cnab_g004_2, :length => 113
29
+ string :cnab_g004_2, :length => 113, :pad_byte => ' '
30
30
 
31
31
  end
32
32
  end
@@ -1,4 +1,4 @@
1
1
  module Cnab240
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
4
4
 
@@ -37,4 +37,13 @@ describe Arquivo do
37
37
  end
38
38
  end
39
39
 
40
+ it "deve ler e escrever em arquivo" do
41
+ arquivo = Cnab240::Arquivo::Arquivo.new
42
+ (1..10).each do |n|
43
+ arquivo.lotes << Cnab240::Lote.new(:pagamento)
44
+ end
45
+ arquivo.save_to_file("spec/tmp/arquivo.test")
46
+ arquivo = Cnab240::Arquivo::Arquivo.load_from_file("spec/tmp/arquivo.test")
47
+ end
48
+
40
49
  end
@@ -38,4 +38,9 @@ describe Cnab240::Arquivo::Header do
38
38
 
39
39
  header.should respond_to(:cnab_g004_3)
40
40
  end
41
+
42
+ it "header deve ter 240 caracteres" do
43
+ header = Cnab240::Arquivo::Header.new
44
+ header.linha.length.should be(240)
45
+ end
41
46
  end
@@ -19,4 +19,9 @@ describe Cnab240::Arquivo::Trailer do
19
19
  header.should respond_to(:cnab_g004_2)
20
20
  end
21
21
 
22
+ it "trailer deve ter 240 caracteres" do
23
+ trailer = Cnab240::Arquivo::Trailer.new
24
+ trailer.linha.length.should be(240)
25
+ end
26
+
22
27
  end
data/spec/spec_helper.rb CHANGED
@@ -5,4 +5,6 @@ require 'cnab240'
5
5
 
6
6
  RSpec.configure do |config|
7
7
 
8
+
9
+
8
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cnab240
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-11 00:00:00.000000000 Z
12
+ date: 2012-07-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -97,7 +97,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
97
97
  version: '0'
98
98
  segments:
99
99
  - 0
100
- hash: -2630031989102335816
100
+ hash: 3619480557024491768
101
101
  required_rubygems_version: !ruby/object:Gem::Requirement
102
102
  none: false
103
103
  requirements:
@@ -106,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
106
  version: '0'
107
107
  segments:
108
108
  - 0
109
- hash: -2630031989102335816
109
+ hash: 3619480557024491768
110
110
  requirements: []
111
111
  rubyforge_project: cnab240
112
112
  rubygems_version: 1.8.24