From 27621515ea0c99c38237b05043d0f14f5eebfff9 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Thu, 6 Dec 2012 01:10:15 +0000 Subject: [PATCH] bal, reg: use posting date if specified, clarify posting effective date not yet supported --- MANUAL.md | 11 ++++++----- hledger-lib/Hledger/Data/Posting.hs | 9 +++++++++ hledger-lib/Hledger/Data/Types.hs | 2 +- hledger-lib/Hledger/Query.hs | 12 ++---------- hledger-lib/Hledger/Reports.hs | 13 +++++++------ 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/MANUAL.md b/MANUAL.md index 5a8f9d8a5..f5776f1f3 100644 --- a/MANUAL.md +++ b/MANUAL.md @@ -410,13 +410,14 @@ NAME=EXACTVALUE` on the command line. ### Posting dates -You can give individual postings a different date (or dates) from their parent transaction, -by adding posting tags `date:ACTUALDATE` and/or `date2:EFFECTIVEDATE`. +You can give individual postings a different date from their parent +transaction, by adding posting tag `date:ACTUALDATE`. If present, this +date will be used by the register and balance reports. For compatibility, ledger's posting date syntax is also supported -(`[ACTUALDATE]`, `[=EFFECTIVEDATE]` or `[ACTUALDATE=EFFECTIVEDATE]` in a -posting comment), and treated as an alterate spelling of the date and -date2 tags. +(`[ACTUALDATE]`, `[ACTUALDATE=EFFECTIVEDATE]` or `[=EFFECTIVEDATE]` in a +posting comment) and treated as an alterate spelling of the date: tag. +Note effective dates are not csurrently here are ignored, currently. ### Including other files diff --git a/hledger-lib/Hledger/Data/Posting.hs b/hledger-lib/Hledger/Data/Posting.hs index c22aeee77..4791a9c4f 100644 --- a/hledger-lib/Hledger/Data/Posting.hs +++ b/hledger-lib/Hledger/Data/Posting.hs @@ -23,6 +23,7 @@ module Hledger.Data.Posting ( transactionAllTags, -- * date operations postingDate, + postingEffectiveDate, isPostingInDateSpan, postingsDateSpan, -- * account name operations @@ -141,6 +142,14 @@ postingDate p = fromMaybe txndate $ pdate p where txndate = maybe nulldate tdate $ ptransaction p +-- | Get a posting's secondary (effective) date - it's own primary date if +-- specified (can't access posting secondary dates yet), otherwise the +-- parent transaction's effective date, otherwise the null date. +postingEffectiveDate :: Posting -> Day +postingEffectiveDate p = maybe nulldate transactionEffectiveDate $ ptransaction p + where + transactionEffectiveDate t = fromMaybe (tdate t) $ teffectivedate t + -- |Is this posting cleared? If this posting was individually marked -- as cleared, returns True. Otherwise, return the parent -- transaction's cleared status or, if there is no parent diff --git a/hledger-lib/Hledger/Data/Types.hs b/hledger-lib/Hledger/Data/Types.hs index 43b0e6801..7e114f8e0 100644 --- a/hledger-lib/Hledger/Data/Types.hs +++ b/hledger-lib/Hledger/Data/Types.hs @@ -76,7 +76,7 @@ data PostingType = RegularPosting | VirtualPosting | BalancedVirtualPosting type Tag = (String, String) data Posting = Posting { - pdate :: Maybe Day, -- ^ this posting's clearing date, if different from the transaction's + pdate :: Maybe Day, -- ^ this posting's date, if different from the transaction's pstatus :: Bool, paccount :: AccountName, pamount :: MixedAmount, diff --git a/hledger-lib/Hledger/Query.hs b/hledger-lib/Hledger/Query.hs index f0cf5aa7d..8dc984f7a 100644 --- a/hledger-lib/Hledger/Query.hs +++ b/hledger-lib/Hledger/Query.hs @@ -470,13 +470,8 @@ matchesPosting (Or qs) p = any (`matchesPosting` p) qs matchesPosting (And qs) p = all (`matchesPosting` p) qs matchesPosting (Desc r) p = regexMatchesCI r $ maybe "" tdescription $ ptransaction p matchesPosting (Acct r) p = regexMatchesCI r $ paccount p -matchesPosting (Date span) p = - case d of Just d' -> spanContainsDate span d' - Nothing -> False - where d = maybe Nothing (Just . tdate) $ ptransaction p -matchesPosting (EDate span) p = - case postingEffectiveDate p of Just d -> spanContainsDate span d - Nothing -> False +matchesPosting (Date span) p = span `spanContainsDate` postingDate p +matchesPosting (EDate span) p = span `spanContainsDate` postingEffectiveDate p matchesPosting (Status v) p = v == postingCleared p matchesPosting (Real v) p = v == isReal p matchesPosting (Depth d) Posting{paccount=a} = Depth d `matchesAccount` a @@ -560,9 +555,6 @@ matchTagName pat name = pat == name matchTagValue :: String -> String -> Bool matchTagValue pat value = regexMatchesCI pat value -postingEffectiveDate :: Posting -> Maybe Day -postingEffectiveDate p = maybe Nothing (Just . transactionEffectiveDate) $ ptransaction p - -- tests tests_Hledger_Query :: Test diff --git a/hledger-lib/Hledger/Reports.hs b/hledger-lib/Hledger/Reports.hs index b1e9512d1..036f07bbe 100644 --- a/hledger-lib/Hledger/Reports.hs +++ b/hledger-lib/Hledger/Reports.hs @@ -248,7 +248,7 @@ tests_entriesReport = [ type PostingsReport = (String -- label for the running balance column XXX remove ,[PostingsReportItem] -- line items, one per posting ) -type PostingsReportItem = (Maybe (Day, String) -- transaction date and description if this is the first posting +type PostingsReportItem = (Maybe (Day, String) -- posting date and description if this is the first posting ,Posting -- the posting, possibly with account name depth-clipped ,MixedAmount -- the running total after this posting ) @@ -296,9 +296,9 @@ postingsReportItems :: [Posting] -> Posting -> Int -> MixedAmount -> (MixedAmoun postingsReportItems [] _ _ _ _ = [] postingsReportItems (p:ps) pprev d b sumfn = i:(postingsReportItems ps p d b' sumfn) where - i = mkpostingsReportItem isfirst p' b' + i = mkpostingsReportItem isfirstintxn p' b' p' = p{paccount=clipAccountName d $ paccount p} - isfirst = ptransaction p /= ptransaction pprev + isfirstintxn = ptransaction p /= ptransaction pprev b' = b `sumfn` pamount p -- | Generate one postings report line item, given a flag indicating @@ -306,9 +306,10 @@ postingsReportItems (p:ps) pprev d b sumfn = i:(postingsReportItems ps p d b' su -- running balance. mkpostingsReportItem :: Bool -> Posting -> MixedAmount -> PostingsReportItem mkpostingsReportItem False p b = (Nothing, p, b) -mkpostingsReportItem True p b = (ds, p, b) - where ds = case ptransaction p of Just (Transaction{tdate=da,tdescription=de}) -> Just (da,de) - Nothing -> Just (nulldate,"") +mkpostingsReportItem True p b = (Just (date,desc), p, b) + where + date = postingDate p + desc = maybe "" tdescription $ ptransaction p -- | Date-sort and split a list of postings into three spans - postings matched -- by the given display expression, and the preceding and following postings.