2019-08-01 19:23:31 +03:00
|
|
|
{-# LANGUAGE FlexibleInstances, RecordWildCards, ScopedTypeVariables, OverloadedStrings, DeriveGeneric #-}
|
2014-03-20 04:11:48 +04:00
|
|
|
{-|
|
|
|
|
|
|
|
|
Multi-column balance reports, used by the balance command.
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
2019-06-14 21:45:25 +03:00
|
|
|
module Hledger.Reports.MultiBalanceReport (
|
2020-01-04 04:13:50 +03:00
|
|
|
MultiBalanceReport,
|
2014-03-20 04:11:48 +04:00
|
|
|
MultiBalanceReportRow,
|
2020-01-04 04:13:50 +03:00
|
|
|
|
2015-08-26 20:38:45 +03:00
|
|
|
multiBalanceReport,
|
2019-08-19 04:16:39 +03:00
|
|
|
multiBalanceReportWith,
|
2018-01-23 22:32:24 +03:00
|
|
|
balanceReportFromMultiBalanceReport,
|
2018-04-03 15:07:13 +03:00
|
|
|
tableAsText,
|
2014-03-20 04:11:48 +04:00
|
|
|
|
|
|
|
-- -- * Tests
|
2019-06-14 21:45:25 +03:00
|
|
|
tests_MultiBalanceReport
|
2014-03-20 04:11:48 +04:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Data.List
|
2020-01-04 09:09:01 +03:00
|
|
|
import Data.List.Extra (nubSort)
|
2019-11-12 04:14:21 +03:00
|
|
|
import qualified Data.Map as M
|
2014-03-20 04:11:48 +04:00
|
|
|
import Data.Maybe
|
|
|
|
import Data.Ord
|
2015-08-26 20:38:45 +03:00
|
|
|
import Data.Time.Calendar
|
2014-04-19 19:40:16 +04:00
|
|
|
import Safe
|
2018-04-03 15:07:13 +03:00
|
|
|
import Text.Tabular as T
|
|
|
|
import Text.Tabular.AsciiWide
|
2014-03-20 04:11:48 +04:00
|
|
|
|
|
|
|
import Hledger.Data
|
|
|
|
import Hledger.Query
|
2019-07-15 13:28:52 +03:00
|
|
|
import Hledger.Utils
|
2017-07-06 19:53:59 +03:00
|
|
|
import Hledger.Read (mamountp')
|
2014-03-20 04:11:48 +04:00
|
|
|
import Hledger.Reports.ReportOptions
|
2020-01-04 04:13:50 +03:00
|
|
|
import Hledger.Reports.ReportTypes
|
2014-03-20 04:11:48 +04:00
|
|
|
import Hledger.Reports.BalanceReport
|
|
|
|
|
|
|
|
|
2020-01-04 04:13:50 +03:00
|
|
|
-- | A multi balance report is a kind of periodic report, where the amounts
|
|
|
|
-- correspond to balance changes or ending balances in a given period. It has:
|
2014-03-20 04:11:48 +04:00
|
|
|
--
|
2016-08-09 01:56:50 +03:00
|
|
|
-- 1. a list of each column's period (date span)
|
2014-03-20 04:11:48 +04:00
|
|
|
--
|
2017-07-26 05:43:45 +03:00
|
|
|
-- 2. a list of rows, each containing:
|
2014-03-20 04:11:48 +04:00
|
|
|
--
|
2016-08-09 01:56:50 +03:00
|
|
|
-- * the full account name
|
2014-03-20 04:11:48 +04:00
|
|
|
--
|
2016-08-09 01:56:50 +03:00
|
|
|
-- * the account's depth
|
2014-03-20 04:11:48 +04:00
|
|
|
--
|
2020-01-04 04:13:50 +03:00
|
|
|
-- * A list of amounts, one for each column.
|
2014-12-26 22:04:23 +03:00
|
|
|
--
|
2020-01-04 04:13:50 +03:00
|
|
|
-- * the total of the row's amounts for a periodic report
|
2014-12-26 22:04:23 +03:00
|
|
|
--
|
2016-08-09 01:56:50 +03:00
|
|
|
-- * the average of the row's amounts
|
|
|
|
--
|
2019-05-11 21:55:04 +03:00
|
|
|
-- 3. the column totals, and the overall grand total (or zero for
|
|
|
|
-- cumulative/historical reports) and grand average.
|
2014-03-20 04:11:48 +04:00
|
|
|
|
2020-01-05 04:58:08 +03:00
|
|
|
type MultiBalanceReport = PeriodicReport AccountName MixedAmount
|
|
|
|
type MultiBalanceReportRow = PeriodicReportRow AccountName MixedAmount
|
2014-03-20 04:11:48 +04:00
|
|
|
|
2014-03-26 06:27:18 +04:00
|
|
|
-- type alias just to remind us which AccountNames might be depth-clipped, below.
|
|
|
|
type ClippedAccountName = AccountName
|
|
|
|
|
2014-04-13 22:07:39 +04:00
|
|
|
-- | Generate a multicolumn balance report for the matched accounts,
|
|
|
|
-- showing the change of balance, accumulated balance, or historical balance
|
2018-01-23 22:32:24 +03:00
|
|
|
-- in each of the specified periods. Does not support tree-mode boring parent eliding.
|
2019-07-15 13:28:52 +03:00
|
|
|
-- If the normalbalance_ option is set, it adjusts the sorting and sign of amounts
|
2018-01-23 22:32:24 +03:00
|
|
|
-- (see ReportOpts and CompoundBalanceCommand).
|
2019-05-09 17:58:45 +03:00
|
|
|
-- hledger's most powerful and useful report, used by the balance
|
2019-08-19 04:16:39 +03:00
|
|
|
-- command (in multiperiod mode) and (via multiBalanceReport') by the bs/cf/is commands.
|
2020-05-24 00:08:04 +03:00
|
|
|
multiBalanceReport :: Day -> ReportOpts -> Journal -> MultiBalanceReport
|
|
|
|
multiBalanceReport today ropts j = multiBalanceReportWith ropts (queryFromOpts today ropts) j (journalPriceOracle j)
|
2019-08-19 04:16:39 +03:00
|
|
|
|
2020-05-24 00:08:04 +03:00
|
|
|
-- | A helper for multiBalanceReport. This one takes an explicit Query
|
|
|
|
-- instead of deriving one from ReportOpts, and an extra argument, a
|
2019-08-19 04:16:39 +03:00
|
|
|
-- PriceOracle to be used for looking up market prices. Commands which
|
2020-05-24 00:08:04 +03:00
|
|
|
-- run multiple reports (bs etc.) can generate the price oracle just
|
|
|
|
-- once for efficiency, passing it to each report by calling this
|
|
|
|
-- function directly.
|
2019-08-19 04:16:39 +03:00
|
|
|
multiBalanceReportWith :: ReportOpts -> Query -> Journal -> PriceOracle -> MultiBalanceReport
|
2019-12-15 02:44:59 +03:00
|
|
|
multiBalanceReportWith ropts@ReportOpts{..} q j priceoracle =
|
2020-01-04 04:13:50 +03:00
|
|
|
(if invert_ then prNegate else id) $
|
2020-01-04 05:39:04 +03:00
|
|
|
PeriodicReport colspans mappedsortedrows mappedtotalsrow
|
2014-03-20 04:11:48 +04:00
|
|
|
where
|
2019-05-09 17:58:45 +03:00
|
|
|
dbg1 s = let p = "multiBalanceReport" in Hledger.Utils.dbg1 (p++" "++s) -- add prefix in this function's debug output
|
|
|
|
-- dbg1 = const id -- exclude this function from debug output
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
-- 1. Queries, report/column dates.
|
|
|
|
|
2015-05-14 22:49:17 +03:00
|
|
|
symq = dbg1 "symq" $ filterQuery queryIsSym $ dbg1 "requested q" q
|
|
|
|
depthq = dbg1 "depthq" $ filterQuery queryIsDepth q
|
2014-04-15 00:10:34 +04:00
|
|
|
depth = queryDepth depthq
|
2015-05-14 22:49:17 +03:00
|
|
|
depthless = dbg1 "depthless" . filterQuery (not . queryIsDepth)
|
|
|
|
datelessq = dbg1 "datelessq" $ filterQuery (not . queryIsDateOrDate2) q
|
2019-05-04 22:34:59 +03:00
|
|
|
dateqcons = if date2_ then Date2 else Date
|
2018-10-17 23:10:49 +03:00
|
|
|
-- The date span specified by -b/-e/-p options and query args if any.
|
2019-05-04 22:34:59 +03:00
|
|
|
requestedspan = dbg1 "requestedspan" $ queryDateSpan date2_ q
|
2018-10-17 23:10:49 +03:00
|
|
|
-- If the requested span is open-ended, close it using the journal's end dates.
|
|
|
|
-- This can still be the null (open) span if the journal is empty.
|
2019-05-04 22:34:59 +03:00
|
|
|
requestedspan' = dbg1 "requestedspan'" $ requestedspan `spanDefaultsFrom` journalDateSpan date2_ j
|
2018-10-17 23:10:49 +03:00
|
|
|
-- The list of interval spans enclosing the requested span.
|
|
|
|
-- This list can be empty if the journal was empty,
|
|
|
|
-- or if hledger-ui has added its special date:-tomorrow to the query
|
|
|
|
-- and all txns are in the future.
|
2019-07-15 13:28:52 +03:00
|
|
|
intervalspans = dbg1 "intervalspans" $ splitSpan interval_ requestedspan'
|
2018-10-17 23:10:49 +03:00
|
|
|
-- The requested span enlarged to enclose a whole number of intervals.
|
2019-07-15 13:28:52 +03:00
|
|
|
-- This can be the null span if there were no intervals.
|
2018-10-17 23:10:49 +03:00
|
|
|
reportspan = dbg1 "reportspan" $ DateSpan (maybe Nothing spanStart $ headMay intervalspans)
|
|
|
|
(maybe Nothing spanEnd $ lastMay intervalspans)
|
2019-05-09 17:58:45 +03:00
|
|
|
mreportstart = spanStart reportspan
|
2018-10-17 23:10:49 +03:00
|
|
|
-- The user's query with no depth limit, and expanded to the report span
|
|
|
|
-- if there is one (otherwise any date queries are left as-is, which
|
|
|
|
-- handles the hledger-ui+future txns case above).
|
2019-07-15 13:28:52 +03:00
|
|
|
reportq = dbg1 "reportq" $ depthless $
|
|
|
|
if reportspan == nulldatespan
|
|
|
|
then q
|
2018-10-17 23:10:49 +03:00
|
|
|
else And [datelessq, reportspandatesq]
|
|
|
|
where
|
|
|
|
reportspandatesq = dbg1 "reportspandatesq" $ dateqcons reportspan
|
2019-05-09 17:58:45 +03:00
|
|
|
-- The date spans to be included as report columns.
|
|
|
|
colspans :: [DateSpan] = dbg1 "colspans" $ splitSpan interval_ displayspan
|
2014-04-19 19:40:16 +04:00
|
|
|
where
|
|
|
|
displayspan
|
2019-05-04 22:34:59 +03:00
|
|
|
| empty_ = dbg1 "displayspan (-E)" reportspan -- all the requested intervals
|
|
|
|
| otherwise = dbg1 "displayspan" $ requestedspan `spanIntersect` matchedspan -- exclude leading/trailing empty intervals
|
2019-11-12 04:14:21 +03:00
|
|
|
matchedspan = dbg1 "matchedspan" . daysSpan $ map snd ps
|
2019-05-09 17:58:45 +03:00
|
|
|
|
2019-05-24 06:52:21 +03:00
|
|
|
-- If doing cost valuation, convert amounts to cost.
|
|
|
|
j' = journalSelectingAmountFromOpts ropts j
|
2019-05-09 17:58:45 +03:00
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
2019-06-14 23:03:13 +03:00
|
|
|
-- 2. Calculate starting balances, if needed for -H
|
2019-05-09 17:58:45 +03:00
|
|
|
|
2019-05-24 06:52:21 +03:00
|
|
|
-- Balances at report start date, from all earlier postings which otherwise match the query.
|
|
|
|
-- These balances are unvalued except maybe converted to cost.
|
2019-05-09 17:58:45 +03:00
|
|
|
startbals :: [(AccountName, MixedAmount)] = dbg1 "startbals" $ map (\(a,_,_,b) -> (a,b)) startbalanceitems
|
|
|
|
where
|
2019-11-11 23:06:58 +03:00
|
|
|
(startbalanceitems,_) = dbg1 "starting balance report" $ balanceReport ropts''{value_=Nothing, percent_=False} startbalq j'
|
2019-05-09 17:58:45 +03:00
|
|
|
where
|
|
|
|
ropts' | tree_ ropts = ropts{no_elide_=True}
|
|
|
|
| otherwise = ropts{accountlistmode_=ALFlat}
|
|
|
|
ropts'' = ropts'{period_ = precedingperiod}
|
|
|
|
where
|
|
|
|
precedingperiod = dateSpanAsPeriod $ spanIntersect (DateSpan Nothing mreportstart) $ periodAsDateSpan period_
|
|
|
|
-- q projected back before the report start date.
|
|
|
|
-- When there's no report start date, in case there are future txns (the hledger-ui case above),
|
2019-07-15 13:28:52 +03:00
|
|
|
-- we use emptydatespan to make sure they aren't counted as starting balance.
|
2019-05-09 17:58:45 +03:00
|
|
|
startbalq = dbg1 "startbalq" $ And [datelessq, dateqcons precedingspan]
|
|
|
|
where
|
|
|
|
precedingspan = case mreportstart of
|
|
|
|
Just d -> DateSpan Nothing (Just d)
|
2019-07-15 13:28:52 +03:00
|
|
|
Nothing -> emptydatespan
|
2019-05-09 17:58:45 +03:00
|
|
|
-- The matched accounts with a starting balance. All of these should appear
|
|
|
|
-- in the report even if they have no postings during the report period.
|
|
|
|
startaccts = dbg1 "startaccts" $ map fst startbals
|
|
|
|
-- Helpers to look up an account's starting balance.
|
|
|
|
startingBalanceFor a = fromMaybe nullmixedamt $ lookup a startbals
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
2019-06-14 23:03:13 +03:00
|
|
|
-- 3. Gather postings for each column.
|
2019-05-09 17:58:45 +03:00
|
|
|
|
|
|
|
-- Postings matching the query within the report period.
|
2019-11-12 04:14:21 +03:00
|
|
|
ps :: [(Posting, Day)] =
|
2019-05-09 17:58:45 +03:00
|
|
|
dbg1 "ps" $
|
2019-11-12 04:14:21 +03:00
|
|
|
map postingWithDate $
|
2019-05-09 17:58:45 +03:00
|
|
|
journalPostings $
|
2019-05-24 06:52:21 +03:00
|
|
|
filterJournalAmounts symq $ -- remove amount parts excluded by cur:
|
|
|
|
filterJournalPostings reportq $ -- remove postings not matched by (adjusted) query
|
|
|
|
j'
|
2019-11-12 04:14:21 +03:00
|
|
|
where
|
|
|
|
postingWithDate p = case whichDateFromOpts ropts of
|
|
|
|
PrimaryDate -> (p, postingDate p)
|
|
|
|
SecondaryDate -> (p, postingDate2 p)
|
2019-05-24 06:52:21 +03:00
|
|
|
|
2019-05-09 17:58:45 +03:00
|
|
|
-- Group postings into their columns, with the column end dates.
|
|
|
|
colps :: [([Posting], Maybe Day)] =
|
|
|
|
dbg1 "colps"
|
2019-11-12 04:14:21 +03:00
|
|
|
[ (posts, end) | (DateSpan _ end, posts) <- M.toList colMap ]
|
|
|
|
where
|
|
|
|
colMap = foldr addPosting emptyMap ps
|
|
|
|
addPosting (p, d) = maybe id (M.adjust (p:)) $ latestSpanContaining colspans d
|
|
|
|
emptyMap = M.fromList . zip colspans $ repeat []
|
2019-05-09 17:58:45 +03:00
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
2019-06-14 23:03:13 +03:00
|
|
|
-- 4. Calculate account balance changes in each column.
|
2019-05-09 17:58:45 +03:00
|
|
|
|
|
|
|
-- In each column, gather the accounts that have postings and their change amount.
|
|
|
|
acctChangesFromPostings :: [Posting] -> [(ClippedAccountName, MixedAmount)]
|
|
|
|
acctChangesFromPostings ps = [(aname a, (if tree_ ropts then aibalance else aebalance) a) | a <- as]
|
2014-07-18 02:18:40 +04:00
|
|
|
where
|
2019-05-09 17:58:45 +03:00
|
|
|
as = depthLimit $
|
|
|
|
(if tree_ ropts then id else filter ((>0).anumpostings)) $
|
|
|
|
drop 1 $ accountsFromPostings ps
|
|
|
|
depthLimit
|
|
|
|
| tree_ ropts = filter ((depthq `matchesAccount`).aname) -- exclude deeper balances
|
|
|
|
| otherwise = clipAccountsAndAggregate depth -- aggregate deeper balances at the depth limit
|
2019-05-24 06:52:21 +03:00
|
|
|
colacctchanges :: [[(ClippedAccountName, MixedAmount)]] =
|
|
|
|
dbg1 "colacctchanges" $ map (acctChangesFromPostings . fst) colps
|
2019-05-09 17:58:45 +03:00
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
2019-06-14 23:03:13 +03:00
|
|
|
-- 5. Gather the account balance changes into a regular matrix including the accounts
|
2019-05-09 17:58:45 +03:00
|
|
|
-- from all columns (and with -H, accounts with starting balances), adding zeroes where needed.
|
|
|
|
|
2019-05-05 03:46:52 +03:00
|
|
|
-- All account names that will be displayed, possibly depth-clipped.
|
2019-05-09 17:58:45 +03:00
|
|
|
displayaccts :: [ClippedAccountName] =
|
|
|
|
dbg1 "displayaccts" $
|
2019-05-04 22:34:59 +03:00
|
|
|
(if tree_ ropts then expandAccountNames else id) $
|
2014-10-20 04:53:20 +04:00
|
|
|
nub $ map (clipOrEllipsifyAccountName depth) $
|
2019-05-09 17:58:45 +03:00
|
|
|
if empty_ || balancetype_ == HistoricalBalance
|
2020-01-04 09:09:01 +03:00
|
|
|
then nubSort $ startaccts ++ allpostedaccts
|
2019-05-09 17:58:45 +03:00
|
|
|
else allpostedaccts
|
|
|
|
where
|
2019-11-12 04:14:21 +03:00
|
|
|
allpostedaccts :: [AccountName] =
|
|
|
|
dbg1 "allpostedaccts" . sort . accountNamesFromPostings $ map fst ps
|
2019-05-09 17:58:45 +03:00
|
|
|
-- Each column's balance changes for each account, adding zeroes where needed.
|
|
|
|
colallacctchanges :: [[(ClippedAccountName, MixedAmount)]] =
|
|
|
|
dbg1 "colallacctchanges"
|
2019-11-12 04:14:21 +03:00
|
|
|
[ sortOn fst $ unionBy (\(a,_) (a',_) -> a == a') postedacctchanges zeroes
|
|
|
|
| postedacctchanges <- colacctchanges ]
|
2019-05-09 17:58:45 +03:00
|
|
|
where zeroes = [(a, nullmixedamt) | a <- displayaccts]
|
|
|
|
-- Transpose to get each account's balance changes across all columns.
|
|
|
|
acctchanges :: [(ClippedAccountName, [MixedAmount])] =
|
|
|
|
dbg1 "acctchanges"
|
|
|
|
[(a, map snd abs) | abs@((a,_):_) <- transpose colallacctchanges] -- never null, or used when null...
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
2019-06-14 23:03:13 +03:00
|
|
|
-- 6. Build the report rows.
|
2019-05-09 17:58:45 +03:00
|
|
|
|
2019-05-24 06:52:21 +03:00
|
|
|
-- One row per account, with account name info, row amounts, row total and row average.
|
2019-05-09 17:58:45 +03:00
|
|
|
rows :: [MultiBalanceReportRow] =
|
|
|
|
dbg1 "rows" $
|
2020-01-05 04:58:08 +03:00
|
|
|
[ PeriodicReportRow a (accountNameLevel a) valuedrowbals rowtot rowavg
|
2019-05-24 06:52:21 +03:00
|
|
|
| (a,changes) <- dbg1 "acctchanges" acctchanges
|
|
|
|
-- The row amounts to be displayed: per-period changes,
|
|
|
|
-- zero-based cumulative totals, or
|
|
|
|
-- starting-balance-based historical balances.
|
|
|
|
, let rowbals = dbg1 "rowbals" $ case balancetype_ of
|
|
|
|
PeriodChange -> changes
|
2019-05-09 17:58:45 +03:00
|
|
|
CumulativeChange -> drop 1 $ scanl (+) 0 changes
|
|
|
|
HistoricalBalance -> drop 1 $ scanl (+) (startingBalanceFor a) changes
|
2019-09-05 23:41:36 +03:00
|
|
|
-- We may be converting amounts to value, per hledger_options.m4.md "Effect of --value on reports".
|
|
|
|
, let valuedrowbals = dbg1 "valuedrowbals" $ [avalue periodlastday amt | (amt,periodlastday) <- zip rowbals lastdays]
|
|
|
|
-- The total and average for the row.
|
|
|
|
-- These are always simply the sum/average of the displayed row amounts.
|
2019-05-24 06:52:21 +03:00
|
|
|
-- Total for a cumulative/historical report is always zero.
|
|
|
|
, let rowtot = if balancetype_==PeriodChange then sum valuedrowbals else 0
|
|
|
|
, let rowavg = averageMixedAmounts valuedrowbals
|
2020-05-30 04:57:22 +03:00
|
|
|
, empty_ || depth == 0 || any (not . mixedAmountLooksZero) valuedrowbals
|
2014-04-13 22:07:39 +04:00
|
|
|
]
|
2019-06-14 23:03:13 +03:00
|
|
|
where
|
2019-09-05 23:41:36 +03:00
|
|
|
avalue periodlast =
|
|
|
|
maybe id (mixedAmountApplyValuation priceoracle styles periodlast mreportlast today multiperiod) value_
|
|
|
|
where
|
|
|
|
-- Some things needed if doing valuation.
|
|
|
|
styles = journalCommodityStyles j
|
|
|
|
mreportlast = reportPeriodLastDay ropts
|
|
|
|
today = fromMaybe (error' "multiBalanceReport: could not pick a valuation date, ReportOpts today_ is unset") today_ -- XXX shouldn't happen
|
|
|
|
multiperiod = interval_ /= NoInterval
|
|
|
|
-- The last day of each column's subperiod.
|
2019-06-14 23:03:13 +03:00
|
|
|
lastdays =
|
|
|
|
map ((maybe
|
|
|
|
(error' "multiBalanceReport: expected all spans to have an end date") -- XXX should not happen
|
|
|
|
(addDays (-1)))
|
|
|
|
. spanEnd) colspans
|
2019-07-15 13:28:52 +03:00
|
|
|
|
2019-05-09 17:58:45 +03:00
|
|
|
----------------------------------------------------------------------
|
2019-06-14 23:03:13 +03:00
|
|
|
-- 7. Sort the report rows.
|
2019-05-09 17:58:45 +03:00
|
|
|
|
|
|
|
-- Sort the rows by amount or by account declaration order. This is a bit tricky.
|
|
|
|
-- TODO: is it always ok to sort report rows after report has been generated, as a separate step ?
|
2019-05-24 06:52:21 +03:00
|
|
|
sortedrows :: [MultiBalanceReportRow] =
|
|
|
|
dbg1 "sortedrows" $
|
|
|
|
sortrows rows
|
2017-09-30 05:19:07 +03:00
|
|
|
where
|
2019-05-09 17:58:45 +03:00
|
|
|
sortrows
|
2019-05-04 22:34:59 +03:00
|
|
|
| sort_amount_ && accountlistmode_ == ALTree = sortTreeMBRByAmount
|
|
|
|
| sort_amount_ = sortFlatMBRByAmount
|
|
|
|
| otherwise = sortMBRByAccountDeclaration
|
2017-09-30 05:19:07 +03:00
|
|
|
where
|
|
|
|
-- Sort the report rows, representing a tree of accounts, by row total at each level.
|
journal: a new account sorting mechanism, and a bunch of sorting fixes
A bunch of account sorting changes that got intermingled.
First, account codes have been dropped. They can still be parsed and
will be ignored, for now. I don't know if anyone used them.
Instead, account display order is now controlled by the order of account
directives, if any. From the mail list:
I'd like to drop account codes, introduced in hledger 1.9 to control
the display order of accounts. In my experience,
- they are tedious to maintain
- they duplicate/compete with the natural tendency to arrange account
directives to match your mental chart of accounts
- they duplicate/compete with the tree structure created by account
names
and it gets worse if you think about using them more extensively,
eg to classify accounts by type.
Instead, I plan to just let the position (parse order) of account
directives determine the display order of those declared accounts.
Undeclared accounts will be displayed after declared accounts,
sorted alphabetically as usual.
Second, the various account sorting modes have been implemented more
widely and more correctly. All sorting modes (alphabetically, by account
declaration, by amount) should now work correctly in almost all commands
and modes (non-tabular and tabular balance reports, tree and flat modes,
the accounts command). Sorting bugs have been fixed, eg #875.
Only the budget report (balance --budget) does not yet support sorting.
Comprehensive functional tests for sorting in the accounts and balance
commands have been added. If you are confused by some sorting behaviour,
studying these tests is recommended, as sorting gets tricky.
2018-09-23 10:45:07 +03:00
|
|
|
-- Similar to sortMBRByAccountDeclaration/sortAccountNamesByDeclaration.
|
2020-01-04 04:13:50 +03:00
|
|
|
sortTreeMBRByAmount :: [MultiBalanceReportRow] -> [MultiBalanceReportRow]
|
journal: a new account sorting mechanism, and a bunch of sorting fixes
A bunch of account sorting changes that got intermingled.
First, account codes have been dropped. They can still be parsed and
will be ignored, for now. I don't know if anyone used them.
Instead, account display order is now controlled by the order of account
directives, if any. From the mail list:
I'd like to drop account codes, introduced in hledger 1.9 to control
the display order of accounts. In my experience,
- they are tedious to maintain
- they duplicate/compete with the natural tendency to arrange account
directives to match your mental chart of accounts
- they duplicate/compete with the tree structure created by account
names
and it gets worse if you think about using them more extensively,
eg to classify accounts by type.
Instead, I plan to just let the position (parse order) of account
directives determine the display order of those declared accounts.
Undeclared accounts will be displayed after declared accounts,
sorted alphabetically as usual.
Second, the various account sorting modes have been implemented more
widely and more correctly. All sorting modes (alphabetically, by account
declaration, by amount) should now work correctly in almost all commands
and modes (non-tabular and tabular balance reports, tree and flat modes,
the accounts command). Sorting bugs have been fixed, eg #875.
Only the budget report (balance --budget) does not yet support sorting.
Comprehensive functional tests for sorting in the accounts and balance
commands have been added. If you are confused by some sorting behaviour,
studying these tests is recommended, as sorting gets tricky.
2018-09-23 10:45:07 +03:00
|
|
|
sortTreeMBRByAmount rows = sortedrows
|
2017-09-30 05:19:07 +03:00
|
|
|
where
|
2020-01-05 04:58:08 +03:00
|
|
|
anamesandrows = [(prrName r, r) | r <- rows]
|
2017-09-30 05:19:07 +03:00
|
|
|
anames = map fst anamesandrows
|
2020-01-05 04:58:08 +03:00
|
|
|
atotals = [(prrName r, prrTotal r) | r <- rows]
|
journal: a new account sorting mechanism, and a bunch of sorting fixes
A bunch of account sorting changes that got intermingled.
First, account codes have been dropped. They can still be parsed and
will be ignored, for now. I don't know if anyone used them.
Instead, account display order is now controlled by the order of account
directives, if any. From the mail list:
I'd like to drop account codes, introduced in hledger 1.9 to control
the display order of accounts. In my experience,
- they are tedious to maintain
- they duplicate/compete with the natural tendency to arrange account
directives to match your mental chart of accounts
- they duplicate/compete with the tree structure created by account
names
and it gets worse if you think about using them more extensively,
eg to classify accounts by type.
Instead, I plan to just let the position (parse order) of account
directives determine the display order of those declared accounts.
Undeclared accounts will be displayed after declared accounts,
sorted alphabetically as usual.
Second, the various account sorting modes have been implemented more
widely and more correctly. All sorting modes (alphabetically, by account
declaration, by amount) should now work correctly in almost all commands
and modes (non-tabular and tabular balance reports, tree and flat modes,
the accounts command). Sorting bugs have been fixed, eg #875.
Only the budget report (balance --budget) does not yet support sorting.
Comprehensive functional tests for sorting in the accounts and balance
commands have been added. If you are confused by some sorting behaviour,
studying these tests is recommended, as sorting gets tricky.
2018-09-23 10:45:07 +03:00
|
|
|
accounttree = accountTree "root" anames
|
2017-09-30 05:19:07 +03:00
|
|
|
accounttreewithbals = mapAccounts setibalance accounttree
|
|
|
|
where
|
2019-07-15 13:28:52 +03:00
|
|
|
-- should not happen, but it's dangerous; TODO
|
journal: a new account sorting mechanism, and a bunch of sorting fixes
A bunch of account sorting changes that got intermingled.
First, account codes have been dropped. They can still be parsed and
will be ignored, for now. I don't know if anyone used them.
Instead, account display order is now controlled by the order of account
directives, if any. From the mail list:
I'd like to drop account codes, introduced in hledger 1.9 to control
the display order of accounts. In my experience,
- they are tedious to maintain
- they duplicate/compete with the natural tendency to arrange account
directives to match your mental chart of accounts
- they duplicate/compete with the tree structure created by account
names
and it gets worse if you think about using them more extensively,
eg to classify accounts by type.
Instead, I plan to just let the position (parse order) of account
directives determine the display order of those declared accounts.
Undeclared accounts will be displayed after declared accounts,
sorted alphabetically as usual.
Second, the various account sorting modes have been implemented more
widely and more correctly. All sorting modes (alphabetically, by account
declaration, by amount) should now work correctly in almost all commands
and modes (non-tabular and tabular balance reports, tree and flat modes,
the accounts command). Sorting bugs have been fixed, eg #875.
Only the budget report (balance --budget) does not yet support sorting.
Comprehensive functional tests for sorting in the accounts and balance
commands have been added. If you are confused by some sorting behaviour,
studying these tests is recommended, as sorting gets tricky.
2018-09-23 10:45:07 +03:00
|
|
|
setibalance a = a{aibalance=fromMaybe (error "sortTreeMBRByAmount 1") $ lookup (aname a) atotals}
|
2019-05-04 22:34:59 +03:00
|
|
|
sortedaccounttree = sortAccountTreeByAmount (fromMaybe NormallyPositive normalbalance_) accounttreewithbals
|
journal: a new account sorting mechanism, and a bunch of sorting fixes
A bunch of account sorting changes that got intermingled.
First, account codes have been dropped. They can still be parsed and
will be ignored, for now. I don't know if anyone used them.
Instead, account display order is now controlled by the order of account
directives, if any. From the mail list:
I'd like to drop account codes, introduced in hledger 1.9 to control
the display order of accounts. In my experience,
- they are tedious to maintain
- they duplicate/compete with the natural tendency to arrange account
directives to match your mental chart of accounts
- they duplicate/compete with the tree structure created by account
names
and it gets worse if you think about using them more extensively,
eg to classify accounts by type.
Instead, I plan to just let the position (parse order) of account
directives determine the display order of those declared accounts.
Undeclared accounts will be displayed after declared accounts,
sorted alphabetically as usual.
Second, the various account sorting modes have been implemented more
widely and more correctly. All sorting modes (alphabetically, by account
declaration, by amount) should now work correctly in almost all commands
and modes (non-tabular and tabular balance reports, tree and flat modes,
the accounts command). Sorting bugs have been fixed, eg #875.
Only the budget report (balance --budget) does not yet support sorting.
Comprehensive functional tests for sorting in the accounts and balance
commands have been added. If you are confused by some sorting behaviour,
studying these tests is recommended, as sorting gets tricky.
2018-09-23 10:45:07 +03:00
|
|
|
sortedanames = map aname $ drop 1 $ flattenAccounts sortedaccounttree
|
2019-07-15 13:28:52 +03:00
|
|
|
sortedrows = sortAccountItemsLike sortedanames anamesandrows
|
2018-01-21 06:42:05 +03:00
|
|
|
|
2019-07-15 13:28:52 +03:00
|
|
|
-- Sort the report rows, representing a flat account list, by row total.
|
2020-01-04 05:39:04 +03:00
|
|
|
sortFlatMBRByAmount = sortBy (maybeflip $ comparing (normaliseMixedAmountSquashPricesForDisplay . prrTotal))
|
2018-01-21 06:42:05 +03:00
|
|
|
where
|
2019-05-04 22:34:59 +03:00
|
|
|
maybeflip = if normalbalance_ == Just NormallyNegative then id else flip
|
2018-01-21 06:42:05 +03:00
|
|
|
|
2019-07-15 13:28:52 +03:00
|
|
|
-- Sort the report rows by account declaration order then account name.
|
journal: a new account sorting mechanism, and a bunch of sorting fixes
A bunch of account sorting changes that got intermingled.
First, account codes have been dropped. They can still be parsed and
will be ignored, for now. I don't know if anyone used them.
Instead, account display order is now controlled by the order of account
directives, if any. From the mail list:
I'd like to drop account codes, introduced in hledger 1.9 to control
the display order of accounts. In my experience,
- they are tedious to maintain
- they duplicate/compete with the natural tendency to arrange account
directives to match your mental chart of accounts
- they duplicate/compete with the tree structure created by account
names
and it gets worse if you think about using them more extensively,
eg to classify accounts by type.
Instead, I plan to just let the position (parse order) of account
directives determine the display order of those declared accounts.
Undeclared accounts will be displayed after declared accounts,
sorted alphabetically as usual.
Second, the various account sorting modes have been implemented more
widely and more correctly. All sorting modes (alphabetically, by account
declaration, by amount) should now work correctly in almost all commands
and modes (non-tabular and tabular balance reports, tree and flat modes,
the accounts command). Sorting bugs have been fixed, eg #875.
Only the budget report (balance --budget) does not yet support sorting.
Comprehensive functional tests for sorting in the accounts and balance
commands have been added. If you are confused by some sorting behaviour,
studying these tests is recommended, as sorting gets tricky.
2018-09-23 10:45:07 +03:00
|
|
|
sortMBRByAccountDeclaration rows = sortedrows
|
2019-07-15 13:28:52 +03:00
|
|
|
where
|
2020-01-05 04:58:08 +03:00
|
|
|
anamesandrows = [(prrName r, r) | r <- rows]
|
2018-01-21 06:42:05 +03:00
|
|
|
anames = map fst anamesandrows
|
2019-05-04 22:34:59 +03:00
|
|
|
sortedanames = sortAccountNamesByDeclaration j (tree_ ropts) anames
|
2019-07-15 13:28:52 +03:00
|
|
|
sortedrows = sortAccountItemsLike sortedanames anamesandrows
|
2014-12-26 22:04:23 +03:00
|
|
|
|
2019-05-09 17:58:45 +03:00
|
|
|
----------------------------------------------------------------------
|
2019-06-14 23:03:13 +03:00
|
|
|
-- 8. Build the report totals row.
|
2019-05-09 17:58:45 +03:00
|
|
|
|
2019-05-24 06:52:21 +03:00
|
|
|
-- Calculate the column totals. These are always the sum of column amounts.
|
2019-05-09 17:58:45 +03:00
|
|
|
highestlevelaccts = [a | a <- displayaccts, not $ any (`elem` displayaccts) $ init $ expandAccountName a]
|
2020-01-04 05:39:04 +03:00
|
|
|
colamts = transpose . map prrAmounts $ filter isHighest rows
|
2020-01-05 04:58:08 +03:00
|
|
|
where isHighest row = not (tree_ ropts) || prrName row `elem` highestlevelaccts
|
2019-05-09 17:58:45 +03:00
|
|
|
coltotals :: [MixedAmount] =
|
2019-05-24 06:52:21 +03:00
|
|
|
dbg1 "coltotals" $ map sum colamts
|
|
|
|
-- Calculate the grand total and average. These are always the sum/average
|
|
|
|
-- of the column totals.
|
2019-05-09 17:58:45 +03:00
|
|
|
[grandtotal,grandaverage] =
|
2019-05-11 21:55:04 +03:00
|
|
|
let amts = map ($ map sum colamts)
|
|
|
|
[if balancetype_==PeriodChange then sum else const 0
|
|
|
|
,averageMixedAmounts
|
|
|
|
]
|
2019-05-24 06:52:21 +03:00
|
|
|
in amts
|
2019-05-09 17:58:45 +03:00
|
|
|
-- Totals row.
|
2020-01-04 06:05:55 +03:00
|
|
|
totalsrow :: PeriodicReportRow () MixedAmount =
|
|
|
|
dbg1 "totalsrow" $ PeriodicReportRow () 0 coltotals grandtotal grandaverage
|
2014-04-19 22:26:01 +04:00
|
|
|
|
2019-11-11 23:06:58 +03:00
|
|
|
----------------------------------------------------------------------
|
|
|
|
-- 9. Map the report rows to percentages if needed
|
|
|
|
-- It is not correct to do this before step 6 due to the total and average columns.
|
|
|
|
-- This is not done in step 6, since the report totals are calculated in 8.
|
|
|
|
-- Perform the divisions to obtain percentages
|
|
|
|
mappedsortedrows :: [MultiBalanceReportRow] =
|
|
|
|
if not percent_ then sortedrows
|
|
|
|
else dbg1 "mappedsortedrows"
|
2020-01-04 06:05:55 +03:00
|
|
|
[ PeriodicReportRow aname alevel
|
2020-01-04 05:39:04 +03:00
|
|
|
(zipWith perdivide rowvals coltotals)
|
|
|
|
(rowtotal `perdivide` grandtotal)
|
|
|
|
(rowavg `perdivide` grandaverage)
|
2020-01-04 06:05:55 +03:00
|
|
|
| PeriodicReportRow aname alevel rowvals rowtotal rowavg <- sortedrows
|
2019-11-11 23:06:58 +03:00
|
|
|
]
|
2020-01-04 06:05:55 +03:00
|
|
|
mappedtotalsrow :: PeriodicReportRow () MixedAmount
|
|
|
|
| percent_ = dbg1 "mappedtotalsrow" $ PeriodicReportRow () 0
|
2020-01-04 05:39:04 +03:00
|
|
|
(map (\t -> perdivide t t) coltotals)
|
|
|
|
(perdivide grandtotal grandtotal)
|
|
|
|
(perdivide grandaverage grandaverage)
|
|
|
|
| otherwise = totalsrow
|
2018-04-03 15:07:13 +03:00
|
|
|
|
2019-07-15 13:28:52 +03:00
|
|
|
-- | Generates a simple non-columnar BalanceReport, but using multiBalanceReport,
|
|
|
|
-- in order to support --historical. Does not support tree-mode boring parent eliding.
|
|
|
|
-- If the normalbalance_ option is set, it adjusts the sorting and sign of amounts
|
2018-01-23 22:32:24 +03:00
|
|
|
-- (see ReportOpts and CompoundBalanceCommand).
|
|
|
|
balanceReportFromMultiBalanceReport :: ReportOpts -> Query -> Journal -> BalanceReport
|
|
|
|
balanceReportFromMultiBalanceReport opts q j = (rows', total)
|
|
|
|
where
|
2020-05-24 00:08:04 +03:00
|
|
|
PeriodicReport _ rows (PeriodicReportRow _ _ totals _ _) = multiBalanceReportWith opts q j (journalPriceOracle j)
|
2020-01-04 05:39:04 +03:00
|
|
|
rows' = [( a
|
2020-01-05 04:58:08 +03:00
|
|
|
, if flat_ opts then a else accountLeafName a -- BalanceReport expects full account name here with --flat
|
2020-01-04 05:39:04 +03:00
|
|
|
, if tree_ opts then d-1 else 0 -- BalanceReport uses 0-based account depths
|
2018-01-23 22:32:24 +03:00
|
|
|
, headDef nullmixedamt amts -- 0 columns is illegal, should not happen, return zeroes if it does
|
2020-01-05 04:58:08 +03:00
|
|
|
) | PeriodicReportRow a d amts _ _ <- rows]
|
2018-01-23 22:32:24 +03:00
|
|
|
total = headDef nullmixedamt totals
|
|
|
|
|
2015-08-26 20:38:45 +03:00
|
|
|
|
2018-04-03 15:07:13 +03:00
|
|
|
-- common rendering helper, XXX here for now
|
|
|
|
|
|
|
|
tableAsText :: ReportOpts -> (a -> String) -> Table String String a -> String
|
|
|
|
tableAsText (ReportOpts{pretty_tables_ = pretty}) showcell =
|
|
|
|
unlines
|
|
|
|
. trimborder
|
|
|
|
. lines
|
|
|
|
. render pretty id id showcell
|
|
|
|
. align
|
|
|
|
where
|
|
|
|
trimborder = drop 1 . init . map (drop 1 . init)
|
|
|
|
align (Table l t d) = Table l' t d
|
|
|
|
where
|
|
|
|
acctswidth = maximum' $ map strWidth (headerContents l)
|
|
|
|
l' = padRightWide acctswidth <$> l
|
|
|
|
|
2018-09-04 22:23:07 +03:00
|
|
|
-- tests
|
|
|
|
|
2019-06-14 21:45:25 +03:00
|
|
|
tests_MultiBalanceReport = tests "MultiBalanceReport" [
|
2019-11-27 23:46:29 +03:00
|
|
|
|
2018-09-04 22:23:07 +03:00
|
|
|
let
|
2019-11-27 23:46:29 +03:00
|
|
|
amt0 = Amount {acommodity="$", aquantity=0, aprice=Nothing, astyle=AmountStyle {ascommodityside = L, ascommodityspaced = False, asprecision = 2, asdecimalpoint = Just '.', asdigitgroups = Nothing}, aismultiplier=False}
|
|
|
|
(opts,journal) `gives` r = do
|
2018-09-04 22:23:07 +03:00
|
|
|
let (eitems, etotal) = r
|
2020-05-24 00:08:04 +03:00
|
|
|
(PeriodicReport _ aitems atotal) = multiBalanceReport nulldate opts journal
|
2020-01-05 04:58:08 +03:00
|
|
|
showw (PeriodicReportRow acct indent lAmt amt amt')
|
|
|
|
= (acct, accountLeafName acct, indent, map showMixedAmountDebug lAmt, showMixedAmountDebug amt, showMixedAmountDebug amt')
|
2019-11-27 00:56:14 +03:00
|
|
|
(map showw aitems) @?= (map showw eitems)
|
2020-01-04 05:39:04 +03:00
|
|
|
showMixedAmountDebug (prrTotal atotal) @?= showMixedAmountDebug etotal -- we only check the sum of the totals
|
2019-07-15 13:28:52 +03:00
|
|
|
in
|
2018-09-04 22:23:07 +03:00
|
|
|
tests "multiBalanceReport" [
|
2019-11-29 02:29:03 +03:00
|
|
|
test "null journal" $
|
2018-09-04 22:23:07 +03:00
|
|
|
(defreportopts, nulljournal) `gives` ([], Mixed [nullamt])
|
2019-07-15 13:28:52 +03:00
|
|
|
|
2019-11-29 02:29:03 +03:00
|
|
|
,test "with -H on a populated period" $
|
2018-09-04 22:23:07 +03:00
|
|
|
(defreportopts{period_= PeriodBetween (fromGregorian 2008 1 1) (fromGregorian 2008 1 2), balancetype_=HistoricalBalance}, samplejournal) `gives`
|
|
|
|
(
|
2020-01-05 04:58:08 +03:00
|
|
|
[ PeriodicReportRow "assets:bank:checking" 3 [mamountp' "$1.00"] (Mixed [nullamt]) (Mixed [amt0 {aquantity=1}])
|
|
|
|
, PeriodicReportRow "income:salary" 2 [mamountp' "$-1.00"] (Mixed [nullamt]) (Mixed [amt0 {aquantity=(-1)}])
|
2018-09-04 22:23:07 +03:00
|
|
|
],
|
2019-05-11 21:55:04 +03:00
|
|
|
Mixed [nullamt])
|
2019-07-15 13:28:52 +03:00
|
|
|
|
2019-11-29 02:29:03 +03:00
|
|
|
-- ,test "a valid history on an empty period" $
|
2019-11-27 00:56:14 +03:00
|
|
|
-- (defreportopts{period_= PeriodBetween (fromGregorian 2008 1 2) (fromGregorian 2008 1 3), balancetype_=HistoricalBalance}, samplejournal) `gives`
|
|
|
|
-- (
|
|
|
|
-- [
|
2019-11-27 23:46:29 +03:00
|
|
|
-- ("assets:bank:checking","checking",3, [mamountp' "$1.00"], mamountp' "$1.00",Mixed [amt0 {aquantity=1}])
|
|
|
|
-- ,("income:salary","salary",2, [mamountp' "$-1.00"], mamountp' "$-1.00",Mixed [amt0 {aquantity=(-1)}])
|
2019-11-27 00:56:14 +03:00
|
|
|
-- ],
|
|
|
|
-- Mixed [usd0])
|
|
|
|
|
2019-11-29 02:29:03 +03:00
|
|
|
-- ,test "a valid history on an empty period (more complex)" $
|
2019-11-27 00:56:14 +03:00
|
|
|
-- (defreportopts{period_= PeriodBetween (fromGregorian 2009 1 1) (fromGregorian 2009 1 2), balancetype_=HistoricalBalance}, samplejournal) `gives`
|
|
|
|
-- (
|
|
|
|
-- [
|
2019-11-27 23:46:29 +03:00
|
|
|
-- ("assets:bank:checking","checking",3, [mamountp' "$1.00"], mamountp' "$1.00",Mixed [amt0 {aquantity=1}])
|
|
|
|
-- ,("assets:bank:saving","saving",3, [mamountp' "$1.00"], mamountp' "$1.00",Mixed [amt0 {aquantity=1}])
|
|
|
|
-- ,("assets:cash","cash",2, [mamountp' "$-2.00"], mamountp' "$-2.00",Mixed [amt0 {aquantity=(-2)}])
|
|
|
|
-- ,("expenses:food","food",2, [mamountp' "$1.00"], mamountp' "$1.00",Mixed [amt0 {aquantity=(1)}])
|
|
|
|
-- ,("expenses:supplies","supplies",2, [mamountp' "$1.00"], mamountp' "$1.00",Mixed [amt0 {aquantity=(1)}])
|
|
|
|
-- ,("income:gifts","gifts",2, [mamountp' "$-1.00"], mamountp' "$-1.00",Mixed [amt0 {aquantity=(-1)}])
|
|
|
|
-- ,("income:salary","salary",2, [mamountp' "$-1.00"], mamountp' "$-1.00",Mixed [amt0 {aquantity=(-1)}])
|
2019-11-27 00:56:14 +03:00
|
|
|
-- ],
|
|
|
|
-- Mixed [usd0])
|
2018-09-04 22:23:07 +03:00
|
|
|
]
|
|
|
|
]
|