From 7175c57992d1d7c5466c3b3a213a68fc5ae1f8c7 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 22 Dec 2012 00:24:38 +0000 Subject: [PATCH] reg: --related/-r flag to show other postings in the transaction --- MANUAL.md | 3 +++ hledger-lib/Hledger/Data/Journal.hs | 2 +- hledger-lib/Hledger/Data/Posting.hs | 6 ++++++ hledger-lib/Hledger/Data/Transaction.hs | 5 ++++- hledger-lib/Hledger/Reports.hs | 5 ++++- hledger/Hledger/Cli/Options.hs | 4 +++- 6 files changed, 21 insertions(+), 4 deletions(-) diff --git a/MANUAL.md b/MANUAL.md index 3be724a9f..7a67cd7c6 100644 --- a/MANUAL.md +++ b/MANUAL.md @@ -760,6 +760,9 @@ this is 80 characters. To allow more space for descriptions and account names, use `-w` to increase the width to 120 characters, or `-wN` to set any desired width (at least 50 recommended). +The `--related`/`-r` flag shows the *other* postings in the transactions +of the postings which would normally be shown. + #### balance The balance command displays accounts and their balances, indented to show the account hierarchy. diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index dfacd0286..d317b9f1b 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -364,7 +364,7 @@ journalFinalise tclock tlocal path txt ctx j@Journal{files=fs} = -- depends on display precision. Reports only the first error encountered. journalBalanceTransactions :: Journal -> Either String Journal journalBalanceTransactions j@Journal{jtxns=ts, jcommoditystyles=ss} = - case sequence $ map balance ts of Right ts' -> Right j{jtxns=ts'} + case sequence $ map balance ts of Right ts' -> Right j{jtxns=map txnTieKnot ts'} Left e -> Left e where balance = balanceTransaction (Just ss) diff --git a/hledger-lib/Hledger/Data/Posting.hs b/hledger-lib/Hledger/Data/Posting.hs index a365185cb..711f71d6e 100644 --- a/hledger-lib/Hledger/Data/Posting.hs +++ b/hledger-lib/Hledger/Data/Posting.hs @@ -21,6 +21,7 @@ module Hledger.Data.Posting ( hasAmount, postingAllTags, transactionAllTags, + relatedPostings, -- * date operations postingDate, postingDate2, @@ -149,6 +150,11 @@ postingAllTags p = ptags p ++ maybe [] transactionAllTags (ptransaction p) transactionAllTags :: Transaction -> [Tag] transactionAllTags t = ttags t +-- Get the other postings from this posting's transaction. +relatedPostings :: Posting -> [Posting] +relatedPostings p@Posting{ptransaction=Just t} = filter (/= p) $ tpostings t +relatedPostings _ = [] + -- | Does this posting fall within the given date span ? isPostingInDateSpan :: DateSpan -> Posting -> Bool isPostingInDateSpan s = spanContainsDate s . postingDate diff --git a/hledger-lib/Hledger/Data/Transaction.hs b/hledger-lib/Hledger/Data/Transaction.hs index 0e53526ba..93990ce92 100644 --- a/hledger-lib/Hledger/Data/Transaction.hs +++ b/hledger-lib/Hledger/Data/Transaction.hs @@ -264,7 +264,7 @@ balanceTransaction styles t@Transaction{tpostings=ps} | length rwithoutamounts > 1 || length bvwithoutamounts > 1 = Left $ printerr "could not balance this transaction (too many missing amounts)" | not $ isTransactionBalanced styles t''' = Left $ printerr $ nonzerobalanceerror t''' - | otherwise = Right t''' + | otherwise = Right t'''' where -- maybe infer missing amounts (rwithamounts, rwithoutamounts) = partition hasAmount $ realPostings t @@ -329,6 +329,9 @@ balanceTransaction styles t@Transaction{tpostings=ps} bvamountsinunpricedcommodity = filter ((==unpricedcommodity).acommodity) bvamountsinorder inferprice p = p + -- tie the knot so eg relatedPostings works right + t'''' = txnTieKnot t''' + printerr s = intercalate "\n" [s, showTransactionUnelided t] nonzerobalanceerror :: Transaction -> String diff --git a/hledger-lib/Hledger/Reports.hs b/hledger-lib/Hledger/Reports.hs index df3e51125..bc8a868f6 100644 --- a/hledger-lib/Hledger/Reports.hs +++ b/hledger-lib/Hledger/Reports.hs @@ -91,6 +91,7 @@ data ReportOpts = ReportOpts { ,quarterly_ :: Bool ,yearly_ :: Bool ,format_ :: Maybe FormatStr + ,related_ :: Bool ,query_ :: String -- all arguments, as a string } deriving (Show) @@ -120,6 +121,7 @@ defreportopts = ReportOpts def def def + def instance Default ReportOpts where def = defreportopts @@ -259,7 +261,8 @@ postingsReport opts q j = -- trace ("q: "++show q++"\nq': "++show q') $ wd = whichDateFromOpts opts -- delay depth filtering until the end (depth, q') = (queryDepth q, filterQuery (not . queryIsDepth) q) - (precedingps, displayableps, _) = dbg "ps3" $ postingsMatchingDisplayExpr (display_ opts) + (precedingps, displayableps, _) = dbg "ps4" $ postingsMatchingDisplayExpr displayexpr + $ dbg "ps3" $ (if related_ opts then concatMap relatedPostings else id) $ dbg "ps2" $ filter (q' `matchesPosting`) $ dbg "ps1" $ journalPostings j' dbg :: Show a => String -> a -> a diff --git a/hledger/Hledger/Cli/Options.hs b/hledger/Hledger/Cli/Options.hs index 82d9ce26c..306bdf499 100644 --- a/hledger/Hledger/Cli/Options.hs +++ b/hledger/Hledger/Cli/Options.hs @@ -215,6 +215,7 @@ postingsmode = (commandmode ["register","postings"]) { ,modeGroupFlags = Group { groupUnnamed = [ flagOpt (show defaultWidthWithFlag) ["width","w"] (\s opts -> Right $ setopt "width" s opts) "N" "increase or set the output width (default: 80)" + ,flagNone ["related","r"] (\opts -> setboolopt "related" opts) "show the other postings in the transactions of those that would have been shown" ] ,groupHidden = [] ,groupNamed = [(generalflagstitle, generalflags1)] @@ -336,7 +337,7 @@ toCliOpts rawopts = do ,cost_ = boolopt "cost" rawopts ,depth_ = maybeintopt "depth" rawopts ,display_ = maybedisplayopt d rawopts - ,date2_ = boolopt "date2" rawopts + ,date2_ = boolopt "date2" rawopts ,empty_ = boolopt "empty" rawopts ,no_elide_ = boolopt "no-elide" rawopts ,real_ = boolopt "real" rawopts @@ -349,6 +350,7 @@ toCliOpts rawopts = do ,quarterly_ = boolopt "quarterly" rawopts ,yearly_ = boolopt "yearly" rawopts ,format_ = maybestringopt "format" rawopts + ,related_ = boolopt "related" rawopts -- register ,query_ = unwords $ listofstringopt "args" rawopts } }