Skip to content

Commit

Permalink
Manter menu de relatórios em loop
Browse files Browse the repository at this point in the history
  • Loading branch information
dude333 committed Sep 8, 2023
1 parent 9a96718 commit e3a7a3f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 19 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,23 @@ RAIA_DROGASIL_S.A.xlsx

### `rapina.yaml`

Caso deseje mudar o local de gravação do banco de dados e dos arquivos temporários, criar o arquivo `rapina.yaml` no mesmo diretório do executável (`rapinav2` ou `rapinav2.exe`) com os seguintes dados:
Para customizar os parâmetros, crie o arquivo `rapina.yaml` no mesmo diretório do executável (`rapinav2` ou `rapinav2.exe`) com um ou mais dos seguintes parâmetros:


| Parâmetro | Descrição |
|-----------|-----------|
| `dataSrc` | Arquivo onde serão gravados os dados coletados <br> Default: ./.dados |
| `tempDir` | Diretório onde os arquivos temporários serão armazernados <br> Default: ./.dados |
| `reportDir` | Diretório onde os relatórios serão salvos <br> Default: ./ |


Exemplo:
```yaml
dataSrc: "/home/user1/dados/rapinav2.db?cache=shared&mode=rwc&_journal_mode=WAL&_busy_timeout=5000"
dataSrc: "/home/user1/dados/rapinav2.db
tempDir: "/home/user1/dados"
reportDir: "/home/user1/relatorios"
```
* `dataSrc`: arquivo do banco de dados.
* `tempDir`: diretório para arquivos temporários.

## Build
Para compilar o código fonte, basta seguir as instruções deste link: https://go.dev/doc/install
Expand Down
3 changes: 2 additions & 1 deletion cmd/cmd_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func escolherEmpresa(empresas []rapina.Empresa) (rapina.Empresa, bool) {
templates := &promptui.SelectTemplates{
Help: `{{ "Para navegar:" | faint }} [{{ .NextKey | faint }} ` +
`{{ .PrevKey | faint }} {{ .PageUpKey | faint }} {{ .PageDownKey | faint }}]` +
`{{ if .Search }} {{ "Para procurar:" | faint }} [{{ .SearchKey | faint }}]{{ end }}`,
`{{ if .Search }} {{ "Para procurar:" | faint }} [{{ .SearchKey | faint }}]{{ end }}` +
` Para sair: [Ctrl-c]`,
Label: "{{ . }}:",
Active: " > {{ .Nome | red }}",
Inactive: " {{ .Nome | blue }}",
Expand Down
32 changes: 26 additions & 6 deletions cmd/relatorio.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var relatorioCmd = &cobra.Command{
Aliases: []string{"relat", "report"},
Short: "imprimir relatório",
Long: `relatorio das informações financeiras de uma empresa`,
Run: criarRelatório,
Run: menuRelatório,
}

func init() {
Expand All @@ -40,7 +40,7 @@ func init() {
rootCmd.AddCommand(relatorioCmd)
}

func criarRelatório(cmd *cobra.Command, args []string) {
func menuRelatório(cmd *cobra.Command, args []string) {
dfp, err := contabil.NovaDemonstraçãoFinanceira(db(), flags.tempDir)
if err != nil {
progress.Fatal(err)
Expand All @@ -50,11 +50,19 @@ func criarRelatório(cmd *cobra.Command, args []string) {
if err != nil {
progress.Fatal(err)
}
empresa, ok := escolherEmpresa(empresas)
if !ok {
progress.FatalMsg("Nenhuma empresa foi escolhida")

for {
empresa, ok := escolherEmpresa(empresas)
if !ok {
progress.Warning("Até logo!")
os.Exit(0)
}

criarRelatório(empresa, dfp)
}
}

func criarRelatório(empresa rapina.Empresa, dfp *contabil.DemonstraçãoFinanceira) {
filename, err := prepareFilename(flags.relatorio.outputDir, empresa.Nome)
if err != nil {
progress.Fatal(err)
Expand Down Expand Up @@ -102,7 +110,12 @@ func criarRelatório(cmd *cobra.Command, args []string) {
progress.Fatal(err)
os.Exit(1)
}
progress.Status("Relatório salvo como: %s", filename)

status := fmt.Sprintf("Relatório salvo como: %s", filename)
line := strings.Repeat("-", min(len(status), 80))
progress.Status(line)
progress.Status(status)
progress.Status(line + "\n\n")
}

type Excel struct {
Expand Down Expand Up @@ -317,6 +330,13 @@ func stringWidth(str string) float64 {
return width
}

func min(a, b int) int {
if a < b {
return a
}
return b
}

// charWidth foi criado com este script em Python:
// from PIL import ImageFont
// font = ImageFont.truetype("calibri.ttf", 11)
Expand Down
18 changes: 12 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ func initConfig() {
}
progress.Debug("dataSrc = %s", flags.dataSrc)
if err := createDir(flags.dataSrc); err != nil {
progress.Error(err)
os.Exit(1)
progress.Fatal(err)
}

flags.tempDir = tempDirDefault
Expand All @@ -124,9 +123,17 @@ func initConfig() {
}
progress.Debug("tempDir = %s", flags.tempDir)
if err := createDir(flags.tempDir); err != nil {
progress.Error(err)
os.Exit(1)
progress.Fatal(err)
}

if viper.IsSet("reportDir") {
flags.relatorio.outputDir = viper.GetString("reportDir")
}
progress.Debug("reportDir = %s", flags.relatorio.outputDir)
if err := createDir(flags.relatorio.outputDir); err != nil {
progress.Fatal(err)
}

fmt.Printf("\n\n")
}

Expand All @@ -139,8 +146,7 @@ func db() *sqlx.DB {
var err error
_db, err := sqlx.Open("sqlite3", flags.dataSrc)
if err != nil {
progress.ErrorMsg("Erro ao abrir/criar o banco de dados, verificar se o diretório existe: %s", flags.dataSrc)
os.Exit(1)
progress.FatalMsg("Erro ao abrir/criar o banco de dados, verificar se o diretório existe: %s", flags.dataSrc)
}
_db.SetMaxOpenConns(1)

Expand Down
3 changes: 2 additions & 1 deletion rapina.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
dataSrc: "/home/adr/var/rapina/rapina.db?cache=shared&mode=rwc&_journal_mode=WAL&_busy_timeout=5000"
tempDir: "/home/adr/var/rapina"
reportDir: "/mnt/share"

modelos:
global:
Expand Down Expand Up @@ -76,4 +77,4 @@ modelos:

relatórios:
relatório 1:
- ok
- ok

0 comments on commit e3a7a3f

Please sign in to comment.