55 lines
1.1 KiB
Go
Executable File
55 lines
1.1 KiB
Go
Executable File
package main
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"fmt"
|
|
"log"
|
|
"regexp"
|
|
"time"
|
|
)
|
|
|
|
type Transaction struct {
|
|
ID string
|
|
Bank Bank
|
|
Amount string
|
|
Vendor string
|
|
Date string
|
|
Account string
|
|
}
|
|
|
|
func (t *Transaction) Format() string {
|
|
return fmt.Sprintf("(%s) %v/%v: %s @ %s", cleanDate(t.Date), t.Account, t.Bank, t.Amount, t.Vendor)
|
|
}
|
|
|
|
func (t *Transaction) String() string {
|
|
return fmt.Sprint(*t)
|
|
}
|
|
|
|
func NewTransaction(account, amount, vendor, date string, bank Bank) *Transaction {
|
|
regexp := regexp.MustCompile(`\s\s+`)
|
|
t := &Transaction{
|
|
Account: account,
|
|
Amount: regexp.ReplaceAllString(amount, " "),
|
|
Vendor: regexp.ReplaceAllString(vendor, " "),
|
|
Bank: bank,
|
|
Date: date,
|
|
}
|
|
t.ID = fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprint(t))))
|
|
return t
|
|
}
|
|
|
|
func cleanDate(date string) string {
|
|
regexp := regexp.MustCompile(`[A-Z][a-z]{2}, [0-9][0-9]? [A-Z][a-z]{2} 2[0-9]{3}`)
|
|
matches := regexp.FindAllString(date, -1)
|
|
if len(matches) < 1 {
|
|
return date
|
|
}
|
|
date = matches[0]
|
|
time, err := time.Parse(`Mon, 2 Jan 2006`, date)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return date
|
|
}
|
|
return time.Format("Mon Jan 2")
|
|
}
|