Browse Source

Updated ability to include comments in ledger

bmallred 9 years ago
parent
commit
1dbe0e7954
3 changed files with 20 additions and 10 deletions
  1. 11 5
      account.go
  2. 6 3
      commandList.go
  3. 3 2
      transaction.go

+ 11 - 5
account.go

15
15
16
// Convert from a string to an account
16
// Convert from a string to an account
17
func (a *Account) FromString(text string) error {
17
func (a *Account) FromString(text string) error {
18
	fields := strings.Split(text, "\t")
18
	parts := strings.Split(text, "\t")
19
	fields := []string{}
20
	for _, p := range parts {
21
		if p != "" {
22
			fields = append(fields, p)
23
		}
24
	}
19
25
20
	if len(fields) != 3 {
26
	if len(fields) != 2 {
21
		return errors.New("Invalid account format")
27
		return errors.New("Invalid account format")
22
	}
28
	}
23
29
24
	debit := true
30
	debit := true
25
	if strings.HasPrefix(fields[2], "-") {
31
	if strings.HasPrefix(fields[1], "-") {
26
		debit = false
32
		debit = false
27
	}
33
	}
28
34
29
	a.Debit = debit
35
	a.Debit = debit
30
	a.Name = fields[1]
36
	a.Name = fields[0]
31
	a.Amount = new(big.Rat)
37
	a.Amount = new(big.Rat)
32
	a.Amount.SetString(fields[2][1:])
38
	a.Amount.SetString(fields[1][1:])
33
39
34
	return nil
40
	return nil
35
}
41
}

+ 6 - 3
commandList.go

5
	"bytes"
5
	"bytes"
6
	"fmt"
6
	"fmt"
7
	"os"
7
	"os"
8
	"strings"
8
9
9
	"github.com/codegangsta/cli"
10
	"github.com/codegangsta/cli"
10
)
11
)
53
	for scanner.Scan() {
54
	for scanner.Scan() {
54
		text := scanner.Text()
55
		text := scanner.Text()
55
56
56
		t := Transaction{}
57
		t.FromString(text)
58
		l.Transactions = append(l.Transactions, t)
57
		if strings.Index(text, "#") != 0 {
58
			t := Transaction{}
59
			t.FromString(text)
60
			l.Transactions = append(l.Transactions, t)
61
		}
59
	}
62
	}
60
63
61
	fmt.Print(l.ToString())
64
	fmt.Print(l.ToString())

+ 3 - 2
transaction.go

70
		}
70
		}
71
	}
71
	}
72
72
73
	if balance.FloatString(2) != "0.00" {
74
		return errors.New("Transaction does not balance")
73
	b := balance.FloatString(2)
74
	if b != "0.00" {
75
		return errors.New(fmt.Sprintf("Transaction does not balance: %s", b))
75
	}
76
	}
76
77
77
	return nil
78
	return nil