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