hledger-lib-1.3: Core data types, parsers and functionality for the hledger accounting tools

Safe Haskell | None
Language | Haskell2010

Hledger.Data.AutoTransaction

Contents

- Transaction processors
- Accessors

Description

This module provides utilities for applying automated transactions like `ModifierTransaction` and `PeriodicTransaction`.

Synopsis

- runModifierTransaction :: Query -> ModifierTransaction -> Transaction -> Transaction
- runPeriodicTransaction :: PeriodicTransaction -> DateSpan -> [Transaction]
- mtvaluequery :: ModifierTransaction -> Day -> Query
- jdatespan :: Journal -> DateSpan

# Transaction processors

runModifierTransaction :: Query -> ModifierTransaction -> Transaction -> Transaction #

Builds a `Transaction` transformer based on `ModifierTransaction`.

`Query` parameter allows injection of additional restriction on posting match. Don't forget to call `txnTieKnot`.

    >>> runModifierTransaction Any (ModifierTransaction "" ["pong" `post` usd 2]) nulltransaction{tpostings=["ping" `post` usd 1]}
    0000/01/01
        ping           $1.00
        pong           $2.00

    >>> runModifierTransaction Any (ModifierTransaction "miss" ["pong" `post` usd 2]) nulltransaction{tpostings=["ping" `post` usd 1]}
    0000/01/01
        ping           $1.00

    >>> runModifierTransaction None (ModifierTransaction "" ["pong" `post` usd 2]) nulltransaction{tpostings=["ping" `post` usd 1]}
    0000/01/01
        ping           $1.00

    >>> runModifierTransaction Any (ModifierTransaction "ping" ["pong" `post` amount{amultiplier=True, aquantity=3}]) nulltransaction{tpostings=["ping" `post` usd 2]}
    0000/01/01
        ping           $2.00
        pong           $6.00

runPeriodicTransaction :: PeriodicTransaction -> DateSpan -> [Transaction] #

Generate transactions from `PeriodicTransaction` within a `DateSpan`

Note that new transactions require `txnTieKnot` post-processing.

    >>> mapM_ (putStr . show) $ runPeriodicTransaction (PeriodicTransaction "monthly from 2017/1 to 2017/4" ["hi" `post` usd 1]) nulldatespan
    2017/01/01
        hi           $1.00

    2017/02/01
        hi           $1.00

    2017/03/01
        hi           $1.00

# Accessors

mtvaluequery :: ModifierTransaction -> Day -> Query #

Extract `Query` equivalent of `mtvalueexpr` from `ModifierTransaction`

    >>> mtvaluequery (ModifierTransaction "" []) undefined
    Any
    >>> mtvaluequery (ModifierTransaction "ping" []) undefined
    Acct "ping"
    >>> mtvaluequery (ModifierTransaction "date:2016" []) undefined
    Date (DateSpan 2016)
    >>> mtvaluequery (ModifierTransaction "date:today" []) (read "2017-01-01")
    Date (DateSpan 2017/01/01)

jdatespan :: Journal -> DateSpan #

`DateSpan` of all dates mentioned in `Journal`

    >>> jdatespan nulljournal
    DateSpan -
    >>> jdatespan nulljournal{jtxns=[nulltransaction{tdate=read "2016-01-01"}] }
    DateSpan 2016/01/01
    >>> jdatespan nulljournal{jtxns=[nulltransaction{tdate=read "2016-01-01", tpostings=[nullposting{pdate=Just $ read "2016-02-01"}]}] }
    DateSpan 2016/01/01-2016/02/01