# * auto posting rules

<
= ^income
  (liabilities:tax)  *.33  ; income tax

2016/1/1 paycheck
    income:remuneration     $-100
    income:donations         $-15
    assets:bank

2016/1/1 withdraw
    assets:cash             $20
    assets:bank

# ** 1. print. Auto-generated postings are inserted below the matched one.
# With --verbose-tags, informative tags will also be added.
$ hledger print -f- --auto --verbose-tags
2016-01-01 paycheck  ; modified:
    income:remuneration           $-100
    (liabilities:tax)              $-33  ; income tax, generated-posting: = ^income
    income:donations               $-15
    (liabilities:tax)            $-4.95  ; income tax, generated-posting: = ^income
    assets:bank

2016-01-01 withdraw
    assets:cash             $20
    assets:bank

>=

# ** 2. register
$ hledger register -f- --auto
2016-01-01 paycheck             income:remuneration          $-100         $-100
                                (liabilities:tax)             $-33         $-133
                                income:donations              $-15         $-148
                                (liabilities:tax)              $-5         $-153
                                assets:bank                   $115          $-38
2016-01-01 withdraw             assets:cash                    $20          $-18
                                assets:bank                   $-20          $-38
>=

# ** 3. balance
$ hledger balance -f- --auto --tree
                $115  assets
                 $95    bank
                 $20    cash
               $-115  income
                $-15    donations
               $-100    remuneration
                $-38  liabilities:tax
--------------------
                $-38  
>=

# Balance assertions see postings generated by transaction modifier rules.
<
= trigger
  (target)   10

2018/1/1
  (trigger)   1
  (target)    1    = 11  ; this assertion would not fail, auto posting will be taken into account

# ** 4.
$ hledger register -f- --auto
2018-01-01                      (trigger)                        1             1
                                (target)                        10            11
                                (target)                         1            12
>=

# Transaction modifier adding (balanced virtual) budget envelope postings.
<
= ^expenses:groceries
    [budget:groceries]                           *-1
    [assets:bank:checking]                        *1

2018/10/7 * MARKET
    expenses:groceries:food
    assets:bank:checking                         $-20

# ** 5.
$ hledger print -f- --auto
2018-10-07 * MARKET
    expenses:groceries:food
    [budget:groceries]                 $-20
    [assets:bank:checking]              $20
    assets:bank:checking               $-20

>=

# Rules will preserve a transaction price from the original amount.
<
= assets:billable:client1
  assets:receivable:client1       *100
  revenues:client1

2018/1/1
    (assets:billable:client1)         0.50h @ $90

# ** 6.
$ hledger -f- print --auto -x
2018-01-01
    (assets:billable:client1)     0.50h @ $90
    assets:receivable:client1    50.00h @ $90
    revenues:client1                   $-4500

>=

# Rules can set a new commodity in the generated amounts.
<
= assets:billable:client1
  assets:receivable:client1       *$100
  revenues:client1

2018/1/1
    (assets:billable:client1)         0.50h

# ** 7.
$ hledger -f- print --auto -x
2018-01-01
    (assets:billable:client1)           0.50h
    assets:receivable:client1             $50
    revenues:client1                     $-50

>=

# And/or a new transaction price.
<
= assets:billable:client1
  assets:receivable:client1       *1.00 "Client1 Hours" @ $100.00
  revenues:client1

2018/1/1
    (assets:billable:client1)         0.50h @ $90

# ** 8.
$ hledger -f- print --auto -x
2018-01-01
    (assets:billable:client1)                       0.50h @ $90
    assets:receivable:client1    0.50 "Client1 Hours" @ $100.00
    revenues:client1                                    $-50.00

>=

# Balance assignments may not be used with accounts also affected by transaction modifier rules.
<
= ^expenses:foo
    budget:available   *-1
    assets:checking     *1

2018/10/17 * INITIAL
    budget:available   $100
    equity:opening

2018/10/17 * SOME EXPENSE
    expenses:foo                                 $50
    assets:checking

2018/10/17 * ASSERT
    budget:other
    budget:available                              =$0

# ** 9.
$ hledger print -f- --auto
>2 /Balance assignments and auto postings may not be combined/
>=1


# Issue #928:
# Generating auto postings from a unit-priced amount.
<
= ^Expenses:Joint
    Expenses:Joint                                               *-1
    Liabilities:Joint:Bob                                       *0.5
    Liabilities:Joint:Bill                                      *0.5
    
2018/01/01
    Expenses:Joint:Widgets                                   $100.00 @ £0.50
    Assets:Joint:Bank                                        -£50.00

# ** 10.
$ hledger -f- print --auto --verbose-tags
2018-01-01  ; modified:
    Expenses:Joint:Widgets     $100.00 @ £0.50
    Expenses:Joint            $-100.00 @ £0.50  ; generated-posting: = ^Expenses:Joint
    Liabilities:Joint:Bob       $50.00 @ £0.50  ; generated-posting: = ^Expenses:Joint
    Liabilities:Joint:Bill      $50.00 @ £0.50  ; generated-posting: = ^Expenses:Joint
    Assets:Joint:Bank                  £-50.00

>=0

# Generating auto postings from a total-priced amount.
<
= ^Expenses:Joint
    Expenses:Joint                                               *-1
    Liabilities:Joint:Bob                                       *0.5
    Liabilities:Joint:Bill                                      *0.5
    
2018/01/01
    Expenses:Joint:Widgets                                   $100.00 @@ £50
    Assets:Joint:Bank                                        -£50.00

# ** 11.
$ hledger -f- print --auto
2018-01-01
    Expenses:Joint:Widgets     $100.00 @@ £50
    Expenses:Joint            $-100.00 @@ £50
    Liabilities:Joint:Bob       $50.00 @@ £25
    Liabilities:Joint:Bill      $50.00 @@ £25
    Assets:Joint:Bank                 £-50.00

>=0
# alternate approach: convert to unit prices
#2018/01/01
#    Expenses:Joint:Widgets     $100.00 @@ £50
#    Expenses:Joint            $-100.00 @ £0.5
#    Liabilities:Joint:Bob       $50.00 @ £0.5
#    Liabilities:Joint:Bill      $50.00 @ £0.5
#    Assets:Joint:Bank                 £-50.00

# Generating auto postings from an implicitly-priced amount. Should work like the above.
<
= ^Expenses:Joint
    Expenses:Joint                                               *-1
    Liabilities:Joint:Bob                                       *0.5
    Liabilities:Joint:Bill                                      *0.5

2018/01/01
    Expenses:Joint:Widgets                                   $100.00
    Assets:Joint:Bank                                        -£50.00

# ** 12.
$ hledger -f- print --auto
2018-01-01
    Expenses:Joint:Widgets               $100.00
    Expenses:Joint            $-100.00 @@ £50.00
    Liabilities:Joint:Bob       $50.00 @@ £25.00
    Liabilities:Joint:Bill      $50.00 @@ £25.00
    Assets:Joint:Bank                    £-50.00

>=0

#


## Transaction modifiers affect forecast transactions (#959)
<
= ^income
  (liabilities:tax)  *.33  ; income tax

~ monthly from 2016-01   paycheck
    income:remuneration     $-100
    income:donations         $-15
    assets:bank

2016/1/3 withdraw
    assets:cash             $20
    assets:bank

# ** 13.
$ hledger print -f- --auto --forecast -b 2016-01 -e 2016-03
2016-01-03 withdraw
    assets:cash             $20
    assets:bank

2016-02-01 paycheck
    income:remuneration           $-100
    (liabilities:tax)              $-33  ; income tax
    income:donations               $-15
    (liabilities:tax)            $-4.95  ; income tax
    assets:bank

>=

# ** 14. and they don't force --auto on
$ hledger print -f- --forecast -b 2016-01 -e 2016-03
2016-01-03 withdraw
    assets:cash             $20
    assets:bank

2016-02-01 paycheck
    income:remuneration           $-100
    income:donations               $-15
    assets:bank

>=

# #893a
<
= ^expenses:groceries
    [budget:groceries]                           *-1
    [assets:bank:checking]                        *1

2020/10/7 * MARKET
    expenses:groceries:food
    assets:bank:checking                         $-20

# ** 15.
$ hledger -f- register --auto
2020-10-07 MARKET               ex:groceries:food              $20           $20
                                [budget:groceries]            $-20             0
                                [as:bank:checking]             $20           $20
                                assets:bank:checking          $-20             0
>=

# #893b, https://github.com/simonmichael/hledger/issues/893#issuecomment-459125266
<
= tag:tax20
  taxes  *0.2

2018/12/18
  a    EUR -10.00  ; :tax20:
  b    EUR 12.00

# ** 16. Transaction balancing sees auto postings ?
# $ hledger -f- print -x --auto
# 2018-12-18
#     a          EUR -10.00  ; :tax20:
#     taxes       EUR -2.00
#     b           EUR 12.00
#
# >=

# ** 16. No, transaction must be balanced both with and without auto postings.
$ hledger -f- print -x --auto
>2 /unbalanced/
>=1

# ** 17. Commodity display styles are applied to auto posting amounts.
<
2020-01-20
  (a)    1 A
; add an auto posting with decimal places
=
  (b)   *1.5 B
; show B amounts with no decimal places
commodity 0. B

$ hledger -f- print --auto
2020-01-20
    (a)             1 A
    (b)           1.5 B

>=0

# ** 18. Auto-generated postings with currency matching only matches amounts with that currency in an auto posting (#1582)
<
= assets cur:USD
  (b:USD)  *1

= assets cur:EUR
  (b:USD)  5 USD

2017-12-14
  revenue:job  -10 EUR
  revenue:job  -10 USD
  assets

$ hledger -f- print --auto
2017-12-14
    revenue:job         -10 EUR
    revenue:job         -10 USD
    assets
    (b:USD)               5 USD
    (b:USD)              10 USD

>=0

# ** 19. Auto-generated postings match only the amounts which match amount query.
<
= assets amt:>50
  (b)  *1

2017-12-14
  revenue:job   -10 USD
  revenue:job  -100 EUR
  assets

$ hledger -f- print --auto
2017-12-14
    revenue:job         -10 USD
    revenue:job        -100 EUR
    assets
    (b)                 100 EUR

>=0

# ** 20. Auto-generated postings apply on auto-balanced periodic postings.
<
2021-01-01   Fixed
    Checking            -10
    Costs

~ 2021-01-02   Periodic
    Checking            -10
    Costs

= acct:Costs
    (Auto)     *1

$ hledger -f- print --forecast --auto --explicit
2021-01-01 Fixed
    Checking             -10
    Costs                 10
    (Auto)                10

2021-01-02 Periodic
    Checking             -10
    Costs                 10
    (Auto)                10

>=0

# ** 21. Redundant costs are still detected properly with --auto. (#2110)
<
2023-01-01
    assets:shares        1 TEST @ 50.00€
    equity:conversion   -1 TEST
    equity:conversion   50.00€
    equity:opening     -50.00€

= foo
    a   1

$ hledger -f- --auto check
