extra: hledger-rewrite script

This commit is contained in:
Simon Michael 2014-02-05 13:02:24 -08:00
parent f46972714d
commit dc8b687056
3 changed files with 32 additions and 0 deletions

30
extra/hledger-rewrite.hs Executable file
View File

@ -0,0 +1,30 @@
#!/usr/bin/env runhaskell
{-|
hledger-rewrite PATTERNS --add-posting "ACCT AMTEXPR" ...
Skeleton for a minimal generic rewriter of journal entries.
Reads the default journal and prints the entries, like print,
but adds the specified postings to any entries matching PATTERNS.
|-}
import Hledger.Data.Types (Journal(..))
import Hledger
import Hledger.Cli
main = do
putStrLn "(-f option not supported)"
opts <- getCliOpts (defCommandMode ["hledger-rewrite"])
withJournalDo opts $
\opts j@Journal{jtxns=ts} -> print' opts j{jtxns=map rewrite ts}
where
rewrite t = if matched t then t{tpostings=tpostings t ++ newps t}
else t
matched t = Acct "^income" `matchesTransaction` t
newps t = [generatePosting t "(Reserve)" (`divideMixedAmount` 10)]
generatePosting t acct amtfn = nullposting{paccount = accountNameWithoutPostingType acct
,ptype = accountNamePostingType acct
,pamount = amtfn amt
}
where
amt = pamount $ head $ filter (Acct "^income" `matchesPosting`) $ tpostings t

View File

@ -18,6 +18,7 @@ module Hledger.Read (
requireJournalFileExists,
ensureJournalFileExists,
-- * Parsers used elsewhere
postingp,
accountname,
amountp,
amountp',

View File

@ -29,6 +29,7 @@ module Hledger.Read.JournalReader (
datetime,
code,
accountname,
postingp,
amountp,
amountp',
mamountp',