Search code examples
accountingbeancount

How to query Document directives in beancount query language?


I am a big fan of double-entry accounting from text files and use beancount: https://github.com/beancount/beancount Using the beancount query language, I can easily create reports (e.g. tax) from my text files.

In beancount, it is possible to add links to documents (e.g. PDFs of the receipts) in the transaction statements as in

2020-02-04 * "Google" "Server" #tax
  document: "2020-02-04 3688657551.pdf"
  Liabilities:ErsteBank:Credit  -30.40 EUR
  Expenses:Professional:Server   30.40 EUR

At the end of the year, I use the following query to get all tax relevant expenses:

SELECT 
    account,  date, number as value, currency, description
FROM
    year = 2020
WHERE
    account ~ "Expenses:"
    AND 'tax' IN tags
ORDER BY date, account, betrag

Is it possible to include the filename of the Document directive in the query, i.e. "2020-02-04 3688657551.pdf"?


Solution

  • @Robert, Try with the any_meta('document') as briefly introduced in this post.

    Ledger:

    2020-01-01 open Liabilities:ErsteBank:Credit
    2020-01-01 open Expenses:Professional:Server
    option "operating_currency" "EUR"
    
    
    2020-02-04 * "Google" "Server" #tax
      document: "2020-02-04 3688657551.pdf"
      Liabilities:ErsteBank:Credit  -30.40 EUR
      Expenses:Professional:Server   30.40 EUR
    

    Query:

    SELECT
        account, date, number as value, payee, any_meta('document') as document
    FROM
        year = 2020
    WHERE
        account ~ "Expenses:"
        AND 'tax' IN tags
    ORDER BY date, account
    

    Result:

    account date value payee document
    Expenses:Professional:Server 2020-02-04 30.40 Google 2020-02-04 3688657551.pdf