Repository: ledger/ledger (language: cpp)

Issue:
Using commodity equivelancy fails to balance in some cases
When I run the command `ledger -f ledger.dat --price-db price-db bal` with the files

``` ledger.dat
2022/10/01 Sell
  Wallet     -1 BTC
  Bank       $20000
```

``` price-db
C 1.00000000 BTC = 100000000 sat
```

I get the error:

```
While parsing file "/tmp/ledger-commodity-conversion/ledger.dat", line 3:
While balancing transaction from "/tmp/ledger-commodity-conversion/ledger.dat", lines 1-3:
> 2022/10/01 Sell
>   Wallet     -1 BTC
>   Bank       $20000
Unbalanced remainder is:
              $20000
      -100000000 sat
Amount to balance against:
              $20000
Error: Transaction does not balance
```

However if I use the following `ledger.dat` then the command works.

``` ledger.dat
2022/10/01 Sell
  Bank       $20000
  Wallet     -1 BTC
```

I get the following output:

```
              $20000  Bank
     -1.00000000 BTC  Wallet
--------------------
              $20000
     -1.00000000 BTC
```

I would expect the commodity could be entered in any row and ledger could balance the report.




Discussion / hints:

Fix patch:
diff --git a/src/xact.cc b/src/xact.cc
index 749eb3e5f..0c7f65a1b 100644
--- a/src/xact.cc
+++ b/src/xact.cc
@@ -265,7 +265,7 @@ bool xact_base_t::finalize()
         DEBUG("xact.finalize", "per_unit_cost = " << per_unit_cost);
 
         foreach (post_t * post, posts) {
-          const amount_t& amt(post->amount);
+          const amount_t& amt(post->amount.reduced());
 
           if (post->must_balance() && amt.commodity() == comm) {
             balance -= amt;


Test patch:
diff --git a/test/regress/commodity_equivalency.test b/test/regress/commodity_equivalency.test
new file mode 100644
index 000000000..400ab2bf4
--- /dev/null
+++ b/test/regress/commodity_equivalency.test
@@ -0,0 +1,13 @@
+C 1.00000000 BTC = 100000000 sat
+
+2022/10/01 Sell
+    Bank                                                $20000
+    Wallet                                                  -1 BTC
+
+test bal
+              $20000  Bank
+     -1.00000000 BTC  Wallet
+--------------------
+              $20000
+     -1.00000000 BTC
+end test
diff --git a/test/regress/commodity_equivalency_2.test b/test/regress/commodity_equivalency_2.test
new file mode 100644
index 000000000..a5d2de74c
--- /dev/null
+++ b/test/regress/commodity_equivalency_2.test
@@ -0,0 +1,13 @@
+C 1.00000000 BTC = 100000000 sat
+
+2022/10/01 Sell
+    Wallet                                                  -1 BTC
+    Bank                                                $20000
+
+test bal
+              $20000  Bank
+     -1.00000000 BTC  Wallet
+--------------------
+              $20000
+     -1.00000000 BTC
+end test