Improved execution ordering to enforce credits -> late -> standard

This commit is contained in:
2022-09-30 21:38:42 -04:00
parent b78466d32f
commit c8616e569e

40
main.go
View File

@@ -121,12 +121,18 @@ func parseDate(date string) (time.Time, bool) {
hardDate = false hardDate = false
if date == "first" { if date == "first" {
formalDate = current formalDate = current
if formalDate.Before(time.Now()) {
formalDate = formalDate.AddDate(0, 1, 0)
}
} else if date == "last" { } else if date == "last" {
formalDate = lastOfMonth(current) formalDate = lastOfMonth(current)
} else { } else {
dateString = fmt.Sprintf("%d-%d-%s", dateString = fmt.Sprintf("%d-%d-%s",
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()) {
formalDate = formalDate.AddDate(0, 1, 0)
}
} }
} }
return formalDate, hardDate 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) base.Month(), base.Day(), 0, 0, 0, 0, time.UTC)
initDate := startDate initDate := startDate
dateList := make([]time.Time, 0) dateList := make([]time.Time, 0)
for initDate.Before(endDate) { for initDate.Before(endDate.AddDate(0, 0, 1)) {
if initDate.Before(current) && !hardDate { if !initDate.Before(current) || hardDate {
} else {
dateList = append(dateList, initDate) dateList = append(dateList, initDate)
} }
if frequency == "daily" { if frequency == "daily" {
@@ -363,11 +368,36 @@ func main() {
dateCharges := transactionLog.Transactions[date] dateCharges := transactionLog.Transactions[date]
chargeCount := len(dateCharges) chargeCount := len(dateCharges)
executedCharges := make([]Transaction, 0) executedCharges := make([]Transaction, 0)
credits := make([]Transaction, 0)
late := make([]Transaction, 0)
charges := make([]Transaction, 0)
initialBalances := make([]Account, len(config.Accounts)) initialBalances := make([]Account, len(config.Accounts))
copy(initialBalances, config.Accounts) copy(initialBalances, config.Accounts)
for _, charge := range dateCharges { for _, charge := range dateCharges {
executed := executeTransaction(&transactionLog, config.Accounts, &charge) if charge.Amount > 0 {
if executed { 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) executedCharges = append(executedCharges, charge)
} else { } else {
chargeCount-- chargeCount--