2020-09-15 09:53:14 +03:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
2018-04-03 15:07:13 +03:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
|
2020-07-09 22:55:04 +03:00
|
|
|
module Hledger.Reports.BudgetReport (
|
|
|
|
BudgetGoal,
|
|
|
|
BudgetTotal,
|
|
|
|
BudgetAverage,
|
|
|
|
BudgetCell,
|
|
|
|
BudgetReportRow,
|
|
|
|
BudgetReport,
|
|
|
|
budgetReport,
|
|
|
|
budgetReportAsTable,
|
|
|
|
budgetReportAsText,
|
2020-11-19 00:23:31 +03:00
|
|
|
budgetReportAsCsv,
|
2020-07-09 22:55:04 +03:00
|
|
|
-- * Helpers
|
2020-12-30 09:59:12 +03:00
|
|
|
combineBudgetAndActual,
|
2020-07-09 22:55:04 +03:00
|
|
|
-- * Tests
|
|
|
|
tests_BudgetReport
|
|
|
|
)
|
2018-04-03 15:07:13 +03:00
|
|
|
where
|
|
|
|
|
2021-03-01 14:35:21 +03:00
|
|
|
import Control.Applicative ((<|>))
|
2020-12-22 15:35:20 +03:00
|
|
|
import Data.Decimal (roundTo)
|
2020-11-03 14:31:02 +03:00
|
|
|
import Data.Default (def)
|
2020-07-07 15:48:17 +03:00
|
|
|
import Data.HashMap.Strict (HashMap)
|
|
|
|
import qualified Data.HashMap.Strict as HM
|
2021-03-01 14:35:21 +03:00
|
|
|
import Data.List (find, partition, transpose)
|
2020-01-04 09:09:01 +03:00
|
|
|
import Data.List.Extra (nubSort)
|
2020-12-22 15:35:20 +03:00
|
|
|
import Data.Maybe (fromMaybe)
|
2018-04-03 15:07:13 +03:00
|
|
|
import Data.Map (Map)
|
2021-03-01 14:35:21 +03:00
|
|
|
import qualified Data.Map as Map
|
|
|
|
import qualified Data.Set as S
|
2020-11-09 08:54:28 +03:00
|
|
|
import Data.Text (Text)
|
2018-04-03 15:07:13 +03:00
|
|
|
import qualified Data.Text as T
|
2020-11-09 08:54:28 +03:00
|
|
|
import qualified Data.Text.Lazy as TL
|
|
|
|
import qualified Data.Text.Lazy.Builder as TB
|
2018-04-03 15:07:13 +03:00
|
|
|
--import System.Console.CmdArgs.Explicit as C
|
|
|
|
--import Lucid as L
|
2021-04-22 09:25:02 +03:00
|
|
|
import Text.Tabular.AsciiWide as Tab
|
2018-04-03 15:07:13 +03:00
|
|
|
|
|
|
|
import Hledger.Data
|
|
|
|
import Hledger.Utils
|
2020-11-19 00:23:31 +03:00
|
|
|
import Hledger.Read.CsvReader (CSV)
|
2018-04-03 15:07:13 +03:00
|
|
|
import Hledger.Reports.ReportOptions
|
|
|
|
import Hledger.Reports.ReportTypes
|
2019-06-14 21:45:25 +03:00
|
|
|
import Hledger.Reports.MultiBalanceReport
|
2018-04-03 15:07:13 +03:00
|
|
|
|
|
|
|
|
|
|
|
type BudgetGoal = Change
|
|
|
|
type BudgetTotal = Total
|
|
|
|
type BudgetAverage = Average
|
|
|
|
|
|
|
|
-- | A budget report tracks expected and actual changes per account and subperiod.
|
2018-09-24 21:01:52 +03:00
|
|
|
type BudgetCell = (Maybe Change, Maybe BudgetGoal)
|
2020-06-13 12:58:58 +03:00
|
|
|
type BudgetReportRow = PeriodicReportRow DisplayName BudgetCell
|
2020-07-09 22:55:04 +03:00
|
|
|
type BudgetReport = PeriodicReport DisplayName BudgetCell
|
2018-04-03 15:07:13 +03:00
|
|
|
|
2020-11-09 08:54:28 +03:00
|
|
|
type BudgetDisplayCell = ((Text, Int), Maybe ((Text, Int), Maybe (Text, Int)))
|
2020-09-15 09:53:14 +03:00
|
|
|
|
2020-11-21 03:44:07 +03:00
|
|
|
-- | Calculate per-account, per-period budget (balance change) goals
|
|
|
|
-- from all periodic transactions, calculate actual balance changes
|
|
|
|
-- from the regular transactions, and compare these to get a 'BudgetReport'.
|
2020-11-20 01:59:08 +03:00
|
|
|
-- Unbudgeted accounts may be hidden or renamed (see journalWithBudgetAccountNames).
|
2021-06-04 15:40:10 +03:00
|
|
|
budgetReport :: ReportSpec -> BalancingOpts -> DateSpan -> Journal -> BudgetReport
|
|
|
|
budgetReport rspec bopts reportspan j = dbg4 "sortedbudgetreport" budgetreport
|
2020-07-07 15:48:17 +03:00
|
|
|
where
|
2019-01-16 03:32:35 +03:00
|
|
|
-- Budget report demands ALTree mode to ensure subaccounts and subaccount budgets are properly handled
|
2019-01-25 22:08:13 +03:00
|
|
|
-- and that reports with and without --empty make sense when compared side by side
|
2021-07-23 09:47:48 +03:00
|
|
|
ropts = (_rsReportOpts rspec){ accountlistmode_ = ALTree }
|
2019-01-25 22:08:13 +03:00
|
|
|
showunbudgeted = empty_ ropts
|
2019-07-15 13:28:52 +03:00
|
|
|
budgetedaccts =
|
2020-11-20 01:39:52 +03:00
|
|
|
dbg3 "budgetedacctsinperiod" $
|
2021-03-01 14:35:21 +03:00
|
|
|
S.fromList $
|
|
|
|
expandAccountNames $
|
2019-07-15 13:28:52 +03:00
|
|
|
accountNamesFromPostings $
|
|
|
|
concatMap tpostings $
|
2020-01-04 04:13:50 +03:00
|
|
|
concatMap (`runPeriodicTransaction` reportspan) $
|
2018-04-24 00:18:13 +03:00
|
|
|
jperiodictxns j
|
2020-11-20 01:59:08 +03:00
|
|
|
actualj = journalWithBudgetAccountNames budgetedaccts showunbudgeted j
|
2021-06-04 15:40:10 +03:00
|
|
|
budgetj = journalAddBudgetGoalTransactions bopts ropts reportspan j
|
2020-01-04 05:39:04 +03:00
|
|
|
actualreport@(PeriodicReport actualspans _ _) =
|
2021-07-23 09:47:48 +03:00
|
|
|
dbg5 "actualreport" $ multiBalanceReport rspec{_rsReportOpts=ropts{empty_=True}} actualj
|
2020-01-04 05:39:04 +03:00
|
|
|
budgetgoalreport@(PeriodicReport _ budgetgoalitems budgetgoaltotals) =
|
2021-07-23 09:47:48 +03:00
|
|
|
dbg5 "budgetgoalreport" $ multiBalanceReport rspec{_rsReportOpts=ropts{empty_=True}} budgetj
|
2018-04-25 09:00:05 +03:00
|
|
|
budgetgoalreport'
|
|
|
|
-- If no interval is specified:
|
2019-07-15 13:28:52 +03:00
|
|
|
-- budgetgoalreport's span might be shorter actualreport's due to periodic txns;
|
|
|
|
-- it should be safe to replace it with the latter, so they combine well.
|
2020-01-04 05:39:04 +03:00
|
|
|
| interval_ ropts == NoInterval = PeriodicReport actualspans budgetgoalitems budgetgoaltotals
|
2019-07-15 13:28:52 +03:00
|
|
|
| otherwise = budgetgoalreport
|
2020-07-07 15:48:17 +03:00
|
|
|
budgetreport = combineBudgetAndActual ropts j budgetgoalreport' actualreport
|
2018-04-03 15:07:13 +03:00
|
|
|
|
2019-07-15 13:28:52 +03:00
|
|
|
-- | Use all periodic transactions in the journal to generate
|
2020-11-21 03:44:07 +03:00
|
|
|
-- budget goal transactions in the specified date span.
|
2020-11-20 01:59:08 +03:00
|
|
|
-- Budget goal transactions are similar to forecast transactions except
|
|
|
|
-- their purpose and effect is to define balance change goals, per account and period,
|
|
|
|
-- for BudgetReport.
|
2021-06-04 15:40:10 +03:00
|
|
|
journalAddBudgetGoalTransactions :: BalancingOpts -> ReportOpts -> DateSpan -> Journal -> Journal
|
|
|
|
journalAddBudgetGoalTransactions bopts _ropts reportspan j =
|
|
|
|
either error' id $ journalBalanceTransactions bopts j{ jtxns = budgetts } -- PARTIAL:
|
2018-04-03 15:07:13 +03:00
|
|
|
where
|
2020-11-20 01:39:52 +03:00
|
|
|
budgetspan = dbg3 "budget span" $ reportspan
|
2018-04-03 15:07:13 +03:00
|
|
|
budgetts =
|
2020-11-20 01:39:52 +03:00
|
|
|
dbg5 "budget goal txns" $
|
2018-04-03 15:07:13 +03:00
|
|
|
[makeBudgetTxn t
|
|
|
|
| pt <- jperiodictxns j
|
|
|
|
, t <- runPeriodicTransaction pt budgetspan
|
|
|
|
]
|
|
|
|
makeBudgetTxn t = txnTieKnot $ t { tdescription = T.pack "Budget transaction" }
|
|
|
|
|
2018-04-24 23:42:12 +03:00
|
|
|
-- | Adjust a journal's account names for budget reporting, in two ways:
|
2018-04-24 00:18:13 +03:00
|
|
|
--
|
2019-07-15 13:28:52 +03:00
|
|
|
-- 1. accounts with no budget goal anywhere in their ancestry are moved
|
2018-04-24 23:42:12 +03:00
|
|
|
-- under the "unbudgeted" top level account.
|
2018-04-24 00:18:13 +03:00
|
|
|
--
|
2018-04-24 23:42:12 +03:00
|
|
|
-- 2. subaccounts with no budget goal are merged with their closest parent account
|
2019-07-15 13:28:52 +03:00
|
|
|
-- with a budget goal, so that only budgeted accounts are shown.
|
2020-11-20 01:59:08 +03:00
|
|
|
-- This can be disabled by -E/--empty.
|
2018-04-24 00:18:13 +03:00
|
|
|
--
|
2021-03-01 14:35:21 +03:00
|
|
|
journalWithBudgetAccountNames :: S.Set AccountName -> Bool -> Journal -> Journal
|
|
|
|
journalWithBudgetAccountNames budgetedaccts showunbudgeted j =
|
|
|
|
dbg5With (("budget account names: "++).pshow.journalAccountNamesUsed) $
|
2020-11-20 01:59:08 +03:00
|
|
|
j { jtxns = remapTxn <$> jtxns j }
|
2018-04-24 00:18:13 +03:00
|
|
|
where
|
2021-03-01 14:35:21 +03:00
|
|
|
remapTxn = txnTieKnot . transactionTransformPostings remapPosting
|
|
|
|
remapPosting p = p { paccount = remapAccount $ paccount p, poriginal = poriginal p <|> Just p }
|
|
|
|
remapAccount a
|
|
|
|
| a `S.member` budgetedaccts = a
|
|
|
|
| Just p <- budgetedparent = if showunbudgeted then a else p
|
|
|
|
| otherwise = if showunbudgeted then u <> acctsep <> a else u
|
2018-04-24 00:18:13 +03:00
|
|
|
where
|
2021-03-01 14:35:21 +03:00
|
|
|
budgetedparent = find (`S.member` budgetedaccts) $ parentAccountNames a
|
|
|
|
u = unbudgetedAccountName
|
2018-04-03 15:07:13 +03:00
|
|
|
|
|
|
|
-- | Combine a per-account-and-subperiod report of budget goals, and one
|
|
|
|
-- of actual change amounts, into a budget performance report.
|
|
|
|
-- The two reports should have the same report interval, but need not
|
|
|
|
-- have exactly the same account rows or date columns.
|
|
|
|
-- (Cells in the combined budget report can be missing a budget goal,
|
|
|
|
-- an actual amount, or both.) The combined report will include:
|
|
|
|
--
|
|
|
|
-- - consecutive subperiods at the same interval as the two reports,
|
|
|
|
-- spanning the period of both reports
|
|
|
|
--
|
|
|
|
-- - all accounts mentioned in either report, sorted by account code or
|
|
|
|
-- account name or amount as appropriate.
|
|
|
|
--
|
2020-07-07 15:48:17 +03:00
|
|
|
combineBudgetAndActual :: ReportOpts -> Journal -> MultiBalanceReport -> MultiBalanceReport -> BudgetReport
|
|
|
|
combineBudgetAndActual ropts j
|
2020-06-13 12:58:58 +03:00
|
|
|
(PeriodicReport budgetperiods budgetrows (PeriodicReportRow _ budgettots budgetgrandtot budgetgrandavg))
|
|
|
|
(PeriodicReport actualperiods actualrows (PeriodicReportRow _ actualtots actualgrandtot actualgrandavg)) =
|
2020-07-07 15:48:17 +03:00
|
|
|
PeriodicReport periods sortedrows totalrow
|
2020-01-04 04:13:50 +03:00
|
|
|
where
|
|
|
|
periods = nubSort . filter (/= nulldatespan) $ budgetperiods ++ actualperiods
|
2018-04-03 15:07:13 +03:00
|
|
|
|
|
|
|
-- first, combine any corresponding budget goals with actual changes
|
|
|
|
rows1 =
|
2020-06-13 12:58:58 +03:00
|
|
|
[ PeriodicReportRow acct amtandgoals totamtandgoal avgamtandgoal
|
|
|
|
| PeriodicReportRow acct actualamts actualtot actualavg <- actualrows
|
2020-07-07 15:48:17 +03:00
|
|
|
, let mbudgetgoals = HM.lookup (displayFull acct) budgetGoalsByAcct :: Maybe ([BudgetGoal], BudgetTotal, BudgetAverage)
|
|
|
|
, let budgetmamts = maybe (Nothing <$ periods) (map Just . first3) mbudgetgoals :: [Maybe BudgetGoal]
|
2020-01-04 04:13:50 +03:00
|
|
|
, let mbudgettot = second3 <$> mbudgetgoals :: Maybe BudgetTotal
|
|
|
|
, let mbudgetavg = third3 <$> mbudgetgoals :: Maybe BudgetAverage
|
2018-04-03 15:07:13 +03:00
|
|
|
, let acctBudgetByPeriod = Map.fromList [ (p,budgetamt) | (p, Just budgetamt) <- zip budgetperiods budgetmamts ] :: Map DateSpan BudgetGoal
|
|
|
|
, let acctActualByPeriod = Map.fromList [ (p,actualamt) | (p, Just actualamt) <- zip actualperiods (map Just actualamts) ] :: Map DateSpan Change
|
2020-01-04 05:39:04 +03:00
|
|
|
, let amtandgoals = [ (Map.lookup p acctActualByPeriod, Map.lookup p acctBudgetByPeriod) | p <- periods ] :: [BudgetCell]
|
2018-04-03 15:07:13 +03:00
|
|
|
, let totamtandgoal = (Just actualtot, mbudgettot)
|
|
|
|
, let avgamtandgoal = (Just actualavg, mbudgetavg)
|
|
|
|
]
|
|
|
|
where
|
2020-07-07 15:48:17 +03:00
|
|
|
budgetGoalsByAcct :: HashMap AccountName ([BudgetGoal], BudgetTotal, BudgetAverage) =
|
|
|
|
HM.fromList [ (displayFull acct, (amts, tot, avg))
|
2020-06-13 12:58:58 +03:00
|
|
|
| PeriodicReportRow acct amts tot avg <- budgetrows ]
|
2018-04-03 15:07:13 +03:00
|
|
|
|
|
|
|
-- next, make rows for budget goals with no actual changes
|
|
|
|
rows2 =
|
2020-06-13 12:58:58 +03:00
|
|
|
[ PeriodicReportRow acct amtandgoals totamtandgoal avgamtandgoal
|
|
|
|
| PeriodicReportRow acct budgetgoals budgettot budgetavg <- budgetrows
|
|
|
|
, displayFull acct `notElem` map prrFullName rows1
|
2018-04-03 15:07:13 +03:00
|
|
|
, let acctBudgetByPeriod = Map.fromList $ zip budgetperiods budgetgoals :: Map DateSpan BudgetGoal
|
2020-01-04 05:39:04 +03:00
|
|
|
, let amtandgoals = [ (Nothing, Map.lookup p acctBudgetByPeriod) | p <- periods ] :: [BudgetCell]
|
2018-04-03 15:07:13 +03:00
|
|
|
, let totamtandgoal = (Nothing, Just budgettot)
|
|
|
|
, let avgamtandgoal = (Nothing, Just budgetavg)
|
|
|
|
]
|
|
|
|
|
|
|
|
-- combine and re-sort rows
|
2018-04-24 00:18:13 +03:00
|
|
|
-- TODO: add --sort-budget to sort by budget goal amount
|
2020-07-07 15:48:17 +03:00
|
|
|
sortedrows :: [BudgetReportRow] = sortRowsLike (mbrsorted unbudgetedrows ++ mbrsorted rows') rows
|
|
|
|
where
|
|
|
|
(unbudgetedrows, rows') = partition ((==unbudgetedAccountName) . prrFullName) rows
|
2021-01-29 15:34:18 +03:00
|
|
|
mbrsorted = map prrFullName . sortRows ropts j . map (fmap $ fromMaybe nullmixedamt . fst)
|
2020-07-07 15:48:17 +03:00
|
|
|
rows = rows1 ++ rows2
|
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
|
|
|
|
2018-04-03 15:07:13 +03:00
|
|
|
-- TODO: grand total & average shows 0% when there are no actual amounts, inconsistent with other cells
|
2020-06-13 12:58:58 +03:00
|
|
|
totalrow = PeriodicReportRow ()
|
2020-01-04 05:39:04 +03:00
|
|
|
[ (Map.lookup p totActualByPeriod, Map.lookup p totBudgetByPeriod) | p <- periods ]
|
|
|
|
( Just actualgrandtot, Just budgetgrandtot )
|
|
|
|
( Just actualgrandavg, Just budgetgrandavg )
|
2018-04-03 15:07:13 +03:00
|
|
|
where
|
|
|
|
totBudgetByPeriod = Map.fromList $ zip budgetperiods budgettots :: Map DateSpan BudgetTotal
|
|
|
|
totActualByPeriod = Map.fromList $ zip actualperiods actualtots :: Map DateSpan Change
|
|
|
|
|
|
|
|
-- | Render a budget report as plain text suitable for console output.
|
2020-11-09 08:54:28 +03:00
|
|
|
budgetReportAsText :: ReportOpts -> BudgetReport -> TL.Text
|
|
|
|
budgetReportAsText ropts@ReportOpts{..} budgetr = TB.toLazyText $
|
|
|
|
TB.fromText title <> TB.fromText "\n\n" <>
|
|
|
|
renderTableB def{tableBorders=False,prettyTable=pretty_tables_}
|
2021-01-13 08:31:13 +03:00
|
|
|
(textCell TopLeft) (textCell TopRight) (uncurry showcell) displayTableWithWidths
|
2018-04-03 15:07:13 +03:00
|
|
|
where
|
2020-11-09 08:54:28 +03:00
|
|
|
title = "Budget performance in " <> showDateSpan (periodicReportSpan budgetr)
|
lib,cli,ui: Separate costing from valuation; each can now be specified
independently.
You can now combine costing and valuation, for example "--cost
--value=then" will first convert to costs, and then value according to
the "--value=then" strategy. Any valuation strategy can be used with or
without costing.
If multiple valuation and costing strategies are specified on the
command line, then if any of them include costing
(-B/--cost/--value=cost) then amounts will be converted to cost, and for
valuation strategy the rightmost will be used.
--value=cost is deprecated, but still supported and is equivalent to
--cost/-B. --value=cost,COMM is no longer supported, but this behaviour can be
achieved with "--cost --value=then,COMM".
2021-01-26 05:13:11 +03:00
|
|
|
<> (case cost_ of
|
|
|
|
Cost -> ", converted to cost"
|
|
|
|
NoCost -> "")
|
2020-11-09 08:54:28 +03:00
|
|
|
<> (case value_ of
|
2020-12-30 08:04:08 +03:00
|
|
|
Just (AtThen _mc) -> ", valued at posting date"
|
2020-11-09 08:54:28 +03:00
|
|
|
Just (AtEnd _mc) -> ", valued at period ends"
|
|
|
|
Just (AtNow _mc) -> ", current value"
|
|
|
|
Just (AtDate d _mc) -> ", valued at " <> showDate d
|
|
|
|
Nothing -> "")
|
|
|
|
<> ":"
|
2020-09-15 09:53:14 +03:00
|
|
|
|
2020-11-09 08:54:28 +03:00
|
|
|
displayTableWithWidths :: Table Text Text ((Int, Int, Int), BudgetDisplayCell)
|
2020-09-15 09:53:14 +03:00
|
|
|
displayTableWithWidths = Table rh ch $ map (zipWith (,) widths) displaycells
|
|
|
|
Table rh ch displaycells = case budgetReportAsTable ropts budgetr of
|
|
|
|
Table rh' ch' vals -> maybetranspose . Table rh' ch' $ map (map displayCell) vals
|
|
|
|
|
|
|
|
displayCell (actual, budget) = (showamt actual', budgetAndPerc <$> budget)
|
2020-06-26 23:57:49 +03:00
|
|
|
where
|
2021-01-29 15:34:18 +03:00
|
|
|
actual' = fromMaybe nullmixedamt actual
|
2020-09-15 09:53:14 +03:00
|
|
|
budgetAndPerc b = (showamt b, showper <$> percentage actual' b)
|
2020-12-31 14:50:44 +03:00
|
|
|
showamt = (\(WideBuilder b w) -> (TL.toStrict $ TB.toLazyText b, w)) . showMixedAmountB oneLine{displayColour=color_, displayMaxWidth=Just 32}
|
2020-11-09 08:54:28 +03:00
|
|
|
showper p = let str = T.pack (show $ roundTo 0 p) in (str, T.length str)
|
2020-09-15 09:53:14 +03:00
|
|
|
cellWidth ((_,wa), Nothing) = (wa, 0, 0)
|
|
|
|
cellWidth ((_,wa), Just ((_,wb), Nothing)) = (wa, wb, 0)
|
|
|
|
cellWidth ((_,wa), Just ((_,wb), Just (_,wp))) = (wa, wb, wp)
|
|
|
|
|
|
|
|
widths = zip3 actualwidths budgetwidths percentwidths
|
|
|
|
actualwidths = map (maximum' . map (first3 . cellWidth)) cols
|
|
|
|
budgetwidths = map (maximum' . map (second3 . cellWidth)) cols
|
|
|
|
percentwidths = map (maximum' . map (third3 . cellWidth)) cols
|
|
|
|
cols = transpose displaycells
|
|
|
|
|
2018-04-23 18:18:51 +03:00
|
|
|
-- XXX lay out actual, percentage and/or goal in the single table cell for now, should probably use separate cells
|
2020-11-03 14:31:02 +03:00
|
|
|
showcell :: (Int, Int, Int) -> BudgetDisplayCell -> Cell
|
2020-09-15 09:53:14 +03:00
|
|
|
showcell (actualwidth, budgetwidth, percentwidth) ((actual,wa), mbudget) =
|
2020-12-24 03:18:25 +03:00
|
|
|
Cell TopRight [WideBuilder ( TB.fromText (T.replicate (actualwidth - wa) " ")
|
|
|
|
<> TB.fromText actual
|
|
|
|
<> budgetstr
|
|
|
|
) (actualwidth + totalbudgetwidth)]
|
2018-04-03 15:07:13 +03:00
|
|
|
where
|
2020-09-15 09:53:14 +03:00
|
|
|
totalpercentwidth = if percentwidth == 0 then 0 else percentwidth + 5
|
|
|
|
totalbudgetwidth = if budgetwidth == 0 then 0 else budgetwidth + totalpercentwidth + 3
|
2020-12-24 03:18:25 +03:00
|
|
|
budgetstr = TB.fromText $ case mbudget of
|
2020-11-09 08:54:28 +03:00
|
|
|
Nothing -> T.replicate totalbudgetwidth " "
|
|
|
|
Just ((budget, wb), Nothing) -> " [" <> T.replicate totalpercentwidth " " <> T.replicate (budgetwidth - wb) " " <> budget <> "]"
|
|
|
|
Just ((budget, wb), Just (pct, wp)) -> " [" <> T.replicate (percentwidth - wp) " " <> pct <> "% of " <> T.replicate (budgetwidth - wb) " " <> budget <> "]"
|
2018-04-03 15:07:13 +03:00
|
|
|
|
2018-04-23 18:18:51 +03:00
|
|
|
-- | Calculate the percentage of actual change to budget goal to show, if any.
|
2019-05-23 23:27:37 +03:00
|
|
|
-- If valuing at cost, both amounts are converted to cost before comparing.
|
2018-04-23 18:18:51 +03:00
|
|
|
-- A percentage will not be shown if:
|
|
|
|
-- - actual or goal are not the same, single, commodity
|
|
|
|
-- - the goal is zero
|
|
|
|
percentage :: Change -> BudgetGoal -> Maybe Percentage
|
|
|
|
percentage actual budget =
|
2021-01-31 07:23:46 +03:00
|
|
|
case (costedAmounts actual, costedAmounts budget) of
|
|
|
|
([a], [b]) | (acommodity a == acommodity b || amountLooksZero a) && not (amountLooksZero b)
|
2018-04-23 18:18:51 +03:00
|
|
|
-> Just $ 100 * aquantity a / aquantity b
|
2019-05-23 23:27:37 +03:00
|
|
|
_ -> -- trace (pshow $ (maybecost actual, maybecost budget)) -- debug missing percentage
|
|
|
|
Nothing
|
2018-04-23 18:18:51 +03:00
|
|
|
where
|
2021-01-31 07:23:46 +03:00
|
|
|
costedAmounts = case cost_ of
|
2021-02-13 14:07:52 +03:00
|
|
|
Cost -> amounts . mixedAmountCost
|
|
|
|
NoCost -> amounts
|
2018-04-03 15:07:13 +03:00
|
|
|
|
2019-05-10 01:39:43 +03:00
|
|
|
maybetranspose | transpose_ = \(Table rh ch vals) -> Table ch rh (transpose vals)
|
|
|
|
| otherwise = id
|
2019-01-24 23:54:23 +03:00
|
|
|
|
2018-04-03 15:07:13 +03:00
|
|
|
-- | Build a 'Table' from a multi-column balance report.
|
2020-11-09 08:54:28 +03:00
|
|
|
budgetReportAsTable :: ReportOpts -> BudgetReport -> Table Text Text (Maybe MixedAmount, Maybe MixedAmount)
|
2019-07-15 13:28:52 +03:00
|
|
|
budgetReportAsTable
|
2021-07-15 02:28:43 +03:00
|
|
|
ropts@ReportOpts{balanceaccum_}
|
2020-07-09 22:54:20 +03:00
|
|
|
(PeriodicReport spans rows (PeriodicReportRow _ coltots grandtot grandavg)) =
|
2019-07-15 13:28:52 +03:00
|
|
|
addtotalrow $
|
2018-04-03 15:07:13 +03:00
|
|
|
Table
|
2021-04-22 09:25:02 +03:00
|
|
|
(Tab.Group NoLine $ map Header accts)
|
|
|
|
(Tab.Group NoLine $ map Header colheadings)
|
2018-04-03 15:07:13 +03:00
|
|
|
(map rowvals rows)
|
|
|
|
where
|
2021-07-15 02:28:43 +03:00
|
|
|
colheadings = map (reportPeriodName balanceaccum_ spans) spans
|
2020-01-04 05:39:04 +03:00
|
|
|
++ [" Total" | row_total_ ropts]
|
|
|
|
++ ["Average" | average_ ropts]
|
2020-07-09 22:54:20 +03:00
|
|
|
|
2018-04-03 15:07:13 +03:00
|
|
|
accts = map renderacct rows
|
2020-06-13 12:58:58 +03:00
|
|
|
-- FIXME. Have to check explicitly for which to render here, since
|
|
|
|
-- budgetReport sets accountlistmode to ALTree. Find a principled way to do
|
|
|
|
-- this.
|
2020-07-07 15:48:17 +03:00
|
|
|
renderacct row = case accountlistmode_ ropts of
|
2020-11-09 08:54:28 +03:00
|
|
|
ALTree -> T.replicate ((prrDepth row - 1)*2) " " <> prrDisplayName row
|
|
|
|
ALFlat -> accountNameDrop (drop_ ropts) $ prrFullName row
|
2020-06-13 12:58:58 +03:00
|
|
|
rowvals (PeriodicReportRow _ as rowtot rowavg) =
|
2020-01-04 05:39:04 +03:00
|
|
|
as ++ [rowtot | row_total_ ropts] ++ [rowavg | average_ ropts]
|
|
|
|
addtotalrow
|
|
|
|
| no_total_ ropts = id
|
|
|
|
| otherwise = (+----+ (row "" $
|
|
|
|
coltots ++ [grandtot | row_total_ ropts && not (null coltots)]
|
|
|
|
++ [grandavg | average_ ropts && not (null coltots)]
|
|
|
|
))
|
2018-04-03 15:07:13 +03:00
|
|
|
|
2020-11-19 00:23:31 +03:00
|
|
|
-- XXX generalise this with multiBalanceReportAsCsv ?
|
|
|
|
-- | Render a budget report as CSV. Like multiBalanceReportAsCsv,
|
|
|
|
-- but includes alternating actual and budget amount columns.
|
|
|
|
budgetReportAsCsv :: ReportOpts -> BudgetReport -> CSV
|
2020-11-05 04:58:04 +03:00
|
|
|
budgetReportAsCsv
|
2020-11-19 00:23:31 +03:00
|
|
|
ReportOpts{average_, row_total_, no_total_, transpose_}
|
|
|
|
(PeriodicReport colspans items (PeriodicReportRow _ abtotals (magrandtot,mbgrandtot) (magrandavg,mbgrandavg)))
|
|
|
|
= (if transpose_ then transpose else id) $
|
|
|
|
|
|
|
|
-- heading row
|
2020-11-05 04:58:04 +03:00
|
|
|
("Account" :
|
2020-11-05 10:59:35 +03:00
|
|
|
concatMap (\span -> [showDateSpan span, "budget"]) colspans
|
2020-11-19 00:23:31 +03:00
|
|
|
++ concat [["Total" ,"budget"] | row_total_]
|
|
|
|
++ concat [["Average","budget"] | average_]
|
|
|
|
) :
|
|
|
|
|
|
|
|
-- account rows
|
2020-11-05 10:59:35 +03:00
|
|
|
[displayFull a :
|
2020-11-19 00:23:31 +03:00
|
|
|
map showmamt (flattentuples abamts)
|
|
|
|
++ concat [[showmamt mactualrowtot, showmamt mbudgetrowtot] | row_total_]
|
|
|
|
++ concat [[showmamt mactualrowavg, showmamt mbudgetrowavg] | average_]
|
|
|
|
| PeriodicReportRow a abamts (mactualrowtot,mbudgetrowtot) (mactualrowavg,mbudgetrowavg) <- items
|
|
|
|
]
|
|
|
|
|
|
|
|
-- totals row
|
|
|
|
++ concat [
|
|
|
|
[
|
|
|
|
"Total:" :
|
|
|
|
map showmamt (flattentuples abtotals)
|
2020-11-05 04:58:04 +03:00
|
|
|
++ concat [[showmamt magrandtot,showmamt mbgrandtot] | row_total_]
|
2020-11-19 00:23:31 +03:00
|
|
|
++ concat [[showmamt magrandavg,showmamt mbgrandavg] | average_]
|
|
|
|
]
|
|
|
|
| not no_total_
|
|
|
|
]
|
|
|
|
|
|
|
|
where
|
|
|
|
flattentuples abs = concat [[a,b] | (a,b) <- abs]
|
2020-12-31 14:50:44 +03:00
|
|
|
showmamt = maybe "" (wbToText . showMixedAmountB oneLine)
|
2020-11-19 00:23:31 +03:00
|
|
|
|
2018-09-04 22:23:07 +03:00
|
|
|
-- tests
|
|
|
|
|
2018-09-06 23:08:26 +03:00
|
|
|
tests_BudgetReport = tests "BudgetReport" [
|
2018-09-04 22:23:07 +03:00
|
|
|
]
|