From 77b0c395f6cb9398961ac311aa5cfd441ddf644c Mon Sep 17 00:00:00 2001
From: Martin Kobetic <magh@kobetic.ca>
Date: Thu, 16 Nov 2023 21:51:26 -0500
Subject: [PATCH] Fix: balance caching broke format command

---
 account.go                |  1 -
 cmd/coin/postings.go      |  4 ++--
 cmd/ofx2coin/main_test.go |  3 ---
 posting.go                | 12 ++++++------
 transaction.go            | 15 ++++++++-------
 5 files changed, 16 insertions(+), 19 deletions(-)

diff --git a/account.go b/account.go
index 832b20e..51d47c8 100644
--- a/account.go
+++ b/account.go
@@ -188,7 +188,6 @@ func (a *Account) CheckPostings() {
 				s.Balance,
 				s.Transaction.Location(),
 			)
-			s.Reconciled = true
 		} else {
 			s.Balance = a.balance.Copy()
 		}
diff --git a/cmd/coin/postings.go b/cmd/coin/postings.go
index 67a8871..e570323 100644
--- a/cmd/coin/postings.go
+++ b/cmd/coin/postings.go
@@ -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{}{
@@ -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{}{
diff --git a/cmd/ofx2coin/main_test.go b/cmd/ofx2coin/main_test.go
index f95cb1c..3dd9956 100644
--- a/cmd/ofx2coin/main_test.go
+++ b/cmd/ofx2coin/main_test.go
@@ -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
diff --git a/posting.go b/posting.go
index 1f52877..5ec4e71 100644
--- a/posting.go
+++ b/posting.go
@@ -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
 }
@@ -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
 		}
diff --git a/transaction.go b/transaction.go
index d6c9b83..a19080e 100644
--- a/transaction.go
+++ b/transaction.go
@@ -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 {
@@ -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)
 	}
@@ -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 {
@@ -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
 		}
 	}
@@ -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
 		}
 	}
 }