Skip to content

Commit

Permalink
Merge pull request #5 from mkobetic/fix-cached-balances
Browse files Browse the repository at this point in the history
Fix: balance caching broke format command
  • Loading branch information
mkobetic authored Nov 17, 2023
2 parents 8088cc1 + 77b0c39 commit 7d2c9e6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 19 deletions.
1 change: 0 additions & 1 deletion account.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ func (a *Account) CheckPostings() {
s.Balance,
s.Transaction.Location(),
)
s.Reconciled = true
} else {
s.Balance = a.balance.Copy()
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/coin/postings.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (ps postings) print(f io.Writer, opts *options) {
}
for i, s := range ps {
reconciled := ' '
if s.Reconciled {
if s.BalanceAsserted {
reconciled = '*'
}
args := []interface{}{
Expand Down Expand Up @@ -104,7 +104,7 @@ func (ps postings) printLong(f io.Writer, opts *options) {
}
for i, s := range ps {
reconciled := ' '
if s.Reconciled {
if s.BalanceAsserted {
reconciled = '*'
}
args := []interface{}{
Expand Down
3 changes: 0 additions & 3 deletions cmd/ofx2coin/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ func Test_ReadTransactions(t *testing.T) {
t.FailNow()
}
assert.Equal(t, len(txs), 10)
// We don't resolve the transactions,
// so set Reconciled on last transaction so that it writes the balance.
txs[9].Postings[1].Reconciled = true
for i, tx := range []string{
`2019/01/04 [CK]NO.272
Unbalanced 704.00 CAD
Expand Down
12 changes: 6 additions & 6 deletions posting.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
type Posting struct {
Note string

Transaction *Transaction
Account *Account
Quantity *Amount // posting amount
Balance *Amount // account balance as of this posting
Reconciled bool // was balance explicitly asserted in the ledger (only set after ResolveTransactions())
Transaction *Transaction
Account *Account
Quantity *Amount // posting amount
Balance *Amount // account balance as of this posting
BalanceAsserted bool // was balance explicitly asserted in the ledger

accountName string
}
Expand All @@ -27,7 +27,7 @@ func (s *Posting) Write(w io.Writer, accountOffset, accountWidth, amountWidth in
if err != nil {
return err
}
if s.Reconciled {
if s.BalanceAsserted {
if _, err = io.WriteString(w, " = "); err != nil {
return err
}
Expand Down
15 changes: 8 additions & 7 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func (transactions TransactionsByTime) Swap(i, j int) {
func (transactions TransactionsByTime) Less(i, j int) bool {
return transactions[i].Posted.Before(transactions[j].Posted) ||
(transactions[i].Posted.Equal(transactions[j].Posted) &&
!transactions[i].HasReconciledPosting() &&
transactions[j].HasReconciledPosting())
!transactions[i].HasBalanceAssertions() &&
transactions[j].HasBalanceAssertions())
}
func (transactions TransactionsByTime) FindEqual(t *Transaction) *Transaction {
for _, t2 := range transactions {
Expand Down Expand Up @@ -165,6 +165,7 @@ func (p *Parser) parseTransaction(fn string) (*Transaction, error) {
if err != nil {
return nil, err
}
s.BalanceAsserted = true
}
t.Postings = append(t.Postings, s)
}
Expand Down Expand Up @@ -201,8 +202,8 @@ func (t *Transaction) PostConversion(
toAmount *Amount,
toBalance *Amount,
) {
sFrom := &Posting{Account: from, Quantity: fromAmount, Balance: fromBalance}
sTo := &Posting{Account: to, Quantity: toAmount, Balance: toBalance}
sFrom := &Posting{Account: from, Quantity: fromAmount, Balance: fromBalance, BalanceAsserted: fromBalance != nil}
sTo := &Posting{Account: to, Quantity: toAmount, Balance: toBalance, BalanceAsserted: toBalance != nil}
if fromAmount.Sign() < 0 {
t.Postings = append(t.Postings, sTo, sFrom)
} else {
Expand All @@ -219,9 +220,9 @@ func (t *Transaction) Other(s *Posting) *Posting {
return nil
}

func (t *Transaction) HasReconciledPosting() bool {
func (t *Transaction) HasBalanceAssertions() bool {
for _, p := range t.Postings {
if p.Reconciled {
if p.BalanceAsserted {
return true
}
}
Expand Down Expand Up @@ -250,7 +251,7 @@ func (t *Transaction) MergeDuplicate(t2 *Transaction) {
for i, p := range t.Postings {
if p2 := t2.Postings[i]; p.Balance == nil && p2.Balance != nil {
p.Balance = p2.Balance
p.Reconciled = p2.Reconciled
p.BalanceAsserted = p2.BalanceAsserted
}
}
}

0 comments on commit 7d2c9e6

Please sign in to comment.