Skip to content

Instantly share code, notes, and snippets.

@adept
Created March 15, 2019 20:24
Show Gist options
  • Save adept/f97058809feb7b23b9a6ee8f2c85b356 to your computer and use it in GitHub Desktop.
Save adept/f97058809feb7b23b9a6ee8f2c85b356 to your computer and use it in GitHub Desktop.
Gist shows two attempts to create csv import rules for transactions in "foreign" currency with cost in "home" currency
2019-01-01 init
equity:opening balances
assets:bank $100
fields date,description,amt,fx_ccy,fx_amt,balance_
skip 1
account1 assets:bank
amount -%fx_ccy%fx_amt @@ $%amt
balance $ %balance_
if
transaction1
account2 expenses:one
if
transaction2
account2 expenses:two
;; With these rules account balances dont work, and even if we ignore them, cost is not being recorded:
;; hledger print -f source.csv --rules rules1
;; 2019/01/02 transaction1
;; assets:bank £-2 @@ $1 = $ 99
;; expenses:one $1
;;
;; 2019/01/03 transaction2
;; assets:bank £-4 @@ $2 = $ 97
;; expenses:two $2
;;
;;
;; (cat init.journal; hledger print -f source.csv --rules rules1) | hledger balance -f -
;; hledger: balance assertion: "-" (line 5, column 34)
;; transaction:
;; 2019/01/02 transaction1
;; assets:bank £-2 @@ $1 = $ 99
;; expenses:one
;;
;; assertion details:
;; date: 2019/01/02
;; account: assets:bank
;; commodity: $
;; calculated: 100
;; asserted: 99
;; difference: -1
;;
fields date,description,amt,fx_ccy,fx_amt,balance_
skip 1
account2 assets:bank
amount %fx_ccy %fx_amt @@ $%amt
balance $ %balance_
if
transaction1
account1 expenses:one
if
transaction2
account1 expenses:two
;; With these rules, costs are recorded in the right place, but balance assertions are now on the wrong account:
;; hledger print -f source.csv --rules rules2
;; 2019/01/02 transaction1
;; expenses:one £ 2 @@ $1 = $ 99
;; assets:bank $-1
;;
;; 2019/01/03 transaction2
;; expenses:two £ 4 @@ $2 = $ 97
;; assets:bank $-2
;;
;;> (cat init.journal; hledger print -f source.csv --rules rules2) | hledger balance -f -
;; hledger: balance assertion: "-" (line 5, column 34)
;; transaction:
;; 2019/01/02 transaction1
;; expenses:one £ 2 @@ $1 = $ 99
;; assets:bank
;;
;; assertion details:
;; date: 2019/01/02
;; account: expenses:one
;; commodity: $
;; calculated: 0
;; asserted: 99
;; difference: 99
;;
Date Description Amount FX Currency FX Amount Balance
2019-01-02 transaction1 1 £ 2 99
2019-01-03 transaction2 2 £ 4 97
;;
;; This is the journal that we want to produce through CSV import. Assets:bank is in $$$, expenses are in foreign currency, and we know
;; dollar cost for them, and want to record it
;;
2019-01-01 init
equity:opening balances
assets:bank $100
2019-01-02 transaction1
assets:bank -$1 = $99
expenses:one £2 @@ $1
2019-01-03 transaction3
assets:bank -$2 = $97
expenses:two £4 @@ $2
;; Now we can produce balance report in foreign currency and in dollar cost:
;; % hledger -f target.journal balance
;; $97 assets:bank
;; $-100 equity:opening balances
;; £6 expenses
;; £2 one
;; £4 two
;; --------------------
;; $-3
;; £6
;;
;;
;; hledger -f target.journal balance --cost
;; $97 assets:bank
;; $-100 equity:opening balances
;; $3 expenses
;; $1 one
;; $2 two
;; --------------------
;; 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment