diff --git a/main.go b/main.go index c05b18e..0d82e20 100644 --- a/main.go +++ b/main.go @@ -149,7 +149,7 @@ func parseConfig(configYaml *ConfigYaml) Config { } for _, transaction := range configYaml.Credit { - formalDate, hardDate := parseDate(transaction.Date) + formalDate, hardDate := parseDate(transaction.Date, transaction.Frequency) if transaction.Delayable == nil { transaction.Delayable = &[]bool{true}[0] } @@ -172,7 +172,7 @@ func parseConfig(configYaml *ConfigYaml) Config { } for _, transaction := range configYaml.Transactions { - formalDate, hardDate := parseDate(transaction.Date) + formalDate, hardDate := parseDate(transaction.Date, transaction.Frequency) if transaction.Delayable == nil { transaction.Delayable = &[]bool{true}[0] } @@ -197,7 +197,21 @@ func parseConfig(configYaml *ConfigYaml) Config { return config } -func parseDate(date string) (time.Time, bool) { +func getFirstDate(date time.Time, frequency string) time.Time { + var firstDate time.Time + + current := time.Date(time.Now().Year(), + time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC) + if date.Before(current) && frequency != "" { + firstDate = calculateFrequency(frequency, date, date.AddDate(0, 1, 1), false)[0] + } else { + firstDate = date.AddDate(0, 1, 0) + } + + return firstDate +} + +func parseDate(date, frequency string) (time.Time, bool) { hardDate := true base := time.Now().AddDate(0, 0, -time.Now().Day()+1) current := time.Date(base.Year(), @@ -209,7 +223,7 @@ func parseDate(date string) (time.Time, bool) { if date == "first" { formalDate = current if formalDate.Before(time.Now()) { - formalDate = formalDate.AddDate(0, 1, 0) + formalDate = getFirstDate(formalDate, frequency) } } else if date == "last" { formalDate = lastOfMonth(current) @@ -218,7 +232,7 @@ func parseDate(date string) (time.Time, bool) { time.Now().Year(), int(time.Now().Month()), date) formalDate, _ = time.Parse("2006-1-2", dateString) if formalDate.Before(time.Now()) { - formalDate = formalDate.AddDate(0, 1, 0) + formalDate = getFirstDate(formalDate, frequency) } } } @@ -435,7 +449,6 @@ func processTransactionLog(transactionLog *TransactionLog, accounts []Account) b copy(logDay.StartingBalance, endBalance) copy(logDay.EndingBalance, endBalance) categorized := categorizeTransactions(logDay.Transactions) - logrus.Debugf("---\nStarting Balance %s: %f\n", date, logDay.StartingBalance[0].Balance) logrus.Debugln(categorized) if processTransactions(transactionLog, categorized["credits"]) { shifted = true @@ -452,7 +465,6 @@ func processTransactionLog(transactionLog *TransactionLog, accounts []Account) b copy(logDay.StartingBalance, endBalance) copy(logDay.EndingBalance, endBalance) categorized := categorizeTransactions(logDay.Transactions) - logrus.Debugf("---\nStarting Balance %s: %f\n", date, logDay.StartingBalance[0].Balance) logrus.Debugln(categorized) if processTransactions(transactionLog, categorized["credits"]) { shifted = true @@ -483,6 +495,7 @@ func processTransactions(transactionLog *TransactionLog, transactionIDs []string shifted := false for _, id := range transactionIDs { transaction, ok := transactionLog.Transactions[id] + logrus.Debugf("Processing %s (%f)", transaction.Name, transaction.Amount) if !ok { logrus.Fatalf("Transaction ID: %s not found in log\n", id) } @@ -543,6 +556,10 @@ func printOutput(logDay LogDay) { } func main() { + if len(os.Getenv("DEBUG")) > 0 { + logrus.SetLevel(logrus.DebugLevel) + } + yfile, err := os.ReadFile(os.Args[1]) if err != nil { @@ -584,8 +601,6 @@ func main() { shifted := true for shifted { - //startingBalance := make([]Account, len(config.Accounts)) - //copy(startingBalance, config.Accounts) shifted = false shifted = processTransactionLog(&transactionLog, config.Accounts) }