{- |
-}

{-# LANGUAGE CPP                 #-}
{-# LANGUAGE NamedFieldPuns      #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE RecordWildCards     #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections       #-}

module Hledger.Reports.BudgetReport (
  BudgetGoal,
  BudgetTotal,
  BudgetAverage,
  BudgetCell,
  BudgetReportRow,
  BudgetReport,
  budgetReport,
  budgetReportAsTable,
  budgetReportAsText,
  budgetReportAsCsv,
  -- * Helpers
  reportPeriodName,
  -- * Tests
  tests_BudgetReport
)
where

import Data.Decimal
import Data.Default (def)
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HM
import Data.List
import Data.List.Extra (nubSort)
import Data.Maybe
#if !(MIN_VERSION_base(4,11,0))
import Data.Monoid ((<>))
#endif
import Safe
--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

import Text.Printf (printf)
import Text.Tabular as T
import Text.Tabular.AsciiWide as T

import Hledger.Data
import Hledger.Utils
import Hledger.Read.CsvReader (CSV)
import Hledger.Reports.ReportOptions
import Hledger.Reports.ReportTypes
import Hledger.Reports.MultiBalanceReport


type BudgetGoal    = Change
type BudgetTotal   = Total
type BudgetAverage = Average

-- | A budget report tracks expected and actual changes per account and subperiod.
type BudgetCell = (Maybe Change, Maybe BudgetGoal)
type BudgetReportRow = PeriodicReportRow DisplayName BudgetCell
type BudgetReport    = PeriodicReport    DisplayName BudgetCell

type BudgetDisplayCell = ((String, Int), Maybe ((String, Int), Maybe (String, Int)))

-- | 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'.
-- Unbudgeted accounts may be hidden or renamed (see journalWithBudgetAccountNames).
budgetReport :: ReportSpec -> Bool -> DateSpan -> Journal -> BudgetReport
budgetReport :: ReportSpec -> Bool -> DateSpan -> Journal -> BudgetReport
budgetReport ReportSpec
rspec Bool
assrt DateSpan
reportspan Journal
j = String -> BudgetReport -> BudgetReport
forall a. Show a => String -> a -> a
dbg4 String
"sortedbudgetreport" BudgetReport
budgetreport
  where
    -- Budget report demands ALTree mode to ensure subaccounts and subaccount budgets are properly handled
    -- and that reports with and without --empty make sense when compared side by side
    ropts :: ReportOpts
ropts = (ReportSpec -> ReportOpts
rsOpts ReportSpec
rspec){ accountlistmode_ :: AccountListMode
accountlistmode_ = AccountListMode
ALTree }
    showunbudgeted :: Bool
showunbudgeted = ReportOpts -> Bool
empty_ ReportOpts
ropts
    budgetedaccts :: [AccountName]
budgetedaccts =
      String -> [AccountName] -> [AccountName]
forall a. Show a => String -> a -> a
dbg3 String
"budgetedacctsinperiod" ([AccountName] -> [AccountName]) -> [AccountName] -> [AccountName]
forall a b. (a -> b) -> a -> b
$
      [AccountName] -> [AccountName]
forall a. Eq a => [a] -> [a]
nub ([AccountName] -> [AccountName]) -> [AccountName] -> [AccountName]
forall a b. (a -> b) -> a -> b
$
      (AccountName -> [AccountName]) -> [AccountName] -> [AccountName]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap AccountName -> [AccountName]
expandAccountName ([AccountName] -> [AccountName]) -> [AccountName] -> [AccountName]
forall a b. (a -> b) -> a -> b
$
      [Posting] -> [AccountName]
accountNamesFromPostings ([Posting] -> [AccountName]) -> [Posting] -> [AccountName]
forall a b. (a -> b) -> a -> b
$
      (Transaction -> [Posting]) -> [Transaction] -> [Posting]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Transaction -> [Posting]
tpostings ([Transaction] -> [Posting]) -> [Transaction] -> [Posting]
forall a b. (a -> b) -> a -> b
$
      (PeriodicTransaction -> [Transaction])
-> [PeriodicTransaction] -> [Transaction]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (PeriodicTransaction -> DateSpan -> [Transaction]
`runPeriodicTransaction` DateSpan
reportspan) ([PeriodicTransaction] -> [Transaction])
-> [PeriodicTransaction] -> [Transaction]
forall a b. (a -> b) -> a -> b
$
      Journal -> [PeriodicTransaction]
jperiodictxns Journal
j
    actualj :: Journal
actualj = [AccountName] -> Bool -> Journal -> Journal
journalWithBudgetAccountNames [AccountName]
budgetedaccts Bool
showunbudgeted Journal
j
    budgetj :: Journal
budgetj = Bool -> ReportOpts -> DateSpan -> Journal -> Journal
journalAddBudgetGoalTransactions Bool
assrt ReportOpts
ropts DateSpan
reportspan Journal
j
    actualreport :: PeriodicReport DisplayName MixedAmount
actualreport@(PeriodicReport [DateSpan]
actualspans [PeriodicReportRow DisplayName MixedAmount]
_ PeriodicReportRow () MixedAmount
_) =
        String
-> PeriodicReport DisplayName MixedAmount
-> PeriodicReport DisplayName MixedAmount
forall a. Show a => String -> a -> a
dbg5 String
"actualreport" (PeriodicReport DisplayName MixedAmount
 -> PeriodicReport DisplayName MixedAmount)
-> PeriodicReport DisplayName MixedAmount
-> PeriodicReport DisplayName MixedAmount
forall a b. (a -> b) -> a -> b
$ ReportSpec -> Journal -> PeriodicReport DisplayName MixedAmount
multiBalanceReport ReportSpec
rspec{rsOpts :: ReportOpts
rsOpts=ReportOpts
ropts{empty_ :: Bool
empty_=Bool
True}} Journal
actualj
    budgetgoalreport :: PeriodicReport DisplayName MixedAmount
budgetgoalreport@(PeriodicReport [DateSpan]
_ [PeriodicReportRow DisplayName MixedAmount]
budgetgoalitems PeriodicReportRow () MixedAmount
budgetgoaltotals) =
        String
-> PeriodicReport DisplayName MixedAmount
-> PeriodicReport DisplayName MixedAmount
forall a. Show a => String -> a -> a
dbg5 String
"budgetgoalreport" (PeriodicReport DisplayName MixedAmount
 -> PeriodicReport DisplayName MixedAmount)
-> PeriodicReport DisplayName MixedAmount
-> PeriodicReport DisplayName MixedAmount
forall a b. (a -> b) -> a -> b
$ ReportSpec -> Journal -> PeriodicReport DisplayName MixedAmount
multiBalanceReport ReportSpec
rspec{rsOpts :: ReportOpts
rsOpts=ReportOpts
ropts{empty_ :: Bool
empty_=Bool
True}} Journal
budgetj
    budgetgoalreport' :: PeriodicReport DisplayName MixedAmount
budgetgoalreport'
      -- If no interval is specified:
      -- 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.
      | ReportOpts -> Interval
interval_ ReportOpts
ropts Interval -> Interval -> Bool
forall a. Eq a => a -> a -> Bool
== Interval
NoInterval = [DateSpan]
-> [PeriodicReportRow DisplayName MixedAmount]
-> PeriodicReportRow () MixedAmount
-> PeriodicReport DisplayName MixedAmount
forall a b.
[DateSpan]
-> [PeriodicReportRow a b]
-> PeriodicReportRow () b
-> PeriodicReport a b
PeriodicReport [DateSpan]
actualspans [PeriodicReportRow DisplayName MixedAmount]
budgetgoalitems PeriodicReportRow () MixedAmount
budgetgoaltotals
      | Bool
otherwise = PeriodicReport DisplayName MixedAmount
budgetgoalreport
    budgetreport :: BudgetReport
budgetreport = ReportOpts
-> Journal
-> PeriodicReport DisplayName MixedAmount
-> PeriodicReport DisplayName MixedAmount
-> BudgetReport
combineBudgetAndActual ReportOpts
ropts Journal
j PeriodicReport DisplayName MixedAmount
budgetgoalreport' PeriodicReport DisplayName MixedAmount
actualreport

-- | Use all periodic transactions in the journal to generate
-- budget goal transactions in the specified date span.
-- 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.
journalAddBudgetGoalTransactions :: Bool -> ReportOpts -> DateSpan -> Journal -> Journal
journalAddBudgetGoalTransactions :: Bool -> ReportOpts -> DateSpan -> Journal -> Journal
journalAddBudgetGoalTransactions Bool
assrt ReportOpts
_ropts DateSpan
reportspan Journal
j =
  (String -> Journal)
-> (Journal -> Journal) -> Either String Journal -> Journal
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> Journal
forall a. String -> a
error' Journal -> Journal
forall a. a -> a
id (Either String Journal -> Journal)
-> Either String Journal -> Journal
forall a b. (a -> b) -> a -> b
$ Bool -> Journal -> Either String Journal
journalBalanceTransactions Bool
assrt Journal
j{ jtxns :: [Transaction]
jtxns = [Transaction]
budgetts }  -- PARTIAL:
  where
    budgetspan :: DateSpan
budgetspan = String -> DateSpan -> DateSpan
forall a. Show a => String -> a -> a
dbg3 String
"budget span" (DateSpan -> DateSpan) -> DateSpan -> DateSpan
forall a b. (a -> b) -> a -> b
$ DateSpan
reportspan
    budgetts :: [Transaction]
budgetts =
      String -> [Transaction] -> [Transaction]
forall a. Show a => String -> a -> a
dbg5 String
"budget goal txns" ([Transaction] -> [Transaction]) -> [Transaction] -> [Transaction]
forall a b. (a -> b) -> a -> b
$
      [Transaction -> Transaction
makeBudgetTxn Transaction
t
      | PeriodicTransaction
pt <- Journal -> [PeriodicTransaction]
jperiodictxns Journal
j
      , Transaction
t <- PeriodicTransaction -> DateSpan -> [Transaction]
runPeriodicTransaction PeriodicTransaction
pt DateSpan
budgetspan
      ]
    makeBudgetTxn :: Transaction -> Transaction
makeBudgetTxn Transaction
t = Transaction -> Transaction
txnTieKnot (Transaction -> Transaction) -> Transaction -> Transaction
forall a b. (a -> b) -> a -> b
$ Transaction
t { tdescription :: AccountName
tdescription = String -> AccountName
T.pack String
"Budget transaction" }

-- | Adjust a journal's account names for budget reporting, in two ways:
--
-- 1. accounts with no budget goal anywhere in their ancestry are moved
--    under the "unbudgeted" top level account.
--
-- 2. subaccounts with no budget goal are merged with their closest parent account
--    with a budget goal, so that only budgeted accounts are shown.
--    This can be disabled by -E/--empty.
--
journalWithBudgetAccountNames :: [AccountName] -> Bool -> Journal -> Journal
journalWithBudgetAccountNames :: [AccountName] -> Bool -> Journal -> Journal
journalWithBudgetAccountNames [AccountName]
budgetedaccts Bool
showunbudgeted Journal
j = 
  (Journal -> String) -> Journal -> Journal
forall a. Show a => (a -> String) -> a -> a
dbg5With ((String
"budget account names: "String -> String -> String
forall a. [a] -> [a] -> [a]
++)(String -> String) -> (Journal -> String) -> Journal -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.[AccountName] -> String
forall a. Show a => a -> String
pshow([AccountName] -> String)
-> (Journal -> [AccountName]) -> Journal -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Journal -> [AccountName]
journalAccountNamesUsed) (Journal -> Journal) -> Journal -> Journal
forall a b. (a -> b) -> a -> b
$ 
  Journal
j { jtxns :: [Transaction]
jtxns = Transaction -> Transaction
remapTxn (Transaction -> Transaction) -> [Transaction] -> [Transaction]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Journal -> [Transaction]
jtxns Journal
j }
  where
    remapTxn :: Transaction -> Transaction
remapTxn = ([Posting] -> [Posting]) -> Transaction -> Transaction
mapPostings ((Posting -> Posting) -> [Posting] -> [Posting]
forall a b. (a -> b) -> [a] -> [b]
map Posting -> Posting
remapPosting)
      where
        mapPostings :: ([Posting] -> [Posting]) -> Transaction -> Transaction
mapPostings [Posting] -> [Posting]
f Transaction
t = Transaction -> Transaction
txnTieKnot (Transaction -> Transaction) -> Transaction -> Transaction
forall a b. (a -> b) -> a -> b
$ Transaction
t { tpostings :: [Posting]
tpostings = [Posting] -> [Posting]
f ([Posting] -> [Posting]) -> [Posting] -> [Posting]
forall a b. (a -> b) -> a -> b
$ Transaction -> [Posting]
tpostings Transaction
t }
        remapPosting :: Posting -> Posting
remapPosting Posting
p = Posting
p { paccount :: AccountName
paccount = AccountName -> AccountName
remapAccount (AccountName -> AccountName) -> AccountName -> AccountName
forall a b. (a -> b) -> a -> b
$ Posting -> AccountName
paccount Posting
p, poriginal :: Maybe Posting
poriginal = Posting -> Maybe Posting
forall a. a -> Maybe a
Just (Posting -> Maybe Posting)
-> (Maybe Posting -> Posting) -> Maybe Posting -> Maybe Posting
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Posting -> Maybe Posting -> Posting
forall a. a -> Maybe a -> a
fromMaybe Posting
p (Maybe Posting -> Maybe Posting) -> Maybe Posting -> Maybe Posting
forall a b. (a -> b) -> a -> b
$ Posting -> Maybe Posting
poriginal Posting
p }
          where
            remapAccount :: AccountName -> AccountName
remapAccount AccountName
a
              | Bool
hasbudget         = AccountName
a
              | Bool
hasbudgetedparent = if Bool
showunbudgeted then AccountName
a else AccountName
budgetedparent
              | Bool
otherwise         = if Bool
showunbudgeted then AccountName
u AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<> AccountName
acctsep AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<> AccountName
a else AccountName
u
              where
                hasbudget :: Bool
hasbudget = AccountName
a AccountName -> [AccountName] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [AccountName]
budgetedaccts
                hasbudgetedparent :: Bool
hasbudgetedparent = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ AccountName -> Bool
T.null AccountName
budgetedparent
                budgetedparent :: AccountName
budgetedparent = AccountName -> [AccountName] -> AccountName
forall a. a -> [a] -> a
headDef AccountName
"" ([AccountName] -> AccountName) -> [AccountName] -> AccountName
forall a b. (a -> b) -> a -> b
$ (AccountName -> Bool) -> [AccountName] -> [AccountName]
forall a. (a -> Bool) -> [a] -> [a]
filter (AccountName -> [AccountName] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [AccountName]
budgetedaccts) ([AccountName] -> [AccountName]) -> [AccountName] -> [AccountName]
forall a b. (a -> b) -> a -> b
$ AccountName -> [AccountName]
parentAccountNames AccountName
a
                u :: AccountName
u = AccountName
unbudgetedAccountName

-- | 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.
--
combineBudgetAndActual :: ReportOpts -> Journal -> MultiBalanceReport -> MultiBalanceReport -> BudgetReport
combineBudgetAndActual :: ReportOpts
-> Journal
-> PeriodicReport DisplayName MixedAmount
-> PeriodicReport DisplayName MixedAmount
-> BudgetReport
combineBudgetAndActual ReportOpts
ropts Journal
j
      (PeriodicReport [DateSpan]
budgetperiods [PeriodicReportRow DisplayName MixedAmount]
budgetrows (PeriodicReportRow ()
_ [MixedAmount]
budgettots MixedAmount
budgetgrandtot MixedAmount
budgetgrandavg))
      (PeriodicReport [DateSpan]
actualperiods [PeriodicReportRow DisplayName MixedAmount]
actualrows (PeriodicReportRow ()
_ [MixedAmount]
actualtots MixedAmount
actualgrandtot MixedAmount
actualgrandavg)) =
    [DateSpan]
-> [PeriodicReportRow DisplayName BudgetCell]
-> PeriodicReportRow () BudgetCell
-> BudgetReport
forall a b.
[DateSpan]
-> [PeriodicReportRow a b]
-> PeriodicReportRow () b
-> PeriodicReport a b
PeriodicReport [DateSpan]
periods [PeriodicReportRow DisplayName BudgetCell]
sortedrows PeriodicReportRow () BudgetCell
totalrow
  where
    periods :: [DateSpan]
periods = [DateSpan] -> [DateSpan]
forall a. Ord a => [a] -> [a]
nubSort ([DateSpan] -> [DateSpan])
-> ([DateSpan] -> [DateSpan]) -> [DateSpan] -> [DateSpan]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DateSpan -> Bool) -> [DateSpan] -> [DateSpan]
forall a. (a -> Bool) -> [a] -> [a]
filter (DateSpan -> DateSpan -> Bool
forall a. Eq a => a -> a -> Bool
/= DateSpan
nulldatespan) ([DateSpan] -> [DateSpan]) -> [DateSpan] -> [DateSpan]
forall a b. (a -> b) -> a -> b
$ [DateSpan]
budgetperiods [DateSpan] -> [DateSpan] -> [DateSpan]
forall a. [a] -> [a] -> [a]
++ [DateSpan]
actualperiods

    -- first, combine any corresponding budget goals with actual changes
    rows1 :: [PeriodicReportRow DisplayName BudgetCell]
rows1 =
      [ DisplayName
-> [BudgetCell]
-> BudgetCell
-> BudgetCell
-> PeriodicReportRow DisplayName BudgetCell
forall a b. a -> [b] -> b -> b -> PeriodicReportRow a b
PeriodicReportRow DisplayName
acct [BudgetCell]
amtandgoals BudgetCell
totamtandgoal BudgetCell
avgamtandgoal
      | PeriodicReportRow DisplayName
acct [MixedAmount]
actualamts MixedAmount
actualtot MixedAmount
actualavg <- [PeriodicReportRow DisplayName MixedAmount]
actualrows
      , let mbudgetgoals :: Maybe ([MixedAmount], MixedAmount, MixedAmount)
mbudgetgoals       = AccountName
-> HashMap AccountName ([MixedAmount], MixedAmount, MixedAmount)
-> Maybe ([MixedAmount], MixedAmount, MixedAmount)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HM.lookup (DisplayName -> AccountName
displayFull DisplayName
acct) HashMap AccountName ([MixedAmount], MixedAmount, MixedAmount)
budgetGoalsByAcct :: Maybe ([BudgetGoal], BudgetTotal, BudgetAverage)
      , let budgetmamts :: [Maybe MixedAmount]
budgetmamts        = [Maybe MixedAmount]
-> (([MixedAmount], MixedAmount, MixedAmount)
    -> [Maybe MixedAmount])
-> Maybe ([MixedAmount], MixedAmount, MixedAmount)
-> [Maybe MixedAmount]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Maybe MixedAmount
forall a. Maybe a
Nothing Maybe MixedAmount -> [DateSpan] -> [Maybe MixedAmount]
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [DateSpan]
periods) ((MixedAmount -> Maybe MixedAmount)
-> [MixedAmount] -> [Maybe MixedAmount]
forall a b. (a -> b) -> [a] -> [b]
map MixedAmount -> Maybe MixedAmount
forall a. a -> Maybe a
Just ([MixedAmount] -> [Maybe MixedAmount])
-> (([MixedAmount], MixedAmount, MixedAmount) -> [MixedAmount])
-> ([MixedAmount], MixedAmount, MixedAmount)
-> [Maybe MixedAmount]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([MixedAmount], MixedAmount, MixedAmount) -> [MixedAmount]
forall a b c. (a, b, c) -> a
first3) Maybe ([MixedAmount], MixedAmount, MixedAmount)
mbudgetgoals :: [Maybe BudgetGoal]
      , let mbudgettot :: Maybe MixedAmount
mbudgettot         = ([MixedAmount], MixedAmount, MixedAmount) -> MixedAmount
forall a b c. (a, b, c) -> b
second3 (([MixedAmount], MixedAmount, MixedAmount) -> MixedAmount)
-> Maybe ([MixedAmount], MixedAmount, MixedAmount)
-> Maybe MixedAmount
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe ([MixedAmount], MixedAmount, MixedAmount)
mbudgetgoals :: Maybe BudgetTotal
      , let mbudgetavg :: Maybe MixedAmount
mbudgetavg         = ([MixedAmount], MixedAmount, MixedAmount) -> MixedAmount
forall a b c. (a, b, c) -> c
third3 (([MixedAmount], MixedAmount, MixedAmount) -> MixedAmount)
-> Maybe ([MixedAmount], MixedAmount, MixedAmount)
-> Maybe MixedAmount
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe ([MixedAmount], MixedAmount, MixedAmount)
mbudgetgoals  :: Maybe BudgetAverage
      , let acctBudgetByPeriod :: Map DateSpan MixedAmount
acctBudgetByPeriod = [(DateSpan, MixedAmount)] -> Map DateSpan MixedAmount
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [ (DateSpan
p,MixedAmount
budgetamt) | (DateSpan
p, Just MixedAmount
budgetamt) <- [DateSpan]
-> [Maybe MixedAmount] -> [(DateSpan, Maybe MixedAmount)]
forall a b. [a] -> [b] -> [(a, b)]
zip [DateSpan]
budgetperiods [Maybe MixedAmount]
budgetmamts ] :: Map DateSpan BudgetGoal
      , let acctActualByPeriod :: Map DateSpan MixedAmount
acctActualByPeriod = [(DateSpan, MixedAmount)] -> Map DateSpan MixedAmount
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [ (DateSpan
p,MixedAmount
actualamt) | (DateSpan
p, Just MixedAmount
actualamt) <- [DateSpan]
-> [Maybe MixedAmount] -> [(DateSpan, Maybe MixedAmount)]
forall a b. [a] -> [b] -> [(a, b)]
zip [DateSpan]
actualperiods ((MixedAmount -> Maybe MixedAmount)
-> [MixedAmount] -> [Maybe MixedAmount]
forall a b. (a -> b) -> [a] -> [b]
map MixedAmount -> Maybe MixedAmount
forall a. a -> Maybe a
Just [MixedAmount]
actualamts) ] :: Map DateSpan Change
      , let amtandgoals :: [BudgetCell]
amtandgoals        = [ (DateSpan -> Map DateSpan MixedAmount -> Maybe MixedAmount
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup DateSpan
p Map DateSpan MixedAmount
acctActualByPeriod, DateSpan -> Map DateSpan MixedAmount -> Maybe MixedAmount
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup DateSpan
p Map DateSpan MixedAmount
acctBudgetByPeriod) | DateSpan
p <- [DateSpan]
periods ] :: [BudgetCell]
      , let totamtandgoal :: BudgetCell
totamtandgoal      = (MixedAmount -> Maybe MixedAmount
forall a. a -> Maybe a
Just MixedAmount
actualtot, Maybe MixedAmount
mbudgettot)
      , let avgamtandgoal :: BudgetCell
avgamtandgoal      = (MixedAmount -> Maybe MixedAmount
forall a. a -> Maybe a
Just MixedAmount
actualavg, Maybe MixedAmount
mbudgetavg)
      ]
      where
        HashMap AccountName ([MixedAmount], MixedAmount, MixedAmount)
budgetGoalsByAcct :: HashMap AccountName ([BudgetGoal], BudgetTotal, BudgetAverage) =
          [(AccountName, ([MixedAmount], MixedAmount, MixedAmount))]
-> HashMap AccountName ([MixedAmount], MixedAmount, MixedAmount)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HM.fromList [ (DisplayName -> AccountName
displayFull DisplayName
acct, ([MixedAmount]
amts, MixedAmount
tot, MixedAmount
avg))
                         | PeriodicReportRow DisplayName
acct [MixedAmount]
amts MixedAmount
tot MixedAmount
avg <- [PeriodicReportRow DisplayName MixedAmount]
budgetrows ]

    -- next, make rows for budget goals with no actual changes
    rows2 :: [PeriodicReportRow DisplayName BudgetCell]
rows2 =
      [ DisplayName
-> [BudgetCell]
-> BudgetCell
-> BudgetCell
-> PeriodicReportRow DisplayName BudgetCell
forall a b. a -> [b] -> b -> b -> PeriodicReportRow a b
PeriodicReportRow DisplayName
acct [BudgetCell]
amtandgoals BudgetCell
forall a. (Maybe a, Maybe MixedAmount)
totamtandgoal BudgetCell
forall a. (Maybe a, Maybe MixedAmount)
avgamtandgoal
      | PeriodicReportRow DisplayName
acct [MixedAmount]
budgetgoals MixedAmount
budgettot MixedAmount
budgetavg <- [PeriodicReportRow DisplayName MixedAmount]
budgetrows
      , DisplayName -> AccountName
displayFull DisplayName
acct AccountName -> [AccountName] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` (PeriodicReportRow DisplayName BudgetCell -> AccountName)
-> [PeriodicReportRow DisplayName BudgetCell] -> [AccountName]
forall a b. (a -> b) -> [a] -> [b]
map PeriodicReportRow DisplayName BudgetCell -> AccountName
forall a. PeriodicReportRow DisplayName a -> AccountName
prrFullName [PeriodicReportRow DisplayName BudgetCell]
rows1
      , let acctBudgetByPeriod :: Map DateSpan MixedAmount
acctBudgetByPeriod = [(DateSpan, MixedAmount)] -> Map DateSpan MixedAmount
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(DateSpan, MixedAmount)] -> Map DateSpan MixedAmount)
-> [(DateSpan, MixedAmount)] -> Map DateSpan MixedAmount
forall a b. (a -> b) -> a -> b
$ [DateSpan] -> [MixedAmount] -> [(DateSpan, MixedAmount)]
forall a b. [a] -> [b] -> [(a, b)]
zip [DateSpan]
budgetperiods [MixedAmount]
budgetgoals :: Map DateSpan BudgetGoal
      , let amtandgoals :: [BudgetCell]
amtandgoals        = [ (Maybe MixedAmount
forall a. Maybe a
Nothing, DateSpan -> Map DateSpan MixedAmount -> Maybe MixedAmount
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup DateSpan
p Map DateSpan MixedAmount
acctBudgetByPeriod) | DateSpan
p <- [DateSpan]
periods ] :: [BudgetCell]
      , let totamtandgoal :: (Maybe a, Maybe MixedAmount)
totamtandgoal      = (Maybe a
forall a. Maybe a
Nothing, MixedAmount -> Maybe MixedAmount
forall a. a -> Maybe a
Just MixedAmount
budgettot)
      , let avgamtandgoal :: (Maybe a, Maybe MixedAmount)
avgamtandgoal      = (Maybe a
forall a. Maybe a
Nothing, MixedAmount -> Maybe MixedAmount
forall a. a -> Maybe a
Just MixedAmount
budgetavg)
      ]

    -- combine and re-sort rows
    -- TODO: add --sort-budget to sort by budget goal amount
    [PeriodicReportRow DisplayName BudgetCell]
sortedrows :: [BudgetReportRow] = [AccountName]
-> [PeriodicReportRow DisplayName BudgetCell]
-> [PeriodicReportRow DisplayName BudgetCell]
forall b.
[AccountName]
-> [PeriodicReportRow DisplayName b]
-> [PeriodicReportRow DisplayName b]
sortRowsLike ([PeriodicReportRow DisplayName BudgetCell] -> [AccountName]
forall b.
[PeriodicReportRow DisplayName (Maybe MixedAmount, b)]
-> [AccountName]
mbrsorted [PeriodicReportRow DisplayName BudgetCell]
unbudgetedrows [AccountName] -> [AccountName] -> [AccountName]
forall a. [a] -> [a] -> [a]
++ [PeriodicReportRow DisplayName BudgetCell] -> [AccountName]
forall b.
[PeriodicReportRow DisplayName (Maybe MixedAmount, b)]
-> [AccountName]
mbrsorted [PeriodicReportRow DisplayName BudgetCell]
rows') [PeriodicReportRow DisplayName BudgetCell]
rows
      where
        ([PeriodicReportRow DisplayName BudgetCell]
unbudgetedrows, [PeriodicReportRow DisplayName BudgetCell]
rows') = (PeriodicReportRow DisplayName BudgetCell -> Bool)
-> [PeriodicReportRow DisplayName BudgetCell]
-> ([PeriodicReportRow DisplayName BudgetCell],
    [PeriodicReportRow DisplayName BudgetCell])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition ((AccountName -> AccountName -> Bool
forall a. Eq a => a -> a -> Bool
==AccountName
unbudgetedAccountName) (AccountName -> Bool)
-> (PeriodicReportRow DisplayName BudgetCell -> AccountName)
-> PeriodicReportRow DisplayName BudgetCell
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PeriodicReportRow DisplayName BudgetCell -> AccountName
forall a. PeriodicReportRow DisplayName a -> AccountName
prrFullName) [PeriodicReportRow DisplayName BudgetCell]
rows
        mbrsorted :: [PeriodicReportRow DisplayName (Maybe MixedAmount, b)]
-> [AccountName]
mbrsorted = (PeriodicReportRow DisplayName MixedAmount -> AccountName)
-> [PeriodicReportRow DisplayName MixedAmount] -> [AccountName]
forall a b. (a -> b) -> [a] -> [b]
map PeriodicReportRow DisplayName MixedAmount -> AccountName
forall a. PeriodicReportRow DisplayName a -> AccountName
prrFullName ([PeriodicReportRow DisplayName MixedAmount] -> [AccountName])
-> ([PeriodicReportRow DisplayName (Maybe MixedAmount, b)]
    -> [PeriodicReportRow DisplayName MixedAmount])
-> [PeriodicReportRow DisplayName (Maybe MixedAmount, b)]
-> [AccountName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReportOpts
-> Journal
-> [PeriodicReportRow DisplayName MixedAmount]
-> [PeriodicReportRow DisplayName MixedAmount]
sortRows ReportOpts
ropts Journal
j ([PeriodicReportRow DisplayName MixedAmount]
 -> [PeriodicReportRow DisplayName MixedAmount])
-> ([PeriodicReportRow DisplayName (Maybe MixedAmount, b)]
    -> [PeriodicReportRow DisplayName MixedAmount])
-> [PeriodicReportRow DisplayName (Maybe MixedAmount, b)]
-> [PeriodicReportRow DisplayName MixedAmount]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PeriodicReportRow DisplayName (Maybe MixedAmount, b)
 -> PeriodicReportRow DisplayName MixedAmount)
-> [PeriodicReportRow DisplayName (Maybe MixedAmount, b)]
-> [PeriodicReportRow DisplayName MixedAmount]
forall a b. (a -> b) -> [a] -> [b]
map (((Maybe MixedAmount, b) -> MixedAmount)
-> PeriodicReportRow DisplayName (Maybe MixedAmount, b)
-> PeriodicReportRow DisplayName MixedAmount
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((Maybe MixedAmount, b) -> MixedAmount)
 -> PeriodicReportRow DisplayName (Maybe MixedAmount, b)
 -> PeriodicReportRow DisplayName MixedAmount)
-> ((Maybe MixedAmount, b) -> MixedAmount)
-> PeriodicReportRow DisplayName (Maybe MixedAmount, b)
-> PeriodicReportRow DisplayName MixedAmount
forall a b. (a -> b) -> a -> b
$ MixedAmount -> Maybe MixedAmount -> MixedAmount
forall a. a -> Maybe a -> a
fromMaybe MixedAmount
0 (Maybe MixedAmount -> MixedAmount)
-> ((Maybe MixedAmount, b) -> Maybe MixedAmount)
-> (Maybe MixedAmount, b)
-> MixedAmount
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe MixedAmount, b) -> Maybe MixedAmount
forall a b. (a, b) -> a
fst)
        rows :: [PeriodicReportRow DisplayName BudgetCell]
rows = [PeriodicReportRow DisplayName BudgetCell]
rows1 [PeriodicReportRow DisplayName BudgetCell]
-> [PeriodicReportRow DisplayName BudgetCell]
-> [PeriodicReportRow DisplayName BudgetCell]
forall a. [a] -> [a] -> [a]
++ [PeriodicReportRow DisplayName BudgetCell]
rows2

    -- TODO: grand total & average shows 0% when there are no actual amounts, inconsistent with other cells
    totalrow :: PeriodicReportRow () BudgetCell
totalrow = ()
-> [BudgetCell]
-> BudgetCell
-> BudgetCell
-> PeriodicReportRow () BudgetCell
forall a b. a -> [b] -> b -> b -> PeriodicReportRow a b
PeriodicReportRow ()
        [ (DateSpan -> Map DateSpan MixedAmount -> Maybe MixedAmount
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup DateSpan
p Map DateSpan MixedAmount
totActualByPeriod, DateSpan -> Map DateSpan MixedAmount -> Maybe MixedAmount
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup DateSpan
p Map DateSpan MixedAmount
totBudgetByPeriod) | DateSpan
p <- [DateSpan]
periods ]
        ( MixedAmount -> Maybe MixedAmount
forall a. a -> Maybe a
Just MixedAmount
actualgrandtot, MixedAmount -> Maybe MixedAmount
forall a. a -> Maybe a
Just MixedAmount
budgetgrandtot )
        ( MixedAmount -> Maybe MixedAmount
forall a. a -> Maybe a
Just MixedAmount
actualgrandavg, MixedAmount -> Maybe MixedAmount
forall a. a -> Maybe a
Just MixedAmount
budgetgrandavg )
      where
        totBudgetByPeriod :: Map DateSpan MixedAmount
totBudgetByPeriod = [(DateSpan, MixedAmount)] -> Map DateSpan MixedAmount
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(DateSpan, MixedAmount)] -> Map DateSpan MixedAmount)
-> [(DateSpan, MixedAmount)] -> Map DateSpan MixedAmount
forall a b. (a -> b) -> a -> b
$ [DateSpan] -> [MixedAmount] -> [(DateSpan, MixedAmount)]
forall a b. [a] -> [b] -> [(a, b)]
zip [DateSpan]
budgetperiods [MixedAmount]
budgettots :: Map DateSpan BudgetTotal
        totActualByPeriod :: Map DateSpan MixedAmount
totActualByPeriod = [(DateSpan, MixedAmount)] -> Map DateSpan MixedAmount
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(DateSpan, MixedAmount)] -> Map DateSpan MixedAmount)
-> [(DateSpan, MixedAmount)] -> Map DateSpan MixedAmount
forall a b. (a -> b) -> a -> b
$ [DateSpan] -> [MixedAmount] -> [(DateSpan, MixedAmount)]
forall a b. [a] -> [b] -> [(a, b)]
zip [DateSpan]
actualperiods [MixedAmount]
actualtots :: Map DateSpan Change

-- | Render a budget report as plain text suitable for console output.
budgetReportAsText :: ReportOpts -> BudgetReport -> String
budgetReportAsText :: ReportOpts -> BudgetReport -> String
budgetReportAsText ropts :: ReportOpts
ropts@ReportOpts{Bool
Int
[AccountName]
[Status]
Maybe Int
Maybe NormalSign
Maybe DateSpan
Maybe ValuationType
Interval
Period
StringFormat
AccountListMode
BalanceType
transpose_ :: ReportOpts -> Bool
forecast_ :: ReportOpts -> Maybe DateSpan
color_ :: ReportOpts -> Bool
normalbalance_ :: ReportOpts -> Maybe NormalSign
invert_ :: ReportOpts -> Bool
percent_ :: ReportOpts -> Bool
sort_amount_ :: ReportOpts -> Bool
pretty_tables_ :: ReportOpts -> Bool
no_total_ :: ReportOpts -> Bool
row_total_ :: ReportOpts -> Bool
drop_ :: ReportOpts -> Int
balancetype_ :: ReportOpts -> BalanceType
txn_dates_ :: ReportOpts -> Bool
related_ :: ReportOpts -> Bool
average_ :: ReportOpts -> Bool
querystring_ :: ReportOpts -> [AccountName]
format_ :: ReportOpts -> StringFormat
real_ :: ReportOpts -> Bool
no_elide_ :: ReportOpts -> Bool
date2_ :: ReportOpts -> Bool
depth_ :: ReportOpts -> Maybe Int
infer_value_ :: ReportOpts -> Bool
value_ :: ReportOpts -> Maybe ValuationType
statuses_ :: ReportOpts -> [Status]
period_ :: ReportOpts -> Period
transpose_ :: Bool
forecast_ :: Maybe DateSpan
color_ :: Bool
normalbalance_ :: Maybe NormalSign
invert_ :: Bool
percent_ :: Bool
sort_amount_ :: Bool
pretty_tables_ :: Bool
no_total_ :: Bool
row_total_ :: Bool
drop_ :: Int
accountlistmode_ :: AccountListMode
balancetype_ :: BalanceType
txn_dates_ :: Bool
related_ :: Bool
average_ :: Bool
querystring_ :: [AccountName]
format_ :: StringFormat
real_ :: Bool
no_elide_ :: Bool
empty_ :: Bool
date2_ :: Bool
depth_ :: Maybe Int
infer_value_ :: Bool
value_ :: Maybe ValuationType
statuses_ :: [Status]
interval_ :: Interval
period_ :: Period
interval_ :: ReportOpts -> Interval
empty_ :: ReportOpts -> Bool
accountlistmode_ :: ReportOpts -> AccountListMode
..} BudgetReport
budgetr =
    String
title String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++
    TableOpts
-> (String -> Cell)
-> (String -> Cell)
-> (((Int, Int, Int), BudgetDisplayCell) -> Cell)
-> Table String String ((Int, Int, Int), BudgetDisplayCell)
-> String
forall rh ch a.
TableOpts
-> (rh -> Cell)
-> (ch -> Cell)
-> (a -> Cell)
-> Table rh ch a
-> String
renderTable TableOpts
forall a. Default a => a
def{tableBorders :: Bool
tableBorders=Bool
False,prettyTable :: Bool
prettyTable=Bool
pretty_tables_}
        (Align -> String -> Cell
alignCell Align
TopLeft) (Align -> String -> Cell
alignCell Align
TopRight) (((Int, Int, Int) -> BudgetDisplayCell -> Cell)
-> ((Int, Int, Int), BudgetDisplayCell) -> Cell
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (Int, Int, Int) -> BudgetDisplayCell -> Cell
showcell) Table String String ((Int, Int, Int), BudgetDisplayCell)
displayTableWithWidths
  where
    multiperiod :: Bool
multiperiod = Interval
interval_ Interval -> Interval -> Bool
forall a. Eq a => a -> a -> Bool
/= Interval
NoInterval
    title :: String
title = String -> String -> String -> String
forall r. PrintfType r => String -> r
printf String
"Budget performance in %s%s:"
      (DateSpan -> String
showDateSpan (DateSpan -> String) -> DateSpan -> String
forall a b. (a -> b) -> a -> b
$ BudgetReport -> DateSpan
forall a b. PeriodicReport a b -> DateSpan
periodicReportSpan BudgetReport
budgetr)
      (case Maybe ValuationType
value_ of
        Just (AtCost Maybe AccountName
_mc)   -> String
", valued at cost"
        Just (AtThen Maybe AccountName
_mc)   -> String -> String
forall a. String -> a
error' String
unsupportedValueThenError  -- PARTIAL:
        Just (AtEnd Maybe AccountName
_mc)    -> String
", valued at period ends"
        Just (AtNow Maybe AccountName
_mc)    -> String
", current value"
        -- XXX duplicates the above
        Just (AtDefault Maybe AccountName
_mc) | Bool
multiperiod -> String
", valued at period ends"
        Just (AtDefault Maybe AccountName
_mc)  -> String
", current value"
        Just (AtDate Day
d Maybe AccountName
_mc) -> String
", valued at "String -> String -> String
forall a. [a] -> [a] -> [a]
++Day -> String
showDate Day
d
        Maybe ValuationType
Nothing             -> String
"")

    displayTableWithWidths :: Table String String ((Int, Int, Int), BudgetDisplayCell)
    displayTableWithWidths :: Table String String ((Int, Int, Int), BudgetDisplayCell)
displayTableWithWidths = Header String
-> Header String
-> [[((Int, Int, Int), BudgetDisplayCell)]]
-> Table String String ((Int, Int, Int), BudgetDisplayCell)
forall rh ch a. Header rh -> Header ch -> [[a]] -> Table rh ch a
Table Header String
rh Header String
ch ([[((Int, Int, Int), BudgetDisplayCell)]]
 -> Table String String ((Int, Int, Int), BudgetDisplayCell))
-> [[((Int, Int, Int), BudgetDisplayCell)]]
-> Table String String ((Int, Int, Int), BudgetDisplayCell)
forall a b. (a -> b) -> a -> b
$ ([BudgetDisplayCell] -> [((Int, Int, Int), BudgetDisplayCell)])
-> [[BudgetDisplayCell]]
-> [[((Int, Int, Int), BudgetDisplayCell)]]
forall a b. (a -> b) -> [a] -> [b]
map (((Int, Int, Int)
 -> BudgetDisplayCell -> ((Int, Int, Int), BudgetDisplayCell))
-> [(Int, Int, Int)]
-> [BudgetDisplayCell]
-> [((Int, Int, Int), BudgetDisplayCell)]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (,) [(Int, Int, Int)]
widths) [[BudgetDisplayCell]]
displaycells
    Table Header String
rh Header String
ch [[BudgetDisplayCell]]
displaycells = case ReportOpts -> BudgetReport -> Table String String BudgetCell
budgetReportAsTable ReportOpts
ropts BudgetReport
budgetr of
        Table Header String
rh' Header String
ch' [[BudgetCell]]
vals -> Table String String BudgetDisplayCell
-> Table String String BudgetDisplayCell
forall rh a. Table rh rh a -> Table rh rh a
maybetranspose (Table String String BudgetDisplayCell
 -> Table String String BudgetDisplayCell)
-> ([[BudgetDisplayCell]] -> Table String String BudgetDisplayCell)
-> [[BudgetDisplayCell]]
-> Table String String BudgetDisplayCell
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Header String
-> Header String
-> [[BudgetDisplayCell]]
-> Table String String BudgetDisplayCell
forall rh ch a. Header rh -> Header ch -> [[a]] -> Table rh ch a
Table Header String
rh' Header String
ch' ([[BudgetDisplayCell]] -> Table String String BudgetDisplayCell)
-> [[BudgetDisplayCell]] -> Table String String BudgetDisplayCell
forall a b. (a -> b) -> a -> b
$ ([BudgetCell] -> [BudgetDisplayCell])
-> [[BudgetCell]] -> [[BudgetDisplayCell]]
forall a b. (a -> b) -> [a] -> [b]
map ((BudgetCell -> BudgetDisplayCell)
-> [BudgetCell] -> [BudgetDisplayCell]
forall a b. (a -> b) -> [a] -> [b]
map BudgetCell -> BudgetDisplayCell
forall (f :: * -> *).
Functor f =>
(Maybe MixedAmount, f MixedAmount)
-> ((String, Int), f ((String, Int), Maybe (String, Int)))
displayCell) [[BudgetCell]]
vals

    displayCell :: (Maybe MixedAmount, f MixedAmount)
-> ((String, Int), f ((String, Int), Maybe (String, Int)))
displayCell (Maybe MixedAmount
actual, f MixedAmount
budget) = (MixedAmount -> (String, Int)
showamt MixedAmount
actual', MixedAmount -> ((String, Int), Maybe (String, Int))
budgetAndPerc (MixedAmount -> ((String, Int), Maybe (String, Int)))
-> f MixedAmount -> f ((String, Int), Maybe (String, Int))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f MixedAmount
budget)
      where
        actual' :: MixedAmount
actual' = MixedAmount -> Maybe MixedAmount -> MixedAmount
forall a. a -> Maybe a -> a
fromMaybe MixedAmount
0 Maybe MixedAmount
actual
        budgetAndPerc :: MixedAmount -> ((String, Int), Maybe (String, Int))
budgetAndPerc MixedAmount
b = (MixedAmount -> (String, Int)
showamt MixedAmount
b, DecimalRaw Integer -> (String, Int)
forall i. (Integral i, Show i) => DecimalRaw i -> (String, Int)
showper (DecimalRaw Integer -> (String, Int))
-> Maybe (DecimalRaw Integer) -> Maybe (String, Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MixedAmount -> MixedAmount -> Maybe (DecimalRaw Integer)
percentage MixedAmount
actual' MixedAmount
b)
        showamt :: MixedAmount -> (String, Int)
showamt = (Amount -> String)
-> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int)
showMixedOneLine Amount -> String
showAmountWithoutPrice Maybe Int
forall a. Maybe a
Nothing (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
32) Bool
color_
        showper :: DecimalRaw i -> (String, Int)
showper DecimalRaw i
p = let str :: String
str = DecimalRaw i -> String
forall a. Show a => a -> String
show (Word8 -> DecimalRaw i -> DecimalRaw i
forall i. Integral i => Word8 -> DecimalRaw i -> DecimalRaw i
roundTo Word8
0 DecimalRaw i
p) in (String
str, String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
str)
    cellWidth :: ((a, a), Maybe ((a, b), Maybe (a, c))) -> (a, b, c)
cellWidth ((a
_,a
wa), Maybe ((a, b), Maybe (a, c))
Nothing)                    = (a
wa,  b
0,  c
0)
    cellWidth ((a
_,a
wa), Just ((a
_,b
wb), Maybe (a, c)
Nothing))     = (a
wa, b
wb,  c
0)
    cellWidth ((a
_,a
wa), Just ((a
_,b
wb), Just (a
_,c
wp))) = (a
wa, b
wb, c
wp)

    widths :: [(Int, Int, Int)]
widths = [Int] -> [Int] -> [Int] -> [(Int, Int, Int)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [Int]
actualwidths [Int]
budgetwidths [Int]
percentwidths
    actualwidths :: [Int]
actualwidths  = ([BudgetDisplayCell] -> Int) -> [[BudgetDisplayCell]] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ([Int] -> Int
forall a. Integral a => [a] -> a
maximum' ([Int] -> Int)
-> ([BudgetDisplayCell] -> [Int]) -> [BudgetDisplayCell] -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (BudgetDisplayCell -> Int) -> [BudgetDisplayCell] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ((Int, Int, Int) -> Int
forall a b c. (a, b, c) -> a
first3  ((Int, Int, Int) -> Int)
-> (BudgetDisplayCell -> (Int, Int, Int))
-> BudgetDisplayCell
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BudgetDisplayCell -> (Int, Int, Int)
forall b c a a a a.
(Num b, Num c) =>
((a, a), Maybe ((a, b), Maybe (a, c))) -> (a, b, c)
cellWidth)) [[BudgetDisplayCell]]
cols
    budgetwidths :: [Int]
budgetwidths  = ([BudgetDisplayCell] -> Int) -> [[BudgetDisplayCell]] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ([Int] -> Int
forall a. Integral a => [a] -> a
maximum' ([Int] -> Int)
-> ([BudgetDisplayCell] -> [Int]) -> [BudgetDisplayCell] -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (BudgetDisplayCell -> Int) -> [BudgetDisplayCell] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ((Int, Int, Int) -> Int
forall a b c. (a, b, c) -> b
second3 ((Int, Int, Int) -> Int)
-> (BudgetDisplayCell -> (Int, Int, Int))
-> BudgetDisplayCell
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BudgetDisplayCell -> (Int, Int, Int)
forall b c a a a a.
(Num b, Num c) =>
((a, a), Maybe ((a, b), Maybe (a, c))) -> (a, b, c)
cellWidth)) [[BudgetDisplayCell]]
cols
    percentwidths :: [Int]
percentwidths = ([BudgetDisplayCell] -> Int) -> [[BudgetDisplayCell]] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ([Int] -> Int
forall a. Integral a => [a] -> a
maximum' ([Int] -> Int)
-> ([BudgetDisplayCell] -> [Int]) -> [BudgetDisplayCell] -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (BudgetDisplayCell -> Int) -> [BudgetDisplayCell] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ((Int, Int, Int) -> Int
forall a b c. (a, b, c) -> c
third3  ((Int, Int, Int) -> Int)
-> (BudgetDisplayCell -> (Int, Int, Int))
-> BudgetDisplayCell
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BudgetDisplayCell -> (Int, Int, Int)
forall b c a a a a.
(Num b, Num c) =>
((a, a), Maybe ((a, b), Maybe (a, c))) -> (a, b, c)
cellWidth)) [[BudgetDisplayCell]]
cols
    cols :: [[BudgetDisplayCell]]
cols = [[BudgetDisplayCell]] -> [[BudgetDisplayCell]]
forall a. [[a]] -> [[a]]
transpose [[BudgetDisplayCell]]
displaycells

    -- XXX lay out actual, percentage and/or goal in the single table cell for now, should probably use separate cells
    showcell :: (Int, Int, Int) -> BudgetDisplayCell -> Cell
    showcell :: (Int, Int, Int) -> BudgetDisplayCell -> Cell
showcell (Int
actualwidth, Int
budgetwidth, Int
percentwidth) ((String
actual,Int
wa), Maybe ((String, Int), Maybe (String, Int))
mbudget) =
        Align -> [(String, Int)] -> Cell
Cell Align
TopRight [(Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
actualwidth Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
wa) Char
' ' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
actual String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
budgetstr, Int
actualwidth Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
totalbudgetwidth)]
      where
        totalpercentwidth :: Int
totalpercentwidth = if Int
percentwidth Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 then Int
0 else Int
percentwidth Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
5
        totalbudgetwidth :: Int
totalbudgetwidth  = if Int
budgetwidth Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 then Int
0 else Int
budgetwidth Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
totalpercentwidth Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3
        budgetstr :: String
budgetstr = case Maybe ((String, Int), Maybe (String, Int))
mbudget of
          Maybe ((String, Int), Maybe (String, Int))
Nothing                             -> Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
totalbudgetwidth Char
' '
          Just ((String
budget, Int
wb), Maybe (String, Int)
Nothing)        -> String
" [" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
totalpercentwidth Char
' ' String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
budgetwidth Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
wb) Char
' ' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
budget String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"]"
          Just ((String
budget, Int
wb), Just (String
pct, Int
wp)) -> String
" [" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
percentwidth Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
wp) Char
' ' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
pct String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"% of " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
budgetwidth Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
wb) Char
' ' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
budget String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"]"

    -- | Calculate the percentage of actual change to budget goal to show, if any.
    -- If valuing at cost, both amounts are converted to cost before comparing.
    -- 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 :: MixedAmount -> MixedAmount -> Maybe (DecimalRaw Integer)
percentage MixedAmount
actual MixedAmount
budget =
      case (MixedAmount -> MixedAmount
maybecost (MixedAmount -> MixedAmount) -> MixedAmount -> MixedAmount
forall a b. (a -> b) -> a -> b
$ MixedAmount -> MixedAmount
normaliseMixedAmount MixedAmount
actual, MixedAmount -> MixedAmount
maybecost (MixedAmount -> MixedAmount) -> MixedAmount -> MixedAmount
forall a b. (a -> b) -> a -> b
$ MixedAmount -> MixedAmount
normaliseMixedAmount MixedAmount
budget) of
        (Mixed [Amount
a], Mixed [Amount
b]) | (Amount -> AccountName
acommodity Amount
a AccountName -> AccountName -> Bool
forall a. Eq a => a -> a -> Bool
== Amount -> AccountName
acommodity Amount
b Bool -> Bool -> Bool
|| Amount -> Bool
amountLooksZero Amount
a) Bool -> Bool -> Bool
&& Bool -> Bool
not (Amount -> Bool
amountLooksZero Amount
b)
            -> DecimalRaw Integer -> Maybe (DecimalRaw Integer)
forall a. a -> Maybe a
Just (DecimalRaw Integer -> Maybe (DecimalRaw Integer))
-> DecimalRaw Integer -> Maybe (DecimalRaw Integer)
forall a b. (a -> b) -> a -> b
$ DecimalRaw Integer
100 DecimalRaw Integer -> DecimalRaw Integer -> DecimalRaw Integer
forall a. Num a => a -> a -> a
* Amount -> DecimalRaw Integer
aquantity Amount
a DecimalRaw Integer -> DecimalRaw Integer -> DecimalRaw Integer
forall a. Fractional a => a -> a -> a
/ Amount -> DecimalRaw Integer
aquantity Amount
b
        (MixedAmount, MixedAmount)
_   -> -- trace (pshow $ (maybecost actual, maybecost budget))  -- debug missing percentage
               Maybe (DecimalRaw Integer)
forall a. Maybe a
Nothing
      where
        maybecost :: MixedAmount -> MixedAmount
maybecost = if ReportOpts -> Bool
valuationTypeIsCost ReportOpts
ropts then MixedAmount -> MixedAmount
mixedAmountCost else MixedAmount -> MixedAmount
forall a. a -> a
id

    maybetranspose :: Table rh rh a -> Table rh rh a
maybetranspose | Bool
transpose_ = \(Table Header rh
rh Header rh
ch [[a]]
vals) -> Header rh -> Header rh -> [[a]] -> Table rh rh a
forall rh ch a. Header rh -> Header ch -> [[a]] -> Table rh ch a
Table Header rh
ch Header rh
rh ([[a]] -> [[a]]
forall a. [[a]] -> [[a]]
transpose [[a]]
vals)
                   | Bool
otherwise  = Table rh rh a -> Table rh rh a
forall a. a -> a
id

-- | Build a 'Table' from a multi-column balance report.
budgetReportAsTable :: ReportOpts -> BudgetReport -> Table String String (Maybe MixedAmount, Maybe MixedAmount)
budgetReportAsTable :: ReportOpts -> BudgetReport -> Table String String BudgetCell
budgetReportAsTable
  ropts :: ReportOpts
ropts@ReportOpts{BalanceType
balancetype_ :: BalanceType
balancetype_ :: ReportOpts -> BalanceType
balancetype_}
  (PeriodicReport [DateSpan]
spans [PeriodicReportRow DisplayName BudgetCell]
rows (PeriodicReportRow ()
_ [BudgetCell]
coltots BudgetCell
grandtot BudgetCell
grandavg)) =
    Table String String BudgetCell -> Table String String BudgetCell
forall ch. Table String ch BudgetCell -> Table String ch BudgetCell
addtotalrow (Table String String BudgetCell -> Table String String BudgetCell)
-> Table String String BudgetCell -> Table String String BudgetCell
forall a b. (a -> b) -> a -> b
$
    Header String
-> Header String
-> [[BudgetCell]]
-> Table String String BudgetCell
forall rh ch a. Header rh -> Header ch -> [[a]] -> Table rh ch a
Table
      (Properties -> [Header String] -> Header String
forall h. Properties -> [Header h] -> Header h
T.Group Properties
NoLine ([Header String] -> Header String)
-> [Header String] -> Header String
forall a b. (a -> b) -> a -> b
$ (String -> Header String) -> [String] -> [Header String]
forall a b. (a -> b) -> [a] -> [b]
map String -> Header String
forall h. h -> Header h
Header [String]
accts)
      (Properties -> [Header String] -> Header String
forall h. Properties -> [Header h] -> Header h
T.Group Properties
NoLine ([Header String] -> Header String)
-> [Header String] -> Header String
forall a b. (a -> b) -> a -> b
$ (String -> Header String) -> [String] -> [Header String]
forall a b. (a -> b) -> [a] -> [b]
map String -> Header String
forall h. h -> Header h
Header [String]
colheadings)
      ((PeriodicReportRow DisplayName BudgetCell -> [BudgetCell])
-> [PeriodicReportRow DisplayName BudgetCell] -> [[BudgetCell]]
forall a b. (a -> b) -> [a] -> [b]
map PeriodicReportRow DisplayName BudgetCell -> [BudgetCell]
forall a a. PeriodicReportRow a a -> [a]
rowvals [PeriodicReportRow DisplayName BudgetCell]
rows)
  where
    colheadings :: [String]
colheadings = (DateSpan -> String) -> [DateSpan] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (BalanceType -> [DateSpan] -> DateSpan -> String
reportPeriodName BalanceType
balancetype_ [DateSpan]
spans) [DateSpan]
spans
                  [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ [String
"  Total" | ReportOpts -> Bool
row_total_ ReportOpts
ropts]
                  [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ [String
"Average" | ReportOpts -> Bool
average_ ReportOpts
ropts]

    accts :: [String]
accts = (PeriodicReportRow DisplayName BudgetCell -> String)
-> [PeriodicReportRow DisplayName BudgetCell] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map PeriodicReportRow DisplayName BudgetCell -> String
forall a. PeriodicReportRow DisplayName a -> String
renderacct [PeriodicReportRow DisplayName BudgetCell]
rows
    -- FIXME. Have to check explicitly for which to render here, since
    -- budgetReport sets accountlistmode to ALTree. Find a principled way to do
    -- this.
    renderacct :: PeriodicReportRow DisplayName a -> String
renderacct PeriodicReportRow DisplayName a
row = case ReportOpts -> AccountListMode
accountlistmode_ ReportOpts
ropts of
        AccountListMode
ALTree -> Int -> Char -> String
forall a. Int -> a -> [a]
replicate ((PeriodicReportRow DisplayName a -> Int
forall a. PeriodicReportRow DisplayName a -> Int
prrDepth PeriodicReportRow DisplayName a
row Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
2) Char
' ' String -> String -> String
forall a. [a] -> [a] -> [a]
++ AccountName -> String
T.unpack (PeriodicReportRow DisplayName a -> AccountName
forall a. PeriodicReportRow DisplayName a -> AccountName
prrDisplayName PeriodicReportRow DisplayName a
row)
        AccountListMode
ALFlat -> AccountName -> String
T.unpack (AccountName -> String)
-> (AccountName -> AccountName) -> AccountName -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> AccountName -> AccountName
accountNameDrop (ReportOpts -> Int
drop_ ReportOpts
ropts) (AccountName -> String) -> AccountName -> String
forall a b. (a -> b) -> a -> b
$ PeriodicReportRow DisplayName a -> AccountName
forall a. PeriodicReportRow DisplayName a -> AccountName
prrFullName PeriodicReportRow DisplayName a
row
    rowvals :: PeriodicReportRow a a -> [a]
rowvals (PeriodicReportRow a
_ [a]
as a
rowtot a
rowavg) =
        [a]
as [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a
rowtot | ReportOpts -> Bool
row_total_ ReportOpts
ropts] [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a
rowavg | ReportOpts -> Bool
average_ ReportOpts
ropts]
    addtotalrow :: Table String ch BudgetCell -> Table String ch BudgetCell
addtotalrow
      | ReportOpts -> Bool
no_total_ ReportOpts
ropts = Table String ch BudgetCell -> Table String ch BudgetCell
forall a. a -> a
id
      | Bool
otherwise = (Table String ch BudgetCell
-> SemiTable String BudgetCell -> Table String ch BudgetCell
forall rh ch a. Table rh ch a -> SemiTable rh a -> Table rh ch a
+----+ (String -> [BudgetCell] -> SemiTable String BudgetCell
forall rh a. rh -> [a] -> SemiTable rh a
row String
"" ([BudgetCell] -> SemiTable String BudgetCell)
-> [BudgetCell] -> SemiTable String BudgetCell
forall a b. (a -> b) -> a -> b
$
                       [BudgetCell]
coltots [BudgetCell] -> [BudgetCell] -> [BudgetCell]
forall a. [a] -> [a] -> [a]
++ [BudgetCell
grandtot | ReportOpts -> Bool
row_total_ ReportOpts
ropts Bool -> Bool -> Bool
&& Bool -> Bool
not ([BudgetCell] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [BudgetCell]
coltots)]
                               [BudgetCell] -> [BudgetCell] -> [BudgetCell]
forall a. [a] -> [a] -> [a]
++ [BudgetCell
grandavg | ReportOpts -> Bool
average_ ReportOpts
ropts Bool -> Bool -> Bool
&& Bool -> Bool
not ([BudgetCell] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [BudgetCell]
coltots)]
                    ))

-- | 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 -> [DateSpan] -> DateSpan -> String
reportPeriodName BalanceType
balancetype [DateSpan]
spans =
  case BalanceType
balancetype of
    BalanceType
PeriodChange -> if Bool
multiyear then DateSpan -> String
showDateSpan else DateSpan -> String
showDateSpanMonthAbbrev
      where
        multiyear :: Bool
multiyear = (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>Int
1) (Int -> Bool) -> Int -> Bool
forall a b. (a -> b) -> a -> b
$ [Maybe Integer] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Maybe Integer] -> Int) -> [Maybe Integer] -> Int
forall a b. (a -> b) -> a -> b
$ [Maybe Integer] -> [Maybe Integer]
forall a. Ord a => [a] -> [a]
nubSort ([Maybe Integer] -> [Maybe Integer])
-> [Maybe Integer] -> [Maybe Integer]
forall a b. (a -> b) -> a -> b
$ (DateSpan -> Maybe Integer) -> [DateSpan] -> [Maybe Integer]
forall a b. (a -> b) -> [a] -> [b]
map DateSpan -> Maybe Integer
spanStartYear [DateSpan]
spans
    BalanceType
_ -> String -> (Day -> String) -> Maybe Day -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" (Day -> String
showDate (Day -> String) -> (Day -> Day) -> Day -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Day -> Day
prevday) (Maybe Day -> String)
-> (DateSpan -> Maybe Day) -> DateSpan -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DateSpan -> Maybe Day
spanEnd

-- 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 -> BudgetReport -> CSV
budgetReportAsCsv 
  ReportOpts{Bool
average_ :: Bool
average_ :: ReportOpts -> Bool
average_, Bool
row_total_ :: Bool
row_total_ :: ReportOpts -> Bool
row_total_, Bool
no_total_ :: Bool
no_total_ :: ReportOpts -> Bool
no_total_, Bool
transpose_ :: Bool
transpose_ :: ReportOpts -> Bool
transpose_}
  (PeriodicReport [DateSpan]
colspans [PeriodicReportRow DisplayName BudgetCell]
items (PeriodicReportRow ()
_ [BudgetCell]
abtotals (Maybe MixedAmount
magrandtot,Maybe MixedAmount
mbgrandtot) (Maybe MixedAmount
magrandavg,Maybe MixedAmount
mbgrandavg)))
  = (if Bool
transpose_ then CSV -> CSV
forall a. [[a]] -> [[a]]
transpose else CSV -> CSV
forall a. a -> a
id) (CSV -> CSV) -> CSV -> CSV
forall a b. (a -> b) -> a -> b
$

  -- heading row
  (String
"Account" String -> [String] -> [String]
forall a. a -> [a] -> [a]
: 
   (DateSpan -> [String]) -> [DateSpan] -> [String]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\DateSpan
span -> [DateSpan -> String
showDateSpan DateSpan
span, String
"budget"]) [DateSpan]
colspans
   [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ CSV -> [String]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[String
"Total"  ,String
"budget"] | Bool
row_total_]
   [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ CSV -> [String]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[String
"Average",String
"budget"] | Bool
average_]
  ) [String] -> CSV -> CSV
forall a. a -> [a] -> [a]
:

  -- account rows
  [AccountName -> String
T.unpack (DisplayName -> AccountName
displayFull DisplayName
a) String -> [String] -> [String]
forall a. a -> [a] -> [a]
:
   (Maybe MixedAmount -> String) -> [Maybe MixedAmount] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Maybe MixedAmount -> String
showmamt ([BudgetCell] -> [Maybe MixedAmount]
forall a. [(a, a)] -> [a]
flattentuples [BudgetCell]
abamts)
   [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ CSV -> [String]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Maybe MixedAmount -> String
showmamt Maybe MixedAmount
mactualrowtot, Maybe MixedAmount -> String
showmamt Maybe MixedAmount
mbudgetrowtot] | Bool
row_total_]
   [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ CSV -> [String]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Maybe MixedAmount -> String
showmamt Maybe MixedAmount
mactualrowavg, Maybe MixedAmount -> String
showmamt Maybe MixedAmount
mbudgetrowavg] | Bool
average_]
  | PeriodicReportRow DisplayName
a [BudgetCell]
abamts (Maybe MixedAmount
mactualrowtot,Maybe MixedAmount
mbudgetrowtot) (Maybe MixedAmount
mactualrowavg,Maybe MixedAmount
mbudgetrowavg) <- [PeriodicReportRow DisplayName BudgetCell]
items
  ]

  -- totals row
  CSV -> CSV -> CSV
forall a. [a] -> [a] -> [a]
++ [CSV] -> CSV
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [
    [
    String
"Total:" String -> [String] -> [String]
forall a. a -> [a] -> [a]
:
    (Maybe MixedAmount -> String) -> [Maybe MixedAmount] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Maybe MixedAmount -> String
showmamt ([BudgetCell] -> [Maybe MixedAmount]
forall a. [(a, a)] -> [a]
flattentuples [BudgetCell]
abtotals)
    [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ CSV -> [String]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Maybe MixedAmount -> String
showmamt Maybe MixedAmount
magrandtot,Maybe MixedAmount -> String
showmamt Maybe MixedAmount
mbgrandtot] | Bool
row_total_] 
    [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ CSV -> [String]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Maybe MixedAmount -> String
showmamt Maybe MixedAmount
magrandavg,Maybe MixedAmount -> String
showmamt Maybe MixedAmount
mbgrandavg] | Bool
average_]
    ]
  | Bool -> Bool
not Bool
no_total_
  ]

  where
    flattentuples :: [(a, a)] -> [a]
flattentuples [(a, a)]
abs = [[a]] -> [a]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[a
a,a
b] | (a
a,a
b) <- [(a, a)]
abs]
    showmamt :: Maybe MixedAmount -> String
showmamt = String -> (MixedAmount -> String) -> Maybe MixedAmount -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" (Bool -> MixedAmount -> String
showMixedAmountOneLineWithoutPrice Bool
False)

-- tests

tests_BudgetReport :: TestTree
tests_BudgetReport = String -> [TestTree] -> TestTree
tests String
"BudgetReport" [
 ]