From 8f93770e8d2cf94d8768404bb5b80f31aa18eaaa Mon Sep 17 00:00:00 2001 From: Samuel Attwood Date: Sat, 11 Mar 2023 03:33:20 -0500 Subject: [PATCH] More logic fixes --- main.go | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index 8ef7385..1cc0eeb 100644 --- a/main.go +++ b/main.go @@ -72,7 +72,7 @@ type Transaction struct { Delayable bool Frequency string ID string - Late bool + Delayed bool Name string OriginDate time.Time Shift string @@ -199,7 +199,7 @@ func parseTransaction(transactionYaml TransactionYaml) (*Transaction, error) { Delayable: delayable, Frequency: transactionYaml.Frequency, ID: uuid.NewString(), - Late: false, + Delayed: false, Name: transactionYaml.Name, OriginDate: date, Shift: transactionYaml.Shift, @@ -305,7 +305,7 @@ func (tl *TransactionLog) addTransaction(transaction *Transaction) { Delayable: transaction.Delayable, Frequency: "", ID: uuid.NewString(), - Late: false, + Delayed: false, Name: transaction.Name, OriginDate: date, Shift: transaction.Shift, @@ -334,23 +334,34 @@ func (tl *TransactionLog) deleteDay(date time.Time) { } func calculateShift(transactionLog TransactionLog, transaction Transaction) time.Time { - newDate := transaction.Date + if transaction.Amount > 0 { + add := 1 + newDate := transaction.Date - for isWeekend(newDate) { if transaction.Shift == "early" { - newDate = newDate.AddDate(0, 0, -1) - } else { - newDate = newDate.AddDate(0, 0, 1) + add = -1 } + + for isWeekend(newDate) { + newDate = newDate.AddDate(0, 0, add) + } + + return newDate + } - return newDate + if transaction.Shift == "early" { + return previousLogDay(&transactionLog, transaction.Date).Date + } + + return nextLogDay(&transactionLog, transaction.Date).Date } func (tl *TransactionLog) delayTransaction(transaction *Transaction) { newDate := calculateShift(*tl, *transaction) if newDate.Before(tl.first()) || newDate == transaction.Date { + logrus.Debugf("Transaction not delayable %s (%s | %f)", transaction.ID, transaction.Name, transaction.Amount) transaction.Delayable = false return } @@ -365,6 +376,7 @@ func (tl *TransactionLog) delayTransaction(transaction *Transaction) { transaction.Date = newDate tl.LogDays[newDate].Transactions[transaction.ID] = transaction + } func (tl *TransactionLog) executeTransaction(transaction *Transaction) bool { @@ -379,12 +391,15 @@ func (tl *TransactionLog) executeTransaction(transaction *Transaction) bool { return false } + logrus.Debugf("Balance: %f | Minimum: %f\n", accounts[0].Balance, accounts[0].Minimum) + logrus.Debugf("Delayable: %t", transaction.Delayable) if !isAffordable(transaction.Amount, accounts[0].Balance, accounts[0].Minimum) && transaction.Delayable { - logrus.Debugf("Balance: %f | Minimum: %f\n", accounts[0].Balance, accounts[0].Minimum) logrus.Debugf("Delaying %s: %f (%s) due to lack of funds\n", transaction.Name, transaction.Amount, transaction.Date) tl.delayTransaction(transaction) + transaction.Delayed = true + return false } @@ -404,7 +419,10 @@ func isAffordable(charge, balance, minimum float64) bool { return true } - return math.Abs(charge) > (balance - minimum) + logrus.Debugf("Charge: %f | Balance - Miniumum: %f", charge, balance-minimum) + logrus.Debugf("abs(charge) < (balance - minimum): %t", math.Abs(charge) < (balance-minimum)) + + return math.Abs(charge) < (balance - minimum) } func currentDay() time.Time { @@ -462,7 +480,7 @@ func categorizeTransactions(transactions map[string]*Transaction) map[string][]s for _, t := range transactions { if t.Amount > 0 { categorized["credits"] = append(categorized["credits"], t.ID) - } else if t.Late { + } else if t.Delayed { categorized["late"] = append(categorized["late"], t.ID) } else { categorized["standard"] = append(categorized["standard"], t.ID) @@ -588,7 +606,7 @@ func printOutput(logDay LogDay) { outString = "+" } outString = fmt.Sprintf("%s$%.2f%s| %s", outString, math.Abs(charge.Amount), spacer, charge.Name) - if charge.Late { + if charge.Delayed { late_tag := fmt.Sprintf("(%s - %s)", "Late", charge.OriginDate.Format("Jan 02 2006")) rear_spacer := strings.Repeat(" ", width-len(late_tag)-len(outString)) fmt.Printf("%s%s%s |\n", outString, rear_spacer, late_tag)