module Hledger.Reports.TransactionsReports (
TransactionsReport,
TransactionsReportItem,
triOrigTransaction,
triDate,
triAmount,
triBalance,
triCommodityAmount,
triCommodityBalance,
journalTransactionsReport,
accountTransactionsReport,
transactionsReportByCommodity
)
where
import Data.List
import Data.Ord
import Hledger.Data
import Hledger.Query
import Hledger.Reports.ReportOptions
type TransactionsReport = (String
,[TransactionsReportItem]
)
type TransactionsReportItem = (Transaction
,Transaction
,Bool
,String
,MixedAmount
,MixedAmount
)
triOrigTransaction (torig,_,_,_,_,_) = torig
triDate (_,tacct,_,_,_,_) = tdate tacct
triAmount (_,_,_,_,a,_) = a
triBalance (_,_,_,_,_,a) = a
triCommodityAmount c = filterMixedAmountByCommodity c . triAmount
triCommodityBalance c = filterMixedAmountByCommodity c . triBalance
journalTransactionsReport :: ReportOpts -> Journal -> Query -> TransactionsReport
journalTransactionsReport opts j q = (totallabel, items)
where
items = reverse $ accountTransactionsReportItems q None nullmixedamt id ts
ts = sortBy (comparing date) $ filter (q `matchesTransaction`) $ jtxns $ journalSelectingAmountFromOpts opts j
date = transactionDateFn opts
type AccountTransactionsReport =
(String
,[AccountTransactionsReportItem]
)
type AccountTransactionsReportItem =
(
Transaction
,Transaction
,Bool
,String
,MixedAmount
,MixedAmount
)
accountTransactionsReport :: ReportOpts -> Journal -> Query -> Query -> AccountTransactionsReport
accountTransactionsReport opts j q thisacctquery = (label, items)
where
ts1 = jtxns $
filterJournalAmounts (filterQuery queryIsSym q) $
journalSelectingAmountFromOpts opts j
ts2 = filter (matchesTransaction thisacctquery) ts1
ts3 = map (setTransactionDateToPostingDate q thisacctquery) ts2
ts = sortBy (comparing tdate) ts3
(startbal,label) | queryIsNull q = (nullmixedamt, balancelabel)
| queryIsStartDateOnly (date2_ opts) q = (sumPostings priorps, balancelabel)
| otherwise = (nullmixedamt, totallabel)
where
priorps =
filter (matchesPosting
(
And [thisacctquery, tostartdatequery]))
$ transactionsPostings ts
tostartdatequery = Date (DateSpan Nothing startdate)
startdate = queryStartDate (date2_ opts) q
items = reverse $
accountTransactionsReportItems q thisacctquery startbal negate ts
setTransactionDateToPostingDate :: Query -> Query -> Transaction -> Transaction
setTransactionDateToPostingDate query thisacctquery t = t'
where
queryps = tpostings $ filterTransactionPostings query t
thisacctps = filter (matchesPosting thisacctquery) queryps
t' = case thisacctps of
[] -> t
_ -> t{tdate=d}
where
d | null ds = tdate t
| otherwise = minimum ds
ds = map postingDate thisacctps
totallabel = "Running Total"
balancelabel = "Historical Balance"
accountTransactionsReportItems :: Query -> Query -> MixedAmount -> (MixedAmount -> MixedAmount) -> [Transaction] -> [TransactionsReportItem]
accountTransactionsReportItems _ _ _ _ [] = []
accountTransactionsReportItems query thisacctquery bal signfn (torig:ts) =
case i of Just i' -> i':is
Nothing -> is
where
origps = tpostings torig
tacct@Transaction{tpostings=queryps} = filterTransactionPostings query torig
(thisacctps, otheracctps) = partition (matchesPosting thisacctquery) origps
amt = negate $ sum $ map pamount thisacctps
numotheraccts = length $ nub $ map paccount otheracctps
otheracctstr | thisacctquery == None = summarisePostingAccounts origps
| numotheraccts == 0 = summarisePostingAccounts thisacctps
| otherwise = summarisePostingAccounts otheracctps
(i,bal') = case queryps of
[] -> (Nothing,bal)
_ -> (Just (torig, tacct, numotheraccts > 1, otheracctstr, a, b), b)
where
a = signfn amt
b = bal + a
is = accountTransactionsReportItems query thisacctquery bal' signfn ts
summarisePostingAccounts :: [Posting] -> String
summarisePostingAccounts = intercalate ", " . map accountLeafName . nub . map paccount
filterTransactionPostings :: Query -> Transaction -> Transaction
filterTransactionPostings m t@Transaction{tpostings=ps} = t{tpostings=filter (m `matchesPosting`) ps}
transactionsReportByCommodity :: TransactionsReport -> [(Commodity, TransactionsReport)]
transactionsReportByCommodity tr =
[(c, filterTransactionsReportByCommodity c tr) | c <- transactionsReportCommodities tr]
where
transactionsReportCommodities (_,items) =
nub $ sort $ map acommodity $ concatMap (amounts . triAmount) items
filterTransactionsReportByCommodity :: Commodity -> TransactionsReport -> TransactionsReport
filterTransactionsReportByCommodity c (label,items) =
(label, fixTransactionsReportItemBalances $ concat [filterTransactionsReportItemByCommodity c i | i <- items])
where
filterTransactionsReportItemByCommodity c (t,t2,s,o,a,bal)
| c `elem` cs = [item']
| otherwise = []
where
cs = map acommodity $ amounts a
item' = (t,t2,s,o,a',bal)
a' = filterMixedAmountByCommodity c a
fixTransactionsReportItemBalances [] = []
fixTransactionsReportItemBalances [i] = [i]
fixTransactionsReportItemBalances items = reverse $ i:(go startbal is)
where
i:is = reverse items
startbal = filterMixedAmountByCommodity c $ triBalance i
go _ [] = []
go bal ((t,t2,s,o,amt,_):is) = (t,t2,s,o,amt,bal'):go bal' is
where bal' = bal + amt