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.
This commit is contained in:
Simon Michael 2017-01-13 02:07:26 -08:00
parent 80827321c4
commit 2e61c037c3

View File

@ -44,6 +44,10 @@ module Hledger.Data.Posting (
concatAccountNames, concatAccountNames,
accountNameApplyAliases, accountNameApplyAliases,
accountNameApplyAliasesMemo, accountNameApplyAliasesMemo,
-- * transaction description operations
transactionPayee,
transactionNote,
payeeAndNoteFromDescription,
-- * arithmetic -- * arithmetic
sumPostings, sumPostings,
-- * rendering -- * rendering
@ -173,9 +177,25 @@ postingStatus Posting{pstatus=s, ptransaction=mt}
transactionImplicitTags :: Transaction -> [Tag] transactionImplicitTags :: Transaction -> [Tag]
transactionImplicitTags t = filter (not . T.null . snd) [("code", tcode t) transactionImplicitTags t = filter (not . T.null . snd) [("code", tcode t)
,("description", tdescription 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. -- | Tags for this posting including implicit and any inherited from its parent transaction.
postingAllImplicitTags :: Posting -> [Tag] postingAllImplicitTags :: Posting -> [Tag]
postingAllImplicitTags p = ptags p ++ maybe [] transactionTags (ptransaction p) postingAllImplicitTags p = ptags p ++ maybe [] transactionTags (ptransaction p)