Improved execution ordering to enforce credits -> late -> standard
This commit is contained in:
40
main.go
40
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--
|
||||
|
||||
Reference in New Issue
Block a user