2018-04-03 15:07:13 +03:00
|
|
|
{- |
|
|
|
|
-}
|
|
|
|
|
2020-09-15 09:53:14 +03:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
2018-04-03 15:07:13 +03:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2020-09-15 09:53:14 +03:00
|
|
|
{-# LANGUAGE TupleSections #-}
|
2018-04-03 15:07:13 +03:00
|
|
|
|
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
|
|
|
|
reportPeriodName,
|
|
|
|
-- * Tests
|
|
|
|
tests_BudgetReport
|
|
|
|
)
|
2018-04-03 15:07:13 +03:00
|
|
|
where
|
|
|
|
|
|
|
|
import Data.Decimal
|
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
|
2018-04-03 15:07:13 +03:00
|
|
|
import Data.List
|
2020-01-04 09:09:01 +03:00
|
|
|
import Data.List.Extra (nubSort)
|
2018-04-03 15:07:13 +03:00
|
|
|
import Data.Maybe
|
|
|
|
#if !(MIN_VERSION_base(4,11,0))
|
|
|
|
import Data.Monoid ((<>))
|
|
|
|
#endif
|
2018-04-24 23:42:12 +03:00
|
|
|
import Safe
|
2018-04-03 15:07:13 +03:00
|
|
|
--import Data.List
|
|
|
|
--import Data.Maybe
|
|
|
|
import qualified Data.Map as Map
|
|
|
|
import Data.Map (Map)
|
|
|
|
import qualified Data.Text as T
|
|
|
|
--import qualified Data.Text.Lazy as TL
|
|
|
|
--import System.Console.CmdArgs.Explicit as C
|
|
|
|
--import Lucid as L
|
2020-11-19 00:23:31 +03:00
|
|
|
|
2018-04-03 15:07:13 +03:00
|
|
|
import Text.Printf (printf)
|
|
|
|
import Text.Tabular as T
|
2020-09-15 05:02:53 +03:00
|
|
|
import Text.Tabular.AsciiWide as T
|
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-09-15 09:53:14 +03:00
|
|
|
type BudgetDisplayCell = ((String, Int), Maybe ((String, Int), Maybe (String, Int)))
|
|
|
|
|
2018-04-24 00:18:13 +03:00
|
|
|
-- | Calculate budget goals from all periodic transactions,
|
|
|
|
-- actual balance changes from the regular transactions,
|
|
|
|
-- and compare these to get a 'BudgetReport'.
|
|
|
|
-- Unbudgeted accounts may be hidden or renamed (see budgetRollup).
|
2020-09-16 04:45:52 +03:00
|
|
|
budgetReport :: ReportSpec -> Bool -> DateSpan -> Journal -> BudgetReport
|
2020-11-20 01:39:52 +03:00
|
|
|
budgetReport rspec assrt 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
|
2020-09-16 04:45:52 +03:00
|
|
|
ropts = (rsOpts 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" $
|
2019-07-15 13:28:52 +03:00
|
|
|
nub $
|
2019-01-16 03:32:35 +03:00
|
|
|
concatMap expandAccountName $
|
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:39:52 +03:00
|
|
|
actualj =
|
|
|
|
dbg5With (("account names adjusted for budget report:\n"++).pshow.journalAccountNamesUsed) $
|
|
|
|
budgetRollUp budgetedaccts showunbudgeted j
|
|
|
|
budgetj =
|
|
|
|
-- dbg5With (("actual txns:\n"++).pshow.jtxns) $
|
|
|
|
budgetJournal assrt ropts reportspan j
|
2020-01-04 05:39:04 +03:00
|
|
|
actualreport@(PeriodicReport actualspans _ _) =
|
2020-11-20 01:39:52 +03:00
|
|
|
dbg5 "actualreport" $ multiBalanceReport rspec{rsOpts=ropts{empty_=True}} actualj
|
2020-01-04 05:39:04 +03:00
|
|
|
budgetgoalreport@(PeriodicReport _ budgetgoalitems budgetgoaltotals) =
|
2020-11-20 01:39:52 +03:00
|
|
|
dbg5 "budgetgoalreport" $ multiBalanceReport rspec{rsOpts=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
|
2018-04-22 20:09:06 +03:00
|
|
|
-- budget transactions in the specified report period.
|
|
|
|
-- Budget transactions are similar to forecast transactions except
|
2020-11-20 01:39:52 +03:00
|
|
|
-- their purpose is to define balance change goals, per account and period.
|
2018-04-03 15:07:13 +03:00
|
|
|
budgetJournal :: Bool -> ReportOpts -> DateSpan -> Journal -> Journal
|
2018-04-22 20:09:06 +03:00
|
|
|
budgetJournal assrt _ropts reportspan j =
|
2020-08-06 02:05:56 +03:00
|
|
|
either error' id $ journalBalanceTransactions assrt 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.
|
2019-01-25 22:08:13 +03:00
|
|
|
-- This can be disabled by --empty.
|
2018-04-24 00:18:13 +03:00
|
|
|
--
|
|
|
|
budgetRollUp :: [AccountName] -> Bool -> Journal -> Journal
|
|
|
|
budgetRollUp budgetedaccts showunbudgeted j = j { jtxns = remapTxn <$> jtxns j }
|
|
|
|
where
|
|
|
|
remapTxn = mapPostings (map remapPosting)
|
|
|
|
where
|
2018-04-03 15:07:13 +03:00
|
|
|
mapPostings f t = txnTieKnot $ t { tpostings = f $ tpostings t }
|
2019-02-21 07:07:40 +03:00
|
|
|
remapPosting p = p { paccount = remapAccount $ paccount p, poriginal = Just . fromMaybe p $ poriginal p }
|
2018-04-24 00:18:13 +03:00
|
|
|
where
|
2018-04-24 23:42:12 +03:00
|
|
|
remapAccount a
|
|
|
|
| hasbudget = a
|
2019-07-15 13:28:52 +03:00
|
|
|
| hasbudgetedparent = if showunbudgeted then a else budgetedparent
|
2018-04-24 23:42:12 +03:00
|
|
|
| otherwise = if showunbudgeted then u <> acctsep <> a else u
|
2018-04-24 00:18:13 +03:00
|
|
|
where
|
2018-04-24 23:42:12 +03:00
|
|
|
hasbudget = a `elem` budgetedaccts
|
|
|
|
hasbudgetedparent = not $ T.null budgetedparent
|
|
|
|
budgetedparent = headDef "" $ filter (`elem` 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
|
|
|
|
mbrsorted = map prrFullName . sortRows ropts j . map (fmap $ fromMaybe 0 . fst)
|
|
|
|
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.
|
|
|
|
budgetReportAsText :: ReportOpts -> BudgetReport -> String
|
2020-01-04 05:39:04 +03:00
|
|
|
budgetReportAsText ropts@ReportOpts{..} budgetr =
|
2020-09-15 05:02:53 +03:00
|
|
|
title ++ "\n\n" ++
|
2020-11-03 14:31:02 +03:00
|
|
|
renderTable def{tableBorders=False,prettyTable=pretty_tables_}
|
|
|
|
(alignCell TopLeft) (alignCell TopRight) (uncurry showcell) displayTableWithWidths
|
2018-04-03 15:07:13 +03:00
|
|
|
where
|
2019-05-24 13:49:01 +03:00
|
|
|
multiperiod = interval_ /= NoInterval
|
2019-05-10 01:39:43 +03:00
|
|
|
title = printf "Budget performance in %s%s:"
|
2020-01-04 04:13:50 +03:00
|
|
|
(showDateSpan $ periodicReportSpan budgetr)
|
2019-05-23 10:36:16 +03:00
|
|
|
(case value_ of
|
2019-05-24 06:52:21 +03:00
|
|
|
Just (AtCost _mc) -> ", valued at cost"
|
2020-08-06 02:05:56 +03:00
|
|
|
Just (AtThen _mc) -> error' unsupportedValueThenError -- PARTIAL:
|
2019-05-23 10:36:16 +03:00
|
|
|
Just (AtEnd _mc) -> ", valued at period ends"
|
|
|
|
Just (AtNow _mc) -> ", current value"
|
2019-05-24 13:49:01 +03:00
|
|
|
-- XXX duplicates the above
|
|
|
|
Just (AtDefault _mc) | multiperiod -> ", valued at period ends"
|
|
|
|
Just (AtDefault _mc) -> ", current value"
|
2019-05-23 10:36:16 +03:00
|
|
|
Just (AtDate d _mc) -> ", valued at "++showDate d
|
|
|
|
Nothing -> "")
|
2020-09-15 09:53:14 +03:00
|
|
|
|
|
|
|
displayTableWithWidths :: Table String String ((Int, Int, Int), BudgetDisplayCell)
|
|
|
|
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
|
2020-09-15 09:53:14 +03:00
|
|
|
actual' = fromMaybe 0 actual
|
|
|
|
budgetAndPerc b = (showamt b, showper <$> percentage actual' b)
|
2020-11-02 06:20:19 +03:00
|
|
|
showamt = showMixedOneLine showAmountWithoutPrice Nothing (Just 32) color_
|
2020-09-15 09:53:14 +03:00
|
|
|
showper p = let str = show (roundTo 0 p) in (str, length str)
|
|
|
|
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-11-03 14:31:02 +03:00
|
|
|
Cell TopRight [(replicate (actualwidth - wa) ' ' ++ 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
|
2018-04-23 18:18:51 +03:00
|
|
|
budgetstr = case mbudget of
|
2020-09-15 09:53:14 +03:00
|
|
|
Nothing -> replicate totalbudgetwidth ' '
|
|
|
|
Just ((budget, wb), Nothing) -> " [" ++ replicate totalpercentwidth ' ' ++ replicate (budgetwidth - wb) ' ' ++ budget ++ "]"
|
|
|
|
Just ((budget, wb), Just (pct, wp)) -> " [" ++ replicate (percentwidth - wp) ' ' ++ pct ++ "% of " ++ 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 =
|
2019-05-23 23:27:37 +03:00
|
|
|
case (maybecost $ normaliseMixedAmount actual, maybecost $ normaliseMixedAmount budget) of
|
2020-05-30 04:57:22 +03:00
|
|
|
(Mixed [a], Mixed [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
|
2020-06-01 01:48:08 +03:00
|
|
|
maybecost = if valuationTypeIsCost ropts then mixedAmountCost else id
|
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.
|
|
|
|
budgetReportAsTable :: ReportOpts -> BudgetReport -> Table String String (Maybe MixedAmount, Maybe MixedAmount)
|
2019-07-15 13:28:52 +03:00
|
|
|
budgetReportAsTable
|
2020-07-09 21:45:46 +03:00
|
|
|
ropts@ReportOpts{balancetype_}
|
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
|
|
|
|
(T.Group NoLine $ map Header accts)
|
|
|
|
(T.Group NoLine $ map Header colheadings)
|
|
|
|
(map rowvals rows)
|
|
|
|
where
|
2020-07-09 22:54:20 +03:00
|
|
|
colheadings = map (reportPeriodName balancetype_ 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
|
|
|
|
ALTree -> replicate ((prrDepth row - 1)*2) ' ' ++ T.unpack (prrDisplayName row)
|
|
|
|
ALFlat -> T.unpack . 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-07-09 22:54:20 +03:00
|
|
|
-- | Make a name for the given period in a multiperiod report, given
|
|
|
|
-- the type of balance being reported and the full set of report
|
|
|
|
-- periods. This will be used as a column heading (or row heading, in
|
|
|
|
-- a register summary report). We try to pick a useful name as follows:
|
|
|
|
--
|
|
|
|
-- - ending-balance reports: the period's end date
|
|
|
|
--
|
|
|
|
-- - balance change reports where the periods are months and all in the same year:
|
|
|
|
-- the short month name in the current locale
|
|
|
|
--
|
|
|
|
-- - all other balance change reports: a description of the datespan,
|
|
|
|
-- abbreviated to compact form if possible (see showDateSpan).
|
|
|
|
--
|
|
|
|
reportPeriodName :: BalanceType -> [DateSpan] -> DateSpan -> String
|
|
|
|
reportPeriodName balancetype spans =
|
|
|
|
case balancetype of
|
2020-07-09 22:56:46 +03:00
|
|
|
PeriodChange -> if multiyear then showDateSpan else showDateSpanMonthAbbrev
|
|
|
|
where
|
|
|
|
multiyear = (>1) $ length $ nubSort $ map spanStartYear spans
|
|
|
|
_ -> maybe "" (showDate . prevday) . spanEnd
|
2020-07-09 22:54:20 +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
|
|
|
|
budgetReportAsCsv
|
|
|
|
ReportOpts{average_, row_total_, no_total_, transpose_}
|
|
|
|
(PeriodicReport colspans items (PeriodicReportRow _ abtotals (magrandtot,mbgrandtot) (magrandavg,mbgrandavg)))
|
|
|
|
= (if transpose_ then transpose else id) $
|
|
|
|
|
|
|
|
-- heading row
|
|
|
|
("Account" :
|
|
|
|
concatMap (\span -> [showDateSpan span, "budget"]) colspans
|
|
|
|
++ concat [["Total" ,"budget"] | row_total_]
|
|
|
|
++ concat [["Average","budget"] | average_]
|
|
|
|
) :
|
|
|
|
|
|
|
|
-- account rows
|
|
|
|
[T.unpack (displayFull a) :
|
|
|
|
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)
|
|
|
|
++ concat [[showmamt magrandtot,showmamt mbgrandtot] | row_total_]
|
|
|
|
++ concat [[showmamt magrandavg,showmamt mbgrandavg] | average_]
|
|
|
|
]
|
|
|
|
| not no_total_
|
|
|
|
]
|
|
|
|
|
|
|
|
where
|
|
|
|
flattentuples abs = concat [[a,b] | (a,b) <- abs]
|
|
|
|
showmamt = maybe "" (showMixedAmountOneLineWithoutPrice False)
|
|
|
|
|
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
|
|
|
]
|