abank 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f1720d3e8b1de4bc6ea635712404363e460b3550af47a665a06652fae028f917
4
- data.tar.gz: eef494213d2f5bf45887a1f643ec2f1becf2d470e9cc5451c2412dd41d9696c2
3
+ metadata.gz: 357eabd6927351fcd006bf58dda24c7b323395749b4ab6ef4ecf6f596a126241
4
+ data.tar.gz: 0c3fc3b9e2ebda8bfe13b0ba072f50b0ff303668dfde4ffa7378e04f1470b81e
5
5
  SHA512:
6
- metadata.gz: eb2aa5a87322b5cb27fb2fac24cd99d5288eb3be339a0976cd2d2cd12c3ee46b63c2e09e8060c16dd3f07197ba837ecf6fa612a658f86cb3c7875a5546dd16b5
7
- data.tar.gz: 9498f7fa1b1a5cbdf4c56b39cd770073eb13642d669847fecdd5a1b3285dd3ffc15fb0e6437a8e2758d6223e4e128e7eb0664e6f6ffb42cc73d3d1ce46380ea9
6
+ metadata.gz: b11a19c0f7039ff253776441e4f7b7de443808c4bf90dfec078a65dab3342b40197017d36e69d30d09d35500afd6e187ebe7113580e36ef921737085599c26af
7
+ data.tar.gz: 98daf017bef2b5860fe498c9f11f6881374ebaa87451a10bf24fe1fa95006230c734a6dc1176e036983ec0e991777e59cfebaa34f2c2bc67828d3b95563163ae
@@ -1,4 +1,5 @@
1
1
  AllCops:
2
+ TargetRubyVersion: 2.6
2
3
  EnabledByDefault: true
3
4
 
4
5
  Style/ClassAndModuleChildren:
@@ -27,3 +28,6 @@ Layout/FirstMethodParameterLineBreak:
27
28
 
28
29
  Layout/FirstHashElementLineBreak:
29
30
  Enabled: false
31
+
32
+ Style/MissingElse:
33
+ Enabled: false
data/Gemfile CHANGED
@@ -1,6 +1,8 @@
1
- source "https://rubygems.org"
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in abank.gemspec
4
6
  gemspec
5
7
 
6
- gem "rake", "~> 12.0"
8
+ gem 'rake', '~> 12.0'
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- abank (0.2.4)
4
+ abank (0.2.5)
5
5
  google-cloud-bigquery
6
6
  roo
7
7
  thor
@@ -17,7 +17,7 @@ GEM
17
17
  declarative-option (0.1.0)
18
18
  faraday (1.0.1)
19
19
  multipart-post (>= 1.2, < 3)
20
- google-api-client (0.39.5)
20
+ google-api-client (0.40.1)
21
21
  addressable (~> 2.5, >= 2.5.1)
22
22
  googleauth (~> 0.9)
23
23
  httpclient (>= 2.8.1, < 3.0)
@@ -25,7 +25,7 @@ GEM
25
25
  representable (~> 3.0)
26
26
  retriable (>= 2.0, < 4.0)
27
27
  signet (~> 0.12)
28
- google-cloud-bigquery (1.21.0)
28
+ google-cloud-bigquery (1.21.1)
29
29
  concurrent-ruby (~> 1.0)
30
30
  google-api-client (~> 0.33)
31
31
  google-cloud-core (~> 1.2)
@@ -34,9 +34,9 @@ GEM
34
34
  google-cloud-core (1.5.0)
35
35
  google-cloud-env (~> 1.0)
36
36
  google-cloud-errors (~> 1.0)
37
- google-cloud-env (1.3.1)
37
+ google-cloud-env (1.3.2)
38
38
  faraday (>= 0.17.3, < 2.0)
39
- google-cloud-errors (1.0.0)
39
+ google-cloud-errors (1.0.1)
40
40
  googleauth (0.12.0)
41
41
  faraday (>= 0.17.3, < 2.0)
42
42
  jwt (>= 1.4, < 3.0)
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Abank [![Build Status](https://travis-ci.com/hernanirvaz/abank.svg?branch=master)](https://travis-ci.com/hernanirvaz/abank)
2
2
 
3
- Arquiva movimentos conta-corrente, conta-cartao do activobank no bigquery. Permite apagar movimentos similares/existentes ja no bigquery. Permite ainda classificar movimentos ja no bigquery.
3
+ Arquiva movimentos conta-corrente, conta-cartao do activobank no bigquery. Permite apagar/recriar movimentos/rendas ja no bigquery. Permite ainda classificar movimentos no bigquery.
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,10 +20,14 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- $ abank help [COMANDO] # ajuda aos comandos
24
- $ abank mostra # mostra dados da folha calculo
25
- $ abank load # carrega dados da folha calculo no bigquery
26
- $ abank classifica # classifica arquivo no bigquery
23
+ $ abank apagamv -k=KEY[,KEY...] # apaga movimentos
24
+ $ abank apagact -c=CONTRATO # apaga contrato arrendamento
25
+ $ abank criact -c=CONTRATO # cria contrato arrendamento
26
+ $ abank recriact -c=CONTRATO # atualiza rendas de contrato arrendamento
27
+ $ abank recriare # atualiza rendas dos contratos ativos
28
+ $ abank load # carrega dados da folha calculo
29
+ $ abank show # mostra dados da folha calculo
30
+ $ abank tag # classifica movimentos
27
31
 
28
32
  ## Development
29
33
 
data/Rakefile CHANGED
@@ -1,2 +1,4 @@
1
- require "bundler/gem_tasks"
2
- task :default => :build
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ task default: :build
@@ -10,12 +10,9 @@ Gem::Specification.new do |spec|
10
10
  spec.homepage = 'https://github.com/hernanirvaz/abank'
11
11
  spec.license = 'MIT'
12
12
 
13
- spec.summary = 'Arquiva <conta-corrente>.xlsx,' \
14
- ' <conta-cartao>.xlsx no bigquery.'
15
- spec.description = spec.summary +
16
- ' Pode apagar movimentos similares/existentes' \
17
- ' ja no bigquery.' \
18
- ' Pode ainda classificar movimentos ja no bigquery.'
13
+ spec.summary = 'Arquiva movimentos conta-corrente, conta-cartao do activobank no bigquery.'
14
+ spec.description = spec.summary + ' Permite apagar/recriar movimentos/rendas ja no bigquery. ' \
15
+ ' Permite ainda classificar movimentos no bigquery.'
19
16
 
20
17
  spec.metadata['homepage_uri'] = spec.homepage
21
18
  spec.metadata['yard.run'] = 'yard'
@@ -24,10 +21,10 @@ Gem::Specification.new do |spec|
24
21
 
25
22
  # Specify which files should be added to the gem when it is released.
26
23
  # The `git ls-files -z` loads files in RubyGem that have been added into git.
27
- spec.files = Dir.chdir(File.expand_path(__dir__)) do
28
- `git ls-files -z`.split("\x0")
29
- .reject { |f| f.match(%r{^(test|spec|features)/}) }
30
- end
24
+ spec.files =
25
+ Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
31
28
  spec.bindir = 'exe'
32
29
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
30
  spec.require_paths = ['lib']
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require "bundler/setup"
4
- require "abank"
4
+ require 'bundler/setup'
5
+ require 'abank'
5
6
 
6
7
  # You can add fixtures and/or initialization code here to make experimenting
7
8
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +11,5 @@ require "abank"
10
11
  # require "pry"
11
12
  # Pry.start
12
13
 
13
- require "irb"
14
+ require 'irb'
14
15
  IRB.start(__FILE__)
@@ -9,92 +9,86 @@ require 'abank/version'
9
9
 
10
10
  # @author Hernani Rodrigues Vaz
11
11
  module Abank
12
- ID = `whoami`.chomp
12
+ DR = "/home/#{`whoami`.chomp}/Downloads"
13
13
 
14
14
  class Error < StandardError; end
15
15
 
16
16
  # CLI para carregar folhas calculo comuns no bigquery
17
17
  class CLI < Thor
18
- desc 'tag', 'classifica movimentos no bigquery'
19
- # classifica movimentos no bigquery
18
+ desc 'tag', 'classifica movimentos'
19
+ # classifica movimentos
20
20
  def tag
21
- Big.new(i: true).mv_classifica
21
+ Big.new.mv_classifica
22
22
  end
23
23
 
24
- desc 'rendas', 'atualiza rendas no bigquery'
25
- # atualiza rendas no bigquery
26
- def rendas
27
- Big.new.re_atualiza
24
+ desc 'apagamv', 'apaga movimentos'
25
+ option :k, banner: 'KEY[,KEY...]', required: true, desc: 'keys movimentos a apagar'
26
+ # apaga movimentos
27
+ def apagamv
28
+ Big.new(k: options[:k]).mv_delete.mv_insert.re_work
28
29
  end
29
30
 
30
- desc 'apaga', 'apaga movimentos no bigquery'
31
- option :k, banner: 'k1[,k2,...]', required: true,
32
- desc: 'Keys movimentos a apagar'
33
- # apaga movimentos no bigquery
34
- def apaga
35
- Big.new(k: options[:k]).mv_apaga
31
+ desc 'criact', 'cria contrato arrendamento'
32
+ option :c, banner: 'CONTRATO', required: true, desc: 'Identificador contrato arrendamento'
33
+ option :t, type: :boolean, default: true, desc: 'cria todas as rendas?'
34
+ option :d, banner: 'DATA', default: '', desc: 'data contrato arrendamento'
35
+ # cria contrato arrendamento
36
+ def criact
37
+ Big.new(c: options[:c], t: options[:t], d: options[:d]).ct_cria
36
38
  end
37
39
 
38
- desc 'load', 'carrega dados da folha calculo no bigquery'
39
- option :d, banner: 'DIR', default: "/home/#{ID}/Downloads",
40
- desc: 'Onde procurar folhas calculo'
41
- option :v, banner: 'DATA', default: '',
42
- desc: 'data valor para movimentos a carregar'
43
- option :g, banner: 'TAG', default: '',
44
- desc: 'classificacao para movimentos a carregar'
45
- option :s, type: :boolean, default: false,
46
- desc: 'apaga linha similar no bigquery'
47
- option :e, type: :boolean, default: false,
48
- desc: 'apaga linha igual no bigquery'
49
- option :m, type: :boolean, default: false,
50
- desc: 'apaga linhas existencia multipla no bigquery'
40
+ desc 'apagact', 'apaga contrato arrendamento'
41
+ option :c, banner: 'CONTRATO', required: true, desc: 'Identificador contrato arrendamento'
42
+ option :t, type: :boolean, default: false, desc: 'apaga todas as rendas?'
43
+ # apaga contrato arrendamento
44
+ def apagact
45
+ Big.new(c: options[:c], t: options[:t]).ct_apaga
46
+ end
47
+
48
+ desc 'recriact', 'atualiza rendas de contrato arrendamento'
49
+ option :c, banner: 'CONTRATO', required: true, desc: 'Identificador contrato arrendamento'
50
+ option :t, type: :boolean, default: false, desc: 'apaga todas as rendas?'
51
+ option :d, banner: 'DATA', default: '', desc: 'data contrato arrendamento'
52
+ # atualiza rendas de contrato arrendamento
53
+ def recriact
54
+ Big.new(c: options[:c], t: options[:t]).ct_apaga
55
+ Big.new(c: options[:c], t: true, d: options[:d]).ct_cria
56
+ end
57
+
58
+ desc 'recriare', 'atualiza rendas dos contratos ativos'
59
+ option :t, type: :boolean, default: false, desc: 'atualiza todas as rendas?'
60
+ # atualiza rendas dos contratos ativos
61
+ def recriare
62
+ Big.new(t: options[:t]).re_atualiza
63
+ end
64
+
65
+ desc 'load', 'carrega dados da folha calculo'
66
+ option :s, type: :boolean, default: false, desc: 'apaga movimento similar'
67
+ option :e, type: :boolean, default: false, desc: 'apaga movimento igual'
68
+ option :v, banner: 'DATA', default: '', desc: 'data valor para movimentos a carregar'
69
+ option :g, banner: 'TAG', default: '', desc: 'classificacao para movimentos a carregar'
51
70
  # carrega folha calculo
52
71
  def load
53
- Dir.glob("#{options[:d]}/*.xlsx").sort.each do |f|
54
- Big::Folha.new(f, load_ops).processa_folha
72
+ Dir.glob("#{DR}/*.xlsx").sort.each do |f|
73
+ Big::Folha.new(load_opc.merge(f: f)).processa_xls
55
74
  end
56
75
  end
57
76
 
58
77
  desc 'show', 'mostra dados da folha calculo'
59
- option :d, banner: 'DIR', default: "/home/#{ID}/Downloads",
60
- desc: 'Onde procurar folhas calculo'
61
78
  # mostra folha calculo
62
79
  def show
63
- Dir.glob("#{options[:d]}/*.xlsx").sort.each do |f|
64
- Big::Folha.new(f).processa_folha
80
+ Dir.glob("#{DR}/*.xlsx").sort.each do |f|
81
+ Big::Folha.new(f: f).processa_xls
65
82
  end
66
83
  end
67
84
 
68
- desc 'criare', 'cria contrato arrendamento/rendas no bigquery'
69
- option :c, banner: 'CONTRATO', required: true,
70
- desc: 'Identificador contrato arrendamento a criar'
71
- option :t, type: :boolean, default: true,
72
- desc: 'cria todas as rendas?'
73
- option :v, banner: 'DATA', default: '',
74
- desc: 'data contrato arrendamento a criar'
75
- # cria contrato arrendamento/rendas no bigquery
76
- def criare
77
- Big::Contrato.new(options[:c], { t: options[:t], v: options[:v] }).re_cria
78
- end
79
-
80
- desc 'apagare', 'apaga contrato arrendamento/rendas no bigquery'
81
- option :c, banner: 'CONTRATO', required: true,
82
- desc: 'Identificador contrato arrendamento a apagar'
83
- option :t, type: :boolean, default: false,
84
- desc: 'apaga todas as rendas?'
85
- # apaga contrato arrendamento/rendas no bigquery
86
- def apagare
87
- Big::Contrato.new(options[:c], { t: options[:t], v: '' }).re_apaga
88
- end
89
-
90
85
  no_commands do
91
- # @return [Hash] opcoes trabalho com linhas para load
92
- def load_ops
93
- { s: options[:s], e: options[:e], m: options[:m], i: true,
94
- v: options[:v], g: options[:g] }
86
+ # @return [Hash] opcoes trabalho com movimentos para load
87
+ def load_opc
88
+ { s: options[:s], e: options[:e], i: true, v: options[:v], g: options[:g] }
95
89
  end
96
90
  end
97
91
 
98
- default_task :rendas
92
+ default_task :show
99
93
  end
100
94
  end
@@ -2,137 +2,133 @@
2
2
 
3
3
  require 'google/cloud/bigquery'
4
4
 
5
- # class Contrato
5
+ # @see Abank::Big
6
6
  class Abank::Big
7
7
  DF = '%Y-%m-%d'
8
8
 
9
+ # @return [Hash] opcoes trabalho
10
+ attr_reader :opcao
11
+
9
12
  # @return [Google::Cloud::Bigquery] API bigquery
10
- attr_reader :api
11
- # @return [Hash] opcoes trabalho com linhas
12
- attr_reader :opl
13
+ attr_reader :bqapi
13
14
 
14
15
  # @return [Google::Cloud::Bigquery::QueryJob] job bigquery
15
- attr_reader :job
16
- # @return [Google::Cloud::Bigquery::Data] lista devolvida pelo select
17
- attr_reader :resultados
18
-
19
- # @param [Hash] ops opcoes trabalho
20
- # @option ops [Boolean] :s (false) apaga linha similar? (mv)
21
- # @option ops [Boolean] :e (false) apaga linha igual? (mv)
22
- # @option ops [Boolean] :m (false) apaga linhas existencia multipla? (mv)
23
- # @option ops [Boolean] :i (false) insere linha nova? (mv)
24
- # @option ops [String] :v ('') data valor (mv)/data contrato (re)
25
- # @option ops [String] :g ('') classificacao movimentos (mv)
26
- # @option ops [Boolean] :t (false) trabalha todoas as rendas? (re)
27
- # @option ops [String] :k ('') keys movimentos a apagar (mv)
28
- # @return [Big] acesso bigquery dataset
29
- def initialize(ops = {})
30
- @opl = ops
31
- @api ||= Google::Cloud::Bigquery.new
32
- p ['Big', ops, api]
33
- end
34
-
35
- # (see CLI#classifica)
36
- def mv_classifica
37
- return unless opl[:i]
38
-
39
- i = dml('update hernanilr.ab.mv set mv.ct=tt.nct ' \
40
- 'from (select * from hernanilr.ab.cl) as tt ' \
41
- 'where mv.dl=tt.dl and mv.dv=tt.dv ' \
42
- 'and mv.ds=tt.ds and mv.vl=tt.vl')
43
- puts 'LINHAS CLASSIFICADAS ' + i.to_s
44
- return unless i.positive?
45
-
46
- re_atualiza
47
- end
48
-
49
- # (see CLI#atualiza)
50
- def re_atualiza
51
- r = re_join(lista_ativos)
52
- if r.size.zero?
53
- puts 'NAO EXISTEM RENDAS NOVAS'
54
- else
55
- puts 'RENDAS CRIADAS ' + dml('insert hernanilr.ab.re VALUES' + r).to_s
56
- end
57
- end
16
+ attr_reader :bqjob
58
17
 
59
- # (see CLI#apagamv)
60
- def mv_apaga
61
- e = ct_envolvidos
62
- i = dml(sql_apaga_mv)
63
- puts 'MOVIMENTOS APAGADOS ' + i.to_s
64
- return unless i.positive? && e.count.positive?
18
+ # @return [Google::Cloud::Bigquery::Data] resultado do select
19
+ attr_reader :bqres
65
20
 
66
- e.map { |c| Contrato.new(c).re_apaga }
21
+ # @return [Integer] numero linhas afetadas pela Data Manipulation Language (DML)
22
+ attr_reader :bqnrs
67
23
 
68
- re_atualiza
69
- end
24
+ # @return [String] movimentos a inserir (values.mv)
25
+ attr_reader :mvvls
70
26
 
71
- def ct_envolvidos
72
- sel(sql_sel_mv).group_by { |r| r[:ct] }
73
- .delete_if { |k, _| !k || k[0] != 'r' }.keys
74
- end
27
+ # @return [String] movimentos a apagar (keysin.mv)
28
+ attr_reader :mvkys
75
29
 
76
- # @return [Array<Hash>] lista contratos com lista movimentos novos
77
- def lista_ativos
78
- sel(sql_ativos_re).map { |c| Contrato.new(c[:ct]).dados_contrato }.compact
30
+ # acesso a base dados abank no bigquery
31
+ #
32
+ # @param [Hash] opc opcoes trabalho
33
+ # @option opc [String] :k ('') movimentos a apagar (keysin.mv)
34
+ # @option opc [String] :c ('') id contrato arrendamento (re)
35
+ # @option opc [String] :d ('') data inicio contrato arrendamento (re)
36
+ # @option opc [Boolean] :t (false) trabalha todas as rendas? (re)
37
+ # @return [Hash] opcoes trabalho
38
+ def initialize(opc = {})
39
+ @opcao = opc
40
+ @bqapi = Google::Cloud::Bigquery.new
41
+ @mvvls = ''
42
+ @mvkys = opc.fetch(:k, '')
43
+ @ctide = opc.fetch(:c, '')
44
+ # p ['B', opcao]
45
+ opcao
79
46
  end
80
47
 
81
- # @param [Array<Hash>] lct lista contratos com lista movimentos novos
82
- # @return [String] row formatada das novas rendas para inserir bigquery
83
- def re_join(lct)
84
- lct.map { |c| Contrato::Rendas.new(c).rendas }.flatten(1).join(',')
48
+ # (see CLI#tag)
49
+ def mv_classifica
50
+ dml('update hernanilr.ab.mv set mv.ct=tt.nct ' \
51
+ 'from (select * from hernanilr.ab.cl) as tt ' \
52
+ "where #{ky_mv}=tt.ky")
53
+ puts 'MOVIMENTOS CLASSIFICADOS ' + bqnrs.to_s
85
54
  end
86
55
 
87
- def sql_ativos_re
88
- 'SELECT ct from hernanilr.ab.re group by 1 order by 1'
56
+ # apaga movimentos & suas rendas associadas no bigquery
57
+ #
58
+ # @return [Big] acesso a base dados abank no bigquery
59
+ def mv_delete
60
+ vars_mv_work
61
+ if mvkys.size.positive?
62
+ # obtem lista contratos arrendamento associados aos movimentos a apagar
63
+ @ctlct = sel("select ct from hernanilr.ab.mv where #{ky_mv} in(#{mvkys}) and substr(ct,1,1)='r' group by 1")
64
+
65
+ # apaga rendas associadas e depois movimentos
66
+ @opcao[:t] = true
67
+ lr_apaga.mv_delete_dml
68
+
69
+ # para obrigar re_work a trabalhar com lista contratos (ctlct)
70
+ @bqnrs = 0
71
+ end
72
+ self
89
73
  end
90
74
 
91
- def sql_sel_mv
92
- 'select * ' + sql_where_mv
75
+ # insere & classifica movimentos no bigquery
76
+ #
77
+ # @return [Big] acesso a base dados abank no bigquery
78
+ def mv_insert
79
+ if mvvls.size.positive?
80
+ dml('insert hernanilr.ab.mv VALUES' + mvvls)
81
+ puts 'MOVIMENTOS INSERIDOS ' + bqnrs.to_s
82
+ mv_classifica if bqnrs.positive?
83
+ end
84
+ self
93
85
  end
94
86
 
95
- # @return [String] sql apaga movimentos
96
- def sql_apaga_mv
97
- 'delete ' + sql_where_mv
87
+ # inicializa variaveis para delete/insert movimentos
88
+ def vars_mv_work
89
+ @bqnrs = 0
90
+ @ctlct = []
91
+ @mvkys = mvkys[1..] if mvkys[0] == ','
92
+ @mvvls = mvvls[1..] if mvvls[0] == ','
98
93
  end
99
94
 
100
- # @return [String] parte sql para processamento movimentos
101
- def sql_where_mv
102
- "from hernanilr.ab.mv where #{sql_digest_mv} in(#{opl[:k]})"
95
+ # apaga movimentos no bigquery
96
+ def mv_delete_dml
97
+ dml("delete from hernanilr.ab.mv where #{ky_mv} in(#{mvkys})")
98
+ puts 'MOVIMENTOS APAGADOS ' + bqnrs.to_s
103
99
  end
104
100
 
105
- def sql_digest_mv
106
- 'FARM_FINGERPRINT(CONCAT(CAST(nc as STRING),' \
107
- 'ds,CAST(dl as STRING),CAST(vl as STRING)))'
101
+ # @return [String] expressao sql da chave de movimentos
102
+ def ky_mv
103
+ 'FARM_FINGERPRINT(CONCAT(CAST(mv.nc as STRING),mv.ds,CAST(mv.dl as STRING),CAST(mv.vl as STRING)))'
108
104
  end
109
105
 
110
106
  # cria job bigquery & verifica execucao
111
107
  #
112
- # @param [String] sql comando sql a executar
108
+ # @param [String] sql comando a executar
113
109
  # @return [Boolean] job ok?
114
- def job_bigquery?(sql)
115
- p sql
116
- @job = api.query_job(sql)
117
- @job.wait_until_done!
118
- puts @job.error['message'] if @job.failed?
119
- @job.failed?
110
+ def job?(sql)
111
+ # p sql
112
+ @bqjob = bqapi.query_job(sql)
113
+ @bqjob.wait_until_done!
114
+ puts @bqjob.error['message'] if @bqjob.failed?
115
+ @bqjob.failed?
120
116
  end
121
117
 
122
- # executa Data Manipulation Language (DML) job no bigquery
118
+ # executa sql & devolve resultado do bigquery
123
119
  #
124
- # @param (see job_bigquery?)
125
- # @return [Integer] numero linhas afetadas
126
- def dml(sql)
127
- job_bigquery?(sql) ? 0 : job.num_dml_affected_rows
120
+ # @param (see job?)
121
+ # @param [Array] erro quando da erro no bigquery
122
+ # @return [Google::Cloud::Bigquery::Data] resultado do sql
123
+ def sel(sql, erro = [])
124
+ @bqres = job?(sql) ? erro : bqjob.data
128
125
  end
129
126
 
130
- # executa sql & devolve resultados do bigquery
127
+ # executa Data Manipulation Language (DML) no bigquery
131
128
  #
132
- # @param sql (see job_bigquery?)
133
- # @param [Array] arr resultado quando da erro no bigquery
134
- # @return [Google::Cloud::Bigquery::Data] resultado do sql num array<hash>
135
- def sel(sql, arr = [])
136
- @resultados = job_bigquery?(sql) ? arr : job.data
129
+ # @param (see job?)
130
+ # @return [Integer] numero rows afetadas pelo dml
131
+ def dml(sql)
132
+ @bqnrs = job?(sql) ? 0 : bqjob.num_dml_affected_rows
137
133
  end
138
134
  end
@@ -1,80 +1,107 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # class Big::Contrato
4
- class Abank::Big::Contrato < Abank::Big
5
- # @return [String] identificador contrato arrendamento
6
- attr_reader :rct
7
-
8
- # {:ct=>"r03000"}
9
- def initialize(con, ops = { t: false, v: '' })
10
- p ['Contrato', con, ops]
11
- @rct = con
12
- super(ops)
13
- end
3
+ # acesso a base dados abank no bigquery
4
+ class Abank::Big
5
+ # @return [String] id contrato arrendamento
6
+ attr_reader :ctide
7
+
8
+ # @return [Array<Hash>] lista ids contratos arrendamento
9
+ # @example
10
+ # [{ ct: 'r03000' }, ...]
11
+ attr_reader :ctlct
12
+
13
+ # @return [Array<Hash>] lista dados contrato arrendamento (inclui lista movimentos novos)
14
+ # @example
15
+ # [{ct: 'r03000', dc: '2020-03-01', ano: 2020, cnt: 0, dl: '2020-03-01', mv: [{dl: '2020-03-02', vl: 30}, ...] }]
16
+ attr_reader :ctlcm
14
17
 
15
- # (see CLI#cria)
16
- def re_cria
18
+ # (see CLI#criact)
19
+ def ct_cria
17
20
  if existe_contrato?
18
- i = 0
19
- puts 'JA EXISTE CONTRATO'
21
+ @bqnrs = 1
22
+ puts 'CONTRATO JA EXISTE'
20
23
  else
21
- i = dml('insert into hernanilr.ab.re ' + sql_contrato_mv)
22
- puts i.zero? ? 'NAO EXISTE CONTRATO' : "CONTRATO #{rct} INSERIDO"
24
+ dml('insert into hernanilr.ab.re ' + sql_contrato_mv)
25
+ puts "CONTRATO #{ctide} " + (bqnrs.zero? ? 'NAO EXISTE' : 'INSERIDO')
23
26
  end
24
- return unless i.positive? && opl[:t]
27
+ return unless existem_rendas?
25
28
 
26
- re_atualiza
29
+ # processa rendas associadas ao contrato arrendamento
30
+ cm_cria.vr_cria.re_insert
27
31
  end
28
32
 
29
- # (see CLI#apagare)
30
- def re_apaga
31
- puts "RENDAS #{rct} APAGADAS " + dml(sql_apaga_re).to_s
33
+ # (see CLI#apagact)
34
+ def ct_apaga
35
+ @ctlct = [{ ct: ctide }]
36
+ lc_apaga
32
37
  end
33
38
 
34
- # @return [Hash] dados contrato & movimentos novos
35
- def dados_contrato
36
- c = sel(sql_last_re).first
37
- sel(sql_novo_mv(c[:dl]))
38
- return unless resultados.count.positive?
39
+ # apaga rendas da lista de contrato arrendamento
40
+ #
41
+ # @return [Big] acesso a base dados abank no bigquery
42
+ def lr_apaga
43
+ return self unless opcao[:t] && ctlct.count.positive?
39
44
 
40
- { mv: resultados }.merge(c)
45
+ # para nao apagar contrato arrendamento - somente as rendas
46
+ @opcao[:t] = false
47
+
48
+ lc_apaga
49
+ self
41
50
  end
42
51
 
43
- def sql_last_re
44
- 'select ct,DATE_SUB(DATE_SUB(dl,INTERVAL dias DAY)' \
45
- ',INTERVAL IF(cnt=0,0,cnt-1) MONTH) as dc,ano,cnt,dl ' \
46
- "from hernanilr.ab.re where ct='#{rct}' " \
47
- 'order by ano desc,cnt desc limit 1'
52
+ # apaga rendas da lista de contratos arrendamento
53
+ def lc_apaga
54
+ dml("delete from hernanilr.ab.re where ct in(#{str_lc})#{opcao[:t] ? '' : ' and cnt>0'}")
55
+ puts "RENDAS #{str_lc('')} APAGADAS " + bqnrs.to_s
48
56
  end
49
57
 
50
- def sql_novo_mv(mdl)
51
- "select dl,vl from hernanilr.ab.mv where ct='#{rct}' " \
52
- "and dl>='#{(mdl + 1).strftime(DF)}' order by dl,dv"
58
+ # @return [String] texto formatado que representa lista de contratos arrendamento
59
+ def str_lc(sep = "'")
60
+ ctlct.map { |c| sep + c[:ct] + sep }.join(',')
61
+ end
62
+
63
+ # optem lista dados contrato arrendamento (inclui lista movimentos novos)
64
+ #
65
+ # @return [Big] acesso a base dados abank no bigquery
66
+ def cm_cria
67
+ @ctlcm = []
68
+ ctlct.each do |c|
69
+ @ctide = c[:ct]
70
+ sel(sql_last_re)
71
+ @ctlcm << bqres[0].merge({ mv: sel(sql_novo_mv(bqres[0][:dl])) })
72
+ end
73
+ self
53
74
  end
54
75
 
76
+ # @return [Boolean] existem rendas para processar sim/nao?
77
+ def existem_rendas?
78
+ @ctlct = [{ ct: ctide }]
79
+ bqnrs.positive? && opcao[:t]
80
+ end
81
+
82
+ # @return [Boolean] contrato arrendamento ja existe sim/nao?
55
83
  def existe_contrato?
56
- sel(sql_contrato_re).count.positive?
84
+ sel("select ct from hernanilr.ab.re where ct='#{ctide}' and cnt=0").count.positive?
85
+ end
86
+
87
+ # @return [String] sql para obter ultima renda do contrato arrendamento
88
+ def sql_last_re
89
+ 'select ct,DATE_SUB(DATE_SUB(dl,INTERVAL dias DAY),INTERVAL IF(cnt=0,0,cnt-1) MONTH) as dc,ano,cnt,dl ' \
90
+ "from hernanilr.ab.re where ct='#{ctide}' order by ano desc,cnt desc limit 1"
57
91
  end
58
92
 
59
- def sql_contrato_re
60
- "select * from hernanilr.ab.re where ct='#{rct}' and cnt=0"
93
+ # @return [String] sql para obter movimentos novos (depois da ultima renda do contrato arrendamento)
94
+ def sql_novo_mv(mdl)
95
+ "select dl,vl from hernanilr.ab.mv where ct='#{ctide}' and dl>='#{(mdl + 1).strftime(DF)}' order by dl,dv"
61
96
  end
62
97
 
63
- # @return [String] sql obtem dados inicio contrato arrendamento
98
+ # @return [String] sql para obter dados do inicio contrato arrendamento
64
99
  def sql_contrato_mv
65
- if opl[:v].size.zero?
66
- 'select ct,EXTRACT(YEAR FROM DATE_TRUNC(dl,MONTH)) as ano,0 as cnt' \
67
- ',DATE_TRUNC(dl,MONTH) as dl,0 dias ' \
68
- "from hernanilr.ab.mv where ct='#{rct}' order by dl limit 1"
100
+ if opcao[:d].size.zero?
101
+ 'select ct,EXTRACT(YEAR FROM DATE_TRUNC(dl,MONTH)) as ano,0 as cnt,DATE_TRUNC(dl,MONTH) as dl,0 dias ' \
102
+ "from hernanilr.ab.mv where ct='#{ctide}' order by dl limit 1"
69
103
  else
70
- "select '#{rct}' as ct" \
71
- ",EXTRACT(YEAR FROM DATE '#{opl[:v]}') as ano,0 as cnt" \
72
- ",DATE '#{opl[:v]}' as dl,0 dias "
104
+ "select '#{ctide}' as ct,EXTRACT(YEAR FROM DATE '#{opcao[:d]}') as ano,0 as cnt,DATE '#{opcao[:d]}' as dl,0 dias"
73
105
  end
74
106
  end
75
-
76
- def sql_apaga_re
77
- "delete from hernanilr.ab.re where ct='#{rct}'" +
78
- (opl[:t] ? '' : ' and cnt>0')
79
- end
80
107
  end
@@ -2,158 +2,153 @@
2
2
 
3
3
  require 'roo'
4
4
 
5
- # class Big::Contrato
5
+ # acesso a folha calculo & base dados abank no bigquery
6
6
  class Abank::Big::Folha < Abank::Big
7
- HT = ['Data Lanc.', 'Data Valor', 'Descrição', 'Valor'].freeze
8
- RF = '%<v3>-34.34s %<v4>8.2f'
9
- EN = ' %<v1>20d'
10
- ES = ' %<v1>-20.20s'
11
-
12
7
  # @return [Roo::Excelx] folha calculo a processar
13
8
  attr_reader :folha
14
- # @return [Integer] numero conta
9
+
10
+ # @return [Integer] numero conta associado a folha calculo
11
+ # @example
12
+ # mov*.xlsx --> 1 --> conta-corrente
13
+ # movCard*.xlsx --> 2 --> conta-cartao
15
14
  attr_reader :conta
16
15
 
17
16
  # @return [Array] row folha calculo em processamento
18
- attr_reader :row
19
-
20
- def initialize(xls, ops = { s: false, e: false, m: false, i: false,
21
- v: '', g: '' })
22
- p ['Folha', xls, ops]
23
- @folha = Roo::Spreadsheet.open(xls) if xls.size.positive?
24
- @conta = xls.match?(/card/i) ? 2 : 1
25
- super(ops)
26
- end
17
+ attr_reader :rowfc
27
18
 
28
- # processa linhas folha calculo & classifica bigquery
29
- def processa_folha
19
+ # acesso a folha calculo & base dados abank no bigquery
20
+ #
21
+ # @param [Hash] opc opcoes trabalho
22
+ # @option opc [String] :f ('') folha calculo a processar
23
+ # @option opc [Boolean] :s (false) apaga movimento similar? (mv)
24
+ # @option opc [Boolean] :e (false) apaga movimento igual? (mv)
25
+ # @option opc [Boolean] :i (false) insere movimento novo? (mv)
26
+ # @option opc [String] :v ('') data valor movimentos (mv)
27
+ # @option opc [String] :g ('') classificacao movimentos (mv)
28
+ def initialize(opc = {})
29
+ @opcao = super
30
+ @folha = Roo::Spreadsheet.open(opc.fetch(:f))
31
+ @conta = opc.fetch(:f).match?(/card/i) ? 2 : 1
32
+ @opcao[:s] = opc.fetch(:s, false)
33
+ @opcao[:e] = opc.fetch(:e, false)
34
+ @opcao[:i] = opc.fetch(:i, false)
35
+ @opcao[:v] = opc.fetch(:v, '')
36
+ @opcao[:g] = opc.fetch(:g, '')
37
+ end
38
+
39
+ # carrega/mostra folha calculo
40
+ def processa_xls
30
41
  n = 0
31
- folha.sheet(0).parse(header_search: HT) do |r|
42
+ folha.sheet(0).parse(header_search: ['Data Lanc.', 'Data Valor', 'Descrição', 'Valor']) do |r|
32
43
  n += 1
33
- puts n == 1 ? "\n" + folha.info : processa_row(r)
44
+ puts n == 1 ? "\n" + folha.info : processa_linha(r)
34
45
  end
35
- mv_classifica
46
+ return unless opcao[:i]
47
+
48
+ # processa movimentos & atualiza rendas
49
+ mv_delete.mv_insert.re_work
36
50
  end
37
51
 
38
- # processa linha folha calculo para arquivo
52
+ # processa linha folha calculo
39
53
  #
40
- # @param (see corrige_hash)
41
- # @return [String] texto informativo do processamento
42
- def processa_row(has)
43
- corrige_hash(has)
54
+ # @param [Hash] linha da folha calculo em processamento
55
+ # @return [String] texto informativo formatado da linha em processamento
56
+ def processa_linha(linha)
57
+ vars_xls(linha)
44
58
  # pesquisa existencia linha folha calculo no bigquery
45
59
  # array.count = 0 ==> pode carregar esta linha
46
60
  # array.count = 1 ==> mais testes necessarios
47
61
  # array.count > 1 ==> nao pode carregar esta linha
48
- sel(sql_sel_mv, [{}, {}])
49
- if row_naoexiste? then row_str + (insert_mv == 1 ? ' NOVA' : ' ERRO')
50
- elsif row_simila? then row_similar
51
- elsif row_existe? then row_existente
52
- else row_multiplas
62
+ sel(sql_existe_mv, [{}, {}])
63
+ if linha_naoexiste? then linha_base + values_mv
64
+ elsif linha_existe? then linha_existe
65
+ elsif linha_simila? then linha_similar
66
+ else linha_multiplas
53
67
  end
54
68
  end
55
69
 
56
- # corrige linha folha calculo para processamento
70
+ # inicializa variavel para processar linha folha calculo
57
71
  #
58
- # @param [Hash] has da linha em processamento
59
- def corrige_hash(has)
60
- @row = has.values
61
- @row[2] = row[2].strip
62
- @row[3] = -1 * row[3] if conta > 1
63
- end
64
-
65
- # @return [String] linha folha calculo formatada
66
- def row_str
67
- "#{row[0].strftime(DF)} #{format(RF, v3: row[2], v4: row[3])}"
72
+ # @param (see processa_linha)
73
+ def vars_xls(linha)
74
+ @rowfc = linha.values
75
+ @rowfc[2] = rowfc[2].strip
76
+ @rowfc[3] = -1 * rowfc[3] if conta > 1
68
77
  end
69
78
 
70
- # @return [String] linha folha calculo similar
71
- def row_similar
72
- d = opl[:s] ? delete_mv : 0
73
- row_str + ' SIMI' + str_apagadas(d) + str_extra_s(resultados.first[:ds])
79
+ # @return [String] texto base formatado para display
80
+ def linha_base
81
+ "#{rowfc[0].strftime(DF)} #{format('%<v3>-34.34s %<v4>8.2f', v3: rowfc[2], v4: rowfc[3])}"
74
82
  end
75
83
 
76
- # @return [String] linha folha calculo existente
77
- def row_existente
78
- d = opl[:e] ? delete_mv : 0
79
- row_str + ' EXIS' + str_apagadas(d) + str_extra_n(resultados.first[:ky])
84
+ # @return [String] texto linha existente formatada para display
85
+ def linha_existe
86
+ add_kys if opcao[:e]
87
+ linha_base + ' EXIS ' + format('%<v1>20d', v1: bqres.first[:ky])
80
88
  end
81
89
 
82
- def str_extra_s(ext)
83
- format(ES, v1: ext.strip)
90
+ # @return [String] texto linha similar formatada para display
91
+ def linha_similar
92
+ add_kys if opcao[:s]
93
+ linha_base + ' SIMI ' + format('%<v1>-20.20s', v1: bqres.first[:ds].strip)
84
94
  end
85
95
 
86
- def str_extra_n(ext)
87
- format(EN, v1: ext)
96
+ # @return [String] texto linha existencia multipla formatada para display
97
+ def linha_multiplas
98
+ linha_base + ' MULT(' + bqres.count.to_s + ')'
88
99
  end
89
100
 
90
- # @return [String] linha folha calculo existencia multipla
91
- def row_multiplas
92
- d = opl[:m] ? delete_mv : 0
93
- row_str + ' M(' + resultados.count.to_s + ')' + str_apagadas(d)
94
- end
95
-
96
- # @param [Integer] numero linhas apagadas
97
- # @return [String] texto formatado linhas apagadas
98
- def str_apagadas(num)
99
- num.positive? ? ' A(' + num.to_s + ')' : ''
101
+ # obtem chaves movimento (keysin.mv) para apagar
102
+ def add_kys
103
+ bqres.each { |r| @mvkys += ",#{r[:ky]}" }
100
104
  end
101
105
 
102
106
  # @return [Boolean] linha folha calculo nao existe no bigquery?
103
- def row_naoexiste?
104
- resultados.count.zero?
107
+ def linha_naoexiste?
108
+ bqres.count.zero?
105
109
  end
106
110
 
107
111
  # @return [Boolean] linha folha calculo existe no bigquery?
108
- def row_existe?
109
- resultados.count == 1 && resultados.first[:ds].strip == row[2]
112
+ def linha_existe?
113
+ bqres.count == 1 && bqres.first[:ds].strip == rowfc[2]
110
114
  end
111
115
 
112
116
  # @return [Boolean] linha folha calculo existe parecida no bigquery?
113
- def row_simila?
114
- resultados.count == 1 && resultados.first[:ds].strip != row[2]
117
+ def linha_simila?
118
+ bqres.count == 1 && bqres.first[:ds].strip != rowfc[2]
115
119
  end
116
120
 
117
- def sql_sel_mv
118
- "select *,#{sql_digest_mv} as ky " + sql_where_mv
121
+ # @return [String] sql para movimentos no bigquery
122
+ def sql_existe_mv
123
+ "select *,#{ky_mv} as ky from hernanilr.ab.mv " \
124
+ "where nc=#{conta} and dl='#{rowfc[0].strftime(DF)}' and vl=#{rowfc[3]}"
119
125
  end
120
126
 
121
- # @return [String] parte sql para processamento movimentos
122
- def sql_where_mv
123
- "from hernanilr.ab.mv where nc=#{conta} " \
124
- "and dl='#{row[0].strftime(DF)}' " \
125
- "and vl=#{row[3]}"
127
+ # obtem movimento (values.mv) para inserir
128
+ #
129
+ # @return [String] ' NOVO'
130
+ def values_mv
131
+ @mvvls += ",('#{rowfc[0].strftime(DF)}','#{dvc.strftime(DF)}','#{rowfc[2]}',#{rowfc[3]}" + values_mv_extra
132
+ ' NOVO'
126
133
  end
127
134
 
128
- # @return [Integer] numero linhas inseridas
129
- def insert_mv
130
- return 1 unless opl[:i]
131
-
132
- dml('insert hernanilr.ab.mv(dl,dv,ds,vl,nc,ano,mes,ct,tp) VALUES(' \
133
- "'#{row[0].strftime(DF)}','#{dvc.strftime(DF)}','#{row[2]}',#{row[3]}" +
134
- str_ins_pc)
135
+ # @return [String] campos extra do movimento (values.mv) para inserir
136
+ def values_mv_extra
137
+ ",#{conta},#{dvc.year},#{dvc.month},'#{tpc}',#{ctc})"
135
138
  end
136
139
 
137
140
  # @return [Date] data valor corrigida
138
141
  def dvc
139
- opl[:v].size.zero? ? row[1] : Date.parse(opl[:v])
140
- end
141
-
142
- # @return [String] campos extra da linha bigquery
143
- def str_ins_pc
144
- ",#{conta},#{dvc.year},#{dvc.month},#{ctc},'#{tpc}')"
142
+ opcao[:v].size.zero? ? rowfc[1] : Date.parse(opcao[:v])
145
143
  end
146
144
 
145
+ # @return [String] classificacao do movimento (null --> classificacao automatica)
147
146
  def ctc
148
- opl[:g].size.zero? ? 'null' : ("'" + opl[:g] + "'")
147
+ opcao[:g].size.zero? ? 'null' : ("'" + opcao[:g] + "'")
149
148
  end
150
149
 
150
+ # @return [String] tipo movimento c[redito] ou d[ebito]
151
151
  def tpc
152
- row[3].positive? ? 'c' : 'd'
153
- end
154
-
155
- # @return [Integer] numero linhas apagadas
156
- def delete_mv
157
- dml('delete ' + sql_where_mv + " and ds='#{resultados.first[:ds]}'")
152
+ rowfc[3].positive? ? 'c' : 'd'
158
153
  end
159
154
  end
@@ -1,95 +1,121 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # class Big::Renda
4
- class Abank::Big::Contrato::Rendas < Abank::Big::Contrato
5
- attr_reader :ren
6
-
7
- # @return [Integer] ano renda
8
- attr_reader :ran
9
- # @return [Integer] mes renda
10
- attr_reader :rcn
3
+ # @see Abank::Big
4
+ class Abank::Big
5
+ # @return [Integer] ano renda em tratamento
6
+ attr_reader :reano
7
+
8
+ # @return [Integer] mes renda em tratamento
9
+ attr_reader :repos
10
+
11
11
  # @return [Float] valor renda mensal
12
- attr_reader :rru
12
+ attr_reader :revre
13
+
14
+ # @return [String] rendas a inserir (values.re)
15
+ attr_reader :revls
13
16
 
14
- # @return [Integer] total de movimentos novos
15
- attr_reader :mcn
16
17
  # @return [Integer] movimento em tratamento
17
- attr_reader :mpn
18
+ attr_reader :mvpos
19
+
18
20
  # @return [Date] data lancamento movimento em tratamento
19
- attr_reader :mdl
21
+ attr_reader :mvdlm
22
+
20
23
  # @return [Float] valor movimento em tratamento
21
- attr_reader :mvl
22
-
23
- # @return [Date] data contrato arrendamento
24
- # {:mv=>[{:dl=>#<Date: 2020-03-02>, :vl=>0.3e2},...]
25
- # ,:ct=>"r03000"
26
- # ,:dc=>#<Date: 2020-03-01>
27
- # ,:ano=>2020
28
- # ,:cnt=>0
29
- # ,:dl=>#<Date: 2020-03-01>}
30
- def initialize(ren)
31
- p ['Rendas', ren]
32
- @ren = ren
33
- super(ren[:ct])
24
+ attr_reader :mvvlm
25
+
26
+ # (see CLI#recriare)
27
+ def re_atualiza
28
+ # obtem contratos ativos
29
+ @ctlct = sel('SELECT ct from hernanilr.ab.re group by 1')
30
+
31
+ # [re]cria rendas [novas|todas]
32
+ lr_apaga.cm_cria.vr_cria.re_insert
34
33
  end
35
34
 
36
- # @param [Hash] con dados contrato & lista movimentos novos
37
- # @return [Array<Hash>] lista rendas novas para criar
38
- def rendas
39
- vars_re
40
- vars_mv
41
- r = []
42
- while mvl >= rru && mpn < mcn
43
- r << nova_re
44
- proximo_mv
35
+ # cria rendas associadas a lista ids contratos arrendamento
36
+ def re_work
37
+ bqnrs.zero? || ctlct.count.positive? ? cm_cria.vr_cria.re_insert : re_atualiza
38
+ end
39
+
40
+ # obtem rendas a inserir (values.re)
41
+ #
42
+ # @return [Big] acesso a base dados abank no bigquery
43
+ def vr_cria
44
+ @revls = ctlcm.map { |c| rendas_novas(c) }.flatten(1).join(',')
45
+ self
46
+ end
47
+
48
+ # insere rendas no bigquery
49
+ def re_insert
50
+ if revls.size.zero?
51
+ puts 'NAO EXISTEM RENDAS NOVAS'
52
+ else
53
+ dml('insert hernanilr.ab.re VALUES' + revls)
54
+ puts "RENDAS #{str_lc('')} CRIADAS " + bqnrs.to_s
45
55
  end
46
- r
47
56
  end
48
57
 
49
- # @param (see rendas)
50
- def vars_mv
51
- @mpn = 0
52
- @mcn = ren[:mv].count
53
- vars_dados_mv
58
+ # @param [Hash] cmv dados contrato arrendamento (inclui lista movimentos novos)
59
+ # @return [Array<String>] lista rendas novas dum contrato arrendamento (values.re)
60
+ def rendas_novas(cmv)
61
+ return [] unless cmv[:mv].count.positive?
62
+
63
+ vars_re(cmv)
64
+ r = []
65
+ while mvvlm >= revre && mvpos < cmv[:mv].count
66
+ r << nova_re(cmv)
67
+ proximo_mv(cmv)
68
+ end
69
+ r
54
70
  end
55
71
 
56
- # @param (see rendas)
57
- def vars_dados_mv
58
- @mdl = ren[:mv][mpn][:dl]
59
- @mvl = ren[:mv][mpn][:vl]
72
+ # inicializa variaveis para processar rendas do contrato arrendamento
73
+ # @param (see rendas_novas)
74
+ def vars_re(cmv)
75
+ @reano = cmv[:ano]
76
+ @repos = cmv[:cnt]
77
+ @revre = Float(cmv[:ct][/\d+/]) / 100
78
+ @mvpos = 0
79
+ vars_re_mv(cmv)
60
80
  end
61
81
 
62
- # @param (see rendas)
63
- def vars_re
64
- @ran = ren[:ano]
65
- @rcn = ren[:cnt]
66
- @rru = Float(ren[:ct][/\d+/]) / 100
82
+ # inicializa variaveis para processar movimentos associados ao contrato arrendamento
83
+ # @param (see rendas_novas)
84
+ def vars_re_mv(cmv)
85
+ @mvdlm = cmv[:mv][mvpos][:dl]
86
+ @mvvlm = cmv[:mv][mvpos][:vl]
67
87
  end
68
88
 
69
- # @return [Array] lista dados da renda nova
70
- def nova_re
71
- if rcn == 12
72
- @rcn = 1
73
- @ran += 1
89
+ # @param (see rendas_novas)
90
+ # @return [String] renda formatada (values.re)
91
+ def nova_re(cmv)
92
+ # inicializa proxima renda
93
+ if repos == 12
94
+ @repos = 1
95
+ @reano += 1
74
96
  else
75
- @rcn += 1
97
+ @repos += 1
76
98
  end
77
- # [rct, ran, rcn, mdl, mdl.mjd - vencimento.mjd]
78
- "('#{rct}',#{ran},#{rcn},'#{mdl.strftime(DF)}',#{dias})"
99
+ "('#{cmv[:ct]}',#{reano},#{repos},'#{mvdlm.strftime(DF)}',#{dias(cmv)})"
79
100
  end
80
101
 
81
- def dias
82
- mdl.mjd - (Date.new(ran, rcn, 1) >> (ren[:dc].month - 1)).mjd
102
+ # @param (see rendas_novas)
103
+ # @return [Integer] dias atraso no pagamento da renda
104
+ def dias(cmv)
105
+ mvdlm.mjd - (Date.new(reano, repos, 1) >> (cmv[:dc].month - 1)).mjd
83
106
  end
84
107
 
85
- # @param (see rendas)
86
- def proximo_mv
87
- @mvl -= rru
88
- return unless mvl < rru
108
+ # inicializa variaveis para processar proximo movimento
109
+ # @param (see rendas_novas)
110
+ def proximo_mv(cmv)
111
+ # valor renda paga retirado do valor do movimento
112
+ @mvvlm -= revre
113
+ return unless mvvlm < revre
89
114
 
90
- @mpn += 1
91
- return unless mpn < mcn
115
+ # avanca na lista de movimentos
116
+ @mvpos += 1
117
+ return unless mvpos < cmv[:mv].count
92
118
 
93
- vars_dados_mv
119
+ vars_re_mv(cmv)
94
120
  end
95
121
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Abank
4
- VERSION = '0.2.4'
4
+ VERSION = '0.2.5'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: abank
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hernâni Rodrigues Vaz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-05-26 00:00:00.000000000 Z
11
+ date: 2020-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,9 +94,9 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
- description: Arquiva <conta-corrente>.xlsx, <conta-cartao>.xlsx no bigquery. Pode
98
- apagar movimentos similares/existentes ja no bigquery. Pode ainda classificar movimentos
99
- ja no bigquery.
97
+ description: Arquiva movimentos conta-corrente, conta-cartao do activobank no bigquery.
98
+ Permite apagar/recriar movimentos/rendas ja no bigquery. Permite ainda classificar
99
+ movimentos no bigquery.
100
100
  email:
101
101
  - hernanirvaz@gmail.com
102
102
  executables:
@@ -143,8 +143,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
143
  - !ruby/object:Gem::Version
144
144
  version: '0'
145
145
  requirements: []
146
- rubygems_version: 3.0.3
146
+ rubygems_version: 3.0.8
147
147
  signing_key:
148
148
  specification_version: 4
149
- summary: Arquiva <conta-corrente>.xlsx, <conta-cartao>.xlsx no bigquery.
149
+ summary: Arquiva movimentos conta-corrente, conta-cartao do activobank no bigquery.
150
150
  test_files: []