# * rewrite command

# ** 1. Add proportional income tax (from documentation)
# With --verbose-tags, an informative extra tag is added.
<
2016/1/1 paycheck
    income:remuneration     $-100
    income:donations         $-15
    assets:bank

2016/1/1 withdraw
    assets:cash             $20
    assets:bank
$ hledger rewrite -f- ^income --add-posting '(liabilities:tax)  *.33  ; income tax' --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

>= 0

# ** 2. Duplicate posting for budgeting (from documentation).
# It uses same date as the matched posting.
<
2016/1/1 withdraw
    assets:cash     $20
    assets:bank

2016/1/1 gift
    assets:cash     $-15
    expenses:gifts  ; [1/2]
$ hledger rewrite -f- expenses:gifts --add-posting '(budget:gifts)  *-1'
2016-01-01 withdraw
    assets:cash             $20
    assets:bank

2016-01-01 gift
    assets:cash               $-15
    expenses:gifts                  ; [1/2]
    (budget:gifts)            $-15  ; [2016-01-02]

>= 0

# ** 3. Add postings in another commodity
<
2017/04/24 * 09:00-09:25
    (assets:unbilled:client1)           0.42h

2017/04/25 * 10:00-11:15
    (assets:unbilled:client1)           1.25h

2017/04/25 * 14:00-15:32
    (assets:unbilled:client2)           1.54h

; billing rules
= ^assets:unbilled:client1
  (assets:to bill:client1)   *100.00 CAD

= ^assets:unbilled:client2
  (assets:to bill:client2)   *150.00 CAD
$ hledger rewrite -f-
2017-04-24 * 09:00-09:25
    (assets:unbilled:client1)           0.42h
    (assets:to bill:client1)        42.00 CAD

2017-04-25 * 10:00-11:15
    (assets:unbilled:client1)           1.25h
    (assets:to bill:client1)       125.00 CAD

2017-04-25 * 14:00-15:32
    (assets:unbilled:client2)           1.54h
    (assets:to bill:client2)       231.00 CAD

>= 0


# ** 4. Add postings with prices
<
2017/04/24 * 09:00-09:25
    (assets:unbilled:client1)         0.42h

2017/04/25 * 10:00-11:15
    (assets:unbilled:client1)         1.25h

2017/04/25 * 14:00-15:32
    (assets:unbilled:client2)         1.54h

; billing rules
= ^assets:unbilled:client1
  assets:to bill:client1   *1.00 hours @ $100.00
  income:consulting:client1

= ^assets:unbilled:client2
  assets:to bill:client2   *1.00 hours @ $150.00
  income:consulting:client2
$ hledger rewrite -f- -B
2017-04-24 * 09:00-09:25
    (assets:unbilled:client1)           0.42h
    assets:to bill:client1             $42.00
    income:consulting:client1

2017-04-25 * 10:00-11:15
    (assets:unbilled:client1)           1.25h
    assets:to bill:client1            $125.00
    income:consulting:client1

2017-04-25 * 14:00-15:32
    (assets:unbilled:client2)           1.54h
    assets:to bill:client2            $231.00
    income:consulting:client2

>= 0

# ** 5. Add absolute bank processing fee
# XXX is "and" really processed here ?
<
2016/1/1 withdraw
    assets:cash     $20
    assets:bank

2016/1/2 withdraw
    assets:cash
    assets:bank     $-30

# # TODO: Fix to filter out this txn by missing amt:<0 condition on assets:bank
# 2016/1/31 paycheck
#     income:remuneration     $-100
#     assets:bank
$ hledger rewrite -f- assets:bank and 'amt:<0' --add-posting 'expenses:fee  $5'  --add-posting 'assets:bank  $-5'
2016-01-01 withdraw
    assets:cash              $20
    assets:bank
    expenses:fee              $5
    assets:bank              $-5

2016-01-02 withdraw
    assets:cash
    assets:bank             $-30
    expenses:fee              $5
    assets:bank              $-5

>= 0

# ** 6. Rewrite rule within journal
<
= ^assets:bank$ date:2017/1 amt:<0
    assets:bank  *0.008
    expenses:fee  *-0.008  ; cash withdraw fee
= ^expenses:housing
    (budget:housing)  *-1
= ^expenses:grocery ^expenses:food
    (budget:food)  *-1

2016/12/31
    expenses:housing  $600
    assets:cash

2017/1/1
    expenses:food  $20
    expenses:leisure  $15
    expenses:grocery  $30
    assets:cash

2017/1/2
    assets:cash  $200.00
    assets:bank

2017/2/1
    assets:cash  $100.00
    assets:bank

; order with normal entries doesn't matter
; but relative order matters to refer-rewritten transactions
= ^expenses not:housing not:grocery not:food
    (budget:misc)  *-1

$ hledger rewrite -f- date:2017/1  --add-posting 'Here comes Santa  $0' --verbose-tags
2016-12-31  ; modified:
    expenses:housing            $600
    (budget:housing)           $-600  ; generated-posting: = ^expenses:housing
    assets:cash

2017-01-01  ; modified:
    expenses:food                $20
    (budget:food)               $-20  ; generated-posting: = ^expenses:grocery ^expenses:food
    Here comes Santa              $0  ; generated-posting: = date:2017/1
    expenses:leisure             $15
    (budget:misc)               $-15  ; generated-posting: = ^expenses not:housing not:grocery not:food
    Here comes Santa              $0  ; generated-posting: = date:2017/1
    expenses:grocery             $30
    (budget:food)               $-30  ; generated-posting: = ^expenses:grocery ^expenses:food
    Here comes Santa              $0  ; generated-posting: = date:2017/1
    assets:cash
    Here comes Santa              $0  ; generated-posting: = date:2017/1

2017-01-02  ; modified:
    assets:cash              $200.00
    Here comes Santa              $0  ; generated-posting: = date:2017/1
    assets:bank
    assets:bank               $-1.60  ; generated-posting: = ^assets:bank$ date:2017/1 amt:<0
    expenses:fee               $1.60  ; cash withdraw fee, generated-posting: = ^assets:bank$ date:2017/1 amt:<0
    (budget:misc)             $-1.60  ; generated-posting: = ^expenses not:housing not:grocery not:food
    Here comes Santa              $0  ; generated-posting: = date:2017/1

2017-02-01
    assets:cash         $100.00
    assets:bank

>= 0

# ** 7. Rewrite using diff output
<
2016/01/01 withdraw
    assets:cash              $20
    assets:bank

2016/01/02 withdraw
    assets:cash
    assets:bank             $-30
$ hledger rewrite --diff -f- assets:bank and 'amt:<0' --add-posting 'expenses:fee  $5'  --add-posting 'assets:bank  $-5'
--- -
+++ -
@@ -1,3 +1,5 @@
-2016/01/01 withdraw
+2016-01-01 withdraw
     assets:cash              $20
     assets:bank
+    expenses:fee              $5
+    assets:bank              $-5
@@ -5,3 +7,5 @@
-2016/01/02 withdraw
+2016-01-02 withdraw
     assets:cash
     assets:bank             $-30
+    expenses:fee              $5
+    assets:bank              $-5
