Skip to content

Commit

Permalink
Melhorar união de linhas similares
Browse files Browse the repository at this point in the history
  • Loading branch information
dude333 committed Sep 15, 2023
1 parent 8866d4c commit 5be797b
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 95 deletions.
33 changes: 21 additions & 12 deletions cmd/relatorio.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,6 @@ func excelReport(x *Excel, itr []rapina.InformeTrimestral, decrescente bool) {

// ===== Relatório - início =====

x.printCell(1, 1, titleStyle, "Código")
x.printCell(1, 2, titleStyle, "Descrição")

seq := []int{0, 1, 2, 3}
anos := rapina.RangeAnos(itr)

Expand All @@ -211,20 +208,32 @@ func excelReport(x *Excel, itr []rapina.InformeTrimestral, decrescente bool) {
}

const initCol = 3
col := initCol
for _, ano := range anos {
x.printCell(1, col+seq[0], titleStyle, fmt.Sprintf("1T%d", ano))
x.printCell(1, col+seq[1], titleStyle, fmt.Sprintf("2T%d", ano))
x.printCell(1, col+seq[2], titleStyle, fmt.Sprintf("3T%d", ano))
x.printCell(1, col+seq[3], titleStyle, fmt.Sprintf("4T%d", ano))
col += 4

cabeçalho := func(row, col int) {
x.printCell(row, 1, titleStyle, "Código")
x.printCell(row, 2, titleStyle, "Descrição")
for _, ano := range anos {
x.printCell(row, col+seq[0], titleStyle, fmt.Sprintf("1T%d", ano))
x.printCell(row, col+seq[1], titleStyle, fmt.Sprintf("2T%d", ano))
x.printCell(row, col+seq[2], titleStyle, fmt.Sprintf("3T%d", ano))
x.printCell(row, col+seq[3], titleStyle, fmt.Sprintf("4T%d", ano))
col += 4
}
}

row := 2
for _, informe := range itr {
row := 1
col := initCol
cabeçalho(row, col)

row++
for i, informe := range itr {
if rapina.Zerado(informe.Valores) {
continue
}
if i > 1 && (itr[i-1].Codigo[0] != itr[i].Codigo[0]) {
x.printCell(row, 1, fontStyle, "______________")
row++
}
spc := space(informe.Codigo)
x.printCell(row, 1, fontStyle, spc+informe.Codigo)
x.printCell(row, 2, fontStyle, spc+informe.Descr)
Expand Down
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/dude333/rapinav2

go 1.17
go 1.18

require (
github.com/dustin/go-humanize v1.0.0
Expand All @@ -14,7 +14,10 @@ require (
gopkg.in/yaml.v2 v2.4.0
)

require github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
require (
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
github.com/dude333/rslp-go v0.0.3
)

require (
github.com/fsnotify/fsnotify v1.5.1 // indirect
Expand All @@ -36,6 +39,6 @@ require (
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 // indirect
golang.org/x/crypto v0.8.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/sys v0.12.0 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
)
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsr
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dude333/rslp-go v0.0.3 h1:/efSfxekrlatR2WMC6pFnCbXMOc/cutOQW0wMPo0s/4=
github.com/dude333/rslp-go v0.0.3/go.mod h1:2W0vwG4umRZUL+MROAz9LAnSWVjYUECUhLm8/2uImBE=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down Expand Up @@ -513,8 +515,9 @@ golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand Down
5 changes: 3 additions & 2 deletions normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"unicode"

"github.com/dude333/rslp-go"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
Expand Down Expand Up @@ -36,8 +37,8 @@ func NormalizeString(s string) string {
}

func Similar(s1, s2 string) bool {
normalizedS1 := NormalizeString(s1)
normalizedS2 := NormalizeString(s2)
normalizedS1 := NormalizeString(rslp.Frase(s1))
normalizedS2 := NormalizeString(rslp.Frase(s2))

return normalizedS1 == normalizedS2
}
164 changes: 87 additions & 77 deletions rapina.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package rapina

import (
"strings"

"github.com/dude333/rapinav2/pkg/progress"
)

Expand All @@ -22,96 +24,104 @@ type ValoresTrimestrais struct {
T4 float64
}

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 {
itr2 := make([]InformeTrimestral, 1, len(itr))
itrUnificado := make([]InformeTrimestral, 1, len(itr))
unida := make([]bool, len(itr))
anos := RangeAnos(itr)
ultimaLinha := len(itr) - 1
for linha := 1; linha <= ultimaLinha; linha++ {
unir := false
if Similar(itr[linha-1].Codigo+itr[linha-1].Descr, itr[linha].Codigo+itr[linha].Descr) {
unir = true
var novosValores []ValoresTrimestrais
for _, ano := range anos {
v1, existe1 := valorAno(ano, itr[linha-1].Valores)
v2, existe2 := valorAno(ano, itr[linha].Valores)

if existe1 && !existe2 {
novosValores = append(novosValores, v1)
}
if !existe1 && existe2 {
novosValores = append(novosValores, v2)
}
if existe1 && existe2 {
if (v1.T1 != 0.0 && v2.T1 != 0.0) ||
(v1.T2 != 0.0 && v2.T2 != 0.0) ||
(v1.T3 != 0.0 && v2.T3 != 0.0) ||
(v1.T4 != 0.0 && v2.T4 != 0.0) {
itr2 = append(itr2, itr[linha-1])
unir = false
break
}
novosValores = append(novosValores, equalizarValores(ano, v1, v2))
}
} // next ano

if linha == ultimaLinha {
itr2 = append(itr2, itr[linha])
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
}
if unir {
informe := InformeTrimestral{
Codigo: itr[linha-1].Codigo,
Descr: itr[linha-1].Descr,
Valores: novosValores,
cod1 := codPai(itr[linha1].Codigo)
cod2 := codPai(itr[linha2].Codigo)
if 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])
}
itr[linha].Valores = novosValores
itr2 = append(itr2, informe)
progress.Trace("Joining:\n\t+ %v\n\t+ %v\n\t= %v\n\n", itr[linha-1], itr[linha], informe)
linha++
}
} else {
itr2 = append(itr2, itr[linha-1])
if linha == ultimaLinha {
itr2 = append(itr2, itr[linha])
}
} // next linha2
informe := InformeTrimestral{
Codigo: itr[linha1].Codigo,
Descr: itr[linha1].Descr,
Valores: valoresUnificados,
}
}
return itr2
itrUnificado = append(itrUnificado, informe)
} // next linha1
return itrUnificado
}

func equalizarValores(ano int, v1, v2 ValoresTrimestrais) ValoresTrimestrais {
func equalizarValores(ano int, v1, v2 ValoresTrimestrais) (ValoresTrimestrais, bool) {
var v ValoresTrimestrais
v.Ano = ano
if v1.T1 != 0.0 && v2.T1 == 0.0 {
v.T1 = v1.T1
} else {
v.T1 = v2.T1
}
if v1.T2 != 0.0 && v2.T2 == 0.0 {
v.T2 = v1.T2
} else {
v.T2 = v2.T2
}
if v1.T3 != 0.0 && v2.T3 == 0.0 {
v.T3 = v1.T3
} else {
v.T3 = v2.T3
}
if v1.T4 != 0.0 && v2.T4 == 0.0 {
v.T4 = v1.T4
} else {
v.T4 = v2.T4
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
} else {
return v2Tn, true
}
}
return v
}

// func existeAno(ano int, valores []ValoresTrimestrais) bool {
// for _, v := range valores {
// if v.Ano == ano {
// return true
// }
// }
// return false
// }
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 {
Expand Down
11 changes: 11 additions & 0 deletions rapina_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,14 @@ func Test_Zerado(t *testing.T) {
})
}
}

func Test_codPai(t *testing.T) {
tests := []string{"1", "2", "1.02", "2.03.04", "3.01.02.04", "6.01", "7.02.01"}
expected := []string{"1", "2", "1.02", "2.03.04", "3.01.02", "6.01", "7.02.01"}

for i := range tests {
if got := codPai(tests[i]); got != expected[i] {
t.Errorf("codPai(%s) = %s, want %s", tests[i], got, expected[i])
}
}
}

0 comments on commit 5be797b

Please sign in to comment.