From 2e61c037c3612f9524884eba3eb86f4f0d5c6ada Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 13 Jan 2017 02:07:26 -0800 Subject: [PATCH] cli: --pivot payee/note uses description parts before/after | This assumes a "PAYEE | NOTE" convention in the description field, similar to Beancount's journal syntax. If the description has no pipe character, payee and note are the same as the full description. --- hledger-lib/Hledger/Data/Posting.hs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/hledger-lib/Hledger/Data/Posting.hs b/hledger-lib/Hledger/Data/Posting.hs index b6be048e0..9be7b4b56 100644 --- a/hledger-lib/Hledger/Data/Posting.hs +++ b/hledger-lib/Hledger/Data/Posting.hs @@ -44,6 +44,10 @@ module Hledger.Data.Posting ( concatAccountNames, accountNameApplyAliases, accountNameApplyAliasesMemo, + -- * transaction description operations + transactionPayee, + transactionNote, + payeeAndNoteFromDescription, -- * arithmetic sumPostings, -- * rendering @@ -173,9 +177,25 @@ postingStatus Posting{pstatus=s, ptransaction=mt} transactionImplicitTags :: Transaction -> [Tag] transactionImplicitTags t = filter (not . T.null . snd) [("code", tcode t) ,("description", tdescription t) - ,("payee", tdescription t) + ,("payee", transactionPayee t) + ,("note", transactionNote t) ] +transactionPayee :: Transaction -> Text +transactionPayee = fst . payeeAndNoteFromDescription . tdescription + +transactionNote :: Transaction -> Text +transactionNote = fst . payeeAndNoteFromDescription . tdescription + +-- | Parse a transaction's description into payee and note (aka narration) fields, +-- assuming a convention of separating these with | (like Beancount). +-- Ie, everything up to the first | is the payee, everything after it is the note. +-- When there's no |, payee == note == description. +payeeAndNoteFromDescription :: Text -> (Text,Text) +payeeAndNoteFromDescription t = (textstrip p, textstrip $ T.tail n) + where + (p,n) = T.breakOn "|" t + -- | Tags for this posting including implicit and any inherited from its parent transaction. postingAllImplicitTags :: Posting -> [Tag] postingAllImplicitTags p = ptags p ++ maybe [] transactionTags (ptransaction p)