2007-02-15 05:08:18 +03:00
|
|
|
|
2007-02-16 12:00:17 +03:00
|
|
|
module Account
|
2007-02-15 05:08:18 +03:00
|
|
|
where
|
|
|
|
import Utils
|
2007-02-16 14:51:30 +03:00
|
|
|
import BasicTypes
|
2007-02-15 05:08:18 +03:00
|
|
|
|
|
|
|
-- AccountNames are strings like "assets:cash:petty"; from these we build
|
2007-02-16 14:51:30 +03:00
|
|
|
-- the chart of accounts, which should be a simple hierarchy.
|
2007-02-15 05:08:18 +03:00
|
|
|
type AccountName = String
|
|
|
|
|
|
|
|
accountNameComponents :: AccountName -> [String]
|
|
|
|
accountNameComponents = splitAtElement ':'
|
|
|
|
|
|
|
|
accountNameFromComponents :: [String] -> AccountName
|
|
|
|
accountNameFromComponents = concat . intersperse ":"
|
|
|
|
|
|
|
|
accountLeafName :: AccountName -> String
|
|
|
|
accountLeafName = rhead . accountNameComponents
|
|
|
|
|
|
|
|
accountNameLevel :: AccountName -> Int
|
|
|
|
accountNameLevel = length . accountNameComponents
|
|
|
|
|
|
|
|
-- ["a:b:c","d:e"] -> ["a","a:b","a:b:c","d","d:e"]
|
|
|
|
expandAccountNames :: [AccountName] -> [AccountName]
|
|
|
|
expandAccountNames as = nub $ concat $ map expand as
|
|
|
|
where expand as = map accountNameFromComponents (tail $ inits $ accountNameComponents as)
|
|
|
|
|
|
|
|
-- ["a:b:c","d:e"] -> ["a","d"]
|
|
|
|
topAccountNames :: [AccountName] -> [AccountName]
|
|
|
|
topAccountNames as = [a | a <- expandAccountNames as, accountNameLevel a == 1]
|
|
|
|
|
|
|
|
parentAccountName :: AccountName -> Maybe AccountName
|
|
|
|
parentAccountName a =
|
|
|
|
case accountNameLevel a > 1 of
|
|
|
|
True -> Just $ accountNameFromComponents $ rtail $ accountNameComponents a
|
|
|
|
False -> Nothing
|
|
|
|
|
|
|
|
s `isSubAccountNameOf` p =
|
|
|
|
((p ++ ":") `isPrefixOf` s) && (accountNameLevel s == (accountNameLevel p + 1))
|
|
|
|
|
|
|
|
subAccountNamesFrom :: [AccountName] -> AccountName -> [AccountName]
|
|
|
|
subAccountNamesFrom accts a = filter (`isSubAccountNameOf` a) accts
|
|
|
|
|
|
|
|
matchAccountName :: String -> AccountName -> Bool
|
|
|
|
matchAccountName s a =
|
|
|
|
case matchRegex (mkRegex s) a of
|
|
|
|
Nothing -> False
|
|
|
|
otherwise -> True
|
|
|
|
|
2007-02-16 14:51:30 +03:00
|
|
|
indentAccountName :: AccountName -> String
|
|
|
|
indentAccountName a = replicate (((accountNameLevel a) - 1) * 2) ' ' ++ (accountLeafName a)
|
|
|
|
|
2007-02-15 05:08:18 +03:00
|
|
|
|
2007-02-16 14:51:30 +03:00
|
|
|
-- We could almost get by with just the above, but we need smarter
|
|
|
|
-- structures to eg display the account tree with boring accounts elided.
|
|
|
|
-- first, here is a tree of AccountNames; Account and Account tree are
|
|
|
|
-- defined later.
|
2007-02-15 05:08:18 +03:00
|
|
|
|
2007-02-16 14:51:30 +03:00
|
|
|
antacctname = fst . node
|
|
|
|
antsubs = snd . node
|
2007-02-15 05:08:18 +03:00
|
|
|
|
2007-02-16 14:51:30 +03:00
|
|
|
accountNameTreeFrom_props =
|
2007-02-15 05:08:18 +03:00
|
|
|
[
|
2007-02-16 14:51:30 +03:00
|
|
|
accountNameTreeFrom ["a"] == Tree ("top", [Tree ("a",[])]),
|
|
|
|
accountNameTreeFrom ["a","b"] == Tree ("top", [Tree ("a", []), Tree ("b", [])]),
|
|
|
|
accountNameTreeFrom ["a","a:b"] == Tree ("top", [Tree ("a", [Tree ("a:b", [])])]),
|
|
|
|
accountNameTreeFrom ["a:b"] == Tree ("top", [Tree ("a", [Tree ("a:b", [])])])
|
2007-02-15 05:08:18 +03:00
|
|
|
]
|
2007-02-16 14:51:30 +03:00
|
|
|
accountNameTreeFrom :: [AccountName] -> Tree AccountName
|
|
|
|
accountNameTreeFrom accts =
|
2007-02-15 05:08:18 +03:00
|
|
|
Tree ("top", accountsFrom (topAccountNames accts))
|
|
|
|
where
|
2007-02-16 14:51:30 +03:00
|
|
|
accountsFrom :: [AccountName] -> [Tree AccountName]
|
2007-02-15 05:08:18 +03:00
|
|
|
accountsFrom [] = []
|
|
|
|
accountsFrom as = [Tree (a, accountsFrom $ subs a) | a <- as]
|
|
|
|
subs = (subAccountNamesFrom accts)
|
|
|
|
|
2007-02-16 14:51:30 +03:00
|
|
|
showAccountNameTree :: Tree AccountName -> String
|
|
|
|
showAccountNameTree at = showAccountNameTrees $ antsubs at
|
2007-02-15 05:08:18 +03:00
|
|
|
|
2007-02-16 14:51:30 +03:00
|
|
|
showAccountNameTrees :: [Tree AccountName] -> String
|
|
|
|
showAccountNameTrees ats =
|
|
|
|
concatMap showAccountNameBranch ats
|
2007-02-15 05:08:18 +03:00
|
|
|
where
|
2007-02-16 14:51:30 +03:00
|
|
|
showAccountNameBranch at = topacct ++ "\n" ++ subs
|
2007-02-15 05:08:18 +03:00
|
|
|
where
|
2007-02-16 14:51:30 +03:00
|
|
|
topacct = indentAccountName $ antacctname at
|
|
|
|
subs = showAccountNameTrees $ antsubs at
|
2007-02-15 05:08:18 +03:00
|
|
|
|