-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrapina.go
474 lines (429 loc) · 10.3 KB
/
rapina.go
1
2
3
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// SPDX-FileCopyrightText: 2023 Adriano Prado <[email protected]>
//
// SPDX-License-Identifier: MIT
package rapina
import (
"strings"
"github.com/dude333/rapinav2/pkg/progress"
)
type InformeTrimestral struct {
Codigo string
Descr string
Valores []ValoresTrimestrais
}
type ValoresTrimestrais struct {
Ano int
T1 float64
T2 float64
T3 float64
T4 float64
}
// T retorna o valor do trimestre pelo índice (0 <= n < 4)
func (v *ValoresTrimestrais) T(n int) float64 {
switch n {
case 1:
return v.T1
case 2:
return v.T2
case 3:
return v.T3
case 4:
return v.T4
}
return 0.0
}
// SetT salva o valor do trimestre pelo índice (0 <= n < 4)
func (v *ValoresTrimestrais) SetT(n int, val float64) {
switch n {
case 1:
v.T1 = val
case 2:
v.T2 = val
case 3:
v.T3 = val
case 4:
v.T4 = val
}
}
func (v ValoresTrimestrais) Add(other ValoresTrimestrais) ValoresTrimestrais {
if v.Ano != other.Ano {
return v
}
return ValoresTrimestrais{
Ano: v.Ano,
T1: v.T1 + other.T1,
T2: v.T2 + other.T2,
T3: v.T3 + other.T3,
T4: v.T4 + other.T4,
}
}
func (v ValoresTrimestrais) Sub(other ValoresTrimestrais) ValoresTrimestrais {
if v.Ano != other.Ano {
return v
}
return ValoresTrimestrais{
Ano: v.Ano,
T1: v.T1 - other.T1,
T2: v.T2 - other.T2,
T3: v.T3 - other.T3,
T4: v.T4 - other.T4,
}
}
func (v ValoresTrimestrais) Mult(other ValoresTrimestrais) ValoresTrimestrais {
if v.Ano != other.Ano {
return v
}
return ValoresTrimestrais{
Ano: v.Ano,
T1: v.T1 * other.T1,
T2: v.T2 * other.T2,
T3: v.T3 * other.T3,
T4: v.T4 * other.T4,
}
}
func (v ValoresTrimestrais) Div(other ValoresTrimestrais) ValoresTrimestrais {
safeDiv := func(num, divisor float64) float64 {
if divisor == 0 {
return 0.0
}
return num / divisor
}
return ValoresTrimestrais{
Ano: v.Ano,
T1: safeDiv(v.T1, other.T1),
T2: safeDiv(v.T2, other.T2),
T3: safeDiv(v.T3, other.T3),
T4: safeDiv(v.T4, other.T4),
}
}
func (v ValoresTrimestrais) MultNum(factor float64) ValoresTrimestrais {
return ValoresTrimestrais{
Ano: v.Ano,
T1: v.T1 * factor,
T2: v.T2 * factor,
T3: v.T3 * factor,
T4: v.T4 * factor,
}
}
func (v ValoresTrimestrais) DivNum(divisor float64) ValoresTrimestrais {
if divisor == 0 {
return ValoresTrimestrais{v.Ano, 0.0, 0.0, 0.0, 0.0}
}
return ValoresTrimestrais{
Ano: v.Ano,
T1: v.T1 / divisor,
T2: v.T2 / divisor,
T3: v.T3 / divisor,
T4: v.T4 / divisor,
}
}
func OpVTs(op rune, v1, v2 []ValoresTrimestrais) []ValoresTrimestrais {
type par struct {
// ano int
p1Idx int
p2Idx int
p1 bool
p2 bool
}
anos := RangeAnosVTs(v1, v2)
pares := make([]par, len(anos))
for i := range v1 {
pares[v1[i].Ano-anos[0]].p1 = true
pares[v1[i].Ano-anos[0]].p1Idx = i
}
for j := range v2 {
pares[v2[j].Ano-anos[0]].p2 = true
pares[v2[j].Ano-anos[0]].p2Idx = j
}
v := make([]ValoresTrimestrais, 0, len(anos))
for k := range anos {
var p1Ptr, p2Ptr ValoresTrimestrais
i := pares[k].p1Idx
j := pares[k].p2Idx
if pares[k].p1 && pares[k].p2 {
p1Ptr = v1[i]
p2Ptr = v2[j]
} else if pares[k].p1 && !pares[k].p2 {
p1Ptr = v1[i]
p2Ptr = ValoresTrimestrais{v1[i].Ano, 0.0, 0.0, 0.0, 0.0}
} else if !pares[k].p1 && pares[k].p2 {
p1Ptr = ValoresTrimestrais{v2[j].Ano, 0.0, 0.0, 0.0, 0.0}
p2Ptr = v2[j]
} else {
continue
}
r := ValoresTrimestrais{}
switch op {
case '+':
r = p1Ptr.Add(p2Ptr)
case '-':
r = p1Ptr.Sub(p2Ptr)
case '*':
r = p1Ptr.Mult(p2Ptr)
case '/':
r = p1Ptr.Div(p2Ptr)
}
v = append(v, r)
}
return v
}
func AddVTs(v1, v2 []ValoresTrimestrais) []ValoresTrimestrais {
return OpVTs('+', v1, v2)
}
func SubVTs(v1, v2 []ValoresTrimestrais) []ValoresTrimestrais {
return OpVTs('-', v1, v2)
}
func MultVTs(v1, v2 []ValoresTrimestrais) []ValoresTrimestrais {
return OpVTs('*', v1, v2)
}
func DivVTs(v1, v2 []ValoresTrimestrais) []ValoresTrimestrais {
return OpVTs('/', v1, v2)
}
func codPai(codigo string) string {
if len(codigo) < 1 {
return codigo
}
lvl := strings.Count(codigo, ".") + 1
if lvl <= 3 {
return codigo
}
idx := strings.LastIndex(codigo, ".")
if idx <= 0 {
return codigo
}
return codigo[:idx]
}
// UnificarContasSimilares unifica as linhas similares do InformeTrimestral
// comparando o código, sem o último grupo (ex.: 1.02.05.01 => 1.02.05),
// com as próximas linhas.
// Cada linha (InformeTrimestral) possui o seguinte formato:
// Linha n => [Ano:ano Valor trimestre 1 | Valor T2 | Valor T3 | Valor T4]
// Exemplo:
// "Tributo a recuperar" => [2019 1|0|5|3; 2021 5|2|0|0]
// "Tributos a recuperar" => [2019 0|2|0|0; 2020 1|4|2|2; 2021 0|0|1|2]
// Resultado:
// "Tributo a recuperar" => [2019 1|2|5|3; 2020 1|4|2|2; 2021 5|2|1|2]
func UnificarContasSimilares(itr []InformeTrimestral) []InformeTrimestral {
itrUnificado := make([]InformeTrimestral, 1, len(itr))
unida := make([]bool, len(itr))
anos := RangeAnos(itr, false)
ultimaLinha := len(itr) - 1
for linha1 := 0; linha1 <= ultimaLinha; linha1++ {
if unida[linha1] {
continue
}
valoresUnificados := itr[linha1].Valores
for linha2 := linha1 + 1; linha2 <= ultimaLinha; linha2++ {
if unida[linha2] {
continue
}
cod1 := codPai(itr[linha1].Codigo)
cod2 := codPai(itr[linha2].Codigo)
if cod1 == cod2 && Similar(cod1+itr[linha1].Descr, cod2+itr[linha2].Descr) {
unida[linha2] = true
for _, ano := range anos {
v1, existe1 := valorAno(ano, valoresUnificados)
v2, existe2 := valorAno(ano, itr[linha2].Valores)
if !existe1 && existe2 {
valoresUnificados = append(valoresUnificados, v2)
}
if existe1 && existe2 {
v, ok := equalizarValores(ano, v1, v2)
unida[linha2] = ok
if ok {
valoresUnificados = append(valoresUnificados, v)
} else {
break
}
}
} // next ano
if unida[linha2] {
progress.Trace("Joining:\n\t+ %v\n\t+ %v\n\t", itr[linha1], itr[linha2])
}
}
} // next linha2
informe := InformeTrimestral{
Codigo: itr[linha1].Codigo,
Descr: itr[linha1].Descr,
Valores: valoresUnificados,
}
itrUnificado = append(itrUnificado, informe)
} // next linha1
return itrUnificado
}
func equalizarValores(ano int, v1, v2 ValoresTrimestrais) (ValoresTrimestrais, bool) {
var v ValoresTrimestrais
v.Ano = ano
ok := true
check := func(v1Tn, v2Tn float64) (float64, bool) {
if !ok || (v1Tn != 0.0 && v2Tn != 0.0) {
return 0.0, false
}
if v1Tn != 0.0 && v2Tn == 0.0 {
return v1Tn, true
}
return v2Tn, true
}
v.T1, ok = check(v1.T1, v2.T1)
v.T2, ok = check(v1.T2, v2.T2)
v.T3, ok = check(v1.T3, v2.T3)
v.T4, ok = check(v1.T4, v2.T4)
return v, ok
}
func valorAno(ano int, valores []ValoresTrimestrais) (ValoresTrimestrais, bool) {
for _, v := range valores {
if v.Ano == ano {
return v, true
}
}
return ValoresTrimestrais{}, false
}
func Zerado(valores []ValoresTrimestrais) bool {
for _, v := range valores {
if v.T1 != 0 || v.T2 != 0 || v.T3 != 0 || v.T4 != 0 {
return false
}
}
return true
}
func TrimestresComDados(itr []InformeTrimestral) []bool {
minAno, maxAno := MinMax(itr)
colunas := make([]bool, 4*(1+maxAno-minAno))
for _, informe := range itr {
for ano := minAno; ano <= maxAno; ano++ {
for _, v := range informe.Valores {
if v.Ano != ano {
continue
}
i := (v.Ano - minAno) * 4
if !colunas[i+0] && v.T1 != 0.0 {
colunas[i+0] = true
}
if v.T2 != 0.0 {
colunas[i+1] = true
}
if v.T3 != 0.0 {
colunas[i+2] = true
}
if v.T4 != 0.0 {
colunas[i+3] = true
}
}
}
}
return colunas
}
func MinMax(itr []InformeTrimestral) (int, int) {
minAno := 99999
maxAno := 0
for i := range itr {
for _, valores := range itr[i].Valores {
if valores.Ano < minAno {
minAno = valores.Ano
}
if valores.Ano > maxAno {
maxAno = valores.Ano
}
}
}
if minAno > maxAno {
return 0, 0
}
return minAno, maxAno
}
func RangeAnos(itr []InformeTrimestral, reverse bool) []int {
min, max := MinMax(itr)
seq := make([]int, max-min+1)
if reverse {
for i := max; i >= min; i-- {
seq[max-i] = i
}
return seq
}
for i := min; i <= max; i++ {
seq[i-min] = i
}
return seq
}
func RangeAnosVTs(v1, v2 []ValoresTrimestrais) []int {
itr := []InformeTrimestral{
{"", "", v1},
{"", "", v2},
}
return RangeAnos(itr, false)
}
// ÚÚltimoTrimestre retorna o último trimestre com valor não nulo
func ÚltimoTrimestre(ano int, valores []ValoresTrimestrais) int {
for _, valor := range valores {
if valor.Ano != ano {
continue
}
if valor.T4 != 0.0 {
return 4
} else if valor.T3 != 0.0 {
return 3
} else if valor.T2 != 0.0 {
return 2
}
}
return 1
}
// TTM armazena a soma dos últimos 4 trimestres em cada um dos trimestres; usado em métricas
// que comparam como valores do balanço patrimonial.
// Exemplo: ROE = Lucro Líq. dos últimos 12 meses / Patrim.Líq.
func TTM(acct []ValoresTrimestrais) []ValoresTrimestrais {
min, max := MinMax([]InformeTrimestral{{Codigo: "", Descr: "", Valores: acct}})
t := ÚltimoTrimestre(max, acct)
valores := make([]float64, (max-min+1)*4)
for ano := min; ano <= max; ano++ {
for _, valor := range acct {
if valor.Ano != ano {
continue
}
idx := 4 * (ano - min)
valores[idx+0] = valor.T1
valores[idx+1] = valor.T2
valores[idx+2] = valor.T3
valores[idx+3] = valor.T4
}
}
somaValores := func(from, to int) float64 {
invalidParm := from < 0 || to >= len(valores) || from > to
invalidPeriod := to >= (len(valores) - (4 - t))
if invalidParm || invalidPeriod {
return 0.0
}
total := 0.0
for i := from; i <= to; i++ {
total += valores[i]
}
return total
}
valoresAcum := make([]ValoresTrimestrais, (max-min+1)*4)
for ano := min; ano <= max; ano++ {
for _, valor := range acct {
if valor.Ano != ano {
continue
}
idx := 4 * (ano - min)
valoresAcum[ano-min] = ValoresTrimestrais{
Ano: ano,
T1: somaValores(idx-3, idx+0),
T2: somaValores(idx-2, idx+1),
T3: somaValores(idx-1, idx+2),
T4: somaValores(idx-0, idx+3),
}
}
}
return valoresAcum
}
// ManterÚltimoTrimestre mantém apenas o último trimestre não nulo de cada ano
func ManterÚltimoTrimestre(vts []ValoresTrimestrais) []ValoresTrimestrais {
w := make([]ValoresTrimestrais, len(vts))
for i, v := range vts {
t := ÚltimoTrimestre(v.Ano, vts)
w[i].Ano = v.Ano
w[i].SetT(t, v.T(t))
}
return w
}