ref!: postingDateOrDate2, transactionDateOrDate2, whichDateFromOpts -> whichDate (#1731)

This commit is contained in:
Simon Michael 2021-10-10 09:27:09 -10:00
parent 9ff5ac1288
commit b4c1188c26
5 changed files with 25 additions and 19 deletions

View File

@ -42,6 +42,7 @@ module Hledger.Data.Posting (
-- * date operations
postingDate,
postingDate2,
postingDateOrDate2,
isPostingInDateSpan,
isPostingInDateSpan',
-- * account name operations
@ -220,6 +221,11 @@ postingDate2 p = fromMaybe nulldate $ asum dates
, tdate <$> ptransaction p
]
-- | Get a posting's primary or secondary date, as specified.
postingDateOrDate2 :: WhichDate -> Posting -> Day
postingDateOrDate2 PrimaryDate = postingDate
postingDateOrDate2 SecondaryDate = postingDate2
-- | Get a posting's status. This is cleared or pending if those are
-- explicitly set on the posting, otherwise the status of its parent
-- transaction, or unmarked if there is no parent transaction. (Note

View File

@ -34,6 +34,7 @@ module Hledger.Data.Transaction
-- nonzerobalanceerror
-- * date operations
, transactionDate2
, transactionDateOrDate2
-- * transaction description parts
, transactionPayee
, transactionNote
@ -320,10 +321,15 @@ balancedVirtualPostings = filter isBalancedVirtual . tpostings
transactionsPostings :: [Transaction] -> [Posting]
transactionsPostings = concatMap tpostings
-- Get a transaction's secondary date, defaulting to the primary date.
-- Get a transaction's secondary date, or the primary date if there is none.
transactionDate2 :: Transaction -> Day
transactionDate2 t = fromMaybe (tdate t) $ tdate2 t
-- Get a transaction's primary or secondary date, as specified.
transactionDateOrDate2 :: WhichDate -> Transaction -> Day
transactionDateOrDate2 PrimaryDate = tdate
transactionDateOrDate2 SecondaryDate = transactionDate2
-- | Ensure a transaction's postings refer back to it, so that eg
-- relatedPostings works right.
txnTieKnot :: Transaction -> Transaction

View File

@ -244,9 +244,7 @@ getPostingsByColumn rspec j priceoracle reportspan =
ps = dbg5 "getPostingsByColumn" $ getPostings rspec j priceoracle
-- The date spans to be included as report columns.
colspans = dbg3 "colspans" $ splitSpan (interval_ $ _rsReportOpts rspec) reportspan
getDate = case whichDateFromOpts (_rsReportOpts rspec) of
PrimaryDate -> postingDate
SecondaryDate -> postingDate2
getDate = postingDateOrDate2 (whichDate (_rsReportOpts rspec))
-- | Gather postings matching the query within the report period.
getPostings :: ReportSpec -> Journal -> PriceOracle -> [Posting]

View File

@ -64,7 +64,7 @@ postingsReport :: ReportSpec -> Journal -> PostingsReport
postingsReport rspec@ReportSpec{_rsReportOpts=ropts@ReportOpts{..}} j = items
where
reportspan = reportSpanBothDates j rspec
whichdate = whichDateFromOpts ropts
whichdate = whichDate ropts
mdepth = queryDepth $ _rsQuery rspec
multiperiod = interval_ /= NoInterval
@ -116,9 +116,11 @@ matchedPostingsBeforeAndDuring rspec@ReportSpec{_rsReportOpts=ropts,_rsQuery=q}
dbg5 "beforeps, duringps" $ span (beforestartq `matchesPosting`) beforeandduringps
where
beforestartq = dbg3 "beforestartq" $ dateqtype $ DateSpan Nothing $ spanStart reportspan
beforeandduringps = sortOn (if date2_ ropts then postingDate2 else postingDate) -- sort postings by date or date2
beforeandduringps =
sortOn (postingDateOrDate2 (whichDate ropts)) -- sort postings by date or date2
. (if invert_ ropts then map negatePostingAmount else id) -- with --invert, invert amounts
. journalPostings $ journalValueAndFilterPostings rspec{_rsQuery=beforeandduringq} j
. journalPostings
$ journalValueAndFilterPostings rspec{_rsQuery=beforeandduringq} j
-- filter postings by the query, with no start date or depth limit
beforeandduringq = dbg4 "beforeandduringq" $ And [depthless $ dateless q, beforeendq]
@ -152,15 +154,12 @@ postingsReportItems ((p,mperiod):ps) (pprev,mperiodprev) wd d b runningcalcfn it
-- the transaction description.
mkpostingsReportItem :: Bool -> Bool -> WhichDate -> Maybe Period -> Posting -> MixedAmount -> PostingsReportItem
mkpostingsReportItem showdate showdesc wd mperiod p b =
(if showdate then Just date else Nothing
(if showdate then Just $ postingDateOrDate2 wd p else Nothing
,mperiod
,if showdesc then tdescription <$> ptransaction p else Nothing
,p
,b
)
where
date = case wd of PrimaryDate -> postingDate p
SecondaryDate -> postingDate2 p
-- | Convert a list of postings into summary postings, one per interval,
-- aggregated to the specified depth if any.
@ -170,13 +169,10 @@ summarisePostingsByInterval interval wd mdepth showempty reportspan =
concatMap (\(s,ps) -> summarisePostingsInDateSpan s wd mdepth showempty ps)
-- Group postings into their columns. We try to be efficient, since
-- there can possibly be a very large number of intervals (cf #1683)
. groupByDateSpan showempty getDate colspans
. groupByDateSpan showempty (postingDateOrDate2 wd) colspans
where
-- The date spans to be included as report columns.
colspans = splitSpan interval reportspan
getDate = case wd of
PrimaryDate -> postingDate
SecondaryDate -> postingDate2
-- | Given a date span (representing a report interval) and a list of
-- postings within it, aggregate the postings into one summary posting per

View File

@ -38,7 +38,7 @@ module Hledger.Reports.ReportOptions (
tree_,
reportOptsToggleStatus,
simplifyStatuses,
whichDateFromOpts,
whichDate,
journalValueAndFilterPostings,
journalValueAndFilterPostingsWith,
journalApplyValuationFromOpts,
@ -486,8 +486,8 @@ postingDateFn :: ReportOpts -> (Posting -> Day)
postingDateFn ReportOpts{..} = if date2_ then postingDate2 else postingDate
-- | Report which date we will report on based on --date2.
whichDateFromOpts :: ReportOpts -> WhichDate
whichDateFromOpts ReportOpts{..} = if date2_ then SecondaryDate else PrimaryDate
whichDate :: ReportOpts -> WhichDate
whichDate ReportOpts{..} = if date2_ then SecondaryDate else PrimaryDate
-- | Legacy-compatible convenience aliases for accountlistmode_.
tree_ :: ReportOpts -> Bool