Adding detection for charges scheduled on weekends
This commit is contained in:
122
main.go
122
main.go
@@ -198,15 +198,27 @@ func addTransaction(transactionLog *TransactionLog, transaction *Transaction) {
|
||||
}
|
||||
|
||||
func delayTransaction(transactionLog *TransactionLog, transaction *Transaction) {
|
||||
transaction.Late = true
|
||||
newDate := transaction.Date.AddDate(0, 0, 1)
|
||||
_, ok := transactionLog.Transactions[newDate]
|
||||
for !ok {
|
||||
if newDate == transactionLog.EndDate {
|
||||
break
|
||||
newDate := transaction.Date
|
||||
if transaction.Date.Weekday() == 0 || transaction.Date.Weekday() == 6 {
|
||||
if transaction.Date.Weekday() == 0 {
|
||||
newDate = newDate.AddDate(0, 0, 1)
|
||||
} else if transaction.Date.Weekday() == 6 {
|
||||
newDate = newDate.AddDate(0, 0, 2)
|
||||
}
|
||||
if _, ok := transactionLog.Transactions[newDate]; !ok {
|
||||
transactionLog.Transactions[newDate] = make([]Transaction, 0)
|
||||
}
|
||||
} else {
|
||||
transaction.Late = true
|
||||
newDate = newDate.AddDate(0, 0, 1)
|
||||
_, ok = transactionLog.Transactions[newDate]
|
||||
_, ok := transactionLog.Transactions[newDate]
|
||||
for !ok {
|
||||
if newDate == transactionLog.EndDate {
|
||||
break
|
||||
}
|
||||
newDate = newDate.AddDate(0, 0, 1)
|
||||
_, ok = transactionLog.Transactions[newDate]
|
||||
}
|
||||
}
|
||||
transaction.Date = newDate
|
||||
transactionLog.Transactions[newDate] = append(transactionLog.Transactions[newDate], *transaction)
|
||||
@@ -215,11 +227,12 @@ func delayTransaction(transactionLog *TransactionLog, transaction *Transaction)
|
||||
func executeTransaction(transactionLog *TransactionLog, accounts []Account, transaction *Transaction) bool {
|
||||
executed := false
|
||||
finalDate := transactionLog.EndDate
|
||||
if transaction.Amount > 0 {
|
||||
if transaction.Date.Weekday() == 0 || transaction.Date.Weekday() == 6 {
|
||||
delayTransaction(transactionLog, transaction)
|
||||
} else if transaction.Amount > 0 {
|
||||
accounts[0].Balance = accounts[0].Balance + transaction.Amount
|
||||
executed = true
|
||||
}
|
||||
if transaction.Amount < 0 && math.Abs(transaction.Amount) > (accounts[0].Balance-accounts[0].Minimum) && transaction.Date.Before(finalDate) {
|
||||
} else if transaction.Amount < 0 && math.Abs(transaction.Amount) > (accounts[0].Balance-accounts[0].Minimum) && transaction.Date.Before(finalDate) {
|
||||
delayTransaction(transactionLog, transaction)
|
||||
} else if transaction.Amount < 0 {
|
||||
accounts[0].Balance = accounts[0].Balance + transaction.Amount
|
||||
@@ -298,6 +311,10 @@ func printOutput(date time.Time, initialBalances []Account, executedCharges []Tr
|
||||
late_tag := fmt.Sprintf("(%s - %s)", "Late", charge.OrigDate.Format("Jan 02 2006"))
|
||||
rear_spacer := strings.Repeat(" ", 65-len(late_tag)-len(output))
|
||||
fmt.Printf("%s%s%s |\n", output, rear_spacer, late_tag)
|
||||
} else if charge.Date.After(charge.OrigDate) {
|
||||
weekend_tag := fmt.Sprintf("(%s - %s)", "Weekend", charge.OrigDate.Format("Jan 02 2006"))
|
||||
rear_spacer := strings.Repeat(" ", 65-len(weekend_tag)-len(output))
|
||||
fmt.Printf("%s%s%s |\n", output, rear_spacer, weekend_tag)
|
||||
} else {
|
||||
rear_spacer := strings.Repeat(" ", 65-len(output))
|
||||
fmt.Printf("%s%s |\n", output, rear_spacer)
|
||||
@@ -326,6 +343,8 @@ func printOutput(date time.Time, initialBalances []Account, executedCharges []Tr
|
||||
|
||||
func main() {
|
||||
yfile, err := ioutil.ReadFile(os.Args[1])
|
||||
dates := make([]time.Time, 0)
|
||||
base := time.Now()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -361,50 +380,59 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
current := time.Date(base.Year(),
|
||||
base.Month(), base.Day(), 0, 0, 0, 0, time.UTC)
|
||||
for current.Before(transactionLog.EndDate.AddDate(0, 0, 7)) {
|
||||
dates = append(dates, current)
|
||||
current = current.AddDate(0, 0, 1)
|
||||
}
|
||||
|
||||
collectTransactions(config.Credit, &transactionLog)
|
||||
collectTransactions(config.Transactions, &transactionLog)
|
||||
|
||||
for _, date := range transactionLog.Dates {
|
||||
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 {
|
||||
if charge.Amount > 0 {
|
||||
credits = append(credits, charge)
|
||||
} else if charge.Late {
|
||||
late = append(late, charge)
|
||||
} else {
|
||||
charges = append(charges, charge)
|
||||
for _, date := range dates {
|
||||
if _, ok := transactionLog.Transactions[date]; ok {
|
||||
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 {
|
||||
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 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 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--
|
||||
for _, charge := range charges {
|
||||
if executeTransaction(&transactionLog, config.Accounts, &charge) {
|
||||
executedCharges = append(executedCharges, charge)
|
||||
} else {
|
||||
chargeCount--
|
||||
}
|
||||
}
|
||||
if chargeCount > 0 {
|
||||
printOutput(date, initialBalances, executedCharges, config.Accounts)
|
||||
}
|
||||
}
|
||||
if chargeCount > 0 {
|
||||
printOutput(date, initialBalances, executedCharges, config.Accounts)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user