{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
module Hledger.Reports.AccountTransactionsReport (
AccountTransactionsReport,
AccountTransactionsReportItem,
accountTransactionsReport,
accountTransactionsReportItems,
transactionRegisterDate,
tests_AccountTransactionsReport
)
where
import Data.List (mapAccumR, nub, partition, sortBy)
import Data.Maybe (catMaybes)
import Data.Ord (Down(..), comparing)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Time.Calendar (Day, addDays)
import Hledger.Data
import Hledger.Query
import Hledger.Reports.ReportOptions
import Hledger.Utils
type AccountTransactionsReport = [AccountTransactionsReportItem]
type AccountTransactionsReportItem =
(
Transaction
,Transaction
,Bool
,Text
,MixedAmount
,MixedAmount
)
accountTransactionsReport :: ReportSpec -> Journal -> Query -> Query -> AccountTransactionsReport
accountTransactionsReport :: ReportSpec
-> Journal -> Query -> Query -> AccountTransactionsReport
accountTransactionsReport rspec :: ReportSpec
rspec@ReportSpec{rsOpts :: ReportSpec -> ReportOpts
rsOpts=ReportOpts
ropts} Journal
j Query
reportq' Query
thisacctq = AccountTransactionsReport
items
where
reportq :: Query
reportq = Query -> Query
simplifyQuery (Query -> Query) -> Query -> Query
forall a b. (a -> b) -> a -> b
$ [Query] -> Query
And [Query
aregisterq, Query
periodq, Maybe DateSpan -> Query
forall a. Maybe a -> Query
excludeforecastq (ReportOpts -> Maybe DateSpan
forecast_ ReportOpts
ropts)]
where
aregisterq :: Query
aregisterq = (Query -> Bool) -> Query -> Query
filterQuery (Bool -> Bool
not (Bool -> Bool) -> (Query -> Bool) -> Query -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query -> Bool
queryIsCurOrAmt) (Query -> Query) -> Query -> Query
forall a b. (a -> b) -> a -> b
$ (Query -> Bool) -> Query -> Query
filterQuery (Bool -> Bool
not (Bool -> Bool) -> (Query -> Bool) -> Query -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query -> Bool
queryIsDepth) Query
reportq'
periodq :: Query
periodq = DateSpan -> Query
Date (DateSpan -> Query) -> (Period -> DateSpan) -> Period -> Query
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Period -> DateSpan
periodAsDateSpan (Period -> Query) -> Period -> Query
forall a b. (a -> b) -> a -> b
$ ReportOpts -> Period
period_ ReportOpts
ropts
excludeforecastq :: Maybe a -> Query
excludeforecastq (Just a
_) = Query
Any
excludeforecastq Maybe a
Nothing =
[Query] -> Query
And [ Query -> Query
Not (Query -> Query) -> (DateSpan -> Query) -> DateSpan -> Query
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DateSpan -> Query
Date (DateSpan -> Query) -> DateSpan -> Query
forall a b. (a -> b) -> a -> b
$ Maybe Day -> Maybe Day -> DateSpan
DateSpan (Day -> Maybe Day
forall a. a -> Maybe a
Just (Day -> Maybe Day) -> (Day -> Day) -> Day -> Maybe Day
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Day -> Day
addDays Integer
1 (Day -> Maybe Day) -> Day -> Maybe Day
forall a b. (a -> b) -> a -> b
$ ReportSpec -> Day
rsToday ReportSpec
rspec) Maybe Day
forall a. Maybe a
Nothing
, Query -> Query
Not Query
generatedTransactionTag
]
amtq :: Query
amtq = (Query -> Bool) -> Query -> Query
filterQuery Query -> Bool
queryIsCurOrAmt Query
reportq'
queryIsCurOrAmt :: Query -> Bool
queryIsCurOrAmt Query
q = Query -> Bool
queryIsSym Query
q Bool -> Bool -> Bool
|| Query -> Bool
queryIsAmt Query
q
acctJournal :: Journal
acctJournal =
Int -> (Journal -> String) -> Journal -> Journal
forall a. Show a => Int -> (a -> String) -> a -> a
ptraceAtWith Int
5 ((String
"ts3:\n"String -> String -> String
forall a. [a] -> [a] -> [a]
++)(String -> String) -> (Journal -> String) -> Journal -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.[Transaction] -> String
pshowTransactions([Transaction] -> String)
-> (Journal -> [Transaction]) -> Journal -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Journal -> [Transaction]
jtxns)
(Journal -> Journal) -> (Journal -> Journal) -> Journal -> Journal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReportSpec -> Journal -> Journal
journalApplyValuationFromOpts ReportSpec
rspec
(Journal -> Journal) -> (Journal -> Journal) -> Journal -> Journal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> (Journal -> String) -> Journal -> Journal
forall a. Show a => Int -> (a -> String) -> a -> a
ptraceAtWith Int
5 ((String
"ts2:\n"String -> String -> String
forall a. [a] -> [a] -> [a]
++)(String -> String) -> (Journal -> String) -> Journal -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.[Transaction] -> String
pshowTransactions([Transaction] -> String)
-> (Journal -> [Transaction]) -> Journal -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Journal -> [Transaction]
jtxns)
(Journal -> Journal) -> (Journal -> Journal) -> Journal -> Journal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (if Query -> Bool
queryIsNull Query
amtq then Journal -> Journal
forall a. a -> a
id else Query -> Journal -> Journal
filterJournalAmounts Query
amtq)
(Journal -> Journal) -> (Journal -> Journal) -> Journal -> Journal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String -> Journal -> Journal
forall a. Int -> String -> a -> a
traceAt Int
3 (String
"thisacctq: "String -> String -> String
forall a. [a] -> [a] -> [a]
++Query -> String
forall a. Show a => a -> String
show Query
thisacctq)
(Journal -> Journal) -> Journal -> Journal
forall a b. (a -> b) -> a -> b
$ Int -> (Journal -> String) -> Journal -> Journal
forall a. Show a => Int -> (a -> String) -> a -> a
ptraceAtWith Int
5 ((String
"ts1:\n"String -> String -> String
forall a. [a] -> [a] -> [a]
++)(String -> String) -> (Journal -> String) -> Journal -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.[Transaction] -> String
pshowTransactions([Transaction] -> String)
-> (Journal -> [Transaction]) -> Journal -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Journal -> [Transaction]
jtxns)
Journal
j{jtxns :: [Transaction]
jtxns = (Transaction -> Bool) -> [Transaction] -> [Transaction]
forall a. (a -> Bool) -> [a] -> [a]
filter (Query -> Transaction -> Bool
matchesTransaction Query
thisacctq (Transaction -> Bool)
-> (Transaction -> Transaction) -> Transaction -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> Transaction
relevantPostings) ([Transaction] -> [Transaction]) -> [Transaction] -> [Transaction]
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 = Transaction -> Transaction
forall a. a -> a
id
| Bool
otherwise = Query -> Transaction -> Transaction
filterTransactionPostings (Query -> Transaction -> Transaction)
-> (Query -> Query) -> Query -> Transaction -> Transaction
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query -> Query
simplifyQuery (Query -> Transaction -> Transaction)
-> Query -> Transaction -> Transaction
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 -> BalanceType
balancetype_ ReportOpts
ropts BalanceType -> BalanceType -> Bool
forall a. Eq a => a -> a -> Bool
== BalanceType
HistoricalBalance = [Posting] -> MixedAmount
sumPostings [Posting]
priorps
| Bool
otherwise = MixedAmount
nullmixedamt
where
priorps :: [Posting]
priorps = String -> [Posting] -> [Posting]
forall a. Show a => String -> a -> a
dbg5 String
"priorps" ([Posting] -> [Posting])
-> (Journal -> [Posting]) -> Journal -> [Posting]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [Posting]
journalPostings (Journal -> [Posting]) -> Journal -> [Posting]
forall a b. (a -> b) -> a -> b
$ Query -> Journal -> Journal
filterJournalPostings Query
priorq Journal
acctJournal
priorq :: Query
priorq = String -> Query -> Query
forall a. Show a => String -> a -> a
dbg5 String
"priorq" (Query -> Query) -> Query -> Query
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 Day -> Maybe Day -> DateSpan
DateSpan Maybe Day
forall a. Maybe a
Nothing Maybe Day
mstartdate)
Maybe Day
Nothing -> Query
None
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 (Bool -> Bool) -> (Query -> Bool) -> Query -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query -> Bool
queryIsDateOrDate2) Query
reportq
items :: AccountTransactionsReport
items =
Query
-> Query
-> MixedAmount
-> (MixedAmount -> MixedAmount)
-> [(Day, Transaction)]
-> AccountTransactionsReport
accountTransactionsReportItems Query
reportq Query
thisacctq MixedAmount
startbal MixedAmount -> MixedAmount
maNegate
([(Day, Transaction)] -> AccountTransactionsReport)
-> ([Transaction] -> [(Day, Transaction)])
-> [Transaction]
-> AccountTransactionsReport
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int
-> ([(Day, Transaction)] -> String)
-> [(Day, Transaction)]
-> [(Day, Transaction)]
forall a. Show a => Int -> (a -> String) -> a -> a
ptraceAtWith Int
5 ((String
"ts4:\n"String -> String -> String
forall a. [a] -> [a] -> [a]
++)(String -> String)
-> ([(Day, Transaction)] -> String)
-> [(Day, Transaction)]
-> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.[Transaction] -> String
pshowTransactions([Transaction] -> String)
-> ([(Day, Transaction)] -> [Transaction])
-> [(Day, Transaction)]
-> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.((Day, Transaction) -> Transaction)
-> [(Day, Transaction)] -> [Transaction]
forall a b. (a -> b) -> [a] -> [b]
map (Day, Transaction) -> Transaction
forall a b. (a, b) -> b
snd)
([(Day, Transaction)] -> [(Day, Transaction)])
-> ([Transaction] -> [(Day, Transaction)])
-> [Transaction]
-> [(Day, Transaction)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Day, Transaction) -> (Day, Transaction) -> Ordering)
-> [(Day, Transaction)] -> [(Day, Transaction)]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (((Day, Transaction) -> Down Day)
-> (Day, Transaction) -> (Day, Transaction) -> Ordering
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing (((Day, Transaction) -> Down Day)
-> (Day, Transaction) -> (Day, Transaction) -> Ordering)
-> ((Day, Transaction) -> Down Day)
-> (Day, Transaction)
-> (Day, Transaction)
-> Ordering
forall a b. (a -> b) -> a -> b
$ Day -> Down Day
forall a. a -> Down a
Down (Day -> Down Day)
-> ((Day, Transaction) -> Day) -> (Day, Transaction) -> Down Day
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Day, Transaction) -> Day
forall a b. (a, b) -> a
fst)
([(Day, Transaction)] -> [(Day, Transaction)])
-> ([Transaction] -> [(Day, Transaction)])
-> [Transaction]
-> [(Day, Transaction)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Transaction -> (Day, Transaction))
-> [Transaction] -> [(Day, Transaction)]
forall a b. (a -> b) -> [a] -> [b]
map (\Transaction
t -> (Query -> Query -> Transaction -> Day
transactionRegisterDate Query
reportq Query
thisacctq Transaction
t, Transaction
t))
([Transaction] -> AccountTransactionsReport)
-> [Transaction] -> AccountTransactionsReport
forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
acctJournal
pshowTransactions :: [Transaction] -> String
pshowTransactions :: [Transaction] -> String
pshowTransactions = [String] -> String
forall a. Show a => a -> String
pshow ([String] -> String)
-> ([Transaction] -> [String]) -> [Transaction] -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Transaction -> String) -> [Transaction] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (\Transaction
t -> [String] -> String
unwords [Day -> String
forall a. Show a => a -> String
show (Day -> String) -> Day -> String
forall a b. (a -> b) -> a -> b
$ Transaction -> Day
tdate Transaction
t, Text -> String
T.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ Transaction -> Text
tdescription Transaction
t])
accountTransactionsReportItems :: Query -> Query -> MixedAmount -> (MixedAmount -> MixedAmount)
-> [(Day, Transaction)] -> [AccountTransactionsReportItem]
accountTransactionsReportItems :: Query
-> Query
-> MixedAmount
-> (MixedAmount -> MixedAmount)
-> [(Day, Transaction)]
-> AccountTransactionsReport
accountTransactionsReportItems Query
reportq Query
thisacctq MixedAmount
bal MixedAmount -> MixedAmount
signfn =
[Maybe AccountTransactionsReportItem] -> AccountTransactionsReport
forall a. [Maybe a] -> [a]
catMaybes ([Maybe AccountTransactionsReportItem]
-> AccountTransactionsReport)
-> ([(Day, Transaction)] -> [Maybe AccountTransactionsReportItem])
-> [(Day, Transaction)]
-> AccountTransactionsReport
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (MixedAmount, [Maybe AccountTransactionsReportItem])
-> [Maybe AccountTransactionsReportItem]
forall a b. (a, b) -> b
snd ((MixedAmount, [Maybe AccountTransactionsReportItem])
-> [Maybe AccountTransactionsReportItem])
-> ([(Day, Transaction)]
-> (MixedAmount, [Maybe AccountTransactionsReportItem]))
-> [(Day, Transaction)]
-> [Maybe AccountTransactionsReportItem]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (MixedAmount
-> (Day, Transaction)
-> (MixedAmount, Maybe AccountTransactionsReportItem))
-> MixedAmount
-> [(Day, Transaction)]
-> (MixedAmount, [Maybe AccountTransactionsReportItem])
forall (t :: * -> *) a b c.
Traversable t =>
(a -> b -> (a, c)) -> a -> t b -> (a, t c)
mapAccumR (Query
-> Query
-> (MixedAmount -> MixedAmount)
-> MixedAmount
-> (Day, Transaction)
-> (MixedAmount, Maybe AccountTransactionsReportItem)
accountTransactionsReportItem Query
reportq Query
thisacctq MixedAmount -> MixedAmount
signfn) MixedAmount
bal
accountTransactionsReportItem :: Query -> Query -> (MixedAmount -> MixedAmount) -> MixedAmount
-> (Day, Transaction) -> (MixedAmount, Maybe AccountTransactionsReportItem)
accountTransactionsReportItem :: Query
-> Query
-> (MixedAmount -> MixedAmount)
-> MixedAmount
-> (Day, Transaction)
-> (MixedAmount, Maybe AccountTransactionsReportItem)
accountTransactionsReportItem Query
reportq Query
thisacctq MixedAmount -> MixedAmount
signfn MixedAmount
bal (Day
d, Transaction
torig)
| [Posting] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Posting]
reportps = (MixedAmount
bal, Maybe AccountTransactionsReportItem
forall a. Maybe a
Nothing)
| Bool
otherwise = (MixedAmount
b, AccountTransactionsReportItem
-> Maybe AccountTransactionsReportItem
forall a. a -> Maybe a
Just (Transaction
torig, Transaction
tacct{tdate :: Day
tdate=Day
d}, Int
numotheraccts Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1, Text
otheracctstr, MixedAmount
a, MixedAmount
b))
where
tacct :: Transaction
tacct@Transaction{tpostings :: Transaction -> [Posting]
tpostings=[Posting]
reportps} = Query -> Transaction -> Transaction
filterTransactionPostings Query
reportq Transaction
torig
([Posting]
thisacctps, [Posting]
otheracctps) = (Posting -> Bool) -> [Posting] -> ([Posting], [Posting])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition (Query -> Posting -> Bool
matchesPosting Query
thisacctq) [Posting]
reportps
numotheraccts :: Int
numotheraccts = [Text] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Text] -> Int) -> [Text] -> Int
forall a b. (a -> b) -> a -> b
$ [Text] -> [Text]
forall a. Eq a => [a] -> [a]
nub ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ (Posting -> Text) -> [Posting] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Posting -> Text
paccount [Posting]
otheracctps
otheracctstr :: Text
otheracctstr | Query
thisacctq Query -> Query -> Bool
forall a. Eq a => a -> a -> Bool
== Query
None = [Posting] -> Text
summarisePostingAccounts [Posting]
reportps
| Int
numotheraccts Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = [Posting] -> Text
summarisePostingAccounts [Posting]
thisacctps
| Bool
otherwise = [Posting] -> Text
summarisePostingAccounts [Posting]
otheracctps
a :: MixedAmount
a = MixedAmount -> MixedAmount
signfn (MixedAmount -> MixedAmount)
-> (MixedAmount -> MixedAmount) -> MixedAmount -> MixedAmount
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MixedAmount -> MixedAmount
maNegate (MixedAmount -> MixedAmount) -> MixedAmount -> MixedAmount
forall a b. (a -> b) -> a -> b
$ [Posting] -> MixedAmount
sumPostings [Posting]
thisacctps
b :: MixedAmount
b = MixedAmount
bal MixedAmount -> MixedAmount -> MixedAmount
`maPlus` MixedAmount
a
transactionRegisterDate :: Query -> Query -> Transaction -> Day
transactionRegisterDate :: Query -> Query -> Transaction -> Day
transactionRegisterDate Query
reportq Query
thisacctq Transaction
t
| [Posting] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Posting]
thisacctps = Transaction -> Day
tdate Transaction
t
| Bool
otherwise = [Day] -> Day
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum ([Day] -> Day) -> [Day] -> Day
forall a b. (a -> b) -> a -> b
$ (Posting -> Day) -> [Posting] -> [Day]
forall a b. (a -> b) -> [a] -> [b]
map Posting -> Day
postingDate [Posting]
thisacctps
where
reportps :: [Posting]
reportps = Transaction -> [Posting]
tpostings (Transaction -> [Posting]) -> Transaction -> [Posting]
forall a b. (a -> b) -> a -> b
$ Query -> Transaction -> Transaction
filterTransactionPostings Query
reportq Transaction
t
thisacctps :: [Posting]
thisacctps = (Posting -> Bool) -> [Posting] -> [Posting]
forall a. (a -> Bool) -> [a] -> [a]
filter (Query -> Posting -> Bool
matchesPosting Query
thisacctq) [Posting]
reportps
summarisePostingAccounts :: [Posting] -> Text
summarisePostingAccounts :: [Posting] -> Text
summarisePostingAccounts [Posting]
ps =
Text -> [Text] -> Text
T.intercalate Text
", " ([Text] -> Text) -> ([Text] -> [Text]) -> [Text] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
accountSummarisedName ([Text] -> [Text]) -> ([Text] -> [Text]) -> [Text] -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> [Text]
forall a. Eq a => [a] -> [a]
nub ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ (Posting -> Text) -> [Posting] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Posting -> Text
paccount [Posting]
displayps
where
realps :: [Posting]
realps = (Posting -> Bool) -> [Posting] -> [Posting]
forall a. (a -> Bool) -> [a] -> [a]
filter Posting -> Bool
isReal [Posting]
ps
displayps :: [Posting]
displayps | [Posting] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Posting]
realps = [Posting]
ps
| Bool
otherwise = [Posting]
realps
tests_AccountTransactionsReport :: TestTree
tests_AccountTransactionsReport = String -> [TestTree] -> TestTree
tests String
"AccountTransactionsReport" [
]