Skip to content

Commit

Permalink
feat: add flags start-date and end-date to import only a specific dat…
Browse files Browse the repository at this point in the history
…e interval; automatically change value of one installment to the total value (as organizze works importing the total installments)
  • Loading branch information
viniciusgabrielfo committed Dec 26, 2023
1 parent 489af38 commit 527f1f1
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 12 deletions.
31 changes: 29 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@ import (
"flag"
"fmt"
"log/slog"
"time"

"github.com/viniciusgabrielfo/organizze-invoice-itau-converter/internal"
)

var invoicePath string
var (
invoicePath string
startDate string
endDate string
)

func init() {
flag.StringVar(&invoicePath, "file", "", "itaú invoice path to consume")
flag.StringVar(&startDate, "start-date", "", "only consume from start-date (02/01/2006)")
flag.StringVar(&endDate, "end-date", "", "only consume until end-date (02/01/2006)")
flag.Parse()
}

Expand All @@ -20,7 +27,27 @@ func main() {

l.Info("Starting invoice-itau-consumer...")

entries, err := internal.GetEntriesFromItauInvoice(invoicePath)
itauImportConfigs := &internal.ItauImportConfigs{}

if startDate != "" {
tStartDate, err := time.Parse("02/01/2006", startDate)
if err != nil {
panic(err)
}

itauImportConfigs.StartDate = tStartDate
}

if endDate != "" {
tEndDate, err := time.Parse("02/01/2006", endDate)
if err != nil {
panic(err)
}

itauImportConfigs.EndDate = tEndDate
}

entries, err := internal.GetEntriesFromItauInvoice(itauImportConfigs, invoicePath)
if err != nil {
l.Error(err.Error())
return
Expand Down
84 changes: 74 additions & 10 deletions internal/itau.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@ package internal

import (
"errors"
"log/slog"
"regexp"
"strconv"
"strings"
"time"

"github.com/Rhymond/go-money"
"github.com/viniciusgabrielfo/organizze-invoice-itau-converter/pkg/category_definer"
"github.com/viniciusgabrielfo/organizze-invoice-itau-converter/pkg/model"
"github.com/viniciusgabrielfo/xls"
)

func GetEntriesFromItauInvoice(filePath string) ([]model.Entry, error) {
type ItauImportConfigs struct {
StartDate time.Time
EndDate time.Time
}

func GetEntriesFromItauInvoice(configs *ItauImportConfigs, filePath string) ([]model.Entry, error) {
logger := slog.Default()

f, err := xls.Open(filePath, "utf-8")
if err != nil {
return nil, err
Expand All @@ -35,34 +46,87 @@ func GetEntriesFromItauInvoice(filePath string) ([]model.Entry, error) {
continue

Check failure on line 46 in internal/itau.go

View workflow job for this annotation

GitHub Actions / build

branch statements should not be cuddled if block has more than two lines (wsl)
}

col1 := row.Col(0)
col2 := row.Col(1)
date := row.Col(0)
description := row.Col(1)

if col1 == "data" && col2 == "lançamento" {
if date == "data" && description == "lançamento" {
isEntry = true
continue
}

if isEntry {
if col1 == "" || col2 == "dólar de conversão" {
if date == "" || description == "dólar de conversão" {
continue
}

entryDate, err := time.Parse("02/01/2006", date)
if err != nil {
logger.Error(err.Error())
continue
}

col4 := row.Col(3)
if !IsBetweenConfigInternal(configs, entryDate) {
continue
}

value, err := strconv.ParseFloat(col4, 32)
value, err := strconv.ParseFloat(row.Col(3), 32)
if err != nil {
return entries, err
}

if ok, installments := IsInstallmentPurchase(description); ok {
value = value * float64(installments)
}

entries = append(entries, model.Entry{
Date: col1,
Description: col2,
Category: category_definer.GetCategoryFromDescription(col2),
Date: date,
Description: description,
Category: category_definer.GetCategoryFromDescription(description),
Value: money.NewFromFloat(-value, money.BRL),
})
}
}

return entries, nil
}

func IsBetweenConfigInternal(configs *ItauImportConfigs, date time.Time) bool {
if !configs.StartDate.IsZero() {
if date.Before(configs.StartDate) {
return false
}
}

if !configs.EndDate.IsZero() {
if date.After(configs.EndDate) {
return false
}
}

return true
}

func IsInstallmentPurchase(description string) (bool, int32) {
logger := slog.Default()

re, err := regexp.Compile("[0-9]+/[0-9]+")
if err != nil {
logger.Error(err.Error())
return false, 0
}

installmentPattern := re.FindAllString(description, -1)
if len(installmentPattern) == 0 {
return false, 0
}

s := strings.Split(installmentPattern[0], "/")

i, err := strconv.ParseInt(s[1], 10, 32)
if err != nil {
logger.Error(err.Error())
return false, 0
}

return true, int32(i)
}

0 comments on commit 527f1f1

Please sign in to comment.