-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.go
211 lines (178 loc) · 5.89 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"log"
"time"
"github.com/chapzin/parse-efd-fiscal/Controllers"
"github.com/chapzin/parse-efd-fiscal/SpedDB"
"github.com/chapzin/parse-efd-fiscal/config"
"github.com/chapzin/parse-efd-fiscal/pkg/worker"
"github.com/chapzin/parse-efd-fiscal/read"
"github.com/chapzin/parse-efd-fiscal/tools"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/tealeg/xlsx"
)
var (
schema = flag.Bool("schema", false, "Recria as tabelas")
importarSped = flag.Bool("importar-sped", false, "Importa os speds")
importarXml = flag.Bool("importar-xml", false, "Importa os xmls")
inventario = flag.Bool("inventario", false, "Fazer processamento do inventario")
anoInicial = flag.Int("anoInicial", 0, "Ano inicial do processamento do inventário")
anoFinal = flag.Int("anoFinal", 0, "Ano inicial do processamento do inventário")
excel = flag.Bool("excel", false, "Gera arquivo excel do inventario")
h010 = flag.Bool("h010", false, "Gera arquivo h010 e 0200 no layout sped para ser importado")
)
func validateInventoryFlags(anoInicial, anoFinal int) error {
if anoInicial == 0 || anoFinal == 0 {
return fmt.Errorf("favor informar o ano inicial e final que deseja processar. Exemplo -anoInicial=2011 -anoFinal=2016")
}
if anoInicial <= 2011 || anoFinal <= 2011 {
return fmt.Errorf("favor informar um ano maior que 2011")
}
if anoInicial <= 999 || anoFinal <= 999 {
return fmt.Errorf("favor informar o ano com 4 digitos. Exemplo 2017")
}
if anoInicial > anoFinal {
return fmt.Errorf("o ano inicial deve ser menor que o ano final")
}
return nil
}
func generateExcel(db *gorm.DB) error {
file := xlsx.NewFile()
sheet, err := file.AddSheet(tools.PLANILHA)
if err != nil {
return fmt.Errorf("erro ao criar planilha: %v", err)
}
Controllers.ExcelMenu(sheet)
Controllers.ExcelAdd(db, sheet)
if err := file.Save("AnaliseInventario.xlsx"); err != nil {
return fmt.Errorf("erro ao salvar arquivo Excel: %v", err)
}
return nil
}
func processInventory(db *gorm.DB, anoInicial, anoFinal int) error {
cfg, err := config.LoadConfig()
if err != nil {
return fmt.Errorf("erro ao carregar configurações: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), cfg.Worker.TaskTimeout)
defer cancel()
// Recria tabela de inventário
SpedDB.DropSchemaInventario(db)
SpedDB.CreateSchemaInventario(db)
// Cria pool com configurações carregadas
pool := worker.NewPool(ctx, cfg.Worker.MaxWorkers)
pool.Start()
// Primeira fase
log.Printf("Iniciando primeira fase do processamento")
pool.Submit(func() error {
Controllers.ProcessarFatorConversao(db, nil)
return nil
})
pool.Submit(func() error {
Controllers.DeletarItensNotasCanceladas(db, "2012-01-01", "2016-12-31", nil)
return nil
})
if errs := pool.Wait(); len(errs) > 0 {
return fmt.Errorf("erros na primeira fase: %v", errs)
}
// Segunda fase
log.Printf("Iniciando segunda fase do processamento")
pool.Submit(func() error {
Controllers.PopularReg0200(db, nil)
return nil
})
pool.Submit(func() error {
Controllers.PopularItensXmls(db, nil)
return nil
})
if errs := pool.Wait(); len(errs) > 0 {
return fmt.Errorf("erros na segunda fase: %v", errs)
}
// Terceira fase
log.Printf("Iniciando terceira fase do processamento")
pool.Submit(func() error {
Controllers.PopularInventarios(anoInicial, anoFinal, nil, db)
return nil
})
pool.Submit(func() error {
Controllers.PopularEntradas(anoInicial, anoFinal, nil, db)
return nil
})
pool.Submit(func() error {
Controllers.PopularSaidas(anoInicial, anoFinal, nil, db)
return nil
})
if errs := pool.Wait(); len(errs) > 0 {
return fmt.Errorf("erros na terceira fase: %v", errs)
}
// Processamento final
log.Printf("Iniciando processamento final")
Controllers.ProcessarDiferencas(db)
return nil
}
func main() {
flag.Parse()
// Carrega configurações do ambiente
cfg, err := config.LoadConfig()
if err != nil {
log.Fatalf("Erro ao carregar configurações: %v", err)
}
// Conecta ao banco de dados - conexão compartilhada
db, err := gorm.Open(cfg.DB.Dialect, cfg.GetMySQLConnectionString())
if err != nil {
log.Fatalf("Falha ao abrir conexão com banco de dados: %v", err)
}
db.LogMode(true)
defer db.Close()
// Cria schema se necessário
if *schema {
SpedDB.Schema(db)
}
// Importa arquivos XML
if *importarXml {
log.Printf("Iniciando processamento de XML em %v", time.Now())
if err := read.RecursiveXmls(db, cfg.SpedsPath, cfg.DigitCode); err != nil {
log.Fatalf("erro ao processar XMLs: %v", err)
}
log.Printf("Final processamento em %v", time.Now())
}
// Importa arquivos SPED
if *importarSped {
log.Printf("Iniciando processamento de Sped em %v", time.Now())
if err := read.RecursiveSpeds(db, cfg.SpedsPath, cfg.DigitCode); err != nil {
log.Fatalf("erro ao processar SPEDs: %v", err)
}
log.Printf("Final processamento em %v", time.Now())
}
// Processa inventário
if *inventario {
if err := validateInventoryFlags(*anoInicial, *anoFinal); err != nil {
log.Fatal(err)
}
log.Printf("Iniciando processamento do inventário em %v", time.Now())
if err := processInventory(db, *anoInicial, *anoFinal); err != nil {
log.Fatalf("Erro no processamento do inventário: %v", err)
}
log.Printf("Processamento do inventário finalizado em %v", time.Now())
}
// Gera arquivo Excel
if *excel {
log.Printf("Iniciando geração do arquivo Excel")
if err := generateExcel(db); err != nil {
log.Fatalf("Erro ao gerar arquivo Excel: %v", err)
}
log.Printf("Arquivo de Análise de Inventário gerado com sucesso")
}
// Processa H010 se necessário
if *h010 && *anoInicial != 0 {
log.Printf("Iniciando processamento H010 para o ano %d", *anoInicial)
// Controllers.CriarH010InvInicial(*anoInicial, db)
// Controllers.CriarH010InvFinal(*anoInicial, db)
} else if *h010 {
log.Fatal("Favor informar a tag ano. Exemplo: -ano=2016")
}
}