From d35fbac422d09ff98d410eab867335ffb987086e Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 14 Jun 2011 14:29:31 +0000 Subject: [PATCH] web: inacctsubs: includes subs in an account register, with confusing balance --- hledger-lib/Hledger/Data/Matching.hs | 16 +++++++++++++--- hledger-web/Handlers.hs | 11 +++++------ hledger/Hledger/Cli/Register.hs | 6 ++---- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/hledger-lib/Hledger/Data/Matching.hs b/hledger-lib/Hledger/Data/Matching.hs index 370ad8ee8..f1206c2ce 100644 --- a/hledger-lib/Hledger/Data/Matching.hs +++ b/hledger-lib/Hledger/Data/Matching.hs @@ -24,7 +24,7 @@ import Text.ParserCombinators.Parsec import Hledger.Utils import Hledger.Data.Types --- import Hledger.Data.AccountName +import Hledger.Data.AccountName -- import Hledger.Data.Amount -- import Hledger.Data.Commodity (canonicaliseCommodities) import Hledger.Data.Dates @@ -54,13 +54,22 @@ data Matcher = MatchAny -- ^ always match -- XXX could use regular cli Opts ? data QueryOpt = QueryOptInAcct AccountName -- ^ show an account register focussed on this account + | QueryOptInAcctSubs AccountName -- ^ as above but include sub-accounts in the account register -- | QueryOptCostBasis -- ^ show amounts converted to cost where possible -- | QueryOptEffectiveDate -- ^ show effective dates instead of actual dates deriving (Show, Eq) +-- | The account we are currently focussed on, if any. inAccount :: [QueryOpt] -> Maybe AccountName inAccount [] = Nothing inAccount (QueryOptInAcct a:_) = Just a +inAccount (QueryOptInAcctSubs a:_) = Just a + +-- | A matcher for the account(s) we are currently focussed on, if any. +inAccountMatcher :: [QueryOpt] -> Maybe Matcher +inAccountMatcher [] = Nothing +inAccountMatcher (QueryOptInAcct a:_) = Just $ MatchAcct True $ accountNameToAccountOnlyRegex a +inAccountMatcher (QueryOptInAcctSubs a:_) = Just $ MatchAcct True $ accountNameToAccountRegex a -- | Convert a query expression containing zero or more space-separated -- terms to a matcher and zero or more query options. A query term is either: @@ -88,13 +97,14 @@ parseQuery d s = (m,qopts) -- keep synced with patterns below, excluding "not" prefixes = map (++":") [ - "inacct" - ,"desc","acct","date","edate","status","real","empty","depth" + "inacct","subs", + "desc","acct","date","edate","status","real","empty","depth" ] defaultprefix = "acct" -- | Parse a single query term as either a matcher or a query option. parseMatcher :: Day -> String -> Either Matcher QueryOpt +parseMatcher _ ('i':'n':'a':'c':'c':'t':'s':'u':'b':'s':':':s) = Right $ QueryOptInAcctSubs s parseMatcher _ ('i':'n':'a':'c':'c':'t':':':s) = Right $ QueryOptInAcct s parseMatcher d ('n':'o':'t':':':s) = case parseMatcher d $ quoteIfSpaced s of Left m -> Left $ negateMatcher m diff --git a/hledger-web/Handlers.hs b/hledger-web/Handlers.hs index 274c0b9df..c723a8d5d 100644 --- a/hledger-web/Handlers.hs +++ b/hledger-web/Handlers.hs @@ -97,8 +97,8 @@ postRegisterOnlyR = handlePost -- temporary helper - use the new account register report when in:ACCT is specified. accountOrJournalRegisterReport :: ViewData -> Journal -> RegisterReport accountOrJournalRegisterReport VD{opts=opts,m=m,qopts=qopts} j = - case inAccount qopts of Just a -> accountRegisterReport opts j m a - Nothing -> registerReport opts nullfilterspec $ filterJournalPostings2 m j + case inAccountMatcher qopts of Just m' -> accountRegisterReport opts j m m' + Nothing -> registerReport opts nullfilterspec $ filterJournalPostings2 m j -- | A simple accounts view, like hledger balance. If the Accept header -- specifies json, returns the chart of accounts as json. @@ -129,14 +129,13 @@ balanceReportAsHtml :: [Opt] -> ViewData -> BalanceReport -> Hamlet AppRoute balanceReportAsHtml _ vd@VD{here=here,q=q,m=m,qopts=qopts,j=j} (items,total) = $(Settings.hamletFile "balancereport") where filtering = not $ null q - inacct = inAccount qopts + inacctmatcher = inAccountMatcher qopts itemAsHtml :: ViewData -> BalanceReportItem -> Hamlet AppRoute itemAsHtml VD{here=here,q=q} (acct, adisplay, aindent, abal) = $(Settings.hamletFile "balancereportitem") where depthclass = "depth"++show aindent - inclass | Just acct == inacct = "inacct" - | isJust inacct = "notinacct" - | otherwise = "" :: String + inclass = case inacctmatcher of Just m -> if m `matchesAccount` acct then "inacct" else "notinacct" + Nothing -> "" :: String indent = preEscapedString $ concat $ replicate (2 * aindent) " " accturl = (here, [("q", pack $ accountUrl acct)]) diff --git a/hledger/Hledger/Cli/Register.hs b/hledger/Hledger/Cli/Register.hs index 4729516fa..c20939dfd 100644 --- a/hledger/Hledger/Cli/Register.hs +++ b/hledger/Hledger/Cli/Register.hs @@ -113,15 +113,13 @@ registerReport opts fspec j = (totallabel,postingsToRegisterReportItems ps nullp -- -- Does not handle reporting intervals. -- -accountRegisterReport :: [Opt] -> Journal -> Matcher -> AccountName -> RegisterReport -accountRegisterReport opts j m a = (label, postingsToRegisterReportItems displayps nullposting startbal sumfn) +accountRegisterReport :: [Opt] -> Journal -> Matcher -> Matcher -> RegisterReport +accountRegisterReport opts j m thisacctmatcher = (label, postingsToRegisterReportItems displayps nullposting startbal sumfn) where -- displayps' | interval == NoInterval = displayps -- | otherwise = summarisePostingsByInterval interval depth empty filterspan displayps -- transactions affecting this account - a' = accountNameToAccountOnlyRegex a - thisacctmatcher = MatchAcct True a' ts = filter (matchesTransaction thisacctmatcher) $ jtxns j -- all postings in these transactions