Added 'delayable' transaction option. Fixed issue in date pre-population
This commit is contained in:
84
main.go
84
main.go
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
@@ -51,6 +50,7 @@ type ConfigYaml struct {
|
||||
type Transaction struct {
|
||||
Amount float64
|
||||
Date time.Time
|
||||
Delayable bool
|
||||
Frequency string
|
||||
HardDate bool
|
||||
Late bool
|
||||
@@ -62,6 +62,7 @@ type TransactionYaml struct {
|
||||
Name string `yaml:"name"`
|
||||
Amount float64 `yaml:"amount"`
|
||||
Date string `yaml:"date"`
|
||||
Delayable *bool `yaml:"delayable"`
|
||||
Frequency string `yaml:"frequency,omitempty"`
|
||||
}
|
||||
|
||||
@@ -81,9 +82,13 @@ func parseConfig(configYaml *ConfigYaml) Config {
|
||||
|
||||
for _, transaction := range configYaml.Credit {
|
||||
formalDate, hardDate := parseDate(transaction.Date)
|
||||
if transaction.Delayable == nil {
|
||||
transaction.Delayable = &[]bool{true}[0]
|
||||
}
|
||||
newTransaction := Transaction{
|
||||
Amount: transaction.Amount,
|
||||
Date: formalDate,
|
||||
Delayable: *transaction.Delayable,
|
||||
OrigDate: formalDate,
|
||||
HardDate: hardDate,
|
||||
Frequency: transaction.Frequency,
|
||||
@@ -95,9 +100,13 @@ func parseConfig(configYaml *ConfigYaml) Config {
|
||||
|
||||
for _, transaction := range configYaml.Transactions {
|
||||
formalDate, hardDate := parseDate(transaction.Date)
|
||||
if transaction.Delayable == nil {
|
||||
transaction.Delayable = &[]bool{true}[0]
|
||||
}
|
||||
newTransaction := Transaction{
|
||||
Amount: transaction.Amount,
|
||||
Date: formalDate,
|
||||
Delayable: *transaction.Delayable,
|
||||
OrigDate: formalDate,
|
||||
HardDate: hardDate,
|
||||
Frequency: transaction.Frequency,
|
||||
@@ -189,6 +198,7 @@ func addTransaction(transactionLog *TransactionLog, transaction *Transaction) {
|
||||
Name: transaction.Name,
|
||||
Amount: transaction.Amount,
|
||||
Date: date,
|
||||
Delayable: transaction.Delayable,
|
||||
OrigDate: date,
|
||||
HardDate: false,
|
||||
Late: false,
|
||||
@@ -227,12 +237,12 @@ func delayTransaction(transactionLog *TransactionLog, transaction *Transaction)
|
||||
func executeTransaction(transactionLog *TransactionLog, accounts []Account, transaction *Transaction) bool {
|
||||
executed := false
|
||||
finalDate := transactionLog.EndDate
|
||||
if transaction.Date.Weekday() == 0 || transaction.Date.Weekday() == 6 {
|
||||
if transaction.Date.Weekday() == 0 || transaction.Date.Weekday() == 6 && transaction.Delayable {
|
||||
delayTransaction(transactionLog, transaction)
|
||||
} else if transaction.Amount > 0 {
|
||||
accounts[0].Balance = accounts[0].Balance + transaction.Amount
|
||||
executed = true
|
||||
} else 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) && transaction.Delayable {
|
||||
delayTransaction(transactionLog, transaction)
|
||||
} else if transaction.Amount < 0 {
|
||||
accounts[0].Balance = accounts[0].Balance + transaction.Amount
|
||||
@@ -294,57 +304,50 @@ func printOutput(date time.Time, initialBalances []Account, executedCharges []Tr
|
||||
divider2 := delim2 + "|"
|
||||
|
||||
fmt.Println(divider)
|
||||
output := fmt.Sprint(date.Format("Jan 02 2006"))
|
||||
rear_spacer := strings.Repeat(" ", 65-len(output))
|
||||
fmt.Printf("%s%s |\n", output, rear_spacer)
|
||||
outString := fmt.Sprint(date.Format("Jan 02 2006"))
|
||||
rear_spacer := strings.Repeat(" ", 65-len(outString))
|
||||
fmt.Printf("%s%s |\n", outString, rear_spacer)
|
||||
for _, v := range initialBalances {
|
||||
spacer := strings.Repeat(" ", 19-len(fmt.Sprintf("%.2f", v.Balance)))
|
||||
output := fmt.Sprintf("$%.2f%s| %s", v.Balance, spacer, v.Name)
|
||||
rear_spacer := strings.Repeat(" ", 65-len(output))
|
||||
fmt.Printf("%s%s |\n", output, rear_spacer)
|
||||
outString := fmt.Sprintf("$%.2f%s| %s", v.Balance, spacer, v.Name)
|
||||
rear_spacer := strings.Repeat(" ", 65-len(outString))
|
||||
fmt.Printf("%s%s |\n", outString, rear_spacer)
|
||||
}
|
||||
for _, charge := range executedCharges {
|
||||
spacer := strings.Repeat(" ", 18-len(fmt.Sprintf("%.2f", math.Abs(charge.Amount))))
|
||||
outString := ""
|
||||
if charge.Amount <= 0 {
|
||||
output := fmt.Sprintf("-$%.2f%s| %s", math.Abs(charge.Amount), spacer, charge.Name)
|
||||
if charge.Late {
|
||||
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)
|
||||
}
|
||||
outString = "-"
|
||||
} else {
|
||||
output := fmt.Sprintf("+$%.2f%s| %s", charge.Amount, spacer, charge.Name)
|
||||
if charge.Late {
|
||||
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 {
|
||||
rear_spacer := strings.Repeat(" ", 65-len(output))
|
||||
fmt.Printf("%s%s |\n", output, rear_spacer)
|
||||
}
|
||||
outString = "+"
|
||||
}
|
||||
outString = fmt.Sprintf("%s$%.2f%s| %s", outString, math.Abs(charge.Amount), spacer, charge.Name)
|
||||
if charge.Late {
|
||||
late_tag := fmt.Sprintf("(%s - %s)", "Late", charge.OrigDate.Format("Jan 02 2006"))
|
||||
rear_spacer := strings.Repeat(" ", 65-len(late_tag)-len(outString))
|
||||
fmt.Printf("%s%s%s |\n", outString, 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(outString))
|
||||
fmt.Printf("%s%s%s |\n", outString, rear_spacer, weekend_tag)
|
||||
} else {
|
||||
rear_spacer := strings.Repeat(" ", 65-len(outString))
|
||||
fmt.Printf("%s%s |\n", outString, rear_spacer)
|
||||
}
|
||||
}
|
||||
fmt.Println(divider2)
|
||||
for _, v := range accounts {
|
||||
spacer := strings.Repeat(" ", 19-len(fmt.Sprintf("%.2f", v.Balance)))
|
||||
output := fmt.Sprintf("$%.2f%s| %s", v.Balance, spacer, v.Name)
|
||||
rear_spacer := strings.Repeat(" ", 65-len(output))
|
||||
fmt.Printf("%s%s |\n", output, rear_spacer)
|
||||
outString := fmt.Sprintf("$%.2f%s| %s", v.Balance, spacer, v.Name)
|
||||
rear_spacer := strings.Repeat(" ", 65-len(outString))
|
||||
fmt.Printf("%s%s |\n", outString, rear_spacer)
|
||||
}
|
||||
fmt.Println(divider + "\n")
|
||||
}
|
||||
|
||||
func main() {
|
||||
yfile, err := ioutil.ReadFile(os.Args[1])
|
||||
yfile, err := os.ReadFile(os.Args[1])
|
||||
dates := make([]time.Time, 0)
|
||||
base := time.Now()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -380,16 +383,15 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
current := time.Date(base.Year(),
|
||||
base.Month(), base.Day(), 0, 0, 0, 0, time.UTC)
|
||||
collectTransactions(config.Credit, &transactionLog)
|
||||
collectTransactions(config.Transactions, &transactionLog)
|
||||
|
||||
current := transactionLog.StartDate
|
||||
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 dates {
|
||||
if _, ok := transactionLog.Transactions[date]; ok {
|
||||
dateCharges := transactionLog.Transactions[date]
|
||||
|
||||
Reference in New Issue
Block a user