abank 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 357eabd6927351fcd006bf58dda24c7b323395749b4ab6ef4ecf6f596a126241
4
- data.tar.gz: 0c3fc3b9e2ebda8bfe13b0ba072f50b0ff303668dfde4ffa7378e04f1470b81e
3
+ metadata.gz: 4a559c3029471b2ad081cc7ec13b0b61387e2109c780fe98ae1a11ba22d77538
4
+ data.tar.gz: d809890286188b61e47ec9b4eb0cbce1dac6478c85206b336e8820f2060e1321
5
5
  SHA512:
6
- metadata.gz: b11a19c0f7039ff253776441e4f7b7de443808c4bf90dfec078a65dab3342b40197017d36e69d30d09d35500afd6e187ebe7113580e36ef921737085599c26af
7
- data.tar.gz: 98daf017bef2b5860fe498c9f11f6881374ebaa87451a10bf24fe1fa95006230c734a6dc1176e036983ec0e991777e59cfebaa34f2c2bc67828d3b95563163ae
6
+ metadata.gz: 000d52a70da099de5f5eff73cca078787b1c537f48da18ef6dfb140f3935d023445d48516648e461a12a02ffdaf684160da3bc0bcb0ecb11a12926230ad5f37b
7
+ data.tar.gz: 8c73fa2f6c9ec5353dc0c8e8d76648ad11ec3169de63bc796378b425dd509f2d6d7b916d61e05eb1feb4571a8779c9249b7b1bfbba4132c48afbc3465c4c9b22
@@ -2,9 +2,6 @@ AllCops:
2
2
  TargetRubyVersion: 2.6
3
3
  EnabledByDefault: true
4
4
 
5
- Style/ClassAndModuleChildren:
6
- EnforcedStyle: compact
7
-
8
5
  Style/Copyright:
9
6
  Enabled: false
10
7
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- abank (0.2.5)
4
+ abank (0.2.6)
5
5
  google-cloud-bigquery
6
6
  roo
7
7
  thor
@@ -3,8 +3,8 @@
3
3
  require 'thor'
4
4
  require 'abank/big'
5
5
  require 'abank/contrato'
6
- require 'abank/folha'
7
6
  require 'abank/rendas'
7
+ require 'abank/folha'
8
8
  require 'abank/version'
9
9
 
10
10
  # @author Hernani Rodrigues Vaz
@@ -70,7 +70,7 @@ module Abank
70
70
  # carrega folha calculo
71
71
  def load
72
72
  Dir.glob("#{DR}/*.xlsx").sort.each do |f|
73
- Big::Folha.new(load_opc.merge(f: f)).processa_xls
73
+ Folha.new(load_opc.merge(f: f)).processa_xls
74
74
  end
75
75
  end
76
76
 
@@ -78,7 +78,7 @@ module Abank
78
78
  # mostra folha calculo
79
79
  def show
80
80
  Dir.glob("#{DR}/*.xlsx").sort.each do |f|
81
- Big::Folha.new(f: f).processa_xls
81
+ Folha.new(f: f).processa_xls
82
82
  end
83
83
  end
84
84
 
@@ -2,133 +2,135 @@
2
2
 
3
3
  require 'google/cloud/bigquery'
4
4
 
5
- # @see Abank::Big
6
- class Abank::Big
7
- DF = '%Y-%m-%d'
8
-
9
- # @return [Hash] opcoes trabalho
10
- attr_reader :opcao
11
-
12
- # @return [Google::Cloud::Bigquery] API bigquery
13
- attr_reader :bqapi
14
-
15
- # @return [Google::Cloud::Bigquery::QueryJob] job bigquery
16
- attr_reader :bqjob
17
-
18
- # @return [Google::Cloud::Bigquery::Data] resultado do select
19
- attr_reader :bqres
20
-
21
- # @return [Integer] numero linhas afetadas pela Data Manipulation Language (DML)
22
- attr_reader :bqnrs
23
-
24
- # @return [String] movimentos a inserir (values.mv)
25
- attr_reader :mvvls
26
-
27
- # @return [String] movimentos a apagar (keysin.mv)
28
- attr_reader :mvkys
29
-
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
46
- end
5
+ module Abank
6
+ # @see Big
7
+ class Big
8
+ DF = '%Y-%m-%d'
9
+
10
+ # @return [Hash] opcoes trabalho
11
+ attr_reader :opcao
12
+
13
+ # @return [Google::Cloud::Bigquery] API bigquery
14
+ attr_reader :bqapi
15
+
16
+ # @return [Google::Cloud::Bigquery::QueryJob] job bigquery
17
+ attr_reader :bqjob
18
+
19
+ # @return [Google::Cloud::Bigquery::Data] resultado do select
20
+ attr_reader :bqres
21
+
22
+ # @return [Integer] numero linhas afetadas pela Data Manipulation Language (DML)
23
+ attr_reader :bqnrs
24
+
25
+ # @return [String] movimentos a inserir (values.mv)
26
+ attr_reader :mvvls
27
+
28
+ # @return [String] movimentos a apagar (keysin.mv)
29
+ attr_reader :mvkys
30
+
31
+ # acesso a base dados abank no bigquery
32
+ #
33
+ # @param [Hash] opc opcoes trabalho
34
+ # @option opc [String] :k ('') movimentos a apagar (keysin.mv)
35
+ # @option opc [String] :c ('') id contrato arrendamento (re)
36
+ # @option opc [String] :d ('') data inicio contrato arrendamento (re)
37
+ # @option opc [Boolean] :t (false) trabalha todas as rendas? (re)
38
+ # @return [Hash] opcoes trabalho
39
+ def initialize(opc = {})
40
+ @opcao = opc
41
+ @bqapi = Google::Cloud::Bigquery.new
42
+ @mvvls = ''
43
+ @mvkys = opc.fetch(:k, '')
44
+ @ctide = opc.fetch(:c, '')
45
+ # p ['B', opcao]
46
+ opcao
47
+ end
47
48
 
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
54
- end
49
+ # (see CLI#tag)
50
+ def mv_classifica
51
+ dml('update hernanilr.ab.mv set mv.ct=tt.nct ' \
52
+ 'from (select * from hernanilr.ab.cl) as tt ' \
53
+ "where #{ky_mv}=tt.ky")
54
+ puts 'MOVIMENTOS CLASSIFICADOS ' + bqnrs.to_s
55
+ end
55
56
 
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")
57
+ # apaga movimentos & suas rendas associadas no bigquery
58
+ #
59
+ # @return [Big] acesso a base dados abank no bigquery
60
+ def mv_delete
61
+ vars_mv_work
62
+ if mvkys.size.positive?
63
+ # obtem lista contratos arrendamento associados aos movimentos a apagar
64
+ @ctlct = sel("select ct from hernanilr.ab.mv where #{ky_mv} in(#{mvkys}) and substr(ct,1,1)='r' group by 1")
65
+
66
+ # apaga rendas associadas e depois movimentos
67
+ @opcao[:t] = true
68
+ lr_apaga.mv_delete_dml
69
+
70
+ # para obrigar re_work a trabalhar com lista contratos (ctlct)
71
+ @bqnrs = 0
72
+ end
73
+ self
74
+ end
64
75
 
65
- # apaga rendas associadas e depois movimentos
66
- @opcao[:t] = true
67
- lr_apaga.mv_delete_dml
76
+ # insere & classifica movimentos no bigquery
77
+ #
78
+ # @return [Big] acesso a base dados abank no bigquery
79
+ def mv_insert
80
+ if mvvls.size.positive?
81
+ dml('insert hernanilr.ab.mv VALUES' + mvvls)
82
+ puts 'MOVIMENTOS INSERIDOS ' + bqnrs.to_s
83
+ mv_classifica if bqnrs.positive?
84
+ end
85
+ self
86
+ end
68
87
 
69
- # para obrigar re_work a trabalhar com lista contratos (ctlct)
88
+ # inicializa variaveis para delete/insert movimentos
89
+ def vars_mv_work
70
90
  @bqnrs = 0
91
+ @ctlct = []
92
+ @mvkys = mvkys[1..] if mvkys[0] == ','
93
+ @mvvls = mvvls[1..] if mvvls[0] == ','
71
94
  end
72
- self
73
- end
74
95
 
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?
96
+ # apaga movimentos no bigquery
97
+ def mv_delete_dml
98
+ dml("delete from hernanilr.ab.mv where #{ky_mv} in(#{mvkys})")
99
+ puts 'MOVIMENTOS APAGADOS ' + bqnrs.to_s
83
100
  end
84
- self
85
- end
86
-
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] == ','
93
- end
94
101
 
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
99
- end
100
-
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)))'
104
- end
102
+ # @return [String] expressao sql da chave de movimentos
103
+ def ky_mv
104
+ 'FARM_FINGERPRINT(CONCAT(CAST(mv.nc as STRING),mv.ds,CAST(mv.dl as STRING),CAST(mv.vl as STRING)))'
105
+ end
105
106
 
106
- # cria job bigquery & verifica execucao
107
- #
108
- # @param [String] sql comando a executar
109
- # @return [Boolean] job ok?
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?
116
- end
107
+ # cria job bigquery & verifica execucao
108
+ #
109
+ # @param [String] sql comando a executar
110
+ # @return [Boolean] job ok?
111
+ def job?(sql)
112
+ # p sql
113
+ @bqjob = bqapi.query_job(sql)
114
+ @bqjob.wait_until_done!
115
+ puts @bqjob.error['message'] if @bqjob.failed?
116
+ @bqjob.failed?
117
+ end
117
118
 
118
- # executa sql & devolve resultado do bigquery
119
- #
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
125
- end
119
+ # executa sql & devolve resultado do bigquery
120
+ #
121
+ # @param (see job?)
122
+ # @param [Array] erro quando da erro no bigquery
123
+ # @return [Google::Cloud::Bigquery::Data] resultado do sql
124
+ def sel(sql, erro = [])
125
+ @bqres = job?(sql) ? erro : bqjob.data
126
+ end
126
127
 
127
- # executa Data Manipulation Language (DML) no bigquery
128
- #
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
128
+ # executa Data Manipulation Language (DML) no bigquery
129
+ #
130
+ # @param (see job?)
131
+ # @return [Integer] numero rows afetadas pelo dml
132
+ def dml(sql)
133
+ @bqnrs = job?(sql) ? 0 : bqjob.num_dml_affected_rows
134
+ end
133
135
  end
134
136
  end
@@ -1,107 +1,110 @@
1
1
  # frozen_string_literal: true
2
2
 
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
17
-
18
- # (see CLI#criact)
19
- def ct_cria
20
- if existe_contrato?
21
- @bqnrs = 1
22
- puts 'CONTRATO JA EXISTE'
23
- else
24
- dml('insert into hernanilr.ab.re ' + sql_contrato_mv)
25
- puts "CONTRATO #{ctide} " + (bqnrs.zero? ? 'NAO EXISTE' : 'INSERIDO')
3
+ module Abank
4
+ # acesso a base dados abank no bigquery
5
+ class Big
6
+ # @return [String] id contrato arrendamento
7
+ attr_reader :ctide
8
+
9
+ # @return [Array<Hash>] lista ids contratos arrendamento
10
+ # @example
11
+ # [{ ct: 'r03000' }, ...]
12
+ attr_reader :ctlct
13
+
14
+ # @return [Array<Hash>] lista dados contrato arrendamento (inclui lista movimentos novos)
15
+ # @example
16
+ # [{ct: 'r03000', dc: '2020-03-01', ano: 2020, cnt: 0, dl: '2020-03-01', mv: [{dl: '2020-03-02', vl: 30}, ...] }]
17
+ attr_reader :ctlcm
18
+
19
+ # (see CLI#criact)
20
+ def ct_cria
21
+ if existe_contrato?
22
+ @bqnrs = 1
23
+ puts 'CONTRATO JA EXISTE'
24
+ else
25
+ dml('insert into hernanilr.ab.re ' + sql_contrato_mv)
26
+ puts "CONTRATO #{ctide} " + (bqnrs.zero? ? 'NAO EXISTE' : 'INSERIDO')
27
+ end
28
+ return unless existem_rendas?
29
+
30
+ # processa rendas associadas ao contrato arrendamento
31
+ cm_cria.vr_cria.re_insert
26
32
  end
27
- return unless existem_rendas?
28
33
 
29
- # processa rendas associadas ao contrato arrendamento
30
- cm_cria.vr_cria.re_insert
31
- end
32
-
33
- # (see CLI#apagact)
34
- def ct_apaga
35
- @ctlct = [{ ct: ctide }]
36
- lc_apaga
37
- end
34
+ # (see CLI#apagact)
35
+ def ct_apaga
36
+ @ctlct = [{ ct: ctide }]
37
+ lc_apaga
38
+ end
38
39
 
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?
40
+ # apaga rendas da lista de contrato arrendamento
41
+ #
42
+ # @return [Big] acesso a base dados abank no bigquery
43
+ def lr_apaga
44
+ return self unless opcao[:t] && ctlct.count.positive?
44
45
 
45
- # para nao apagar contrato arrendamento - somente as rendas
46
- @opcao[:t] = false
46
+ # para nao apagar contrato arrendamento - somente as rendas
47
+ @opcao[:t] = false
47
48
 
48
- lc_apaga
49
- self
50
- end
49
+ lc_apaga
50
+ self
51
+ end
51
52
 
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
56
- end
53
+ # apaga rendas da lista de contratos arrendamento
54
+ def lc_apaga
55
+ dml("delete from hernanilr.ab.re where ct in(#{str_lc})#{opcao[:t] ? '' : ' and cnt>0'}")
56
+ puts "RENDAS #{str_lc('')} APAGADAS " + bqnrs.to_s
57
+ end
57
58
 
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
59
+ # @return [String] texto formatado que representa lista de contratos arrendamento
60
+ def str_lc(sep = "'")
61
+ ctlct.map { |c| sep + c[:ct] + sep }.join(',')
62
+ end
62
63
 
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])) })
64
+ # optem lista dados contrato arrendamento (inclui lista movimentos novos)
65
+ #
66
+ # @return [Big] acesso a base dados abank no bigquery
67
+ def cm_cria
68
+ @ctlcm = []
69
+ ctlct.each do |c|
70
+ @ctide = c[:ct]
71
+ sel(sql_last_re)
72
+ @ctlcm << bqres[0].merge({ mv: sel(sql_novo_mv(bqres[0][:dl])) })
73
+ end
74
+ self
72
75
  end
73
- self
74
- end
75
76
 
76
- # @return [Boolean] existem rendas para processar sim/nao?
77
- def existem_rendas?
78
- @ctlct = [{ ct: ctide }]
79
- bqnrs.positive? && opcao[:t]
80
- end
77
+ # @return [Boolean] existem rendas para processar sim/nao?
78
+ def existem_rendas?
79
+ @ctlct = [{ ct: ctide }]
80
+ bqnrs.positive? && opcao[:t]
81
+ end
81
82
 
82
- # @return [Boolean] contrato arrendamento ja existe sim/nao?
83
- def existe_contrato?
84
- sel("select ct from hernanilr.ab.re where ct='#{ctide}' and cnt=0").count.positive?
85
- end
83
+ # @return [Boolean] contrato arrendamento ja existe sim/nao?
84
+ def existe_contrato?
85
+ sel("select ct from hernanilr.ab.re where ct='#{ctide}' and cnt=0").count.positive?
86
+ end
86
87
 
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"
91
- end
88
+ # @return [String] sql para obter ultima renda do contrato arrendamento
89
+ def sql_last_re
90
+ 'select ct,DATE_SUB(DATE_SUB(dl,INTERVAL dias DAY),INTERVAL IF(cnt=0,0,cnt-1) MONTH) as dc,ano,cnt,dl ' \
91
+ "from hernanilr.ab.re where ct='#{ctide}' order by ano desc,cnt desc limit 1"
92
+ end
92
93
 
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"
96
- end
94
+ # @return [String] sql para obter movimentos novos (depois da ultima renda do contrato arrendamento)
95
+ def sql_novo_mv(mdl)
96
+ "select dl,vl from hernanilr.ab.mv where ct='#{ctide}' and dl>='#{(mdl + 1).strftime(DF)}' order by dl,dv"
97
+ end
97
98
 
98
- # @return [String] sql para obter dados do inicio contrato arrendamento
99
- def sql_contrato_mv
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"
103
- else
104
- "select '#{ctide}' as ct,EXTRACT(YEAR FROM DATE '#{opcao[:d]}') as ano,0 as cnt,DATE '#{opcao[:d]}' as dl,0 dias"
99
+ # @return [String] sql para obter dados do inicio contrato arrendamento
100
+ def sql_contrato_mv
101
+ if opcao[:d].size.zero?
102
+ 'select ct,EXTRACT(YEAR FROM DATE_TRUNC(dl,MONTH)) as ano,0 as cnt,DATE_TRUNC(dl,MONTH) as dl,0 dias ' \
103
+ "from hernanilr.ab.mv where ct='#{ctide}' order by dl limit 1"
104
+ else
105
+ "select '#{ctide}' as ct,EXTRACT(YEAR FROM DATE '#{opcao[:d]}') as ano" \
106
+ ",0 as cnt,DATE '#{opcao[:d]}' as dl,0 dias"
107
+ end
105
108
  end
106
109
  end
107
110
  end
@@ -2,153 +2,155 @@
2
2
 
3
3
  require 'roo'
4
4
 
5
- # acesso a folha calculo & base dados abank no bigquery
6
- class Abank::Big::Folha < Abank::Big
7
- # @return [Roo::Excelx] folha calculo a processar
8
- attr_reader :folha
9
-
10
- # @return [Integer] numero conta associado a folha calculo
11
- # @example
12
- # mov*.xlsx --> 1 --> conta-corrente
13
- # movCard*.xlsx --> 2 --> conta-cartao
14
- attr_reader :conta
15
-
16
- # @return [Array] row folha calculo em processamento
17
- attr_reader :rowfc
18
-
5
+ module Abank
19
6
  # 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
41
- n = 0
42
- folha.sheet(0).parse(header_search: ['Data Lanc.', 'Data Valor', 'Descrição', 'Valor']) do |r|
43
- n += 1
44
- puts n == 1 ? "\n" + folha.info : processa_linha(r)
7
+ class Folha < Big
8
+ # @return [Roo::Excelx] folha calculo a processar
9
+ attr_reader :folha
10
+
11
+ # @return [Integer] numero conta associado a folha calculo
12
+ # @example
13
+ # mov*.xlsx --> 1 --> conta-corrente
14
+ # movCard*.xlsx --> 2 --> conta-cartao
15
+ attr_reader :conta
16
+
17
+ # @return [Array] row folha calculo em processamento
18
+ attr_reader :rowfc
19
+
20
+ # acesso a folha calculo & base dados abank no bigquery
21
+ #
22
+ # @param [Hash] opc opcoes trabalho
23
+ # @option opc [String] :f ('') folha calculo a processar
24
+ # @option opc [Boolean] :s (false) apaga movimento similar? (mv)
25
+ # @option opc [Boolean] :e (false) apaga movimento igual? (mv)
26
+ # @option opc [Boolean] :i (false) insere movimento novo? (mv)
27
+ # @option opc [String] :v ('') data valor movimentos (mv)
28
+ # @option opc [String] :g ('') classificacao movimentos (mv)
29
+ def initialize(opc = {})
30
+ @opcao = super
31
+ @folha = Roo::Spreadsheet.open(opc.fetch(:f))
32
+ @conta = opc.fetch(:f).match?(/card/i) ? 2 : 1
33
+ @opcao[:s] = opc.fetch(:s, false)
34
+ @opcao[:e] = opc.fetch(:e, false)
35
+ @opcao[:i] = opc.fetch(:i, false)
36
+ @opcao[:v] = opc.fetch(:v, '')
37
+ @opcao[:g] = opc.fetch(:g, '')
45
38
  end
46
- return unless opcao[:i]
47
39
 
48
- # processa movimentos & atualiza rendas
49
- mv_delete.mv_insert.re_work
50
- end
40
+ # carrega/mostra folha calculo
41
+ def processa_xls
42
+ n = 0
43
+ folha.sheet(0).parse(header_search: ['Data Lanc.', 'Data Valor', 'Descrição', 'Valor']) do |r|
44
+ n += 1
45
+ puts n == 1 ? "\n" + folha.info : processa_linha(r)
46
+ end
47
+ return unless opcao[:i]
48
+
49
+ # processa movimentos & atualiza rendas
50
+ mv_delete.mv_insert.re_work
51
+ end
51
52
 
52
- # processa linha folha calculo
53
- #
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)
58
- # pesquisa existencia linha folha calculo no bigquery
59
- # array.count = 0 ==> pode carregar esta linha
60
- # array.count = 1 ==> mais testes necessarios
61
- # array.count > 1 ==> nao pode carregar esta linha
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
+ # processa linha folha calculo
54
+ #
55
+ # @param [Hash] linha da folha calculo em processamento
56
+ # @return [String] texto informativo formatado da linha em processamento
57
+ def processa_linha(linha)
58
+ vars_xls(linha)
59
+ # pesquisa existencia linha folha calculo no bigquery
60
+ # array.count = 0 ==> pode carregar esta linha
61
+ # array.count = 1 ==> mais testes necessarios
62
+ # array.count > 1 ==> nao pode carregar esta linha
63
+ sel(sql_existe_mv, [{}, {}])
64
+ if linha_naoexiste? then linha_base + values_mv
65
+ elsif linha_existe? then linha_existe
66
+ elsif linha_simila? then linha_similar
67
+ else linha_multiplas
68
+ end
67
69
  end
68
- end
69
70
 
70
- # inicializa variavel para processar linha folha calculo
71
- #
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
77
- end
71
+ # inicializa variavel para processar linha folha calculo
72
+ #
73
+ # @param (see processa_linha)
74
+ def vars_xls(linha)
75
+ @rowfc = linha.values
76
+ @rowfc[2] = rowfc[2].strip
77
+ @rowfc[3] = -1 * rowfc[3] if conta > 1
78
+ end
78
79
 
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])}"
82
- end
80
+ # @return [String] texto base formatado para display
81
+ def linha_base
82
+ "#{rowfc[0].strftime(DF)} #{format('%<v3>-34.34s %<v4>8.2f', v3: rowfc[2], v4: rowfc[3])}"
83
+ end
83
84
 
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])
88
- end
85
+ # @return [String] texto linha existente formatada para display
86
+ def linha_existe
87
+ add_kys if opcao[:e]
88
+ linha_base + ' EXIS ' + format('%<v1>20d', v1: bqres.first[:ky])
89
+ end
89
90
 
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)
94
- end
91
+ # @return [String] texto linha similar formatada para display
92
+ def linha_similar
93
+ add_kys if opcao[:s]
94
+ linha_base + ' SIMI ' + format('%<v1>-20.20s', v1: bqres.first[:ds].strip)
95
+ end
95
96
 
96
- # @return [String] texto linha existencia multipla formatada para display
97
- def linha_multiplas
98
- linha_base + ' MULT(' + bqres.count.to_s + ')'
99
- end
97
+ # @return [String] texto linha existencia multipla formatada para display
98
+ def linha_multiplas
99
+ linha_base + ' MULT(' + bqres.count.to_s + ')'
100
+ end
100
101
 
101
- # obtem chaves movimento (keysin.mv) para apagar
102
- def add_kys
103
- bqres.each { |r| @mvkys += ",#{r[:ky]}" }
104
- end
102
+ # obtem chaves movimento (keysin.mv) para apagar
103
+ def add_kys
104
+ bqres.each { |r| @mvkys += ",#{r[:ky]}" }
105
+ end
105
106
 
106
- # @return [Boolean] linha folha calculo nao existe no bigquery?
107
- def linha_naoexiste?
108
- bqres.count.zero?
109
- end
107
+ # @return [Boolean] linha folha calculo nao existe no bigquery?
108
+ def linha_naoexiste?
109
+ bqres.count.zero?
110
+ end
110
111
 
111
- # @return [Boolean] linha folha calculo existe no bigquery?
112
- def linha_existe?
113
- bqres.count == 1 && bqres.first[:ds].strip == rowfc[2]
114
- end
112
+ # @return [Boolean] linha folha calculo existe no bigquery?
113
+ def linha_existe?
114
+ bqres.count == 1 && bqres.first[:ds].strip == rowfc[2]
115
+ end
115
116
 
116
- # @return [Boolean] linha folha calculo existe parecida no bigquery?
117
- def linha_simila?
118
- bqres.count == 1 && bqres.first[:ds].strip != rowfc[2]
119
- end
117
+ # @return [Boolean] linha folha calculo existe parecida no bigquery?
118
+ def linha_simila?
119
+ bqres.count == 1 && bqres.first[:ds].strip != rowfc[2]
120
+ end
120
121
 
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]}"
125
- end
122
+ # @return [String] sql para movimentos no bigquery
123
+ def sql_existe_mv
124
+ "select *,#{ky_mv} as ky from hernanilr.ab.mv " \
125
+ "where nc=#{conta} and dl='#{rowfc[0].strftime(DF)}' and vl=#{rowfc[3]}"
126
+ end
126
127
 
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'
133
- end
128
+ # obtem movimento (values.mv) para inserir
129
+ #
130
+ # @return [String] ' NOVO'
131
+ def values_mv
132
+ @mvvls += ",('#{rowfc[0].strftime(DF)}','#{dvc.strftime(DF)}','#{rowfc[2]}',#{rowfc[3]}" + values_mv_extra
133
+ ' NOVO'
134
+ end
134
135
 
135
- # @return [String] campos extra do movimento (values.mv) para inserir
136
- def values_mv_extra
137
- ",#{conta},#{dvc.year},#{dvc.month},'#{tpc}',#{ctc})"
138
- end
136
+ # @return [String] campos extra do movimento (values.mv) para inserir
137
+ def values_mv_extra
138
+ ",#{conta},#{dvc.year},#{dvc.month},'#{tpc}',#{ctc})"
139
+ end
139
140
 
140
- # @return [Date] data valor corrigida
141
- def dvc
142
- opcao[:v].size.zero? ? rowfc[1] : Date.parse(opcao[:v])
143
- end
141
+ # @return [Date] data valor corrigida
142
+ def dvc
143
+ opcao[:v].size.zero? ? rowfc[1] : Date.parse(opcao[:v])
144
+ end
144
145
 
145
- # @return [String] classificacao do movimento (null --> classificacao automatica)
146
- def ctc
147
- opcao[:g].size.zero? ? 'null' : ("'" + opcao[:g] + "'")
148
- end
146
+ # @return [String] classificacao do movimento (null --> classificacao automatica)
147
+ def ctc
148
+ opcao[:g].size.zero? ? 'null' : ("'" + opcao[:g] + "'")
149
+ end
149
150
 
150
- # @return [String] tipo movimento c[redito] ou d[ebito]
151
- def tpc
152
- rowfc[3].positive? ? 'c' : 'd'
151
+ # @return [String] tipo movimento c[redito] ou d[ebito]
152
+ def tpc
153
+ rowfc[3].positive? ? 'c' : 'd'
154
+ end
153
155
  end
154
156
  end
@@ -1,121 +1,123 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # @see Abank::Big
4
- class Abank::Big
5
- # @return [Integer] ano renda em tratamento
6
- attr_reader :reano
3
+ module Abank
4
+ # @see Big
5
+ class Big
6
+ # @return [Integer] ano renda em tratamento
7
+ attr_reader :reano
7
8
 
8
- # @return [Integer] mes renda em tratamento
9
- attr_reader :repos
9
+ # @return [Integer] mes renda em tratamento
10
+ attr_reader :repos
10
11
 
11
- # @return [Float] valor renda mensal
12
- attr_reader :revre
12
+ # @return [Float] valor renda mensal
13
+ attr_reader :revre
13
14
 
14
- # @return [String] rendas a inserir (values.re)
15
- attr_reader :revls
15
+ # @return [String] rendas a inserir (values.re)
16
+ attr_reader :revls
16
17
 
17
- # @return [Integer] movimento em tratamento
18
- attr_reader :mvpos
18
+ # @return [Integer] movimento em tratamento
19
+ attr_reader :mvpos
19
20
 
20
- # @return [Date] data lancamento movimento em tratamento
21
- attr_reader :mvdlm
21
+ # @return [Date] data lancamento movimento em tratamento
22
+ attr_reader :mvdlm
22
23
 
23
- # @return [Float] valor movimento em tratamento
24
- attr_reader :mvvlm
24
+ # @return [Float] valor movimento em tratamento
25
+ attr_reader :mvvlm
25
26
 
26
- # (see CLI#recriare)
27
- def re_atualiza
28
- # obtem contratos ativos
29
- @ctlct = sel('SELECT ct from hernanilr.ab.re group by 1')
27
+ # (see CLI#recriare)
28
+ def re_atualiza
29
+ # obtem contratos ativos
30
+ @ctlct = sel('SELECT ct from hernanilr.ab.re group by 1')
30
31
 
31
- # [re]cria rendas [novas|todas]
32
- lr_apaga.cm_cria.vr_cria.re_insert
33
- end
34
-
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
32
+ # [re]cria rendas [novas|todas]
33
+ lr_apaga.cm_cria.vr_cria.re_insert
34
+ end
39
35
 
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
36
+ # cria rendas associadas a lista ids contratos arrendamento
37
+ def re_work
38
+ bqnrs.zero? || ctlct.count.positive? ? cm_cria.vr_cria.re_insert : re_atualiza
39
+ end
47
40
 
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
41
+ # obtem rendas a inserir (values.re)
42
+ #
43
+ # @return [Big] acesso a base dados abank no bigquery
44
+ def vr_cria
45
+ @revls = ctlcm.map { |c| rendas_novas(c) }.flatten(1).join(',')
46
+ self
55
47
  end
56
- end
57
48
 
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?
49
+ # insere rendas no bigquery
50
+ def re_insert
51
+ if revls.size.zero?
52
+ puts 'NAO EXISTEM RENDAS NOVAS'
53
+ else
54
+ dml('insert hernanilr.ab.re VALUES' + revls)
55
+ puts "RENDAS #{str_lc('')} CRIADAS " + bqnrs.to_s
56
+ end
57
+ end
62
58
 
63
- vars_re(cmv)
64
- r = []
65
- while mvvlm >= revre && mvpos < cmv[:mv].count
66
- r << nova_re(cmv)
67
- proximo_mv(cmv)
59
+ # @param [Hash] cmv dados contrato arrendamento (inclui lista movimentos novos)
60
+ # @return [Array<String>] lista rendas novas dum contrato arrendamento (values.re)
61
+ def rendas_novas(cmv)
62
+ return [] unless cmv[:mv].count.positive?
63
+
64
+ vars_re(cmv)
65
+ r = []
66
+ while mvvlm >= revre && mvpos < cmv[:mv].count
67
+ r << nova_re(cmv)
68
+ proximo_mv(cmv)
69
+ end
70
+ r
68
71
  end
69
- r
70
- end
71
72
 
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)
80
- end
73
+ # inicializa variaveis para processar rendas do contrato arrendamento
74
+ # @param (see rendas_novas)
75
+ def vars_re(cmv)
76
+ @reano = cmv[:ano]
77
+ @repos = cmv[:cnt]
78
+ @revre = Float(cmv[:ct][/\d+/]) / 100
79
+ @mvpos = 0
80
+ vars_re_mv(cmv)
81
+ end
81
82
 
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]
87
- end
83
+ # inicializa variaveis para processar movimentos associados ao contrato arrendamento
84
+ # @param (see rendas_novas)
85
+ def vars_re_mv(cmv)
86
+ @mvdlm = cmv[:mv][mvpos][:dl]
87
+ @mvvlm = cmv[:mv][mvpos][:vl]
88
+ end
88
89
 
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
96
- else
97
- @repos += 1
90
+ # @param (see rendas_novas)
91
+ # @return [String] renda formatada (values.re)
92
+ def nova_re(cmv)
93
+ # inicializa proxima renda
94
+ if repos == 12
95
+ @repos = 1
96
+ @reano += 1
97
+ else
98
+ @repos += 1
99
+ end
100
+ "('#{cmv[:ct]}',#{reano},#{repos},'#{mvdlm.strftime(DF)}',#{dias(cmv)})"
98
101
  end
99
- "('#{cmv[:ct]}',#{reano},#{repos},'#{mvdlm.strftime(DF)}',#{dias(cmv)})"
100
- end
101
102
 
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
106
- end
103
+ # @param (see rendas_novas)
104
+ # @return [Integer] dias atraso no pagamento da renda
105
+ def dias(cmv)
106
+ mvdlm.mjd - (Date.new(reano, repos, 1) >> (cmv[:dc].month - 1)).mjd
107
+ end
107
108
 
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
109
+ # inicializa variaveis para processar proximo movimento
110
+ # @param (see rendas_novas)
111
+ def proximo_mv(cmv)
112
+ # valor renda paga retirado do valor do movimento
113
+ @mvvlm -= revre
114
+ return unless mvvlm < revre
114
115
 
115
- # avanca na lista de movimentos
116
- @mvpos += 1
117
- return unless mvpos < cmv[:mv].count
116
+ # avanca na lista de movimentos
117
+ @mvpos += 1
118
+ return unless mvpos < cmv[:mv].count
118
119
 
119
- vars_re_mv(cmv)
120
+ vars_re_mv(cmv)
121
+ end
120
122
  end
121
123
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Abank
4
- VERSION = '0.2.5'
4
+ VERSION = '0.2.6'
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.5
4
+ version: 0.2.6
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-06-10 00:00:00.000000000 Z
11
+ date: 2020-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler