| 1 | package domain |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "text/tabwriter" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | // AccountTransactions represents a printable version of a slice of |
| 11 | // account transactions |
| 12 | type AccountTransactions []AccountTransaction |
| 13 | |
| 14 | func (at AccountTransactions) String() string { |
| 15 | var buf bytes.Buffer |
| 16 | buf.WriteString("\n") |
| 17 | if len(at) != 0 { |
| 18 | first := at[0] |
| 19 | fmt.Fprintf( |
| 20 | &buf, "Transactions for account %s/%s\n", |
| 21 | first.Account.BankID, first.Account.AccountID, |
| 22 | ) |
| 23 | fmt.Fprintf( |
| 24 | &buf, "Balance at %s: %.2f %s\n", |
| 25 | first.AccountBalanceBefore.TransmissionDate.Format("2006-01-02"), |
| 26 | first.AccountBalanceBefore.Amount.Amount, |
| 27 | first.AccountBalanceBefore.Amount.Currency, |
| 28 | ) |
| 29 | } |
| 30 | buf.WriteString("BookingDate\tBooking Text\tAmount\tBankID\tAccountID\tName\tPurpose") |
| 31 | buf.WriteString("\n") |
| 32 | for _, a := range at { |
| 33 | buf.WriteString(a.BookingDate.Format("2006-01-02")) |
| 34 | buf.WriteString("\t") |
| 35 | buf.WriteString(a.BookingText) |
| 36 | buf.WriteString("\t") |
| 37 | buf.WriteString(fmt.Sprintf("%.2f %s", a.Amount.Amount, a.Amount.Currency)) |
| 38 | buf.WriteString("\t") |
| 39 | buf.WriteString(a.BankID) |
| 40 | buf.WriteString("\t") |
| 41 | buf.WriteString(a.AccountID) |
| 42 | buf.WriteString("\t") |
| 43 | buf.WriteString(a.Name) |
| 44 | buf.WriteString("\t") |
| 45 | buf.WriteString(a.Purpose) |
| 46 | buf.WriteString("\n") |
| 47 | } |
| 48 | if len(at) != 0 { |
| 49 | last := at[len(at)-1] |
| 50 | fmt.Fprintf( |
| 51 | &buf, "Balance at %s: %.2f %s\n", |
| 52 | last.AccountBalanceAfter.TransmissionDate.Format("2006-01-02"), |
| 53 | last.AccountBalanceAfter.Amount.Amount, |
| 54 | last.AccountBalanceAfter.Amount.Currency, |
| 55 | ) |
| 56 | } |
| 57 | var out bytes.Buffer |
| 58 | tabw := tabwriter.NewWriter(&out, 24, 1, 0, ' ', tabwriter.TabIndent) |
| 59 | fmt.Fprint(tabw, buf.String()) |
| 60 | tabw.Flush() |
| 61 | return out.String() |
| 62 | } |
| 63 | |
| 64 | // AccountTransaction represents one transaction entry for a given account |
| 65 | type AccountTransaction struct { |
| 66 | Account AccountConnection |
| 67 | Amount Amount |
| 68 | ValutaDate time.Time |
| 69 | BookingDate time.Time |
| 70 | BookingText string |
| 71 | BankID string |
| 72 | AccountID string |
| 73 | Name string |
| 74 | Purpose string |
| 75 | Purpose2 string |
| 76 | TransactionID int |
| 77 | AccountBalanceBefore Balance |
| 78 | AccountBalanceAfter Balance |
| 79 | } |
| 80 | |
| 81 | func (a AccountTransaction) String() string { |
| 82 | var buf bytes.Buffer |
| 83 | buf.WriteString("\n") |
| 84 | buf.WriteString("BookingDate\tAmount\tBankID\tAccountID\tPurpose") |
| 85 | buf.WriteString("\n") |
| 86 | buf.WriteString(a.BookingDate.Format("2006-01-02")) |
| 87 | buf.WriteString("\t") |
| 88 | buf.WriteString(fmt.Sprintf("%.2f %s", a.Amount.Amount, a.Amount.Currency)) |
| 89 | buf.WriteString("\t") |
| 90 | buf.WriteString(a.BankID) |
| 91 | buf.WriteString("\t") |
| 92 | buf.WriteString(a.AccountID) |
| 93 | buf.WriteString("\t") |
| 94 | buf.WriteString(a.Purpose) |
| 95 | var out bytes.Buffer |
| 96 | tabw := tabwriter.NewWriter(&out, 0, 8, 0, '\t', 0) |
| 97 | fmt.Fprint(tabw, buf.String()) |
| 98 | tabw.Flush() |
| 99 | return out.String() |
| 100 | } |