ledgerで経理

Tobias Pfeiffer

tgp@preferred.jp

Self-Introduction

  • Tobias Pfeiffer, Software Engineer @ PFN
  • Joined PFI in April 2014, moved to PFN in October 2014
  • 2015-12-26_kinenshashin_family_med.jpg Recently worked on: Stream processing engine, CES demo
  • Hobbies
    • Last year: Wedding planning →
    • Also: Cooking ↓

DSC_0234.jpg DSC_0602.jpg DSC_0800.jpg DSC_0808.jpg DSC_0867.jpg DSC_0899.jpg DSC_0999.jpg

Today's Topic: Background & Motivation

  • Not related to work tasks
  • Not at all an expert or very knowledgeable in the field
    (But: Everyone uses money, so needs to manage it.)
  • Motivation
    • Household CFO
    • Until now: LibreOffice & various spreadsheets
    • One Problem: LibreOffice
    • Another problem: Merging data sources
  • Recently discovered ledger and gave it a try

Accounting

Why Accounting?

  • For companies: usually required by law
    (to calculate taxes, inform shareholders etc.)
  • For private persons/households:
    • Locate and track: How much do you get (from where), how much do you have (where), how much do you spend (how)?
    • Manage: Pay your bills on time.
    • Optimize: What can you do to improve the situation?
    • Plan: What can you afford and when?

Double-Entry Accounting (複式簿記)

  • Published in 1494 (Luca Pacioli: Summa de arithmetica)
  • Concept: Maintain multiple books (”accounts”) and record every transaction in two books
  Cash in Wallet     Food expenses   
Amount   Balance   Amount  Balance   
-750 JPY 2,307 JPY 750 JPY 43,108 JPY
-120 JPY 2,187 JPY 120 JPY 43,228 JPY
        Salary               Bank account     
Amount      Balance      Amount     Balance   
-12,345 JPY -246,789 JPY 12,345 JPY 14,679 JPY

Double-Entry Accounting (2)

  • Notes
    • In the real world, this is more complex. (Why??)
  • Observations
    • The sum of amounts is zero in every transaction.
    • The sum of balances is zero across all accounts.
  • Advantages
    • Location of money is immediately observable.
    • A lot of “virtual accounts” can be added for fine-grained reporting.
    • Invoice date and payment date can be different.

Account Structure

  • Common structure
    • Income (収入): where money comes from
    • Expenses (費用): where money goes to
    • Assets (資産): where money is
    • Liabilities (負債): money you owe
    • Equity (資本?): “the real value of your property”
  • Examples:
    • Income»Salary (will get more and more negative)
    • Expenses»Food (will get more and more positive)
    • Assets»Cash, Assets»Bank Account
    • Liabilities»Credit Card

About ledger

Basic Usage

Concept

  • Maintain a plain text file with your transactions.
    (Such a list of transactions is a “ledger”/原簿.)
    • Plain text: Possible to have it in git, encrypt it, print it, copy & paste, edit it with scripts, …
  • Execute ledger on that file for reporting.
  • ledger checks that everything balances and computes the numbers for the report.
  • In particular, ledger is not a tool to enter transactions!

Input Format

  • General format:

    date payee
      ; note
      account 1    amount
      account 2    amount
    
  • For example:

    2016-02-25 Lawson
      ; Ham Sandwich
      Expenses:Food    325 JPY
      Assets:Cash     -325 JPY
    


  • ledger also allows:
    • One posting without amount
    • More than two postings (must add up to zero)
  • 2016-02-25 Lawson
      Expenses:Food    325 JPY
      Assets:Cash
    
    2016-02-25 Lawson
      Expenses:Food    301 JPY
      Expenses:Taxes    24 JPY
      Assets:Cash     -325 JPY
    

Balance (残高) Report

ledger can compute the balance on each account.

  • Input file:

    2016-02-20 Company
      Assets:Bank   123,456 JPY
      Income:Salary
    
    2016-02-22 Bank
      Assets:Cash    20,000 JPY
      Assets:Bank
    
    2016-02-26 Landlord
      Expenses:Rent  70,000 JPY
      Assets:Bank
    
    2016-02-27 Supermarket
      Expenses:Food   2,017 JPY
      Assets:Cash
    
  • $ ledger balance

              51,439 JPY  Assets
              33,456 JPY    Bank
              17,983 JPY    Cash
              72,017 JPY  Expenses
               2,017 JPY    Food
              70,000 JPY    Rent
            -123,456 JPY  Income:Salary
    --------------------
                       0
    

    (The input file name is passed with -f or the LEDGER_FILE environment variable.)

Register Report

ledger can display all transaction for an account with a running total.

$ ledger register Cash

16-Feb-22 Bank          Assets:Cash     20,000 JPY   20,000 JPY
16-Feb-25 Lawson        Assets:Cash       -325 JPY   19,675 JPY
16-Feb-27 Supermarket   Assets:Cash     -2,017 JPY   17,658 JPY
16-Mar-01 Lawson        Assets:Cash       -288 JPY   17,370 JPY

$ ledger --period "feb 2016" register Food

16-Feb-25 Lawson        Expenses:Food      325 JPY      325 JPY
16-Feb-27 Supermarket   Expenses:Food    2,017 JPY    2,342 JPY

Other Reports

  • Some other commands
    • equity: collapse all transactions into a single one
    • xml, csv: list transactions in other formats
    • xact: create a new transaction from a pattern
  • Some parameters
    • --period, --begin, --end: date limit
    • --strict: check that all used entities exist
    • all commands take a regular expression to filter accounts

Example Use Patterns

Dealing With Cash

  • Problem: Tracking all the little cash expenses
  • One possible solution: Pay as much as possible using credit card, and treat cash as “Expense”, not “Asset”.

    2016-03-02 ATM
      Expenses:Cash   10,000 JPY
      Assets:Bank
    
  • To track an “important” expense, move money from Expenses:Cash to the target account:

    2016-03-05 Expensive Restaurant
      Expenses:Eat Out  8,000 JPY
      Expenses:Cash
    

Dealing With Cash (2)

  • If you do track your cash expenses (collecting receipts or using some app), at some point target/actual check (reconciliation) needs to be done.
  • For checks, balance assertions can be used.

    2016-03-05 Check balance
      ; ledger run will fail if we don't have 123 JPY
      Assets:Cash    = 123 JPY
    
  • To correct the amount, balance resets can be used.

    2016-03-06 Correct balance
      ; take from Expenses:Loss what is required to get to 123 JPY
      Assets:Cash    = 123 JPY
      Expenses:Loss
    

Delayed Payments

  • When paying a purchase with a credit card, the payment is done later. However, there is a liability towards the credit card operator:
    • 2016-02-15 Amazon
        Expenses:Books   780 JPY
        Liabilities:Credit Card
      
    • $ ledger balance

        780 JPY  Expenses:Books
       -780 JPY  Liabilities:Credit Card
      ---------
              0
      
  • Later, the liabilities must be paid back:
    • 2016-02-15 Amazon
        Expenses:Books   780 JPY
        Liabilities:Credit Card
      
      2016-03-27 Pay Invoice
        Liabilities:Credit Card  780 JPY
        Assets:Bank
      
    • $ ledger balance

       -780 JPY  Assets:Bank
        780 JPY  Expenses:Books
      ---------
              0
      

Tracking Reimbursements

  • When paying something on behalf of the company, it is the other way around, the company owes you money:
    • 2016-02-17 Taxi
        ; no bus in inaka
        Liabilities:PFN   3,020 JPY
        Assets:Cash
      
    • $ ledger balance

         -3,020 JPY  Assets:Cash
          3,020 JPY  Liabilities:PFN
      -------------
                  0
      
  • If you hand in the receipts properly, the money will be reimbursed next month:
    • 2016-02-17 Taxi
        Liabilities:PFN   3,020 JPY
        Assets:Cash
      
      2016-03-20 PFN
        Assets:Bank     126,476 JPY
        Liabilities:PFN  -3,020 JPY
        Income:Salary  -123,456 JPY
      
    • $ ledger balance

        123,456 JPY  Assets
        126,476 JPY    Bank
         -3,020 JPY    Cash
       -123,456 JPY  Income:Salary
      -------------
                  0
      

Advanced Features

Currencies/Commodities

  • A “currency” is just any string. You could also do inventory tracking using ledger:
    • 2016-02-29 Shopping day
        Fridge
        Supermarket    -2 Apples
        Konbini        -3 Beers
      
    • $ ledger balance fridge

      2 Apples
       3 Beers  Fridge
      
  • Currencies can also be exchanged into one another with exchange rate computation…
    • 2016-02-29 ATM @ Europe
        Assets:Cash      50 EUR
        Assets:Bank  -6,258 JPY
      
    • $ ledger prices

      2016/02/29 EUR        125.16 JPY
      
  • … or manually given:
    • 2016-02-29 ATM @ Europe
        Assets:Cash  50 EUR @ 125 JPY
        Assets:Bank
      
    • $ ledger balance -X JPY Bank

      JPY-6250  Assets:Bank
      

Stock

  • Stocks can also be treated like currencies.
    • 2015-05-04 Buy Stock
        Assets:Brokerage     10 BMW
        Assets:Bank   -1,067.50 EUR
      
      2015-09-24 Buy Stock
        Assets:Brokerage    15 BMW
        Assets:Bank  -1,135.20 EUR
      
    • $ ledger balance brokerage

      25 BMW  Assets:Brokerage
      
  • If you maintain a price history, you can find out current value of your stock.
    • $ cat prices.db

      P 2015/05/04 BMW 106.75 EUR
      P 2015/09/24 BMW 75.68 EUR
      P 2016/03/04 BMW 82.63 EUR
      
    • $ ledger --market --price-db prices.db balance brokerage

      2,065.75 EUR  Assets:Brokerage
      

      $ ledger --gain --price-db prices.db balance brokerage

      -136.95 EUR  Assets:Brokerage
      

Metadata

  • ledger allows to attach metadata to transactions, such as effective dates, IDs, clearing state, and tags.

    2016-02-15=2016-03-27 ! (123-7654) Rakuten
      Expenses:Books           780 JPY  ; payee: HappyBooks 123
      Expenses:Household     2,350 JPY  ; payee: MyHousehold
      Liabilities:Credit Card
      ; :OnlineShopping:Manga:
      ; ShippingMethod: deliver to home address
      ; ShipmentDate:: [2016-02-16]
    
  • The meaning of metadata is completely up to the user.
  • Use the data when querying:
    • $ ledger --effective balance
    • $ ledger --pending|--cleared register
    • $ ledger register tag manga
    • $ ledger register tag ShipmentDate < [2016-02-20]

Budgeting

Set up a budget and compare it against real expenses.

~ Monthly
  Expenses:Eat Out  10,000 JPY
  Assets:Cash

2016-02-14 Izakaya
  Expenses:Eat Out   3,723 JPY
  Assets:Cash

2016-02-28 Medium Restaurant
  Expenses:Eat Out   5,182 JPY
  Assets:Cash

2016-03-04 Expensive Restaurant
  Expenses:Eat Out  12,300 JPY
  Assets:Cash

$ ledger --monthly --budget register Expenses

16-Feb-01 - 16-Feb-29    Expenses:Eat Out   -1,095 JPY   -1,095 JPY
16-Mar-01 - 16-Mar-31    Expenses:Eat Out    2,300 JPY    1,205 JPY

Forecasting

Set up a budget and forecast your financial situation.

~ Monthly
  Expenses:Eat Out  10,000 JPY
  Expenses:Food     15,000 JPY
  Expenses:Rent     70,000 JPY
  Income:Salary   -123,456 JPY
  Assets

$ ledger --monthly --forecast "d < [2016-09]" register Assets

16-Apr-01 - 16-Apr-30    Assets             28,456 JPY   28,456 JPY
16-May-01 - 16-May-31    Assets             28,456 JPY   56,912 JPY
16-Jun-01 - 16-Jun-30    Assets             28,456 JPY   85,368 JPY
16-Jul-01 - 16-Jul-31    Assets             28,456 JPY  113,824 JPY
16-Aug-01 - 16-Aug-31    Assets             28,456 JPY  142,280 JPY

The ledger Ecosystem

Related Tools

emacs Integration

  • ledger-mode provides syntax highlighting, tab completion, integrated reports etc.

ledger-mode.png

Features Not Presented Today

  • Automatic transactions
  • Python integration
  • Advanced stock valuation
  • Report formatting

Summary

  • ledger helps you track your money, stock, and (optionally) apples.
  • Powerful reporting commands allow to detect spending patterns and plan for future expenses.
  • A number of tools help to get data in and out of your ledger files.