More logic fixes
This commit is contained in:
40
main.go
40
main.go
@@ -72,7 +72,7 @@ type Transaction struct {
|
|||||||
Delayable bool
|
Delayable bool
|
||||||
Frequency string
|
Frequency string
|
||||||
ID string
|
ID string
|
||||||
Late bool
|
Delayed bool
|
||||||
Name string
|
Name string
|
||||||
OriginDate time.Time
|
OriginDate time.Time
|
||||||
Shift string
|
Shift string
|
||||||
@@ -199,7 +199,7 @@ func parseTransaction(transactionYaml TransactionYaml) (*Transaction, error) {
|
|||||||
Delayable: delayable,
|
Delayable: delayable,
|
||||||
Frequency: transactionYaml.Frequency,
|
Frequency: transactionYaml.Frequency,
|
||||||
ID: uuid.NewString(),
|
ID: uuid.NewString(),
|
||||||
Late: false,
|
Delayed: false,
|
||||||
Name: transactionYaml.Name,
|
Name: transactionYaml.Name,
|
||||||
OriginDate: date,
|
OriginDate: date,
|
||||||
Shift: transactionYaml.Shift,
|
Shift: transactionYaml.Shift,
|
||||||
@@ -305,7 +305,7 @@ func (tl *TransactionLog) addTransaction(transaction *Transaction) {
|
|||||||
Delayable: transaction.Delayable,
|
Delayable: transaction.Delayable,
|
||||||
Frequency: "",
|
Frequency: "",
|
||||||
ID: uuid.NewString(),
|
ID: uuid.NewString(),
|
||||||
Late: false,
|
Delayed: false,
|
||||||
Name: transaction.Name,
|
Name: transaction.Name,
|
||||||
OriginDate: date,
|
OriginDate: date,
|
||||||
Shift: transaction.Shift,
|
Shift: transaction.Shift,
|
||||||
@@ -334,23 +334,34 @@ func (tl *TransactionLog) deleteDay(date time.Time) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func calculateShift(transactionLog TransactionLog, transaction Transaction) time.Time {
|
func calculateShift(transactionLog TransactionLog, transaction Transaction) time.Time {
|
||||||
|
if transaction.Amount > 0 {
|
||||||
|
add := 1
|
||||||
newDate := transaction.Date
|
newDate := transaction.Date
|
||||||
|
|
||||||
for isWeekend(newDate) {
|
|
||||||
if transaction.Shift == "early" {
|
if transaction.Shift == "early" {
|
||||||
newDate = newDate.AddDate(0, 0, -1)
|
add = -1
|
||||||
} else {
|
|
||||||
newDate = newDate.AddDate(0, 0, 1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for isWeekend(newDate) {
|
||||||
|
newDate = newDate.AddDate(0, 0, add)
|
||||||
}
|
}
|
||||||
|
|
||||||
return newDate
|
return newDate
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if transaction.Shift == "early" {
|
||||||
|
return previousLogDay(&transactionLog, transaction.Date).Date
|
||||||
|
}
|
||||||
|
|
||||||
|
return nextLogDay(&transactionLog, transaction.Date).Date
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tl *TransactionLog) delayTransaction(transaction *Transaction) {
|
func (tl *TransactionLog) delayTransaction(transaction *Transaction) {
|
||||||
newDate := calculateShift(*tl, *transaction)
|
newDate := calculateShift(*tl, *transaction)
|
||||||
|
|
||||||
if newDate.Before(tl.first()) || newDate == transaction.Date {
|
if newDate.Before(tl.first()) || newDate == transaction.Date {
|
||||||
|
logrus.Debugf("Transaction not delayable %s (%s | %f)", transaction.ID, transaction.Name, transaction.Amount)
|
||||||
transaction.Delayable = false
|
transaction.Delayable = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -365,6 +376,7 @@ func (tl *TransactionLog) delayTransaction(transaction *Transaction) {
|
|||||||
|
|
||||||
transaction.Date = newDate
|
transaction.Date = newDate
|
||||||
tl.LogDays[newDate].Transactions[transaction.ID] = transaction
|
tl.LogDays[newDate].Transactions[transaction.ID] = transaction
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tl *TransactionLog) executeTransaction(transaction *Transaction) bool {
|
func (tl *TransactionLog) executeTransaction(transaction *Transaction) bool {
|
||||||
@@ -379,12 +391,15 @@ func (tl *TransactionLog) executeTransaction(transaction *Transaction) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if !isAffordable(transaction.Amount, accounts[0].Balance, accounts[0].Minimum) && transaction.Delayable {
|
|
||||||
logrus.Debugf("Balance: %f | Minimum: %f\n", accounts[0].Balance, accounts[0].Minimum)
|
logrus.Debugf("Balance: %f | Minimum: %f\n", accounts[0].Balance, accounts[0].Minimum)
|
||||||
|
logrus.Debugf("Delayable: %t", transaction.Delayable)
|
||||||
|
if !isAffordable(transaction.Amount, accounts[0].Balance, accounts[0].Minimum) && transaction.Delayable {
|
||||||
logrus.Debugf("Delaying %s: %f (%s) due to lack of funds\n",
|
logrus.Debugf("Delaying %s: %f (%s) due to lack of funds\n",
|
||||||
transaction.Name, transaction.Amount, transaction.Date)
|
transaction.Name, transaction.Amount, transaction.Date)
|
||||||
tl.delayTransaction(transaction)
|
tl.delayTransaction(transaction)
|
||||||
|
|
||||||
|
transaction.Delayed = true
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -404,7 +419,10 @@ func isAffordable(charge, balance, minimum float64) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
return math.Abs(charge) > (balance - minimum)
|
logrus.Debugf("Charge: %f | Balance - Miniumum: %f", charge, balance-minimum)
|
||||||
|
logrus.Debugf("abs(charge) < (balance - minimum): %t", math.Abs(charge) < (balance-minimum))
|
||||||
|
|
||||||
|
return math.Abs(charge) < (balance - minimum)
|
||||||
}
|
}
|
||||||
|
|
||||||
func currentDay() time.Time {
|
func currentDay() time.Time {
|
||||||
@@ -462,7 +480,7 @@ func categorizeTransactions(transactions map[string]*Transaction) map[string][]s
|
|||||||
for _, t := range transactions {
|
for _, t := range transactions {
|
||||||
if t.Amount > 0 {
|
if t.Amount > 0 {
|
||||||
categorized["credits"] = append(categorized["credits"], t.ID)
|
categorized["credits"] = append(categorized["credits"], t.ID)
|
||||||
} else if t.Late {
|
} else if t.Delayed {
|
||||||
categorized["late"] = append(categorized["late"], t.ID)
|
categorized["late"] = append(categorized["late"], t.ID)
|
||||||
} else {
|
} else {
|
||||||
categorized["standard"] = append(categorized["standard"], t.ID)
|
categorized["standard"] = append(categorized["standard"], t.ID)
|
||||||
@@ -588,7 +606,7 @@ func printOutput(logDay LogDay) {
|
|||||||
outString = "+"
|
outString = "+"
|
||||||
}
|
}
|
||||||
outString = fmt.Sprintf("%s$%.2f%s| %s", outString, math.Abs(charge.Amount), spacer, charge.Name)
|
outString = fmt.Sprintf("%s$%.2f%s| %s", outString, math.Abs(charge.Amount), spacer, charge.Name)
|
||||||
if charge.Late {
|
if charge.Delayed {
|
||||||
late_tag := fmt.Sprintf("(%s - %s)", "Late", charge.OriginDate.Format("Jan 02 2006"))
|
late_tag := fmt.Sprintf("(%s - %s)", "Late", charge.OriginDate.Format("Jan 02 2006"))
|
||||||
rear_spacer := strings.Repeat(" ", width-len(late_tag)-len(outString))
|
rear_spacer := strings.Repeat(" ", width-len(late_tag)-len(outString))
|
||||||
fmt.Printf("%s%s%s |\n", outString, rear_spacer, late_tag)
|
fmt.Printf("%s%s%s |\n", outString, rear_spacer, late_tag)
|
||||||
|
|||||||
Reference in New Issue
Block a user