1{-# LANGUAGE OverloadedStrings, RecordWildCards, FlexibleInstances, ScopedTypeVariables #-}
2{-|
3
4Journal entries report, used by the print command.
5
6-}
7
8module Hledger.Reports.EntriesReport (
9  EntriesReport,
10  EntriesReportItem,
11  entriesReport,
12  -- * Tests
13  tests_EntriesReport
14)
15where
16
17import Data.List (sortBy)
18import Data.Maybe (fromMaybe)
19import Data.Ord (comparing)
20import Data.Time (fromGregorian)
21
22import Hledger.Data
23import Hledger.Query
24import Hledger.Reports.ReportOptions
25import Hledger.Utils
26
27
28-- | A journal entries report is a list of whole transactions as
29-- originally entered in the journal (mostly). This is used by eg
30-- hledger's print command and hledger-web's journal entries view.
31type EntriesReport = [EntriesReportItem]
32type EntriesReportItem = Transaction
33
34-- | Select transactions for an entries report.
35entriesReport :: ReportOpts -> Query -> Journal -> EntriesReport
36entriesReport ropts@ReportOpts{..} q j@Journal{..} =
37  sortBy (comparing getdate) $ filter (q `matchesTransaction`) $ map tvalue jtxns
38  where
39    getdate = transactionDateFn ropts
40    -- We may be converting posting amounts to value, per hledger_options.m4.md "Effect of --value on reports".
41    tvalue t@Transaction{..} = t{tpostings=map pvalue tpostings}
42      where
43        pvalue p = maybe p
44          (postingApplyValuation (journalPriceOracle infer_value_ j) (journalCommodityStyles j) periodlast mreportlast today False p)
45          value_
46          where
47            periodlast  = fromMaybe today $ reportPeriodOrJournalLastDay ropts j
48            mreportlast = reportPeriodLastDay ropts
49            today       = fromMaybe (error' "erValue: could not pick a valuation date, ReportOpts today_ is unset") today_  -- PARTIAL: should not happen
50
51tests_EntriesReport = tests "EntriesReport" [
52  tests "entriesReport" [
53     test "not acct" $ (length $ entriesReport defreportopts (Not . Acct $ toRegex' "bank") samplejournal) @?= 1
54    ,test "date" $ (length $ entriesReport defreportopts (Date $ DateSpan (Just $ fromGregorian 2008 06 01) (Just $ fromGregorian 2008 07 01)) samplejournal) @?= 3
55  ]
56 ]
57
58