{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-|

An account-centric transactions report.

-}

module Hledger.Reports.AccountTransactionsReport (
  AccountTransactionsReport,
  AccountTransactionsReportItem,
  accountTransactionsReport,
  accountTransactionsReportItems,
  transactionRegisterDate,
  triOrigTransaction,
  triDate,
  triAmount,
  triBalance,
  triCommodityAmount,
  triCommodityBalance,
  accountTransactionsReportByCommodity,
  tests_AccountTransactionsReport
)
where

import Data.List (mapAccumR, nub, partition, sortBy)
import Data.List.Extra (nubSort)
import Data.Maybe (catMaybes)
import Data.Ord (Down(..), comparing)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Time.Calendar (Day)

import Hledger.Data
import Hledger.Query
import Hledger.Reports.ReportOptions
import Hledger.Utils


-- | An account transactions report represents transactions affecting
-- a particular account (or possibly several accounts, but we don't
-- use that). It is used eg by hledger-ui's and hledger-web's register
-- view, and hledger's aregister report, where we want to show one row
-- per transaction, in the context of the current account. Report
-- items consist of:
--
-- - the transaction, unmodified
--
-- - the transaction as seen in the context of the current account and query,
--   which means:
--
--   - the transaction date is set to the "transaction context date":
--     the earliest of the transaction date and any other posting dates
--     of postings to the current account (matched by the report query).
--
--   - the transaction's postings are filtered, excluding any which are not
--     matched by the report query
--
-- - a text description of the other account(s) posted to/from
--
-- - a flag indicating whether there's more than one other account involved
--
-- - the total increase/decrease to the current account
--
-- - the report transactions' running total after this transaction;
--   or if historical balance is requested (-H), the historical running total.
--   The historical running total includes transactions from before the
--   report start date if one is specified, filtered by the report query.
--   The historical running total may or may not be the account's historical
--   running balance, depending on the report query.
--
-- Items are sorted by transaction register date (the earliest date the transaction
-- posts to the current account), most recent first.
-- Reporting intervals are currently ignored.
--
type AccountTransactionsReport = [AccountTransactionsReportItem] -- line items, one per transaction

type AccountTransactionsReportItem =
  (
   Transaction -- the transaction, unmodified
  ,Transaction -- the transaction, as seen from the current account
  ,Bool        -- is this a split (more than one posting to other accounts) ?
  ,Text        -- a display string describing the other account(s), if any
  ,MixedAmount -- the amount posted to the current account(s) (or total amount posted)
  ,MixedAmount -- the register's running total or the current account(s)'s historical balance, after this transaction
  )

triOrigTransaction :: (a, b, c, d, e, f) -> a
triOrigTransaction (a
torig,b
_,c
_,d
_,e
_,f
_) = a
torig
triDate :: (a, Transaction, c, d, e, f) -> Day
triDate (a
_,Transaction
tacct,c
_,d
_,e
_,f
_) = Transaction -> Day
tdate Transaction
tacct
triAmount :: (a, b, c, d, e, f) -> e
triAmount (a
_,b
_,c
_,d
_,e
a,f
_) = e
a
triBalance :: (a, b, c, d, e, f) -> f
triBalance (a
_,b
_,c
_,d
_,e
_,f
a) = f
a
triCommodityAmount :: Text -> (a, b, c, d, MixedAmount, f) -> MixedAmount
triCommodityAmount Text
c = Text -> MixedAmount -> MixedAmount
filterMixedAmountByCommodity Text
c  forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a} {b} {c} {d} {e} {f}. (a, b, c, d, e, f) -> e
triAmount
triCommodityBalance :: Text -> (a, b, c, d, e, MixedAmount) -> MixedAmount
triCommodityBalance Text
c = Text -> MixedAmount -> MixedAmount
filterMixedAmountByCommodity Text
c  forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a} {b} {c} {d} {e} {f}. (a, b, c, d, e, f) -> f
triBalance

accountTransactionsReport :: ReportSpec -> Journal -> Query -> AccountTransactionsReport
accountTransactionsReport :: ReportSpec -> Journal -> Query -> AccountTransactionsReport
accountTransactionsReport rspec :: ReportSpec
rspec@ReportSpec{_rsReportOpts :: ReportSpec -> ReportOpts
_rsReportOpts=ReportOpts
ropts} Journal
j Query
thisacctq = AccountTransactionsReport
items
  where
    -- A depth limit should not affect the account transactions report; it should show all transactions in/below this account.
    -- Queries on currency or amount are also ignored at this stage; they are handled earlier, before valuation.
    reportq :: Query
reportq = Query -> Query
simplifyQuery forall a b. (a -> b) -> a -> b
$ [Query] -> Query
And [Query
aregisterq, Query
periodq]
      where
        aregisterq :: Query
aregisterq = (Query -> Bool) -> Query -> Query
filterQuery (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query -> Bool
queryIsCurOrAmt) forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Query -> Bool) -> Query -> Query
filterQuery (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query -> Bool
queryIsDepth) forall a b. (a -> b) -> a -> b
$ ReportSpec -> Query
_rsQuery ReportSpec
rspec
        periodq :: Query
periodq = DateSpan -> Query
Date forall b c a. (b -> c) -> (a -> b) -> a -> c
. Period -> DateSpan
periodAsDateSpan forall a b. (a -> b) -> a -> b
$ ReportOpts -> Period
period_ ReportOpts
ropts
    amtq :: Query
amtq = (Query -> Bool) -> Query -> Query
filterQuery Query -> Bool
queryIsCurOrAmt forall a b. (a -> b) -> a -> b
$ ReportSpec -> Query
_rsQuery ReportSpec
rspec
    queryIsCurOrAmt :: Query -> Bool
queryIsCurOrAmt Query
q = Query -> Bool
queryIsSym Query
q Bool -> Bool -> Bool
|| Query -> Bool
queryIsAmt Query
q
    wd :: WhichDate
wd = ReportOpts -> WhichDate
whichDate ReportOpts
ropts

    -- Note that within this function, we are only allowed limited
    -- transformation of the transaction postings: this is due to the need to
    -- pass the original transactions into accountTransactionsReportItem.
    -- Generally, we either include a transaction in full, or not at all.
    -- Do some limited filtering and valuing of the journal's transactions:
    -- - filter them by the account query if any,
    -- - discard amounts not matched by the currency and amount query if any,
    -- - then apply valuation if any.
    -- Additional reportq filtering, such as date filtering, happens down in 
    -- accountTransactionsReportItem, which discards transactions with no matched postings.
    acctJournal :: Journal
acctJournal =
        -- With most calls we will not require transaction prices past this point, and can get a big
        -- speed improvement by stripping them early. In some cases, such as in hledger-ui, we still
        -- want to keep prices around, so we can toggle between cost and no cost quickly. We can use
        -- the show_costs_ flag to be efficient when we can, and detailed when we have to.
          (if ReportOpts -> Bool
show_costs_ ReportOpts
ropts then forall a. a -> a
id else (MixedAmount -> MixedAmount) -> Journal -> Journal
journalMapPostingAmounts MixedAmount -> MixedAmount
mixedAmountStripPrices)
        forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> (a -> String) -> a -> a
traceOrLogAtWith Int
5 ((String
"ts3:\n"forall a. [a] -> [a] -> [a]
++)forall b c a. (b -> c) -> (a -> b) -> a -> c
.[Transaction] -> String
pshowTransactionsforall b c a. (b -> c) -> (a -> b) -> a -> c
.Journal -> [Transaction]
jtxns)
        -- maybe convert these transactions to cost or value
        forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReportSpec -> Journal -> Journal
journalApplyValuationFromOpts ReportSpec
rspec
        forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> (a -> String) -> a -> a
traceOrLogAtWith Int
5 ((String
"ts2:\n"forall a. [a] -> [a] -> [a]
++)forall b c a. (b -> c) -> (a -> b) -> a -> c
.[Transaction] -> String
pshowTransactionsforall b c a. (b -> c) -> (a -> b) -> a -> c
.Journal -> [Transaction]
jtxns)
        -- apply any cur:SYM filters in reportq
        forall b c a. (b -> c) -> (a -> b) -> a -> c
. (if Query -> Bool
queryIsNull Query
amtq then forall a. a -> a
id else Query -> Journal -> Journal
filterJournalAmounts Query
amtq)
        -- only consider transactions which match thisacctq (possibly excluding postings
        -- which are not real or have the wrong status)
        forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> String -> a -> a
traceOrLogAt Int
3 (String
"thisacctq: "forall a. [a] -> [a] -> [a]
++forall a. Show a => a -> String
show Query
thisacctq)
        forall a b. (a -> b) -> a -> b
$ forall a. Int -> (a -> String) -> a -> a
traceOrLogAtWith Int
5 ((String
"ts1:\n"forall a. [a] -> [a] -> [a]
++)forall b c a. (b -> c) -> (a -> b) -> a -> c
.[Transaction] -> String
pshowTransactionsforall b c a. (b -> c) -> (a -> b) -> a -> c
.Journal -> [Transaction]
jtxns)
          Journal
j{jtxns :: [Transaction]
jtxns = forall a. (a -> Bool) -> [a] -> [a]
filter (Query -> Transaction -> Bool
matchesTransaction Query
thisacctq forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> Transaction
relevantPostings) forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
j}
      where
        relevantPostings :: Transaction -> Transaction
relevantPostings
          | Query -> Bool
queryIsNull Query
realq Bool -> Bool -> Bool
&& Query -> Bool
queryIsNull Query
statusq = forall a. a -> a
id
          | Bool
otherwise = Query -> Transaction -> Transaction
filterTransactionPostings forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query -> Query
simplifyQuery forall a b. (a -> b) -> a -> b
$ [Query] -> Query
And [Query
realq, Query
statusq]
        realq :: Query
realq   = (Query -> Bool) -> Query -> Query
filterQuery Query -> Bool
queryIsReal Query
reportq
        statusq :: Query
statusq = (Query -> Bool) -> Query -> Query
filterQuery Query -> Bool
queryIsStatus Query
reportq

    startbal :: MixedAmount
startbal
      | ReportOpts -> BalanceAccumulation
balanceaccum_ ReportOpts
ropts forall a. Eq a => a -> a -> Bool
== BalanceAccumulation
Historical = [Posting] -> MixedAmount
sumPostings [Posting]
priorps
      | Bool
otherwise                         = MixedAmount
nullmixedamt
      where
        priorps :: [Posting]
priorps = forall a. Show a => String -> a -> a
dbg5 String
"priorps" forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [Posting]
journalPostings forall a b. (a -> b) -> a -> b
$ Query -> Journal -> Journal
filterJournalPostings Query
priorq Journal
acctJournal
        priorq :: Query
priorq = forall a. Show a => String -> a -> a
dbg5 String
"priorq" forall a b. (a -> b) -> a -> b
$ [Query] -> Query
And [Query
thisacctq, Query
tostartdateq, Query
datelessreportq]
        tostartdateq :: Query
tostartdateq =
          case Maybe Day
mstartdate of
            Just Day
_  -> DateSpan -> Query
Date (Maybe EFDay -> Maybe EFDay -> DateSpan
DateSpan forall a. Maybe a
Nothing (Day -> EFDay
Exact forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Day
mstartdate))
            Maybe Day
Nothing -> Query
None  -- no start date specified, there are no prior postings
        mstartdate :: Maybe Day
mstartdate = Bool -> Query -> Maybe Day
queryStartDate (ReportOpts -> Bool
date2_ ReportOpts
ropts) Query
reportq
        datelessreportq :: Query
datelessreportq = (Query -> Bool) -> Query -> Query
filterQuery (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query -> Bool
queryIsDateOrDate2) Query
reportq

    items :: AccountTransactionsReport
items =
        Query
-> Query
-> MixedAmount
-> (MixedAmount -> MixedAmount)
-> (Text -> Maybe AccountType)
-> [(Day, Transaction)]
-> AccountTransactionsReport
accountTransactionsReportItems Query
reportq Query
thisacctq MixedAmount
startbal MixedAmount -> MixedAmount
maNegate (Journal -> Text -> Maybe AccountType
journalAccountType Journal
j)
      -- sort by the transaction's register date, then index, for accurate starting balance
      forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> (a -> String) -> a -> a
traceAtWith Int
5 ((String
"ts4:\n"forall a. [a] -> [a] -> [a]
++)forall b c a. (b -> c) -> (a -> b) -> a -> c
.[Transaction] -> String
pshowTransactionsforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> b
snd)
      forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing (forall a. a -> Down a
Down forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) forall a. Semigroup a => a -> a -> a
<> forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing (forall a. a -> Down a
Down forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> Integer
tindex forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd))
      forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (\Transaction
t -> (WhichDate -> Query -> Query -> Transaction -> Day
transactionRegisterDate WhichDate
wd Query
reportq Query
thisacctq Transaction
t, Transaction
t))
      forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
acctJournal

pshowTransactions :: [Transaction] -> String
pshowTransactions :: [Transaction] -> String
pshowTransactions = forall a. Show a => a -> String
pshow forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (\Transaction
t -> [String] -> String
unwords [forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ Transaction -> Day
tdate Transaction
t, Text -> String
T.unpack forall a b. (a -> b) -> a -> b
$ Transaction -> Text
tdescription Transaction
t])

-- | Generate transactions report items from a list of transactions,
-- using the provided user-specified report query, a query specifying
-- which account to use as the focus, a starting balance, and a sign-setting
-- function.
-- Each transaction is accompanied by the date that should be shown for it
-- in the report. This is not necessarily the transaction date - see
-- transactionRegisterDate.
accountTransactionsReportItems :: Query -> Query -> MixedAmount -> (MixedAmount -> MixedAmount)
                               -> (AccountName -> Maybe AccountType) -> [(Day, Transaction)]
                               -> [AccountTransactionsReportItem]
accountTransactionsReportItems :: Query
-> Query
-> MixedAmount
-> (MixedAmount -> MixedAmount)
-> (Text -> Maybe AccountType)
-> [(Day, Transaction)]
-> AccountTransactionsReport
accountTransactionsReportItems Query
reportq Query
thisacctq MixedAmount
bal MixedAmount -> MixedAmount
signfn Text -> Maybe AccountType
accttypefn =
    forall a. [Maybe a] -> [a]
catMaybes forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) s a b.
Traversable t =>
(s -> a -> (s, b)) -> s -> t a -> (s, t b)
mapAccumR (Query
-> Query
-> (MixedAmount -> MixedAmount)
-> (Text -> Maybe AccountType)
-> MixedAmount
-> (Day, Transaction)
-> (MixedAmount, Maybe AccountTransactionsReportItem)
accountTransactionsReportItem Query
reportq Query
thisacctq MixedAmount -> MixedAmount
signfn Text -> Maybe AccountType
accttypefn) MixedAmount
bal

accountTransactionsReportItem :: Query -> Query -> (MixedAmount -> MixedAmount)
                              -> (AccountName -> Maybe AccountType) -> MixedAmount -> (Day, Transaction)
                              -> (MixedAmount, Maybe AccountTransactionsReportItem)
accountTransactionsReportItem :: Query
-> Query
-> (MixedAmount -> MixedAmount)
-> (Text -> Maybe AccountType)
-> MixedAmount
-> (Day, Transaction)
-> (MixedAmount, Maybe AccountTransactionsReportItem)
accountTransactionsReportItem Query
reportq Query
thisacctq MixedAmount -> MixedAmount
signfn Text -> Maybe AccountType
accttypefn MixedAmount
bal (Day
d, Transaction
t)
    -- 201407: I've lost my grip on this, let's just hope for the best
    -- 201606: we now calculate change and balance from filtered postings, check this still works well for all callers XXX
    | forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Posting]
reportps = (MixedAmount
bal, forall a. Maybe a
Nothing)  -- no matched postings in this transaction, skip it
    | Bool
otherwise     = (MixedAmount
bal', forall a. a -> Maybe a
Just (Transaction
t, Transaction
tacct{tdate :: Day
tdate=Day
d}, Int
numotheraccts forall a. Ord a => a -> a -> Bool
> Int
1, Text
otheracctstr, MixedAmount
amt, MixedAmount
bal'))
    where
      tacct :: Transaction
tacct@Transaction{tpostings :: Transaction -> [Posting]
tpostings=[Posting]
reportps} = (Text -> Maybe AccountType) -> Query -> Transaction -> Transaction
filterTransactionPostingsExtra Text -> Maybe AccountType
accttypefn Query
reportq Transaction
t  -- TODO needs to consider --date2, #1731
      ([Posting]
thisacctps, [Posting]
otheracctps) = forall a. (a -> Bool) -> [a] -> ([a], [a])
partition (Query -> Posting -> Bool
matchesPosting Query
thisacctq) [Posting]
reportps
      numotheraccts :: Int
numotheraccts = forall (t :: * -> *) a. Foldable t => t a -> Int
length forall a b. (a -> b) -> a -> b
$ forall a. Eq a => [a] -> [a]
nub forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Posting -> Text
paccount [Posting]
otheracctps
      otheracctstr :: Text
otheracctstr | Query
thisacctq forall a. Eq a => a -> a -> Bool
== Query
None  = [Posting] -> Text
summarisePostingAccounts [Posting]
reportps     -- no current account ? summarise all matched postings
                   | Int
numotheraccts forall a. Eq a => a -> a -> Bool
== Int
0 = [Posting] -> Text
summarisePostingAccounts [Posting]
thisacctps   -- only postings to current account ? summarise those
                   | Bool
otherwise          = [Posting] -> Text
summarisePostingAccounts [Posting]
otheracctps  -- summarise matched postings to other account(s)
      -- 202302: Impact of t on thisacct - normally the sum of thisacctps,
      -- but if they are null it probably means reportq is an account filter
      -- and we should sum otheracctps instead.
      -- This fixes hledger areg ACCT ACCT2 (#2007), hopefully it's correct in general.
      amt :: MixedAmount
amt
        | forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Posting]
thisacctps = MixedAmount -> MixedAmount
signfn forall a b. (a -> b) -> a -> b
$ [Posting] -> MixedAmount
sumPostings [Posting]
otheracctps
        | Bool
otherwise       = MixedAmount -> MixedAmount
signfn forall b c a. (b -> c) -> (a -> b) -> a -> c
. MixedAmount -> MixedAmount
maNegate forall a b. (a -> b) -> a -> b
$ [Posting] -> MixedAmount
sumPostings [Posting]
thisacctps
      bal' :: MixedAmount
bal' = MixedAmount
bal MixedAmount -> MixedAmount -> MixedAmount
`maPlus` MixedAmount
amt

-- TODO needs checking, cf #1731
-- | What date should be shown for a transaction in an account register report ?
-- This will be in context of a particular account (the "this account" query)
-- and any additional report query. It could be:
--
-- - if postings are matched by both thisacctq and reportq, the earliest of those
--   matched postings' dates (or their secondary dates if --date2 was used)
--
-- - the transaction date, or its secondary date if --date2 was used.
--
transactionRegisterDate :: WhichDate -> Query -> Query -> Transaction -> Day
transactionRegisterDate :: WhichDate -> Query -> Query -> Transaction -> Day
transactionRegisterDate WhichDate
wd Query
reportq Query
thisacctq Transaction
t
  | Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Posting]
thisacctps = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (WhichDate -> Posting -> Day
postingDateOrDate2 WhichDate
wd) [Posting]
thisacctps
  | Bool
otherwise             = WhichDate -> Transaction -> Day
transactionDateOrDate2 WhichDate
wd Transaction
t
  where
    reportps :: [Posting]
reportps   = Transaction -> [Posting]
tpostings forall a b. (a -> b) -> a -> b
$ Query -> Transaction -> Transaction
filterTransactionPostings Query
reportq Transaction
t
    thisacctps :: [Posting]
thisacctps = forall a. (a -> Bool) -> [a] -> [a]
filter (Query -> Posting -> Bool
matchesPosting Query
thisacctq) [Posting]
reportps

-- -- | Generate a short readable summary of some postings, like
-- -- "from (negatives) to (positives)".
-- summarisePostings :: [Posting] -> String
-- summarisePostings ps =
--     case (summarisePostingAccounts froms, summarisePostingAccounts tos) of
--        ("",t) -> "to "++t
--        (f,"") -> "from "++f
--        (f,t)  -> "from "++f++" to "++t
--     where
--       (froms,tos) = partition (fromMaybe False . isNegativeMixedAmount . pamount) ps

-- | Generate a simplified summary of some postings' accounts.
-- To reduce noise, if there are both real and virtual postings, show only the real ones.
summarisePostingAccounts :: [Posting] -> Text
summarisePostingAccounts :: [Posting] -> Text
summarisePostingAccounts [Posting]
ps =
    Text -> [Text] -> Text
T.intercalate Text
", " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
accountSummarisedName forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => [a] -> [a]
nub forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Posting -> Text
paccount [Posting]
displayps
  where
    realps :: [Posting]
realps = forall a. (a -> Bool) -> [a] -> [a]
filter Posting -> Bool
isReal [Posting]
ps
    displayps :: [Posting]
displayps | forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Posting]
realps = [Posting]
ps
              | Bool
otherwise   = [Posting]
realps

-- | Split an  account transactions report whose items may involve several commodities,
-- into one or more single-commodity account transactions reports.
accountTransactionsReportByCommodity :: AccountTransactionsReport -> [(CommoditySymbol, AccountTransactionsReport)]
accountTransactionsReportByCommodity :: AccountTransactionsReport -> [(Text, AccountTransactionsReport)]
accountTransactionsReportByCommodity AccountTransactionsReport
tr =
  [(Text
c, Text -> AccountTransactionsReport -> AccountTransactionsReport
filterAccountTransactionsReportByCommodity Text
c AccountTransactionsReport
tr) | Text
c <- forall {a} {b} {c} {d} {f}.
[(a, b, c, d, MixedAmount, f)] -> [Text]
commodities AccountTransactionsReport
tr]
  where
    commodities :: [(a, b, c, d, MixedAmount, f)] -> [Text]
commodities = forall a. Ord a => [a] -> [a]
nubSort forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map Amount -> Text
acommodity forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (MixedAmount -> [Amount]
amounts forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a} {b} {c} {d} {e} {f}. (a, b, c, d, e, f) -> e
triAmount)

-- | Remove account transaction report items and item amount (and running
-- balance amount) components that don't involve the specified
-- commodity. Other item fields such as the transaction are left unchanged.
filterAccountTransactionsReportByCommodity :: CommoditySymbol -> AccountTransactionsReport -> AccountTransactionsReport
filterAccountTransactionsReportByCommodity :: Text -> AccountTransactionsReport -> AccountTransactionsReport
filterAccountTransactionsReportByCommodity Text
comm =
    forall {a} {b} {c} {d}.
[(a, b, c, d, MixedAmount, MixedAmount)]
-> [(a, b, c, d, MixedAmount, MixedAmount)]
fixTransactionsReportItemBalances forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (forall {a} {b} {c} {d} {f}.
Text
-> (a, b, c, d, MixedAmount, f) -> [(a, b, c, d, MixedAmount, f)]
filterTransactionsReportItemByCommodity Text
comm)
  where
    filterTransactionsReportItemByCommodity :: Text
-> (a, b, c, d, MixedAmount, f) -> [(a, b, c, d, MixedAmount, f)]
filterTransactionsReportItemByCommodity Text
c (a
t,b
t2,c
s,d
o,MixedAmount
a,f
bal)
      | Text
c forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
cs = [(a, b, c, d, MixedAmount, f)
item']
      | Bool
otherwise   = []
      where
        cs :: [Text]
cs = forall a b. (a -> b) -> [a] -> [b]
map Amount -> Text
acommodity forall a b. (a -> b) -> a -> b
$ MixedAmount -> [Amount]
amounts MixedAmount
a
        item' :: (a, b, c, d, MixedAmount, f)
item' = (a
t,b
t2,c
s,d
o,MixedAmount
a',f
bal)
        a' :: MixedAmount
a' = Text -> MixedAmount -> MixedAmount
filterMixedAmountByCommodity Text
c MixedAmount
a

    fixTransactionsReportItemBalances :: [(a, b, c, d, MixedAmount, MixedAmount)]
-> [(a, b, c, d, MixedAmount, MixedAmount)]
fixTransactionsReportItemBalances [] = []
    fixTransactionsReportItemBalances [(a, b, c, d, MixedAmount, MixedAmount)
i] = [(a, b, c, d, MixedAmount, MixedAmount)
i]
    fixTransactionsReportItemBalances [(a, b, c, d, MixedAmount, MixedAmount)]
items = forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ (a, b, c, d, MixedAmount, MixedAmount)
iforall a. a -> [a] -> [a]
:(forall {a} {b} {c} {d} {f}.
MixedAmount
-> [(a, b, c, d, MixedAmount, f)]
-> [(a, b, c, d, MixedAmount, MixedAmount)]
go MixedAmount
startbal [(a, b, c, d, MixedAmount, MixedAmount)]
is)
      where
        (a, b, c, d, MixedAmount, MixedAmount)
i:[(a, b, c, d, MixedAmount, MixedAmount)]
is = forall a. [a] -> [a]
reverse [(a, b, c, d, MixedAmount, MixedAmount)]
items
        startbal :: MixedAmount
startbal = Text -> MixedAmount -> MixedAmount
filterMixedAmountByCommodity Text
comm forall a b. (a -> b) -> a -> b
$ forall {a} {b} {c} {d} {e} {f}. (a, b, c, d, e, f) -> f
triBalance (a, b, c, d, MixedAmount, MixedAmount)
i
        go :: MixedAmount
-> [(a, b, c, d, MixedAmount, f)]
-> [(a, b, c, d, MixedAmount, MixedAmount)]
go MixedAmount
_ [] = []
        go MixedAmount
bal ((a
t,b
t2,c
s,d
o,MixedAmount
amt,f
_):[(a, b, c, d, MixedAmount, f)]
is') = (a
t,b
t2,c
s,d
o,MixedAmount
amt,MixedAmount
bal')forall a. a -> [a] -> [a]
:MixedAmount
-> [(a, b, c, d, MixedAmount, f)]
-> [(a, b, c, d, MixedAmount, MixedAmount)]
go MixedAmount
bal' [(a, b, c, d, MixedAmount, f)]
is'
          where bal' :: MixedAmount
bal' = MixedAmount
bal MixedAmount -> MixedAmount -> MixedAmount
`maPlus` MixedAmount
amt

-- tests

tests_AccountTransactionsReport :: TestTree
tests_AccountTransactionsReport = String -> [TestTree] -> TestTree
testGroup String
"AccountTransactionsReport" [
 ]