abank 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +0 -3
- data/Gemfile.lock +1 -1
- data/lib/abank.rb +3 -3
- data/lib/abank/big.rb +118 -116
- data/lib/abank/contrato.rb +91 -88
- data/lib/abank/folha.rb +130 -128
- data/lib/abank/rendas.rb +97 -95
- data/lib/abank/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a559c3029471b2ad081cc7ec13b0b61387e2109c780fe98ae1a11ba22d77538
|
4
|
+
data.tar.gz: d809890286188b61e47ec9b4eb0cbce1dac6478c85206b336e8820f2060e1321
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 000d52a70da099de5f5eff73cca078787b1c537f48da18ef6dfb140f3935d023445d48516648e461a12a02ffdaf684160da3bc0bcb0ecb11a12926230ad5f37b
|
7
|
+
data.tar.gz: 8c73fa2f6c9ec5353dc0c8e8d76648ad11ec3169de63bc796378b425dd509f2d6d7b916d61e05eb1feb4571a8779c9249b7b1bfbba4132c48afbc3465c4c9b22
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
data/lib/abank.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
81
|
+
Folha.new(f: f).processa_xls
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
data/lib/abank/big.rb
CHANGED
@@ -2,133 +2,135 @@
|
|
2
2
|
|
3
3
|
require 'google/cloud/bigquery'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
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
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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
|
-
|
66
|
-
|
67
|
-
|
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
|
-
|
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
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
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
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
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
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
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
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
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
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
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
|
data/lib/abank/contrato.rb
CHANGED
@@ -1,107 +1,110 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|
-
#
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
-
|
46
|
-
|
46
|
+
# para nao apagar contrato arrendamento - somente as rendas
|
47
|
+
@opcao[:t] = false
|
47
48
|
|
48
|
-
|
49
|
-
|
50
|
-
|
49
|
+
lc_apaga
|
50
|
+
self
|
51
|
+
end
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
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
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
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
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
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
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
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
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
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
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
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
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
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
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
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
|
data/lib/abank/folha.rb
CHANGED
@@ -2,153 +2,155 @@
|
|
2
2
|
|
3
3
|
require 'roo'
|
4
4
|
|
5
|
-
|
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
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
@folha
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
@
|
36
|
-
@
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
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
|
-
#
|
49
|
-
|
50
|
-
|
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
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
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
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
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
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
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
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
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
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
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
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
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
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
102
|
+
# obtem chaves movimento (keysin.mv) para apagar
|
103
|
+
def add_kys
|
104
|
+
bqres.each { |r| @mvkys += ",#{r[:ky]}" }
|
105
|
+
end
|
105
106
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
107
|
+
# @return [Boolean] linha folha calculo nao existe no bigquery?
|
108
|
+
def linha_naoexiste?
|
109
|
+
bqres.count.zero?
|
110
|
+
end
|
110
111
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
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
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
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
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
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
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
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
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
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
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
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
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
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
|
-
|
151
|
-
|
152
|
-
|
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
|
data/lib/abank/rendas.rb
CHANGED
@@ -1,121 +1,123 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
module Abank
|
4
|
+
# @see Big
|
5
|
+
class Big
|
6
|
+
# @return [Integer] ano renda em tratamento
|
7
|
+
attr_reader :reano
|
7
8
|
|
8
|
-
|
9
|
-
|
9
|
+
# @return [Integer] mes renda em tratamento
|
10
|
+
attr_reader :repos
|
10
11
|
|
11
|
-
|
12
|
-
|
12
|
+
# @return [Float] valor renda mensal
|
13
|
+
attr_reader :revre
|
13
14
|
|
14
|
-
|
15
|
-
|
15
|
+
# @return [String] rendas a inserir (values.re)
|
16
|
+
attr_reader :revls
|
16
17
|
|
17
|
-
|
18
|
-
|
18
|
+
# @return [Integer] movimento em tratamento
|
19
|
+
attr_reader :mvpos
|
19
20
|
|
20
|
-
|
21
|
-
|
21
|
+
# @return [Date] data lancamento movimento em tratamento
|
22
|
+
attr_reader :mvdlm
|
22
23
|
|
23
|
-
|
24
|
-
|
24
|
+
# @return [Float] valor movimento em tratamento
|
25
|
+
attr_reader :mvvlm
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
-
|
32
|
-
|
33
|
-
|
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
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
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
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
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
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
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
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
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
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
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
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
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
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
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
|
-
|
116
|
-
|
117
|
-
|
116
|
+
# avanca na lista de movimentos
|
117
|
+
@mvpos += 1
|
118
|
+
return unless mvpos < cmv[:mv].count
|
118
119
|
|
119
|
-
|
120
|
+
vars_re_mv(cmv)
|
121
|
+
end
|
120
122
|
end
|
121
123
|
end
|
data/lib/abank/version.rb
CHANGED
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
|
+
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-
|
11
|
+
date: 2020-06-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|