module Hledger.Data.Ledger
where
import Data.Map (Map, findWithDefault, fromList)
import Data.Tree
import Test.HUnit
import Text.Printf
import Hledger.Utils
import Hledger.Data.Types
import Hledger.Data.Account (nullacct)
import Hledger.Data.AccountName
import Hledger.Data.Journal
import Hledger.Data.Posting
import Hledger.Data.Matching
instance Show Ledger where
show l = printf "Ledger with %d transactions, %d accounts\n%s"
(length (jtxns $ journal l) +
length (jmodifiertxns $ journal l) +
length (jperiodictxns $ journal l))
(length $ accountnames l)
(showtree $ accountnametree l)
nullledger :: Ledger
nullledger = Ledger{
journal = nulljournal,
accountnametree = nullaccountnametree,
accountmap = fromList []
}
journalToLedger :: FilterSpec -> Journal -> Ledger
journalToLedger fs j = nullledger{journal=j',accountnametree=t,accountmap=m}
where j' = filterJournalPostings fs{depth=Nothing} j
(t, m) = journalAccountInfo j'
journalToLedger2 :: Matcher -> Journal -> Ledger
journalToLedger2 m j = nullledger{journal=j',accountnametree=t,accountmap=amap}
where j' = filterJournalPostings2 m j
(t, amap) = journalAccountInfo j'
ledgerAccountNames :: Ledger -> [AccountName]
ledgerAccountNames = drop 1 . flatten . accountnametree
ledgerAccount :: Ledger -> AccountName -> Account
ledgerAccount l a = findWithDefault nullacct a $ accountmap l
ledgerAccounts :: Ledger -> [Account]
ledgerAccounts = drop 1 . flatten . ledgerAccountTree 9999
ledgerTopAccounts :: Ledger -> [Account]
ledgerTopAccounts = map root . branches . ledgerAccountTree 9999
ledgerLeafAccounts :: Ledger -> [Account]
ledgerLeafAccounts = leaves . ledgerAccountTree 9999
ledgerAccountsMatching :: [String] -> Ledger -> [Account]
ledgerAccountsMatching pats = filter (matchpats pats . aname) . accounts
ledgerSubAccounts :: Ledger -> Account -> [Account]
ledgerSubAccounts l Account{aname=a} =
map (ledgerAccount l) $ filter (`isSubAccountNameOf` a) $ accountnames l
ledgerPostings :: Ledger -> [Posting]
ledgerPostings = journalPostings . journal
ledgerAccountTree :: Int -> Ledger -> Tree Account
ledgerAccountTree depth l = treemap (ledgerAccount l) $ treeprune depth $ accountnametree l
ledgerAccountTreeAt :: Ledger -> Account -> Maybe (Tree Account)
ledgerAccountTreeAt l acct = subtreeat acct $ ledgerAccountTree 9999 l
ledgerDateSpan :: Ledger -> DateSpan
ledgerDateSpan = postingsDateSpan . ledgerPostings
accountnames :: Ledger -> [AccountName]
accountnames = ledgerAccountNames
account :: Ledger -> AccountName -> Account
account = ledgerAccount
accounts :: Ledger -> [Account]
accounts = ledgerAccounts
topaccounts :: Ledger -> [Account]
topaccounts = ledgerTopAccounts
accountsmatching :: [String] -> Ledger -> [Account]
accountsmatching = ledgerAccountsMatching
subaccounts :: Ledger -> Account -> [Account]
subaccounts = ledgerSubAccounts
postings :: Ledger -> [Posting]
postings = ledgerPostings
commodities :: Ledger -> Map String Commodity
commodities = journalCanonicalCommodities . journal
accounttree :: Int -> Ledger -> Tree Account
accounttree = ledgerAccountTree
accounttreeat :: Ledger -> Account -> Maybe (Tree Account)
accounttreeat = ledgerAccountTreeAt
rawdatespan :: Ledger -> DateSpan
rawdatespan = journalDateSpan . journal
ledgeramounts :: Ledger -> [MixedAmount]
ledgeramounts = journalAmounts . journal
tests_Hledger_Data_Ledger = TestList
[
]