From c8616e569e484b18cb54f4a0d2774191b928d324 Mon Sep 17 00:00:00 2001 From: Samuel Attwood Date: Fri, 30 Sep 2022 21:38:42 -0400 Subject: [PATCH] Improved execution ordering to enforce credits -> late -> standard --- main.go | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index dfc7c51..46868fb 100644 --- a/main.go +++ b/main.go @@ -121,12 +121,18 @@ func parseDate(date string) (time.Time, bool) { hardDate = false if date == "first" { formalDate = current + if formalDate.Before(time.Now()) { + formalDate = formalDate.AddDate(0, 1, 0) + } } else if date == "last" { formalDate = lastOfMonth(current) } else { dateString = fmt.Sprintf("%d-%d-%s", 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) + } } } return formalDate, hardDate @@ -237,9 +243,8 @@ func calculateFrequency(frequency string, startDate time.Time, endDate time.Time base.Month(), base.Day(), 0, 0, 0, 0, time.UTC) initDate := startDate dateList := make([]time.Time, 0) - for initDate.Before(endDate) { - if initDate.Before(current) && !hardDate { - } else { + for initDate.Before(endDate.AddDate(0, 0, 1)) { + if !initDate.Before(current) || hardDate { dateList = append(dateList, initDate) } if frequency == "daily" { @@ -363,11 +368,36 @@ func main() { dateCharges := transactionLog.Transactions[date] chargeCount := len(dateCharges) executedCharges := make([]Transaction, 0) + credits := make([]Transaction, 0) + late := make([]Transaction, 0) + charges := make([]Transaction, 0) initialBalances := make([]Account, len(config.Accounts)) copy(initialBalances, config.Accounts) for _, charge := range dateCharges { - executed := executeTransaction(&transactionLog, config.Accounts, &charge) - if executed { + if charge.Amount > 0 { + credits = append(credits, charge) + } else if charge.Late { + late = append(late, charge) + } else { + charges = append(charges, charge) + } + } + for _, charge := range credits { + if executeTransaction(&transactionLog, config.Accounts, &charge) { + executedCharges = append(executedCharges, charge) + } else { + chargeCount-- + } + } + for _, charge := range late { + if executeTransaction(&transactionLog, config.Accounts, &charge) { + executedCharges = append(executedCharges, charge) + } else { + chargeCount-- + } + } + for _, charge := range charges { + if executeTransaction(&transactionLog, config.Accounts, &charge) { executedCharges = append(executedCharges, charge) } else { chargeCount--