From 918b3b45a54b9fed6649a48200ee9916016914f1 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 29 Jun 2011 20:18:10 +0000 Subject: [PATCH] web: make register item display a bit more consistent --- .../accountregisterreportitem.hamlet | 8 ++-- hledger/Hledger/Cli/Register.hs | 47 ++++++++++--------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/hledger-web/.hledger/web/templates/accountregisterreportitem.hamlet b/hledger-web/.hledger/web/templates/accountregisterreportitem.hamlet index 4cd18375d..026a37581 100644 --- a/hledger-web/.hledger/web/templates/accountregisterreportitem.hamlet +++ b/hledger-web/.hledger/web/templates/accountregisterreportitem.hamlet @@ -1,8 +1,8 @@ #{date} #{elideRight 30 desc} - - $if split + #{mixedAmountAsHtml bal} -$if split - $forall p <- tpostings t' +$if True + $forall p <- tpostings t Journal -> Matcher -> AccountRegisterReport journalRegisterReport _ Journal{jtxns=ts} m = (totallabel, items) where ts' = sortBy (comparing tdate) $ filter (not . null . tpostings) $ map (filterTransactionPostings m) ts - items = reverse $ accountRegisterReportItems m MatchNone nullmixedamt (+) ts' + items = reverse $ accountRegisterReportItems m MatchNone nullmixedamt id (+) ts' -- | Get a conventional account register report, with the specified -- options, for the currently focussed account (or possibly the focussed @@ -209,9 +209,9 @@ accountRegisterReport opts j m thisacctmatcher = (label, items) ts = sortBy (comparing tdate) $ filter (matchesTransaction thisacctmatcher) $ jtxns j -- starting balance: if we are filtering by a start date and nothing else, -- the sum of postings to this account before that date; otherwise zero. - (startbal,label, sumfn) | matcherIsNull m = (nullmixedamt, balancelabel, (-)) - | matcherIsStartDateOnly effective m = (sumPostings priorps, balancelabel, (-)) - | otherwise = (nullmixedamt, totallabel, (+)) + (startbal,label, sumfn) | matcherIsNull m = (nullmixedamt, balancelabel, (+)) + | matcherIsStartDateOnly effective m = (sumPostings priorps, balancelabel, (+)) + | otherwise = (nullmixedamt, totallabel, (-)) where priorps = -- ltrace "priorps" $ filter (matchesPosting @@ -221,14 +221,14 @@ accountRegisterReport opts j m thisacctmatcher = (label, items) tostartdatematcher = MatchDate True (DateSpan Nothing startdate) startdate = matcherStartDate effective m effective = Effective `elem` opts - items = reverse $ accountRegisterReportItems m thisacctmatcher startbal sumfn ts + items = reverse $ accountRegisterReportItems m thisacctmatcher startbal negate sumfn ts -- | Generate account register line items from a list of transactions, -- using the provided query and "this account" matchers, starting balance, --- and balance summing function. -accountRegisterReportItems :: Matcher -> Matcher -> MixedAmount -> (MixedAmount -> MixedAmount -> MixedAmount) -> [Transaction] -> [AccountRegisterReportItem] -accountRegisterReportItems _ _ _ _ [] = [] -accountRegisterReportItems matcher thisacctmatcher bal sumfn (t:ts) = +-- sign-setting function and balance-summing function. +accountRegisterReportItems :: Matcher -> Matcher -> MixedAmount -> (MixedAmount -> MixedAmount) -> (MixedAmount -> MixedAmount -> MixedAmount) -> [Transaction] -> [AccountRegisterReportItem] +accountRegisterReportItems _ _ _ _ _ [] = [] +accountRegisterReportItems matcher thisacctmatcher bal signfn sumfn (t:ts) = case i of Just i' -> i':is Nothing -> is where @@ -237,26 +237,29 @@ accountRegisterReportItems matcher thisacctmatcher bal sumfn (t:ts) = displaymatcher | numthisacctsposted > 1 = matcher | otherwise = MatchAnd [negateMatcher thisacctmatcher, matcher] t'@Transaction{tpostings=ps'} = filterTransactionPostings displaymatcher t + acct = summarisePostings $ tpostings t (i,bal'') = case ps' of [] -> (Nothing,bal) -- maybe a virtual transaction, or transfer to self [p] -> (Just (t, t', False, acct, amt, bal'), bal') where - acct = paccount p - amt = pamount p + amt = signfn $ pamount p bal' = bal `sumfn` amt - ps' -> (Just (t, t', True, acct, amt, bal'), bal') + ps'' -> (Just (t, t', True, acct, amt, bal'), bal') where - -- describe split as from ..., to ... (not always right) - acct = case (simplify tos, simplify froms) of - ([],ts) -> "to "++commafy ts - (fs,[]) -> "from "++commafy fs - (fs,ts) -> "to "++commafy ts++" from "++commafy fs - where (tos,froms) = partition (fromMaybe False . isNegativeMixedAmount . pamount) ps' - simplify = nub . map (accountLeafName . paccount) - commafy = intercalate ", " - amt = sum $ map pamount ps' + amt = signfn $ sum $ map pamount ps'' bal' = bal `sumfn` amt - is = accountRegisterReportItems matcher thisacctmatcher bal'' sumfn ts + is = accountRegisterReportItems matcher thisacctmatcher bal'' signfn sumfn ts + +-- | Generate a short readable summary of a transaction's postings. +summarisePostings ps = + case (simplify tos, simplify froms) of + ([],ts) -> "to "++commafy ts + (fs,[]) -> "from "++commafy fs + (fs,ts) -> "from "++commafy fs++" to "++commafy ts + where + (tos,froms) = partition (fromMaybe False . isNegativeMixedAmount . pamount) ps + simplify = nub . map (accountLeafName . paccount) + commafy = intercalate ", " filterTransactionPostings :: Matcher -> Transaction -> Transaction filterTransactionPostings m t@Transaction{tpostings=ps} = t{tpostings=filter (m `matchesPosting`) ps}