# * Costs

<
2011/01/01
    expenses:foreign currency       €100 @ $1.35
    assets

# ** 1. print a transaction with an explicit unit cost
$ hledger -f- print --explicit
2011-01-01
    expenses:foreign currency    €100 @ $1.35
    assets                           $-135.00

>=0

# ** 2. -B/--cost converts to the cost price's commodity
$ hledger -f- print --explicit --cost
2011-01-01
    expenses:foreign currency         $135.00
    assets                           $-135.00

>=0
<
2011/01/01
    expenses:foreign currency       €100 @ $1.35
    assets                              $-135.00

# ** 3. --infer-equity generates conversion postings,
# and with --verbose-tags, they will be visibly tagged.
$ hledger -f- print --infer-equity --verbose-tags
2011-01-01
    expenses:foreign currency    €100 @ $1.35
    equity:conversion:$-€:€             €-100  ; generated-posting: conversion
    equity:conversion:$-€:$           $135.00  ; generated-posting: conversion
    assets                           $-135.00

>=0

# ** 4. With --cost, --infer-equity is ignored
$ hledger -f- print --cost --infer-equity
2011-01-01
    expenses:foreign currency         $135.00
    assets                           $-135.00

>=0

<
2011/01/01
    expenses:foreign currency    €100 @@ $135
    assets

# ** 5. print a transaction with a total cost
$ hledger -f - print --explicit
2011-01-01
    expenses:foreign currency    €100 @@ $135
    assets                              $-135

>=0

# ** 6. when the balance has exactly two commodities, both without cost,
# infer an implicit balancing cost (conversion rate) for the first one in terms of the second.
<
2011/01/01
    expenses:foreign currency       €100
    misc                              $2.1
    assets                         $-135.00
    misc                              €1
    misc                             €-1
    misc                             $-2.1

$ hledger -f - print --explicit
2011-01-01
    expenses:foreign currency    €100 @ $1.35
    misc                                 $2.1
    assets                           $-135.00
    misc                           €1 @ $1.35
    misc                          €-1 @ $1.35
    misc                                $-2.1

>=0

# ** 7. another, from ledger tests. Just one posting to price so uses @@.
<
2002/09/30 * 1a1a6305d06ce4b284dba0d267c23f69d70c20be
    c56a21d23a6535184e7152ee138c28974f14280c  866.231000 GGGGG
    a35e82730cf91569c302b313780e5895f75a62b9   $-17,783.72

$ hledger -f - print --explicit
2002-09-30 * 1a1a6305d06ce4b284dba0d267c23f69d70c20be
    c56a21d23a6535184e7152ee138c28974f14280c    866.231000 GGGGG @@ $17,783.72
    a35e82730cf91569c302b313780e5895f75a62b9                       $-17,783.72

>=0

# ** 8. when the balance has more than two commodities, don't bother
<
2011/01/01
    expenses:foreign currency         €100
    assets                           $-135
    expenses:other                    £200
$ hledger -f - print
>2 /transaction is unbalanced/
>= !0

# ** 9. another
<
2011/01/01
    expenses:foreign currency        €99
    assets                         $-130
    expenses:foreign currency         €1
    assets                           $-5

$ hledger -f - balance -B
               $-135  assets
                $135  expenses:foreign currency
--------------------
                   0  
>=0

# ** 10. transaction in two commodities should balance out properly
<
2011/01/01 x
  a  10£ @@ 16$
  b

$ hledger -f - balance --cost
                 16$  a
                -16$  b
--------------------
                   0  
>=0

# ** 11. --value=cost,XXX is deprecated, but should still work (for now)
$ hledger -f - balance --value=cost,XXX
                 16$  a
                -16$  b
--------------------
                   0  
>=0

# ** 12. conversion postings should be generated when called --infer-equity
$ hledger -f - balance --infer-equity
                 10£  a
                -16$  b
                 16$  equity:conversion:$-£:$
                -10£  equity:conversion:$-£:£
--------------------
                   0  
>=0

# ** 13. transaction should be left unbalanced when called without --cost or --infer-equity
$ hledger -f - balance
                 10£  a
                -16$  b
--------------------
                -16$
                 10£  
>=0

# ** 14. The balancing cost should still be inferred properly, with proper sign,
#     if the first amount is negative
<
2011/01/01 x
  a  -10£
  b  16$

$ hledger -f - balance -N
                -10£  a
                 16$  b
>=0

# ** 15. Should not infer balancing costs when --strict is specified
$ hledger -f - balance --strict
>2 /transaction is unbalanced/
>=1

<
2011/01/01 x
  a  -10£
  b  -16$

# ** 16. A balancing cost can not be inferred when BOTH amounts are negative
$ hledger -f - balance
>2 /transaction is unbalanced/
>=1

# ** 17. Differently-priced lots of a commodity should be merged in balance report
<
2011/1/1
  (a)   £1 @ $2

2011/1/1
  (a)   £1 @ $3

$ hledger -f - balance
                  £2  a
--------------------
                  £2  
>=0

# ** 18. this should balance
<
2011/1/1
    a  1h @ $10
    b  1h @ $20
    c  $-30

$ hledger -f - balance --no-total
                  1h  a
                  1h  b
                $-30  c
>= 0

# ** 19. these balance because of the unit costs, and should parse successfully
<
1/1
    a   1X @ 2Y
    a  -2X @ 1Y

$ hledger -f - balance --no-total
                 -1X  a
>= 0

# ** 20.
$ hledger -f - balance --no-total -B
>= 0

# ** 21. likewise with total costs. Note how the primary amount's sign is used.
<
1/1
    a   1X @@ 1Y
    a  -2X @@ 1Y

$ hledger -f - balance --no-total
                 -1X  a
>= 0

# ** 22.
$ hledger -f - balance --no-total -B
>= 0

# In fact, here is how sign works with costs currently.
# See discussion at https://github.com/simonmichael/hledger/issues/1870
<
2022-01-01 Positive Unit cost
    a        A 1
    b        B -1 @ A 1

2022-01-01 Positive Total cost
    a        A 1
    b        B -1 @@ A 1

2022-01-02 Negative unit cost
    a        A 1
    b        B 1 @ A -1

2022-01-02 Negative total cost
    a        A 1
    b        B 1 @@ A -1

2022-01-03 Double Negative unit cost
    a        A -1
    b        B -1 @ A -1

2022-01-03 Double Negative total cost
    a        A -1
    b        B -1 @@ A -1

# ** 23. All these transactions are considered balanced
$ hledger -f- print -x
2022-01-01 Positive Unit cost
    a             A 1
    b      B -1 @ A 1

2022-01-01 Positive Total cost
    a             A 1
    b     B -1 @@ A 1

2022-01-02 Negative unit cost
    a             A 1
    b      B 1 @ A -1

2022-01-02 Negative total cost
    a             A 1
    b     B 1 @@ A -1

2022-01-03 Double Negative unit cost
    a            A -1
    b     B -1 @ A -1

2022-01-03 Double Negative total cost
    a            A -1
    b    B -1 @@ A -1

>=

# ** 24. Here they are converted to cost
$ hledger -f- print -xB
2022-01-01 Positive Unit cost
    a             A 1
    b            A -1

2022-01-01 Positive Total cost
    a             A 1
    b            A -1

2022-01-02 Negative unit cost
    a             A 1
    b            A -1

2022-01-02 Negative total cost
    a             A 1
    b            A -1

2022-01-03 Double Negative unit cost
    a            A -1
    b             A 1

2022-01-03 Double Negative total cost
    a            A -1
    b             A 1

>=

# ** 25. Here are the market prices inferred, since 1.26:
$ hledger -f- --infer-market-prices prices
P 2022-01-01 B A 1
P 2022-01-01 B A 1
P 2022-01-02 B A -1
P 2022-01-02 B A -1
P 2022-01-03 B A -1
P 2022-01-03 B A -1

# ** 26. here, a's primary amount is 0, and its cost is 1Y; b is the assigned auto-balancing amount of -1Y (per issue 69)
<
1/1
    a   1X @@ 1Y
    a   1X @@ 1Y
    a  -2X @@ 1Y
    b

$ hledger -f - balance --no-total -E
                   0  a
                 -1Y  b
>= 0

# ** 27. Without -E, a should be hidden because its balance is zero, even though it has a non-zero cost.
$ hledger -f - balance --no-total
                 -1Y  b
>= 0

# ** 28. the above with -B
$ hledger -f - balance --no-total -E -B
                  1Y  a
                 -1Y  b
>= 0

# ** 29. The equity account used by --infer-equity can be customised
<
account  equity:trades   ; type:V

2011/01/01
    expenses:foreign currency       €100 @ $1.35
    assets

$ hledger -f- print --infer-equity
2011-01-01
    expenses:foreign currency    €100 @ $1.35
    equity:trades:$-€:€                 €-100
    equity:trades:$-€:$               $135.00
    assets

>=0

# ** 30. Inferred equity postings are generated early enough to match filters
<
2011/01/01
    expenses:foreign currency       €100 @ $1.35
    assets

$ hledger -f- areg --infer-equity equity:conversion
Transactions in equity:conversion and subaccounts:
2011-01-01                      ex:foreign currenc..       $135.00       $135.00
                                                             €-100         €-100
>=0

# ** 31. Infer cost with first matching posting when equity postings are present
<
2011/01/01
    expenses:foreign currency       €100
    equity:conversion              €-100
    equity:conversion               $135
    assets                         $-135

$ hledger -f- print --infer-costs
2011-01-01
    expenses:foreign currency    €100 @@ $135
    equity:conversion                   €-100
    equity:conversion                    $135
    assets                              $-135

>=0

# ** 32. Infer cost and convert to cost when equity postings are present
$ hledger -f- print --infer-costs --cost
2011-01-01
    expenses:foreign currency            $135
    assets                              $-135

>=0

# ** 33. Do not infer equity postings when they are specified manually
$ hledger -f- print --infer-equity --infer-costs
2011-01-01
    expenses:foreign currency    €100 @@ $135
    equity:conversion                   €-100
    equity:conversion                    $135
    assets                              $-135

>=0

# ** 34. Inferred equity postings with non-standard conversion account
<
account  whoopwhoop   ; type:V

2011/01/01
    expenses:foreign currency            €100
    whoopwhoop                          €-100
    whoopwhoop                           $135
    assets                              $-135

$ hledger -f- print --infer-costs
2011-01-01
    expenses:foreign currency    €100 @@ $135
    whoopwhoop                          €-100
    whoopwhoop                           $135
    assets                              $-135

>=0

# ** 35. Can manually make another posting match
<
2011/01/01
    assets                              $-135
    equity:conversion                   €-100
    equity:conversion                    $135
    expenses:foreign currency    €100 @@ $135

$ hledger -f- print --infer-costs
2011-01-01
    assets                              $-135
    equity:conversion                   €-100
    equity:conversion                    $135
    expenses:foreign currency    €100 @@ $135

>=0

# ** 36. Can manually match lots of different posting groups so long as there is no conflict
<
2011/01/01
    expenses:food                 €110 @@ £80
    expenses:foreign currency    €100 @@ $135
    equity:conversion                   €-100
    equity:conversion                    $135
    equity:trades                       €-110
    equity:trades                         £80
    assets                              $-135
    assets                               £-80

$ hledger -f- print --show-costs --infer-costs
2011-01-01
    expenses:food                 €110 @@ £80
    expenses:foreign currency    €100 @@ $135
    equity:conversion                   €-100
    equity:conversion                    $135
    equity:trades                       €-110
    equity:trades                         £80
    assets                              $-135
    assets                               £-80

>=0

# ** 37. And convert to cost
$ hledger -f- print --infer-costs --cost
2011-01-01
    expenses:food                         £80
    expenses:foreign currency            $135
    assets                              $-135
    assets                               £-80

>=0

# ** 38. Transaction posts and equity conversion postings are fine
<
2011/01/01
    expenses:foreign currency    €100 @ $1.35
    equity:conversion                   €-100
    equity:conversion                    $135
    assets                              $-135

$ hledger -f- print --infer-costs
2011-01-01
    expenses:foreign currency    €100 @ $1.35
    equity:conversion                   €-100
    equity:conversion                    $135
    assets                              $-135

>=0

# We now tend to just skip cost inference if it doesn't succeed. (#2045)

# # 39. If a conversion pair matches several postings it should throw an error
# <
# 2011/01/01
#     expenses:foreign currency    €100 @@ $135
#     expenses:foreign trades      €100 @@ $135
#     equity:conversion                   €-100
#     equity:conversion                    $135
#     assets                              €-100
#     assets                              $-135

# $ hledger -f- print --infer-costs
# >2 /There is not a unique posting which matches the conversion posting pair/
# >=1

# # 40. If a conversion pair does not match it should throw an error
# <
# 2011/01/01
#     expenses:foreign currency            €120
#     equity:conversion                   €-100
#     equity:conversion                    $135
#     assets:extra                          $20
#     assets                               €-20
#     assets                              $-155

# $ hledger -f- print --infer-costs
# >2 /There is not a unique posting which matches the conversion posting pair/
# >=1

# # 41. Multiple conversion pairs which match a single posting should cause an
# # error, and should not match both ‘sides’ of the conversion
# <
# 2011/01/01
#     expenses:foreign currency            €100
#     equity:conversion                   €-100
#     equity:conversion                    $135
#     equity:conversion                   €-100
#     equity:conversion                    $135
#     assets                              $-270
#     assets                               €100

# $ hledger -f- print --infer-costs
# >2 /There is not a unique posting which matches the conversion posting pair/
# >=1

# ** 39. A multicommodity transaction with conversion postings that we can't
# automatically match up with the corresponding other postings should be ignored,
# not an error. (#2045)
<
2023-01-01
    Expenses:Gift                            HKD 118.00
    Expenses:Personal                        HKD 118.00
    Equity:Trading:Currency:INR-HKD:HKD     HKD -236.00
    Equity:Trading:Currency:INR-HKD:INR       ₹2,150.77
    Liabilities:Credit-Card                  ₹-2,150.77

$ hledger -f- print
2023-01-01
    Expenses:Gift                            HKD 118.00
    Expenses:Personal                        HKD 118.00
    Equity:Trading:Currency:INR-HKD:HKD     HKD -236.00
    Equity:Trading:Currency:INR-HKD:INR       ₹2,150.77
    Liabilities:Credit-Card                  ₹-2,150.77

>=

# ** 40. We can combine ‘other’ amounts into one posting, if they still match up.
<
2011-01-01
    expenses:foreign currency    €100 @ $1.35
    expenses:foreign currency    £100 @ $1.36
    expenses:foreign currency  ¥1000 @@ €8.00
    equity:conversion                   €-100
    equity:conversion                    $135
    equity:conversion                   £-100
    equity:conversion                    $136
    equity:conversion                  ¥-1000
    equity:conversion                   €8.00
    assets                              $-271
    assets                             €-8.00

$ hledger -f- print --infer-costs
2011-01-01
    expenses:foreign currency      €100 @ $1.35
    expenses:foreign currency      £100 @ $1.36
    expenses:foreign currency    ¥1000 @@ €8.00
    equity:conversion                     €-100
    equity:conversion                      $135
    equity:conversion                     £-100
    equity:conversion                      $136
    equity:conversion                    ¥-1000
    equity:conversion                     €8.00
    assets                                $-271
    assets                               €-8.00

>=0

# ** 41. Matching costful postings and conversion postings is done at the precision
# of the conversion posting, tolerating smaller decimal differences (#2041).
# Here 84.01 * 2.495 GEL is 209.60495 GEL, which is considered to match 209.60 GEL
# because we use the latter's 2 digit precision.
<
2023/05/17 * Transfer
    Assets:BOG:Personal                          -84.01 USD @ 2.495 GEL
    Equity:Conversion                             84.01 USD
    Equity:Conversion                           -209.60 GEL
    Assets:BOG:Personal                          209.60 GEL

$ hledger -f- print
2023-05-17 * Transfer
    Assets:BOG:Personal    -84.01 USD @ 2.495 GEL
    Equity:Conversion                   84.01 USD
    Equity:Conversion                 -209.60 GEL
    Assets:BOG:Personal                209.60 GEL

>=
