-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.go
95 lines (78 loc) · 1.72 KB
/
transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"errors"
"fmt"
"math/big"
"strings"
"time"
)
type Transaction struct {
Date time.Time
Project string
Description string
Accounts []Account
}
// Create a transaction from a string
func (t *Transaction) FromString(text string) {
// Parse the lines of text
lines := strings.Split(text, "\n")
for i, line := range lines {
if len(line) == 0 {
continue
}
switch i {
case 0:
fields := parseTabs(text)
date, err := parseDate(fields[0])
check(err)
project := fields[1]
description := ""
if len(fields) > 2 {
description = strings.Join(fields[2:], " ")
}
t.Date = date
t.Project = project
t.Description = description
t.Accounts = []Account{}
break
default:
var a Account
err := a.FromString(line)
check(err)
t.Accounts = append(t.Accounts, a)
break
}
}
}
// Check the transaction to ensure it is balanced
func (t *Transaction) CheckBalance() error {
if len(t.Accounts) == 0 {
return errors.New("Transaction does not have any accounts")
}
// Check that they balance
balance := new(big.Rat)
for _, a := range t.Accounts {
if a.Debit {
balance.Add(balance, a.Amount)
} else {
balance.Sub(balance, a.Amount)
}
}
b := balance.FloatString(2)
if b != "0.00" {
return errors.New(fmt.Sprintf("Transaction does not balance: %s", b))
}
return nil
}
// Convert a transaction to string format
func (t *Transaction) ToString() string {
accounts := ""
for _, account := range t.Accounts {
accounts += account.ToString()
}
err := t.CheckBalance()
if err != nil {
accounts += fmt.Sprintf("Error: %s\n", err)
}
return fmt.Sprintf("%s\t%s\t%s\n%s\n", t.Date.Format("2006-01-02"), t.Project, t.Description, accounts)
}