update example scripts in extra and rename them so they work as add-ons

This commit is contained in:
Simon Michael 2013-09-20 09:38:54 -07:00
parent 61ff8b852a
commit d1de9ba927
6 changed files with 63 additions and 51 deletions

View File

@ -1,7 +0,0 @@
#!/usr/bin/env runhaskell
-- list the default journal's chart of accounts in --flat style
import Hledger
main = do
j <- myJournal
let l = journalToLedger nullfilterspec{empty=True} j
mapM_ putStrLn (accountnames l)

View File

@ -1,23 +0,0 @@
#!/usr/bin/env runhaskell
{-
Print an entry posting the total balance of the specified account and
subaccounts, or all accounts, from the default journal. Like ledger's
equity command. Useful when starting a new journal or closing the books.
Usage: equity.hs [ACCTPAT]
-}
import Hledger
import Hledger.Cli
import System.Environment
main = do
j <- myJournal
d <- getCurrentDay
args <- getArgs
let acctpat = head $ args ++ [""]
(acctbals,_) = balanceReport [Flat] (optsToFilterSpec [] [acctpat] d) j
txn = nulltransaction{
tdate=d,
tpostings=[nullposting{paccount=a,pamount=b} | (a,_,_,b) <- acctbals]
++ [nullposting{paccount="equity:opening balances",pamount=missingamt}]}
putStr $ show txn

9
extra/hledger-accountnames.hs Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env runhaskell
-- Show all account names used in the default journal.
import Hledger
main = do
j <- defaultJournal
let l = ledgerFromJournal Any j
mapM_ putStrLn $ ledgerAccountNames l

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

@ -0,0 +1,30 @@
#!/usr/bin/env runhaskell
{-
Like ledger's equity command, print a journal entry posting the total
balance of all accounts (or the specified account and its subaccounts)
in the default journal.
An entry like this is useful in the transition to a new journal file,
to zero out asset/liability balances in the old file and initialise
them in the new one. This way you get correct balances when reporting
on either file, and when including both files at once.
Usage: hledger-equity [ACCTPAT]
-}
import Hledger
import Hledger.Cli
import System.Environment
main = do
j <- defaultJournal
d <- getCurrentDay
args <- getArgs
let query = Or $ map Acct args
ropts = defreportopts{flat_=True}
(acctbals,_) = accountsReport ropts query j
balancingamt = negate $ sum $ map (\(_,_,_,b) -> b) acctbals
ps = [posting{paccount=a, pamount=b} | (a,_,_,b) <- acctbals]
++ [posting{paccount="equity:opening balances", pamount=balancingamt}]
txn = nulltransaction{tdate=d, tpostings=ps}
putStr $ showTransactionUnelided txn

24
extra/hledger-print-unique.hs Executable file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env runhaskell
{-|
hledger-print-unique [-f JOURNALFILE | -f-]
Print only journal entries which are unique by description (or
something else). Reads the default or specified journal, or stdin.
|-}
import Data.List
import Data.Ord
import Hledger
import Hledger.Cli
import Hledger.Cli.Print (print')
main = do
opts <- getHledgerCliOpts []
withJournalDo opts $
\opts j@Journal{jtxns=ts} -> print' opts j{jtxns=uniquify ts}
where
uniquify = nubBy (\t1 t2 -> thingToCompare t1 == thingToCompare t2) . sortBy (comparing thingToCompare)
thingToCompare = tdescription
-- thingToCompare = tdate

View File

@ -1,21 +0,0 @@
#!/usr/bin/env runhaskell
{-|
Uniquify journal entries based on some id in the description. Reads the
default or specified journal, or stdin.
Usage: uniquify.hs [-f JOURNALFILE | -f-]
|-}
import Data.List
import Hledger
import Hledger.Cli
main = do
opts <- getHledgerOpts
withJournalDo opts uniquifyAndPrint
uniquifyAndPrint :: CliOpts -> Journal -> IO ()
uniquifyAndPrint opts j@Journal{jtxns=ts} = print' opts j{jtxns=uniquify ts}
where
uniquify = nubBy (\t1 t2 -> extractId (tdescription t1) == extractId (tdescription t2))
extractId desc = desc -- extract some part that's supposed to be unique