{-|

A ledger-compatible @balance@ command, with additional support for
multi-column reports.

Here is a description/specification for the balance command.  See also
"Hledger.Reports" -> \"Balance reports\".


/Basic balance report/

With no report interval (@--monthly@ etc.), hledger's balance
command emulates ledger's, showing accounts indented according to
hierarchy, along with their total amount posted (including subaccounts).

Here's an example. With @examples/sample.journal@, which defines the following account tree:

@
 assets
   bank
     checking
     saving
   cash
 expenses
   food
   supplies
 income
   gifts
   salary
 liabilities
   debts
@

the basic @balance@ command gives this output:

@
 $ hledger -f sample.journal balance
                 $-1  assets
                  $1    bank:saving
                 $-2    cash
                  $2  expenses
                  $1    food
                  $1    supplies
                 $-2  income
                 $-1    gifts
                 $-1    salary
                  $1  liabilities:debts
--------------------
                   0
@

Subaccounts are displayed indented below their parent. Only the account leaf name (the final part) is shown.
(With @--flat@, account names are shown in full and unindented.)

Each account's \"balance\" is the sum of postings in that account and any subaccounts during the report period.
When the report period includes all transactions, this is equivalent to the account's current balance.

The overall total of the highest-level displayed accounts is shown below the line.
(The @--no-total/-N@ flag prevents this.)

/Eliding and omitting/

Accounts which have a zero balance, and no non-zero subaccount
balances, are normally omitted from the report.
(The @--empty/-E@ flag forces such accounts to be displayed.)
Eg, above @checking@ is omitted because it has a zero balance and no subaccounts.

Accounts which have a single subaccount also being displayed, with the same balance,
are normally elided into the subaccount's line.
(The @--no-elide@ flag prevents this.)
Eg, above @bank@ is elided to @bank:saving@ because it has only a
single displayed subaccount (@saving@) and their balance is the same
($1). Similarly, @liabilities@ is elided to @liabilities:debts@.

/Date limiting/

The default report period is that of the whole journal, including all
known transactions. The @--begin\/-b@, @--end\/-e@, @--period\/-p@
options or @date:@/@date2:@ patterns can be used to report only
on transactions before and/or after specified dates.

/Depth limiting/

The @--depth@ option can be used to limit the depth of the balance report.
Eg, to see just the top level accounts (still including their subaccount balances):

@
$ hledger -f sample.journal balance --depth 1
                 $-1  assets
                  $2  expenses
                 $-2  income
                  $1  liabilities
--------------------
                   0
@

/Account limiting/

With one or more account pattern arguments, the report is restricted
to accounts whose name matches one of the patterns, plus their parents
and subaccounts. Eg, adding the pattern @o@ to the first example gives:

@
 $ hledger -f sample.journal balance o
                  $1  expenses:food
                 $-2  income
                 $-1    gifts
                 $-1    salary
--------------------
                 $-1
@

* The @o@ pattern matched @food@ and @income@, so they are shown.

* @food@'s parent (@expenses@) is shown even though the pattern didn't
  match it, to clarify the hierarchy. The usual eliding rules cause it to be elided here.

* @income@'s subaccounts are also shown.

/Multi-column balance report/

hledger's balance command will show multiple columns when a reporting
interval is specified (eg with @--monthly@), one column for each sub-period.

There are three accumulation strategies for multi-column balance report, indicated by
the heading:

* A \"period balance\" (or \"flow\") report (with @--change@, the default) shows the
  change of account balance in each period, which is equivalent to the sum of postings
  in each period. Here, checking's balance increased by 10 in Feb:

  > Change of balance (flow):
  >
  >                  Jan   Feb   Mar
  > assets:checking   20    10    -5

* A \"cumulative balance\" report (with @--cumulative@) shows the accumulated ending balance
  across periods, starting from zero at the report's start date.
  Here, 30 is the sum of checking postings during Jan and Feb:

  > Ending balance (cumulative):
  >
  >                  Jan   Feb   Mar
  > assets:checking   20    30    25

* A \"historical balance\" report (with @--historical/-H@) also shows ending balances,
  but it includes the starting balance from any postings before the report start date.
  Here, 130 is the balance from all checking postings at the end of Feb, including
  pre-Jan postings which created a starting balance of 100:

  > Ending balance (historical):
  >
  >                  Jan   Feb   Mar
  > assets:checking  120   130   125

/Eliding and omitting, 2/

Here's a (imperfect?) specification for the eliding/omitting behaviour:

* Each account is normally displayed on its own line.

* An account less deep than the report's max depth, with just one
interesting subaccount, and the same balance as the subaccount, is
non-interesting, and prefixed to the subaccount's line, unless
@--no-elide@ is in effect.

* An account with a zero inclusive balance and less than two interesting
subaccounts is not displayed at all, unless @--empty@ is in effect.

* Multi-column balance reports show full account names with no eliding
  (like @--flat@). Accounts (and periods) are omitted as described below.

/Which accounts to show in balance reports/

By default:

* single-column: accounts with non-zero balance in report period.
                 (With @--flat@: accounts with non-zero balance and postings.)

* change:        accounts with postings and non-zero period balance in any period

* cumulative:    accounts with non-zero cumulative balance in any period

* historical:    accounts with non-zero historical balance in any period

With @-E/--empty@:

* single-column: accounts with postings in report period

* change:        accounts with postings in report period

* cumulative:    accounts with postings in report period

* historical:    accounts with non-zero starting balance +
                 accounts with postings in report period

/Which periods (columns) to show in balance reports/

An empty period/column is one where no report account has any postings.
A zero period/column is one where no report account has a non-zero period balance.

Currently,

by default:

* single-column: N/A

* change:        all periods within the overall report period,
                 except for leading and trailing empty periods

* cumulative:    all periods within the overall report period,
                 except for leading and trailing empty periods

* historical:    all periods within the overall report period,
                 except for leading and trailing empty periods

With @-E/--empty@:

* single-column: N/A

* change:        all periods within the overall report period

* cumulative:    all periods within the overall report period

* historical:    all periods within the overall report period

/What to show in empty cells/

An empty periodic balance report cell is one which has no corresponding postings.
An empty cumulative/historical balance report cell is one which has no corresponding
or prior postings, ie the account doesn't exist yet.
Currently, empty cells show 0.

-}

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

module Hledger.Cli.Commands.Balance (
  -- ** balance command
  balancemode
 ,balance
  -- ** balance output rendering
 ,balanceReportAsText
 ,balanceReportAsCsv
 ,balanceReportAsHtml
 ,balanceReportAsSpreadsheet
 ,balanceReportAsSpreadsheetParts
 ,balanceReportItemAsText
 ,budgetReportAsText
 ,budgetReportAsCsv
 ,budgetReportAsHtml
 ,budgetReportAsSpreadsheet
 ,multiBalanceRowAsCsvText
 ,multiBalanceRowAsText
 ,multiBalanceReportAsText
 ,multiBalanceReportAsCsv
 ,multiBalanceReportAsHtml
 ,multiBalanceReportAsTable
 ,multiBalanceReportAsPartTable
 ,multiBalanceReportTableAsText
 ,multiBalanceReportAsSpreadsheet
 ,multiBalanceReportAsSpreadsheetParts
 ,multiBalanceReportTitle
 ,multiBalanceReportNumHeaderColumns
 ,tidyColumnLabels
  -- ** Tests
 ,tests_Balance
) where

import Control.Arrow (second, (***))
import Control.Monad (guard)
import Data.Decimal (roundTo)
import Data.Default (def)
import Data.Function (on)
import Data.Foldable qualified as Fold
import Data.List.NonEmpty qualified as NonEmpty
import Data.List.NonEmpty (NonEmpty((:|)))
import Data.List (find, transpose)
#if !MIN_VERSION_base(4,20,0)
import Data.List (foldl')
#endif
import Data.Map qualified as Map
import Data.Set qualified as S
import Data.Maybe (mapMaybe, fromMaybe, maybeToList)
import Data.Tuple (swap)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.Lazy qualified as TL
import Data.Text.Lazy.Builder qualified as TB
import Data.Time (fromGregorian)
import System.Console.CmdArgs.Explicit as C (flagNone, flagReq, flagOpt)
import Safe (headMay, maximumMay)
import Text.Tabular.AsciiWide
    (Header(..), Align(..), Properties(..), Cell(..), Table(..), TableOpts(..),
    cellWidth, concatTables, renderColumns, renderRowB, renderTable, renderTableByRowsB, textCell)

import System.IO qualified as IO

import Hledger.Utils.I18n qualified as I18n
import Hledger
import Hledger.Cli.CliOptions
import Hledger.Cli.Utils
import Hledger.Cli.Anchor (linkParams, composeAnchorWith, dateTerm, removeInacct,
  withLink, setAccountAnchor, setAccountAnchorWith, renderPeriodHeading, amountPhrase)
import Hledger.Cli.Commands.Balance.Internal
import Hledger.Cli.Chart (ChartArg(..), chartArg, balanceChartFlag, chartWidthFlag, chartRequested, writeChart, multiBalanceReportAsChart, balanceReportAsChart)
import Hledger.Write.Csv (CSV, printCSV, printTSV)
import Hledger.Write.Ods (printFods)
import Hledger.Write.Html (Html, titledTableHtml, htmlAsLazyText, toHtml)
import Hledger.Write.Spreadsheet (rawTableContent, headerCell,
            addHeaderBorders, addRowSpanHeader, addRowSpanHeaderNE,
            cellFromMixedAmount, cellsFromMixedAmount, cellFromAmount)
import Hledger.Write.Spreadsheet qualified as Ods


-- | Command line options for this command.
balancemode :: Mode RawOpts
balancemode = String
-> [Flag RawOpts]
-> [(String, [Flag RawOpts])]
-> [Flag RawOpts]
-> ([Arg RawOpts], Maybe (Arg RawOpts))
-> Mode RawOpts
hledgerCommandMode
  $(embedFileRelative "Hledger/Cli/Commands/Balance.txt")
  (
    -- https://hledger.org/dev/hledger.html#calculation-mode :
    [[String] -> (RawOpts -> RawOpts) -> String -> Flag RawOpts
forall a. [String] -> (a -> a) -> String -> Flag a
flagNone [String
"sum"] (String -> RawOpts -> RawOpts
setboolopt String
"sum")
      (String
calcprefix String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"show sum of posting amounts (default)")
    ,[String] -> (RawOpts -> RawOpts) -> String -> Flag RawOpts
forall a. [String] -> (a -> a) -> String -> Flag a
flagNone [String
"valuechange"] (String -> RawOpts -> RawOpts
setboolopt String
"valuechange")
      (String
calcprefix String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"show total change of value of period-end historical balances (caused by deposits, withdrawals, market price fluctuations)")
    ,[String] -> (RawOpts -> RawOpts) -> String -> Flag RawOpts
forall a. [String] -> (a -> a) -> String -> Flag a
flagNone [String
"gain"] (String -> RawOpts -> RawOpts
setboolopt String
"gain")
      (String
calcprefix String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"show capital gain/loss (historical balance value minus cost basis, or transacted cost where there is none)")
    -- XXX --budget[=DESCPAT], --forecast[=PERIODEXP], could be more consistent
    ,String
-> [String] -> Update RawOpts -> String -> String -> Flag RawOpts
forall a.
String -> [String] -> Update a -> String -> String -> Flag a
flagOpt String
"" [String
"budget"] (\String
s RawOpts
opts -> RawOpts -> Either String RawOpts
forall a b. b -> Either a b
Right (RawOpts -> Either String RawOpts)
-> RawOpts -> Either String RawOpts
forall a b. (a -> b) -> a -> b
$ String -> String -> RawOpts -> RawOpts
setopt String
"budget" String
s RawOpts
opts) String
"DESCPAT"
      ([String] -> String
unlines
      [ String
calcprefix String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"show sum of posting amounts together with budget goals defined by periodic"
      , String
"transactions. With a DESCPAT argument (must be separated by = not space),"
      , String
"use only periodic transactions with matching description"
      , String
"(case insensitive substring match)."
      ])
    ,[String] -> (RawOpts -> RawOpts) -> String -> Flag RawOpts
forall a. [String] -> (a -> a) -> String -> Flag a
flagNone [String
"count"] (String -> RawOpts -> RawOpts
setboolopt String
"count")
      (String
calcprefix String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"show the count of postings")

    -- https://hledger.org/dev/hledger.html#accumulation-mode :
    ,[String] -> (RawOpts -> RawOpts) -> String -> Flag RawOpts
forall a. [String] -> (a -> a) -> String -> Flag a
flagNone [String
"change"] (String -> RawOpts -> RawOpts
setboolopt String
"change")
      (String
accumprefix String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"accumulate amounts from column start to column end (in multicolumn reports, default)")
    ,[String] -> (RawOpts -> RawOpts) -> String -> Flag RawOpts
forall a. [String] -> (a -> a) -> String -> Flag a
flagNone [String
"cumulative"] (String -> RawOpts -> RawOpts
setboolopt String
"cumulative")
      (String
accumprefix String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"accumulate amounts from report start (specified by e.g. -b/--begin) to column end")
    ,[String] -> (RawOpts -> RawOpts) -> String -> Flag RawOpts
forall a. [String] -> (a -> a) -> String -> Flag a
flagNone [String
"historical",String
"H"] (String -> RawOpts -> RawOpts
setboolopt String
"historical")
      (String
accumprefix String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"accumulate amounts from journal start to column end (includes postings before report start date)")
    ]

    -- other options specific to this command:
    [Flag RawOpts] -> [Flag RawOpts] -> [Flag RawOpts]
forall a. [a] -> [a] -> [a]
++ Bool -> [Flag RawOpts]
flattreeflags Bool
True [Flag RawOpts] -> [Flag RawOpts] -> [Flag RawOpts]
forall a. [a] -> [a] -> [a]
++
    [[String] -> Update RawOpts -> String -> String -> Flag RawOpts
forall a. [String] -> Update a -> String -> String -> Flag a
flagReq  [String
"drop"] (\String
s RawOpts
opts -> RawOpts -> Either String RawOpts
forall a b. b -> Either a b
Right (RawOpts -> Either String RawOpts)
-> RawOpts -> Either String RawOpts
forall a b. (a -> b) -> a -> b
$ String -> String -> RawOpts -> RawOpts
setopt String
"drop" String
s RawOpts
opts) String
"N" String
"in list mode, omit N leading account name parts"
    ,[String] -> (RawOpts -> RawOpts) -> String -> Flag RawOpts
forall a. [String] -> (a -> a) -> String -> Flag a
flagNone [String
"declared"] (String -> RawOpts -> RawOpts
setboolopt String
"declared") String
"include non-parent declared accounts (best used with -E)"
    ,[String] -> (RawOpts -> RawOpts) -> String -> Flag RawOpts
forall a. [String] -> (a -> a) -> String -> Flag a
flagNone [String
"average",String
"A"] (String -> RawOpts -> RawOpts
setboolopt String
"average") String
"show a row average column (in multicolumn reports)"
    ,[String] -> (RawOpts -> RawOpts) -> String -> Flag RawOpts
forall a. [String] -> (a -> a) -> String -> Flag a
flagNone [String
"row-total",String
"T"] (String -> RawOpts -> RawOpts
setboolopt String
"row-total") String
"show a row total column (in multicolumn reports)"
    ,[String] -> (RawOpts -> RawOpts) -> String -> Flag RawOpts
forall a. [String] -> (a -> a) -> String -> Flag a
flagNone [String
"summary-only"] (String -> RawOpts -> RawOpts
setboolopt String
"summary-only") String
"display only row summaries (e.g. row total, average) (in multicolumn reports)"
    ,[String] -> (RawOpts -> RawOpts) -> String -> Flag RawOpts
forall a. [String] -> (a -> a) -> String -> Flag a
flagNone [String
"no-total",String
"N"] (String -> RawOpts -> RawOpts
setboolopt String
"no-total") String
"omit the final total row"
    ,[String] -> (RawOpts -> RawOpts) -> String -> Flag RawOpts
forall a. [String] -> (a -> a) -> String -> Flag a
flagNone [String
"no-elide"] (String -> RawOpts -> RawOpts
setboolopt String
"no-elide") String
"in tree mode, don't squash boring parent accounts; in list mode, also show parent accounts (usually zero, hidden without -E)"
    ,[String] -> (RawOpts -> RawOpts) -> String -> Flag RawOpts
forall a. [String] -> (a -> a) -> String -> Flag a
flagNone [String
"full-names"] (String -> RawOpts -> RawOpts
setboolopt String
"full-names") String
"in tree mode, show full account names instead of indented leaf names"
    ,[String] -> Update RawOpts -> String -> String -> Flag RawOpts
forall a. [String] -> Update a -> String -> String -> Flag a
flagReq  [String
"format"] (\String
s RawOpts
opts -> RawOpts -> Either String RawOpts
forall a b. b -> Either a b
Right (RawOpts -> Either String RawOpts)
-> RawOpts -> Either String RawOpts
forall a b. (a -> b) -> a -> b
$ String -> String -> RawOpts -> RawOpts
setopt String
"format" String
s RawOpts
opts) String
"FORMATSTR" String
"use this custom line format (in simple reports)"
    ,[String] -> (RawOpts -> RawOpts) -> String -> Flag RawOpts
forall a. [String] -> (a -> a) -> String -> Flag a
flagNone [String
"sort-amount",String
"S"] (String -> RawOpts -> RawOpts
setboolopt String
"sort-amount") String
"sort by amount instead of account code/name (in flat mode). With multiple columns, sorts by the row total, or by row average if that is displayed."
    ,[String] -> (RawOpts -> RawOpts) -> String -> Flag RawOpts
forall a. [String] -> (a -> a) -> String -> Flag a
flagNone [String
"percent", String
"%"] (String -> RawOpts -> RawOpts
setboolopt String
"percent") String
"express values in percentage of each column's total"
    ,[String] -> (RawOpts -> RawOpts) -> String -> Flag RawOpts
forall a. [String] -> (a -> a) -> String -> Flag a
flagNone [String
"related",String
"r"] (String -> RawOpts -> RawOpts
setboolopt String
"related") String
"show the other accounts transacted with, instead"
    ,[String] -> (RawOpts -> RawOpts) -> String -> Flag RawOpts
forall a. [String] -> (a -> a) -> String -> Flag a
flagNone [String
"invert"] (String -> RawOpts -> RawOpts
setboolopt String
"invert") String
"display all amounts with reversed sign"
    ,[String] -> (RawOpts -> RawOpts) -> String -> Flag RawOpts
forall a. [String] -> (a -> a) -> String -> Flag a
flagNone [String
"transpose"] (String -> RawOpts -> RawOpts
setboolopt String
"transpose") String
"switch rows and columns (use vertical time axis); repeat to cancel"
    ,[String] -> Update RawOpts -> String -> String -> Flag RawOpts
forall a. [String] -> Update a -> String -> String -> Flag a
flagReq  [String
"layout"] (\String
s RawOpts
opts -> RawOpts -> Either String RawOpts
forall a b. b -> Either a b
Right (RawOpts -> Either String RawOpts)
-> RawOpts -> Either String RawOpts
forall a b. (a -> b) -> a -> b
$ String -> String -> RawOpts -> RawOpts
setopt String
"layout" String
s RawOpts
opts) String
"ARG"
      ([String] -> String
unlines
        [String
"how to lay out multi-commodity amounts and the overall table:"
        ,String
"'wide[,W]': commodities on same line, up to W wide"
        ,String
"'tall'    : commodities on separate lines"
        ,String
"'bare'    : commodity symbols in a separate column"
        ,String
"'tidy'    : each data field in its own column"
        ])
     ,[String] -> Update RawOpts -> String -> String -> Flag RawOpts
forall a. [String] -> Update a -> String -> String -> Flag a
flagReq  [String
"base-url"] (\String
s RawOpts
opts -> RawOpts -> Either String RawOpts
forall a b. b -> Either a b
Right (RawOpts -> Either String RawOpts)
-> RawOpts -> Either String RawOpts
forall a b. (a -> b) -> a -> b
$ String -> String -> RawOpts -> RawOpts
setopt String
"base-url" String
s RawOpts
opts) String
"URLPREFIX" String
"in html output, generate links to hledger-web, with this prefix. (Usually the base url shown by hledger-web; can also be relative.)"

    -- output:
    ,[String] -> Flag RawOpts
outputFormatFlag [String
"txt",String
"html",String
"csv",String
"tsv",String
"json",String
"fods",String
"svg"]
    ,Flag RawOpts
outputFileFlag
    ,Flag RawOpts
outputViewFlag
    ,Flag RawOpts
balanceChartFlag
    ,Flag RawOpts
chartWidthFlag

    ]
  )
  [(String, [Flag RawOpts])]
cligeneralflagsgroups1
  ([Flag RawOpts]
hiddenflags [Flag RawOpts] -> [Flag RawOpts] -> [Flag RawOpts]
forall a. [a] -> [a] -> [a]
++
    [ [String] -> (RawOpts -> RawOpts) -> String -> Flag RawOpts
forall a. [String] -> (a -> a) -> String -> Flag a
flagNone [String
"commodity-column"] (String -> RawOpts -> RawOpts
setboolopt String
"commodity-column")
      String
"show commodity symbols in a separate column, amounts as bare numbers, one row per commodity"
    ])
  ([], Arg RawOpts -> Maybe (Arg RawOpts)
forall a. a -> Maybe a
Just (Arg RawOpts -> Maybe (Arg RawOpts))
-> Arg RawOpts -> Maybe (Arg RawOpts)
forall a b. (a -> b) -> a -> b
$ String -> Arg RawOpts
argsFlag String
"[QUERY]")

  where
    calcprefix :: String
calcprefix = String
"calculation mode: "
    accumprefix :: String
accumprefix = String
"accumulation mode: "

-- | The balance command, prints a balance report.
balance :: CliOpts -> Journal -> IO ()
balance :: CliOpts -> Journal -> IO ()
balance CliOpts
opts Journal
j | CliOpts -> Bool
chartRequested CliOpts
opts = CliOpts -> Journal -> IO ()
balanceChart CliOpts
opts Journal
j
balance opts :: CliOpts
opts@CliOpts{reportspec_ :: CliOpts -> ReportSpec
reportspec_=ReportSpec
rspec} Journal
j = case ReportOpts -> BalanceCalculation
balancecalc_ ReportOpts
ropts of
    BalanceCalculation
CalcBudget -> do  -- single or multi period budget report
      ReportSpec -> Journal -> IO ()
warnIfLargeMultiPeriodReport ReportSpec
rspec Journal
j
      let rspan :: DateSpan
rspan = (DateSpan, Maybe DayPartition) -> DateSpan
forall a b. (a, b) -> a
fst ((DateSpan, Maybe DayPartition) -> DateSpan)
-> (DateSpan, Maybe DayPartition) -> DateSpan
forall a b. (a -> b) -> a -> b
$ Journal -> ReportSpec -> (DateSpan, Maybe DayPartition)
reportSpan Journal
j ReportSpec
rspec
          budgetreport :: BudgetReport
budgetreport = Map Text AmountStyle -> BudgetReport -> BudgetReport
forall a. HasAmounts a => Map Text AmountStyle -> a -> a
styleAmounts Map Text AmountStyle
styles (BudgetReport -> BudgetReport) -> BudgetReport -> BudgetReport
forall a b. (a -> b) -> a -> b
$ ReportSpec -> BalancingOpts -> DateSpan -> Journal -> BudgetReport
budgetReport ReportSpec
rspec (InputOpts -> BalancingOpts
balancingopts_ (InputOpts -> BalancingOpts) -> InputOpts -> BalancingOpts
forall a b. (a -> b) -> a -> b
$ CliOpts -> InputOpts
inputopts_ CliOpts
opts) DateSpan
rspan Journal
j
          render :: BudgetReport -> Text
render = case String
fmt of
            String
"txt"  -> ReportOpts -> BudgetReport -> Text
budgetReportAsText ReportOpts
ropts
            String
"json" -> (Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>Text
"\n") (Text -> Text) -> (BudgetReport -> Text) -> BudgetReport -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BudgetReport -> Text
forall a. ToJSON a => a -> Text
toJsonText
            String
"csv"  -> CSV -> Text
printCSV (CSV -> Text) -> (BudgetReport -> CSV) -> BudgetReport -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReportOpts -> BudgetReport -> CSV
budgetReportAsCsv ReportOpts
ropts
            String
"tsv"  -> CSV -> Text
printTSV (CSV -> Text) -> (BudgetReport -> CSV) -> BudgetReport -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReportOpts -> BudgetReport -> CSV
budgetReportAsCsv ReportOpts
ropts
            String
"html" -> (Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>Text
"\n") (Text -> Text) -> (BudgetReport -> Text) -> BudgetReport -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Html -> Text
htmlAsLazyText (Html -> Text) -> (BudgetReport -> Html) -> BudgetReport -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReportOpts -> BudgetReport -> Html
budgetReportAsHtml ReportOpts
ropts
            String
"fods" -> TextEncoding
-> Map Text ((Int, Int), [[Cell NumLines Text]]) -> Text
printFods TextEncoding
IO.localeEncoding (Map Text ((Int, Int), [[Cell NumLines Text]]) -> Text)
-> (BudgetReport -> Map Text ((Int, Int), [[Cell NumLines Text]]))
-> BudgetReport
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                      Text
-> ((Int, Int), [[Cell NumLines Text]])
-> Map Text ((Int, Int), [[Cell NumLines Text]])
forall k a. k -> a -> Map k a
Map.singleton (Text -> Text
tr Text
"Budget Report") (((Int, Int), [[Cell NumLines Text]])
 -> Map Text ((Int, Int), [[Cell NumLines Text]]))
-> (BudgetReport -> ((Int, Int), [[Cell NumLines Text]]))
-> BudgetReport
-> Map Text ((Int, Int), [[Cell NumLines Text]])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountFormat
-> ReportOpts
-> BudgetReport
-> ((Int, Int), [[Cell NumLines Text]])
budgetReportAsSpreadsheet AmountFormat
oneLineNoCostFmt ReportOpts
ropts
            String
_      -> String -> BudgetReport -> Text
forall a. String -> a
error' (String -> BudgetReport -> Text) -> String -> BudgetReport -> Text
forall a b. (a -> b) -> a -> b
$ String -> String
unsupportedOutputFormatError String
fmt
      CliOpts -> Text -> IO ()
writeOutputLazyText CliOpts
opts (Text -> IO ()) -> Text -> IO ()
forall a b. (a -> b) -> a -> b
$ BudgetReport -> Text
render BudgetReport
budgetreport

    BalanceCalculation
_ | Bool
multiperiod -> do  -- multi period balance report
        ReportSpec -> Journal -> IO ()
warnIfLargeMultiPeriodReport ReportSpec
rspec Journal
j
        let report :: MultiBalanceReport
report = Map Text AmountStyle -> MultiBalanceReport -> MultiBalanceReport
forall a. HasAmounts a => Map Text AmountStyle -> a -> a
styleAmounts Map Text AmountStyle
styles (MultiBalanceReport -> MultiBalanceReport)
-> MultiBalanceReport -> MultiBalanceReport
forall a b. (a -> b) -> a -> b
$ ReportSpec -> Journal -> MultiBalanceReport
multiBalanceReport ReportSpec
rspec Journal
j
            render :: MultiBalanceReport -> Text
render = case String
fmt of
              String
"txt"  -> ReportOpts -> MultiBalanceReport -> Text
multiBalanceReportAsText ReportOpts
ropts
              String
"csv"  -> CSV -> Text
printCSV (CSV -> Text)
-> (MultiBalanceReport -> CSV) -> MultiBalanceReport -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReportOpts -> MultiBalanceReport -> CSV
multiBalanceReportAsCsv ReportOpts
ropts
              String
"tsv"  -> CSV -> Text
printTSV (CSV -> Text)
-> (MultiBalanceReport -> CSV) -> MultiBalanceReport -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReportOpts -> MultiBalanceReport -> CSV
multiBalanceReportAsCsv ReportOpts
ropts
              String
"html" -> (Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>Text
"\n") (Text -> Text)
-> (MultiBalanceReport -> Text) -> MultiBalanceReport -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Html -> Text
htmlAsLazyText (Html -> Text)
-> (MultiBalanceReport -> Html) -> MultiBalanceReport -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReportOpts -> MultiBalanceReport -> Html
multiBalanceReportAsHtml ReportOpts
ropts
              String
"json" -> (Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>Text
"\n") (Text -> Text)
-> (MultiBalanceReport -> Text) -> MultiBalanceReport -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MultiBalanceReport -> Text
forall a. ToJSON a => a -> Text
toJsonText
              String
"fods" -> TextEncoding
-> Map Text ((Int, Int), [[Cell NumLines Text]]) -> Text
printFods TextEncoding
IO.localeEncoding (Map Text ((Int, Int), [[Cell NumLines Text]]) -> Text)
-> (MultiBalanceReport
    -> Map Text ((Int, Int), [[Cell NumLines Text]]))
-> MultiBalanceReport
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                        Text
-> ((Int, Int), [[Cell NumLines Text]])
-> Map Text ((Int, Int), [[Cell NumLines Text]])
forall k a. k -> a -> Map k a
Map.singleton (Text -> Text
tr Text
"Multi-period Balance Report") (((Int, Int), [[Cell NumLines Text]])
 -> Map Text ((Int, Int), [[Cell NumLines Text]]))
-> (MultiBalanceReport -> ((Int, Int), [[Cell NumLines Text]]))
-> MultiBalanceReport
-> Map Text ((Int, Int), [[Cell NumLines Text]])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReportOpts
-> MultiBalanceReport -> ((Int, Int), [[Cell NumLines Text]])
multiBalanceReportAsSpreadsheet ReportOpts
ropts
              String
_      -> Text -> MultiBalanceReport -> Text
forall a b. a -> b -> a
const (Text -> MultiBalanceReport -> Text)
-> Text -> MultiBalanceReport -> Text
forall a b. (a -> b) -> a -> b
$ String -> Text
forall a. String -> a
error' (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ String -> String
unsupportedOutputFormatError String
fmt  -- PARTIAL:
        CliOpts -> Text -> IO ()
writeOutputLazyText CliOpts
opts (Text -> IO ()) -> Text -> IO ()
forall a b. (a -> b) -> a -> b
$ MultiBalanceReport -> Text
render MultiBalanceReport
report

    BalanceCalculation
_ -> do  -- single period simple balance report
        let report :: BalanceReport
report = Map Text AmountStyle -> BalanceReport -> BalanceReport
forall a. HasAmounts a => Map Text AmountStyle -> a -> a
styleAmounts Map Text AmountStyle
styles (BalanceReport -> BalanceReport) -> BalanceReport -> BalanceReport
forall a b. (a -> b) -> a -> b
$ ReportSpec -> Journal -> BalanceReport
balanceReport ReportSpec
rspec Journal
j -- simple Ledger-style balance report
            render :: BalanceReport -> Text
render = case String
fmt of
              String
"txt"  -> ReportOpts -> Text -> Text
withTitle ReportOpts
ropts (Text -> Text) -> (BalanceReport -> Text) -> BalanceReport -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReportOpts -> BalanceReport -> Text
balanceReportAsText ReportOpts
ropts
              String
"csv"  -> CSV -> Text
printCSV (CSV -> Text) -> (BalanceReport -> CSV) -> BalanceReport -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReportOpts -> BalanceReport -> CSV
balanceReportAsCsv ReportOpts
ropts
              String
"tsv"  -> CSV -> Text
printTSV (CSV -> Text) -> (BalanceReport -> CSV) -> BalanceReport -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReportOpts -> BalanceReport -> CSV
balanceReportAsCsv ReportOpts
ropts
              String
"html" -> (Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>Text
"\n") (Text -> Text) -> (BalanceReport -> Text) -> BalanceReport -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Html -> Text
htmlAsLazyText (Html -> Text) -> (BalanceReport -> Html) -> BalanceReport -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReportOpts -> BalanceReport -> Html
balanceReportAsHtml ReportOpts
ropts
              String
"json" -> (Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>Text
"\n") (Text -> Text) -> (BalanceReport -> Text) -> BalanceReport -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BalanceReport -> Text
forall a. ToJSON a => a -> Text
toJsonText
              String
"fods" -> TextEncoding
-> Map Text ((Int, Int), [[Cell NumLines Text]]) -> Text
printFods TextEncoding
IO.localeEncoding (Map Text ((Int, Int), [[Cell NumLines Text]]) -> Text)
-> (BalanceReport -> Map Text ((Int, Int), [[Cell NumLines Text]]))
-> BalanceReport
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> ((Int, Int), [[Cell NumLines Text]])
-> Map Text ((Int, Int), [[Cell NumLines Text]])
forall k a. k -> a -> Map k a
Map.singleton (Text -> Text
tr Text
"Balance Report") (((Int, Int), [[Cell NumLines Text]])
 -> Map Text ((Int, Int), [[Cell NumLines Text]]))
-> (BalanceReport -> ((Int, Int), [[Cell NumLines Text]]))
-> BalanceReport
-> Map Text ((Int, Int), [[Cell NumLines Text]])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (,) (Int
1,Int
0) ([[Cell NumLines Text]] -> ((Int, Int), [[Cell NumLines Text]]))
-> (BalanceReport -> [[Cell NumLines Text]])
-> BalanceReport
-> ((Int, Int), [[Cell NumLines Text]])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountFormat
-> ReportOpts -> BalanceReport -> [[Cell NumLines Text]]
balanceReportAsSpreadsheet AmountFormat
oneLineNoCostFmt ReportOpts
ropts
              String
_      -> String -> BalanceReport -> Text
forall a. String -> a
error' (String -> BalanceReport -> Text)
-> String -> BalanceReport -> Text
forall a b. (a -> b) -> a -> b
$ String -> String
unsupportedOutputFormatError String
fmt  -- PARTIAL:
        CliOpts -> Text -> IO ()
writeOutputLazyText CliOpts
opts (Text -> IO ()) -> Text -> IO ()
forall a b. (a -> b) -> a -> b
$ BalanceReport -> Text
render BalanceReport
report
  where
    styles :: Map Text AmountStyle
styles = ReportOpts -> Journal -> Map Text AmountStyle
balanceReportStyles ReportOpts
ropts Journal
j
    tr :: Text -> Text
tr = Translations -> Text -> Text
I18n.tr (ReportOpts -> Translations
translations_ ReportOpts
ropts)
    ropts :: ReportOpts
ropts =
        let ropts0 :: ReportOpts
ropts0 = ReportSpec -> ReportOpts
_rsReportOpts ReportSpec
rspec in
        ReportOpts
ropts0 {
            -- tidy csv is defined externally and must not include totals or averages
            no_total_ = no_total_ ropts0 || layout_ ropts0 == LayoutTidy
        }
    -- Tidy csv/tsv should be consistent between single period and multiperiod reports.
    multiperiod :: Bool
multiperiod = ReportOpts -> Interval
interval_ ReportOpts
ropts Interval -> Interval -> Bool
forall a. Eq a => a -> a -> Bool
/= Interval
NoInterval Bool -> Bool -> Bool
|| (ReportOpts -> Layout
layout_ ReportOpts
ropts Layout -> Layout -> Bool
forall a. Eq a => a -> a -> Bool
== Layout
LayoutTidy Bool -> Bool -> Bool
&& Bool
delimited)
    delimited :: Bool
delimited   = String
fmt String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"csv" Bool -> Bool -> Bool
|| String
fmt String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"tsv"
    fmt :: String
fmt         = CliOpts -> String
outputFormatFromOpts CliOpts
opts

-- | The balance command with --chart (or svg output): shows a chart of the balance report, or the chart's data.
balanceChart :: CliOpts -> Journal -> IO ()
balanceChart :: CliOpts -> Journal -> IO ()
balanceChart opts :: CliOpts
opts@CliOpts{reportspec_ :: CliOpts -> ReportSpec
reportspec_=ReportSpec
rspec} Journal
j = case ReportOpts -> BalanceCalculation
balancecalc_ ReportOpts
ropts of
    BalanceCalculation
CalcBudget -> String -> IO ()
forall a. String -> a
error' String
"--chart does not yet support --budget"  -- PARTIAL:
    -- with a report interval, or a grouped chart (a bar per account type, which needs the stacked multi-period chart, here with one period)
    BalanceCalculation
_ | ReportOpts -> Interval
interval_ ReportOpts
ropts Interval -> Interval -> Bool
forall a. Eq a => a -> a -> Bool
/= Interval
NoInterval Bool -> Bool -> Bool
|| ChartArg -> String
caKind (Map Text AmountStyle -> CliOpts -> ChartArg
chartArg Map Text AmountStyle
forall a. Monoid a => a
mempty CliOpts
opts) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"grouped" -> do
      ReportSpec -> Journal -> IO ()
warnIfLargeMultiPeriodReport ReportSpec
rspec Journal
j
      let report :: MultiBalanceReport
report = Map Text AmountStyle -> MultiBalanceReport -> MultiBalanceReport
forall a. HasAmounts a => Map Text AmountStyle -> a -> a
styleAmounts Map Text AmountStyle
styles (MultiBalanceReport -> MultiBalanceReport)
-> MultiBalanceReport -> MultiBalanceReport
forall a b. (a -> b) -> a -> b
$ ReportSpec -> Journal -> MultiBalanceReport
multiBalanceReport ReportSpec
rspec Journal
j
      CliOpts -> Maybe Text -> Chart -> IO ()
writeChart CliOpts
opts (Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Maybe Text) -> Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ (Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>Text
"\n") (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Html -> Text
htmlAsLazyText (Html -> Text) -> Html -> Text
forall a b. (a -> b) -> a -> b
$ ReportOpts -> MultiBalanceReport -> Html
multiBalanceReportAsHtml ReportOpts
ropts MultiBalanceReport
report) (Chart -> IO ()) -> Chart -> IO ()
forall a b. (a -> b) -> a -> b
$
        (Text -> Maybe AccountType)
-> ReportOpts -> Text -> MultiBalanceReport -> Chart
multiBalanceReportAsChart (Journal -> Text -> Maybe AccountType
journalAccountType Journal
j) ReportOpts
ropts (ReportOpts -> MultiBalanceReport -> Text
multiBalanceReportTitle ReportOpts
ropts MultiBalanceReport
report) MultiBalanceReport
report
    BalanceCalculation
_ -> do
      let report :: BalanceReport
report = Map Text AmountStyle -> BalanceReport -> BalanceReport
forall a. HasAmounts a => Map Text AmountStyle -> a -> a
styleAmounts Map Text AmountStyle
styles (BalanceReport -> BalanceReport) -> BalanceReport -> BalanceReport
forall a b. (a -> b) -> a -> b
$ ReportSpec -> Journal -> BalanceReport
balanceReport ReportSpec
rspec Journal
j
      CliOpts -> Maybe Text -> Chart -> IO ()
writeChart CliOpts
opts (Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Maybe Text) -> Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ (Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>Text
"\n") (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Html -> Text
htmlAsLazyText (Html -> Text) -> Html -> Text
forall a b. (a -> b) -> a -> b
$ ReportOpts -> BalanceReport -> Html
balanceReportAsHtml ReportOpts
ropts BalanceReport
report) (Chart -> IO ()) -> Chart -> IO ()
forall a b. (a -> b) -> a -> b
$
        (Text -> Maybe AccountType)
-> ReportOpts -> Text -> BalanceReport -> Chart
balanceReportAsChart (Journal -> Text -> Maybe AccountType
journalAccountType Journal
j) ReportOpts
ropts (ReportOpts -> Text -> Text
effectiveTitle ReportOpts
ropts Text
"") BalanceReport
report
  where
    ropts :: ReportOpts
ropts  = ReportSpec -> ReportOpts
_rsReportOpts ReportSpec
rspec
    styles :: Map Text AmountStyle
styles = ReportOpts -> Journal -> Map Text AmountStyle
balanceReportStyles ReportOpts
ropts Journal
j

-- Rendering

budgetClass :: RowClass -> Ods.Class
budgetClass :: RowClass -> Class
budgetClass RowClass
rc =
    Text -> Class
Ods.Class (Text -> Class) -> Text -> Class
forall a b. (a -> b) -> a -> b
$
    case RowClass
rc of RowClass
Value -> Text
"budget"; RowClass
Total -> Text
"budget coltotal"

budgetTotalClass :: RowClass -> Ods.Class
budgetTotalClass :: RowClass -> Class
budgetTotalClass RowClass
rc =
    Text -> Class
Ods.Class (Text -> Class) -> Text -> Class
forall a b. (a -> b) -> a -> b
$
    case RowClass
rc of RowClass
Value -> Text
"budget rowtotal"; RowClass
Total -> Text
"budget coltotal"

budgetAverageClass :: RowClass -> Ods.Class
budgetAverageClass :: RowClass -> Class
budgetAverageClass RowClass
rc =
    Text -> Class
Ods.Class (Text -> Class) -> Text -> Class
forall a b. (a -> b) -> a -> b
$
    case RowClass
rc of RowClass
Value -> Text
"budget rowaverage"; RowClass
Total -> Text
"budget colaverage"


-- Single-column balance reports

-- | Render a single-column balance report as CSV.
balanceReportAsCsv :: ReportOpts -> BalanceReport -> CSV
balanceReportAsCsv :: ReportOpts -> BalanceReport -> CSV
balanceReportAsCsv ReportOpts
opts =
    [[Cell NumLines Text]] -> CSV
forall (f :: * -> *) border text.
Functor f =>
[f (Cell border text)] -> [f text]
rawTableContent ([[Cell NumLines Text]] -> CSV)
-> (BalanceReport -> [[Cell NumLines Text]])
-> BalanceReport
-> CSV
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountFormat
-> ReportOpts -> BalanceReport -> [[Cell NumLines Text]]
balanceReportAsSpreadsheet AmountFormat
machineFmt ReportOpts
opts

-- | Render a single-column balance report as plain text.
balanceReportAsText :: ReportOpts -> BalanceReport -> TL.Text
balanceReportAsText :: ReportOpts -> BalanceReport -> Text
balanceReportAsText ReportOpts
originalopts report :: BalanceReport
report@(([BalanceReportItem]
items, Change
total)) =
  case ReportOpts -> Layout
layout_ ReportOpts
originalopts of
    Layout
LayoutBare | Bool
iscustom -> String -> Text
forall a. String -> a
error' String
"Custom format not supported with commodity columns"  -- PARTIAL:
    Layout
LayoutBare ->
      Builder -> Text
TB.toLazyText (Builder -> Text) -> Builder -> Text
forall a b. (a -> b) -> a -> b
$ ReportOpts -> BalanceReport -> Builder
bareLayoutBalanceReportAsText ReportOpts
originalopts BalanceReport
report
    Layout
LayoutBareWide -> ReportOpts -> BalanceReport -> Text
bareWideLayoutBalanceReportAsText ReportOpts
originalopts BalanceReport
report
    Layout
_ ->
      Builder -> Text
TB.toLazyText (Builder -> Text) -> Builder -> Text
forall a b. (a -> b) -> a -> b
$
      [Builder] -> Builder
unlinesB [Builder]
ls Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> [Builder] -> Builder
unlinesB (if ReportOpts -> Bool
no_total_ ReportOpts
opts then [] else [Builder
overline, Builder
totalLines])
  where
    opts :: ReportOpts
opts = ReportOpts -> BalanceReport -> ReportOpts
widenDefaultBalanceLineFormat ReportOpts
originalopts BalanceReport
report
    ([Builder]
ls, [[Int]]
sizes) = [(Builder, [Int])] -> ([Builder], [[Int]])
forall a b. [(a, b)] -> ([a], [b])
unzip ([(Builder, [Int])] -> ([Builder], [[Int]]))
-> [(Builder, [Int])] -> ([Builder], [[Int]])
forall a b. (a -> b) -> a -> b
$ (BalanceReportItem -> (Builder, [Int]))
-> [BalanceReportItem] -> [(Builder, [Int])]
forall a b. (a -> b) -> [a] -> [b]
map (ReportOpts -> BalanceReportItem -> (Builder, [Int])
balanceReportItemAsText ReportOpts
opts) [BalanceReportItem]
items
    -- abuse renderBalanceReportItem to render the total with similar format
    (Builder
totalLines, [Int]
_totalSizes) = ReportOpts -> (Text, Int, Change) -> (Builder, [Int])
renderBalanceReportItem ReportOpts
opts (Text
"",Int
0,Change
total)
    -- with a custom format, extend the line to the full report width;
    -- otherwise show the amount column's width.
    iscustom :: Bool
iscustom = case ReportOpts -> StringFormat
format_ ReportOpts
originalopts of
        OneLine       ((FormatField Bool
_ Maybe Int
_ Maybe Int
_ ReportItemField
TotalField):[StringFormatComponent]
_) -> Bool
False
        TopAligned    ((FormatField Bool
_ Maybe Int
_ Maybe Int
_ ReportItemField
TotalField):[StringFormatComponent]
_) -> Bool
False
        BottomAligned ((FormatField Bool
_ Maybe Int
_ Maybe Int
_ ReportItemField
TotalField):[StringFormatComponent]
_) -> Bool
False
        StringFormat
_ -> Bool
True
    isdefault :: Bool
isdefault = ReportOpts -> StringFormat
format_ ReportOpts
originalopts StringFormat -> StringFormat -> Bool
forall a. Eq a => a -> a -> Bool
== StringFormat
defaultBalanceLineFormat
    overlinewidth :: Int
overlinewidth
      | Bool
iscustom  = [Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (([Int] -> Int) -> [[Int]] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map [Int] -> Int
forall a. Integral a => [a] -> a
maximum' ([[Int]] -> [Int]) -> [[Int]] -> [Int]
forall a b. (a -> b) -> a -> b
$ [[Int]] -> [[Int]]
forall a. [[a]] -> [[a]]
transpose [[Int]]
sizes)
      | Bool
isdefault = ReportOpts -> BalanceReport -> Int
defaultBalanceAmountColumnWidth ReportOpts
originalopts BalanceReport
report
      | Bool
otherwise = Int
20
    overline :: Builder
overline   = Text -> Builder
TB.fromText (Text -> Builder) -> Text -> Builder
forall a b. (a -> b) -> a -> b
$ Int -> Text -> Text
T.replicate Int
overlinewidth Text
"-"

-- | The default balance format historically used a 20-character amount
-- column. Keep that as the minimum, but widen it when any rendered amount
-- needs more room.
widenDefaultBalanceLineFormat :: ReportOpts -> BalanceReport -> ReportOpts
widenDefaultBalanceLineFormat :: ReportOpts -> BalanceReport -> ReportOpts
widenDefaultBalanceLineFormat ReportOpts
opts BalanceReport
report
  | ReportOpts -> StringFormat
format_ ReportOpts
opts StringFormat -> StringFormat -> Bool
forall a. Eq a => a -> a -> Bool
== StringFormat
defaultBalanceLineFormat = ReportOpts
opts{format_ = setTotalMinWidth amountwidth defaultBalanceLineFormat}
  | Bool
otherwise = ReportOpts
opts
  where
    amountwidth :: Int
amountwidth = ReportOpts -> BalanceReport -> Int
defaultBalanceAmountColumnWidth ReportOpts
opts BalanceReport
report
    setTotalMinWidth :: Int -> StringFormat -> StringFormat
setTotalMinWidth Int
w (BottomAligned (FormatField Bool
l Maybe Int
_ Maybe Int
m ReportItemField
TotalField : [StringFormatComponent]
rest)) =
      [StringFormatComponent] -> StringFormat
BottomAligned (Bool
-> Maybe Int
-> Maybe Int
-> ReportItemField
-> StringFormatComponent
FormatField Bool
l (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
w) Maybe Int
m ReportItemField
TotalField StringFormatComponent
-> [StringFormatComponent] -> [StringFormatComponent]
forall a. a -> [a] -> [a]
: [StringFormatComponent]
rest)
    setTotalMinWidth Int
_ StringFormat
fmt = StringFormat
fmt

defaultBalanceAmountColumnWidth :: ReportOpts -> BalanceReport -> Int
defaultBalanceAmountColumnWidth :: ReportOpts -> BalanceReport -> Int
defaultBalanceAmountColumnWidth ReportOpts
opts (([BalanceReportItem]
items, Change
total)) =
    [Int] -> Int
forall a. Integral a => [a] -> a
maximum' ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ Int
20 Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: (Change -> Int) -> [Change] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Change -> Int
renderedAmountWidth [Change]
visibleAmounts
  where
    visibleAmounts :: [Change]
visibleAmounts =
      (BalanceReportItem -> Change) -> [BalanceReportItem] -> [Change]
forall a b. (a -> b) -> [a] -> [b]
map (\(Text
_,Text
_,Int
_,Change
amt) -> Change
amt) [BalanceReportItem]
items [Change] -> [Change] -> [Change]
forall a. [a] -> [a] -> [a]
++
      [Change
total | Bool -> Bool
not (ReportOpts -> Bool
no_total_ ReportOpts
opts)]
    renderedAmountWidth :: Change -> Int
renderedAmountWidth =
      WideBuilder -> Int
wbWidth (WideBuilder -> Int) -> (Change -> WideBuilder) -> Change -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountFormat -> Change -> WideBuilder
showMixedAmountB AmountFormat
noCostFmt
        { displayCommodity = layout_ opts /= LayoutBare
        , displayMinWidth = Just 0
        , displayColour = False
        }

-- | Render a single-column balance report as plain text with a separate commodity column (--layout=bare)
bareLayoutBalanceReportAsText :: ReportOpts -> BalanceReport -> TB.Builder
bareLayoutBalanceReportAsText :: ReportOpts -> BalanceReport -> Builder
bareLayoutBalanceReportAsText ReportOpts
opts ([BalanceReportItem]
items, Change
total) =
  [Builder] -> Builder
unlinesB ([Builder] -> Builder)
-> ([[Cell]] -> [Builder]) -> [[Cell]] -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  ([Cell] -> Builder) -> [[Cell]] -> [Builder]
forall a b. (a -> b) -> [a] -> [b]
map
    (TableOpts -> [Int] -> Header Cell -> Builder
renderColumns TableOpts
forall a. Default a => a
def{tableBorders=singleColumnTableOuterBorder} [Int]
sizes (Header Cell -> Builder)
-> ([Cell] -> Header Cell) -> [Cell] -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     Properties -> [Header Cell] -> Header Cell
forall h. Properties -> [Header h] -> Header h
Group Properties
singleColumnTableInterColumnBorder ([Header Cell] -> Header Cell)
-> ([Cell] -> [Header Cell]) -> [Cell] -> Header Cell
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Cell -> Header Cell) -> [Cell] -> [Header Cell]
forall a b. (a -> b) -> [a] -> [b]
map Cell -> Header Cell
forall h. h -> Header h
Header) ([[Cell]] -> Builder) -> [[Cell]] -> Builder
forall a b. (a -> b) -> a -> b
$
  [[Cell]]
ls [[Cell]] -> [[Cell]] -> [[Cell]]
forall a. [a] -> [a] -> [a]
++ [[[Cell]]] -> [[Cell]]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[[Cell
overline], [Cell]
totalline] | Bool -> Bool
not (ReportOpts -> Bool
no_total_ ReportOpts
opts)]
  where
    render :: (a, Text, Int, Change) -> [Cell]
render (a
_, Text
acctname, Int
dep, Change
amt) =
        [ Align -> [WideBuilder] -> Cell
Cell Align
TopRight [WideBuilder]
damts
        , Align -> [WideBuilder] -> Cell
Cell Align
TopLeft ((Text -> WideBuilder) -> [Text] -> [WideBuilder]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> WideBuilder
wbFromText [Text]
cs)
        , Align -> [WideBuilder] -> Cell
Cell Align
TopLeft (Int -> WideBuilder -> [WideBuilder]
forall a. Int -> a -> [a]
replicate ([WideBuilder] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [WideBuilder]
damts Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) WideBuilder
forall a. Monoid a => a
mempty [WideBuilder] -> [WideBuilder] -> [WideBuilder]
forall a. [a] -> [a] -> [a]
++ [Text -> WideBuilder
wbFromText Text
dispname]) ]
      where dopts :: AmountFormat
dopts = AmountFormat
oneLineNoCostFmt{displayCommodity=layout_ opts /= LayoutBare, displayCommodityOrder=Just cs, displayColour=color_ opts}
            cs :: [Text]
cs    = if Change -> Bool
mixedAmountLooksZero Change
amt then [Text
""] else Set Text -> [Text]
forall a. Set a -> [a]
S.toList (Set Text -> [Text]) -> Set Text -> [Text]
forall a b. (a -> b) -> a -> b
$ Change -> Set Text
maCommodities Change
amt
            dispname :: Text
dispname = Int -> Text -> Text
T.replicate ((Int
dep Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2) Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
acctname
            damts :: [WideBuilder]
damts = AmountFormat -> Change -> [WideBuilder]
showMixedAmountLinesB AmountFormat
dopts Change
amt
    ls :: [[Cell]]
ls = (BalanceReportItem -> [Cell]) -> [BalanceReportItem] -> [[Cell]]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap BalanceReportItem -> [Cell]
forall {a}. (a, Text, Int, Change) -> [Cell]
render [BalanceReportItem]
items
    totalline :: [Cell]
totalline = (String, Text, Int, Change) -> [Cell]
forall {a}. (a, Text, Int, Change) -> [Cell]
render (String
"", Text
"", Int
0, Change
total)
    sizes :: [Int]
sizes = Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe Int
0 (Maybe Int -> Int) -> ([Cell] -> Maybe Int) -> [Cell] -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Int] -> Maybe Int
forall a. Ord a => [a] -> Maybe a
maximumMay ([Int] -> Maybe Int) -> ([Cell] -> [Int]) -> [Cell] -> Maybe Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Cell -> Int) -> [Cell] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Cell -> Int
cellWidth ([Cell] -> Int) -> [[Cell]] -> [Int]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
        [[Cell]] -> [[Cell]]
forall a. [[a]] -> [[a]]
transpose ([[Cell]
totalline | Bool -> Bool
not (ReportOpts -> Bool
no_total_ ReportOpts
opts)] [[Cell]] -> [[Cell]] -> [[Cell]]
forall a. [a] -> [a] -> [a]
++ [[Cell]]
ls)
    overline :: Cell
overline = Align -> [WideBuilder] -> Cell
Cell Align
TopLeft ([WideBuilder] -> Cell)
-> (Maybe Int -> [WideBuilder]) -> Maybe Int -> Cell
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WideBuilder -> [WideBuilder]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (WideBuilder -> [WideBuilder])
-> (Maybe Int -> WideBuilder) -> Maybe Int -> [WideBuilder]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> WideBuilder
wbFromText (Text -> WideBuilder)
-> (Maybe Int -> Text) -> Maybe Int -> WideBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Text -> Text) -> Text -> Int -> Text
forall a b c. (a -> b -> c) -> b -> a -> c
flip Int -> Text -> Text
T.replicate Text
"-" (Int -> Text) -> (Maybe Int -> Int) -> Maybe Int -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe Int
0 (Maybe Int -> Cell) -> Maybe Int -> Cell
forall a b. (a -> b) -> a -> b
$ [Int] -> Maybe Int
forall a. [a] -> Maybe a
headMay [Int]
sizes
    singleColumnTableOuterBorder :: Bool
singleColumnTableOuterBorder       = ReportOpts -> Bool
pretty_ ReportOpts
opts
    singleColumnTableInterColumnBorder :: Properties
singleColumnTableInterColumnBorder = if ReportOpts -> Bool
pretty_ ReportOpts
opts then Properties
SingleLine else Properties
NoLine

-- | Render a single-column balance report as plain text with a separate commodity column (--layout=barewide)
bareWideLayoutBalanceReportAsText :: ReportOpts -> BalanceReport -> TL.Text
bareWideLayoutBalanceReportAsText :: ReportOpts -> BalanceReport -> Text
bareWideLayoutBalanceReportAsText ReportOpts
opts BalanceReport
br =
  TableOpts
-> (Text -> Cell)
-> (Text -> Cell)
-> (Text -> Cell)
-> Table Text Text Text
-> Text
forall a rh ch.
Show a =>
TableOpts
-> (rh -> Cell)
-> (ch -> Cell)
-> (a -> Cell)
-> Table rh ch a
-> Text
renderTable TableOpts
tableopts (Align -> Text -> Cell
textCell Align
TopLeft) (Align -> Text -> Cell
textCell Align
TopLeft) (Align -> Text -> Cell
textCell Align
TopRight) (Table Text Text Text -> Text) -> Table Text Text Text -> Text
forall a b. (a -> b) -> a -> b
$
  Header Text -> Header Text -> CSV -> Table Text Text Text
forall rh ch a. Header rh -> Header ch -> [[a]] -> Table rh ch a
Table
    (if [Header Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Header Text]
totalheadings
        then Properties -> [Header Text] -> Header Text
forall h. Properties -> [Header h] -> Header h
Group Properties
NoLine [Header Text]
rowheadings
        else Properties -> [Header Text] -> Header Text
forall h. Properties -> [Header h] -> Header h
Group Properties
SingleLine ([Header Text] -> Header Text) -> [Header Text] -> Header Text
forall a b. (a -> b) -> a -> b
$ ([Header Text] -> Header Text) -> [[Header Text]] -> [Header Text]
forall a b. (a -> b) -> [a] -> [b]
map (Properties -> [Header Text] -> Header Text
forall h. Properties -> [Header h] -> Header h
Group Properties
NoLine) [[Header Text]
rowheadings, [Header Text]
totalheadings])
    (Properties -> [Header Text] -> Header Text
forall h. Properties -> [Header h] -> Header h
Group Properties
NoLine ([Header Text] -> Header Text) -> [Header Text] -> Header Text
forall a b. (a -> b) -> a -> b
$ (Cell NumLines Text -> Header Text)
-> [Cell NumLines Text] -> [Header Text]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> Header Text
forall h. h -> Header h
Header (Text -> Header Text)
-> (Cell NumLines Text -> Text)
-> Cell NumLines Text
-> Header Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Cell NumLines Text -> Text
forall border text. Cell border text -> text
Ods.cellContent) ([Cell NumLines Text] -> [Header Text])
-> [Cell NumLines Text] -> [Header Text]
forall a b. (a -> b) -> a -> b
$ NonEmpty (Cell NumLines Text) -> [Cell NumLines Text]
forall a. NonEmpty a -> [a]
NonEmpty.tail NonEmpty (Cell NumLines Text)
header)
    (CSV
rowsCSV -> CSV -> CSV
forall a. [a] -> [a] -> [a]
++CSV
totaldata)
  where
    tableopts :: TableOpts
tableopts = TableOpts
forall a. Default a => a
def{tableBorders=singleColumnTableOuterBorder, prettyTable=pretty_ opts}
    (NonEmpty (Cell NumLines Text)
header, [NonEmpty (Cell NumLines Text)]
body, [NonEmpty (Cell NumLines Text)]
totals) = AmountFormat
-> ReportOpts
-> BalanceReport
-> (NonEmpty (Cell NumLines Text), [NonEmpty (Cell NumLines Text)],
    [NonEmpty (Cell NumLines Text)])
balanceReportAsSpreadsheetParts AmountFormat
fmt ReportOpts
opts BalanceReport
br
    fmt :: AmountFormat
fmt = AmountFormat
oneLineNoCostFmt{displayColour=color_ opts}
    separateHeaders :: [NonEmpty (Cell border h)] -> ([Header h], [[h]])
separateHeaders =
        [(Header h, [h])] -> ([Header h], [[h]])
forall a b. [(a, b)] -> ([a], [b])
unzip ([(Header h, [h])] -> ([Header h], [[h]]))
-> ([NonEmpty (Cell border h)] -> [(Header h, [h])])
-> [NonEmpty (Cell border h)]
-> ([Header h], [[h]])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NonEmpty h -> (Header h, [h]))
-> [NonEmpty h] -> [(Header h, [h])]
forall a b. (a -> b) -> [a] -> [b]
map (\(h
hd:|[h]
tl) -> (h -> Header h
forall h. h -> Header h
Header h
hd, [h]
tl)) ([NonEmpty h] -> [(Header h, [h])])
-> ([NonEmpty (Cell border h)] -> [NonEmpty h])
-> [NonEmpty (Cell border h)]
-> [(Header h, [h])]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [NonEmpty (Cell border h)] -> [NonEmpty h]
forall (f :: * -> *) border text.
Functor f =>
[f (Cell border text)] -> [f text]
rawTableContent
    ([Header Text]
rowheadings,CSV
rows) = [NonEmpty (Cell NumLines Text)] -> ([Header Text], CSV)
forall {border} {h}.
[NonEmpty (Cell border h)] -> ([Header h], [[h]])
separateHeaders [NonEmpty (Cell NumLines Text)]
body
    ([Header Text]
totalheadings,CSV
totaldata) = [NonEmpty (Cell NumLines Text)] -> ([Header Text], CSV)
forall {border} {h}.
[NonEmpty (Cell border h)] -> ([Header h], [[h]])
separateHeaders [NonEmpty (Cell NumLines Text)]
totals
    singleColumnTableOuterBorder :: Bool
singleColumnTableOuterBorder = ReportOpts -> Bool
pretty_ ReportOpts
opts

{-
This implementation turned out to be a bit convoluted but implements the following algorithm for formatting:

- If there is a single amount, print it with the account name directly:
- Otherwise, only print the account name on the last line.

    a         USD 1   ; Account 'a' has a single amount
              EUR -1
    b         USD -1  ; Account 'b' has two amounts. The account name is printed on the last line.
-}
-- | Render one balance report line item as plain text suitable for console output (or
-- whatever string format is specified). Note, prices will not be rendered, and
-- differently-priced quantities of the same commodity will appear merged.
-- The output will be one or more lines depending on the format and number of commodities.
balanceReportItemAsText :: ReportOpts -> BalanceReportItem -> (TB.Builder, [Int])
balanceReportItemAsText :: ReportOpts -> BalanceReportItem -> (Builder, [Int])
balanceReportItemAsText ReportOpts
opts (Text
fullName, Text
displayName, Int
dep, Change
amt)
  | ReportOpts -> AccountListMode
accountlistmode_ ReportOpts
opts AccountListMode -> AccountListMode -> Bool
forall a. Eq a => a -> a -> Bool
== AccountListMode
ALTree Bool -> Bool -> Bool
&& ReportOpts -> Bool
full_names_ ReportOpts
opts =
      ReportOpts -> (Text, Int, Change) -> (Builder, [Int])
renderBalanceReportItem ReportOpts
opts (Int -> Text -> Text
accountNameDrop (ReportOpts -> Int
drop_ ReportOpts
opts) Text
fullName, Int
0, Change
amt)
  | Bool
otherwise =
      ReportOpts -> (Text, Int, Change) -> (Builder, [Int])
renderBalanceReportItem ReportOpts
opts (Text
displayName, Int
dep, Change
amt)

-- | Render a balance report item, using the StringFormat specified by --format.
--
renderBalanceReportItem :: ReportOpts -> (AccountName, Int, MixedAmount) -> (TB.Builder, [Int])
renderBalanceReportItem :: ReportOpts -> (Text, Int, Change) -> (Builder, [Int])
renderBalanceReportItem ReportOpts
opts (Text
acctname, Int
dep, Change
total) =
  case ReportOpts -> StringFormat
format_ ReportOpts
opts of
      OneLine       [StringFormatComponent]
comps -> [Cell] -> (Builder, [Int])
renderRowFromComponents ([Cell] -> (Builder, [Int])) -> [Cell] -> (Builder, [Int])
forall a b. (a -> b) -> a -> b
$ Bool -> Bool -> [StringFormatComponent] -> [Cell]
renderComponents Bool
True  Bool
True  [StringFormatComponent]
comps
      TopAligned    [StringFormatComponent]
comps -> [Cell] -> (Builder, [Int])
renderRowFromComponents ([Cell] -> (Builder, [Int])) -> [Cell] -> (Builder, [Int])
forall a b. (a -> b) -> a -> b
$ Bool -> Bool -> [StringFormatComponent] -> [Cell]
renderComponents Bool
True  Bool
False [StringFormatComponent]
comps
      BottomAligned [StringFormatComponent]
comps -> [Cell] -> (Builder, [Int])
renderRowFromComponents ([Cell] -> (Builder, [Int])) -> [Cell] -> (Builder, [Int])
forall a b. (a -> b) -> a -> b
$ Bool -> Bool -> [StringFormatComponent] -> [Cell]
renderComponents Bool
False Bool
False [StringFormatComponent]
comps

  where
    -- Combine the rendered component cells horizontally, as a possibly multi-line text (builder),
    -- aligned in borderless columns (? XXX). Also returns the rendered width of each cell.
    renderRowFromComponents :: [Cell] -> (TB.Builder, [Int])
    renderRowFromComponents :: [Cell] -> (Builder, [Int])
renderRowFromComponents [Cell]
cs =
      ( TableOpts -> Header Cell -> Builder
renderRowB TableOpts
forall a. Default a => a
def{tableBorders=False, borderSpaces=False} (Header Cell -> Builder)
-> ([Header Cell] -> Header Cell) -> [Header Cell] -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Properties -> [Header Cell] -> Header Cell
forall h. Properties -> [Header h] -> Header h
Group Properties
NoLine ([Header Cell] -> Builder) -> [Header Cell] -> Builder
forall a b. (a -> b) -> a -> b
$ (Cell -> Header Cell) -> [Cell] -> [Header Cell]
forall a b. (a -> b) -> [a] -> [b]
map Cell -> Header Cell
forall h. h -> Header h
Header [Cell]
cs
      , (Cell -> Int) -> [Cell] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Cell -> Int
cellWidth [Cell]
cs
      )

    -- Render each of the given StringFormat components for the balance report item,
    -- returning each as a Cell.
    renderComponents :: Bool -> Bool -> [StringFormatComponent] -> [Cell]
    renderComponents :: Bool -> Bool -> [StringFormatComponent] -> [Cell]
renderComponents Bool
topaligned Bool
oneline = (StringFormatComponent -> Cell)
-> [StringFormatComponent] -> [Cell]
forall a b. (a -> b) -> [a] -> [b]
map (Bool
-> Bool
-> ReportOpts
-> (Text, Int, Change)
-> StringFormatComponent
-> Cell
renderComponent Bool
topaligned Bool
oneline ReportOpts
opts (Text
acctname, Int
dep, Change
total))

-- Render one StringFormat component for a balance report item.
-- Returns a Cell, containing 0 or more lines of text (as builders).
renderComponent :: Bool -> Bool -> ReportOpts -> (AccountName, Int, MixedAmount) -> StringFormatComponent -> Cell
renderComponent :: Bool
-> Bool
-> ReportOpts
-> (Text, Int, Change)
-> StringFormatComponent
-> Cell
renderComponent Bool
_ Bool
_ ReportOpts
_ (Text, Int, Change)
_ (FormatLiteral Text
s) = Align -> Text -> Cell
textCell Align
TopLeft Text
s
renderComponent Bool
topaligned Bool
oneline ReportOpts
opts (Text
acctname, Int
dep, Change
total) (FormatField Bool
ljust Maybe Int
mmin Maybe Int
mmax ReportItemField
field) = case ReportItemField
field of
    ReportItemField
DepthSpacerField -> Align -> [WideBuilder] -> Cell
Cell Align
align [Builder -> Int -> WideBuilder
WideBuilder (Text -> Builder
TB.fromText (Text -> Builder) -> Text -> Builder
forall a b. (a -> b) -> a -> b
$ Int -> Text -> Text
T.replicate Int
d Text
" ") Int
d]
                        where d :: Int
d = (Int -> Int) -> (Int -> Int -> Int) -> Maybe Int -> Int -> Int
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int -> Int
forall a. a -> a
id Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Maybe Int
mmax (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int
dep Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe Int
1 Maybe Int
mmin
    ReportItemField
AccountField     -> Align -> Text -> Cell
textCell Align
align (Text -> Cell) -> Text -> Cell
forall a b. (a -> b) -> a -> b
$ Bool -> Maybe Int -> Maybe Int -> Text -> Text
formatText Bool
ljust Maybe Int
mmin Maybe Int
mmax Text
acctname
    ReportItemField
TotalField       -> Align -> [WideBuilder] -> Cell
Cell Align
align ([WideBuilder] -> Cell)
-> (WideBuilder -> [WideBuilder]) -> WideBuilder -> Cell
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WideBuilder -> [WideBuilder]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (WideBuilder -> Cell) -> WideBuilder -> Cell
forall a b. (a -> b) -> a -> b
$ AmountFormat -> Change -> WideBuilder
showMixedAmountB AmountFormat
dopts Change
total
    ReportItemField
_                -> Align -> [WideBuilder] -> Cell
Cell Align
align [WideBuilder
forall a. Monoid a => a
mempty]
  where
    align :: Align
align | Bool
topaligned Bool -> Bool -> Bool
&& Bool
ljust = Align
TopLeft
          | Bool
topaligned          = Align
TopRight
          | Bool
ljust               = Align
BottomLeft
          | Bool
otherwise           = Align
BottomRight
    dopts :: AmountFormat
dopts = AmountFormat
noCostFmt{displayCommodity = layout_ opts /= LayoutBare
                  ,displayOneLine   = oneline
                  ,displayMinWidth  = mmin
                  ,displayMaxWidth  = mmax
                  ,displayColour    = color_ opts
                  }


-- | Render a single-column balance report as HTML.
-- This report has no default heading, so one is shown only with --title.
balanceReportAsHtml :: ReportOpts -> BalanceReport -> Html
balanceReportAsHtml :: ReportOpts -> BalanceReport -> Html
balanceReportAsHtml ReportOpts
ropts BalanceReport
br =
  Text -> [[Cell NumLines Html]] -> Html
forall border. Lines border => Text -> [[Cell border Html]] -> Html
titledTableHtml (ReportOpts -> Text -> Text
effectiveTitle ReportOpts
ropts Text
"") ([[Cell NumLines Html]] -> Html)
-> ([[Cell NumLines Text]] -> [[Cell NumLines Html]])
-> [[Cell NumLines Text]]
-> Html
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Cell NumLines Text] -> [Cell NumLines Html])
-> [[Cell NumLines Text]] -> [[Cell NumLines Html]]
forall a b. (a -> b) -> [a] -> [b]
map ((Cell NumLines Text -> Cell NumLines Html)
-> [Cell NumLines Text] -> [Cell NumLines Html]
forall a b. (a -> b) -> [a] -> [b]
map ((Text -> Html) -> Cell NumLines Text -> Cell NumLines Html
forall a b. (a -> b) -> Cell NumLines a -> Cell NumLines b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Html
forall a. ToMarkup a => a -> Html
toHtml)) ([[Cell NumLines Text]] -> Html) -> [[Cell NumLines Text]] -> Html
forall a b. (a -> b) -> a -> b
$
    AmountFormat
-> ReportOpts -> BalanceReport -> [[Cell NumLines Text]]
balanceReportAsSpreadsheet AmountFormat
oneLineNoCostFmt ReportOpts
ropts BalanceReport
br

-- | Render a single-column balance report as FODS.
balanceReportAsSpreadsheet ::
    AmountFormat -> ReportOpts -> BalanceReport -> [[Ods.Cell Ods.NumLines Text]]
balanceReportAsSpreadsheet :: AmountFormat
-> ReportOpts -> BalanceReport -> [[Cell NumLines Text]]
balanceReportAsSpreadsheet AmountFormat
fmt ReportOpts
opts BalanceReport
mbr =
    (if ReportOpts -> Bool
transpose_ ReportOpts
opts then [[Cell NumLines Text]] -> [[Cell NumLines Text]]
forall border text. [[Cell border text]] -> [[Cell border text]]
Ods.transpose else [[Cell NumLines Text]] -> [[Cell NumLines Text]]
forall a. a -> a
id) ([[Cell NumLines Text]] -> [[Cell NumLines Text]])
-> [[Cell NumLines Text]] -> [[Cell NumLines Text]]
forall a b. (a -> b) -> a -> b
$
    (NonEmpty (Cell NumLines Text) -> [Cell NumLines Text])
-> [NonEmpty (Cell NumLines Text)] -> [[Cell NumLines Text]]
forall a b. (a -> b) -> [a] -> [b]
map NonEmpty (Cell NumLines Text) -> [Cell NumLines Text]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
Fold.toList ([NonEmpty (Cell NumLines Text)] -> [[Cell NumLines Text]])
-> [NonEmpty (Cell NumLines Text)] -> [[Cell NumLines Text]]
forall a b. (a -> b) -> a -> b
$ NonEmpty (Cell NumLines Text)
header NonEmpty (Cell NumLines Text)
-> [NonEmpty (Cell NumLines Text)]
-> [NonEmpty (Cell NumLines Text)]
forall a. a -> [a] -> [a]
: [NonEmpty (Cell NumLines Text)]
body [NonEmpty (Cell NumLines Text)]
-> [NonEmpty (Cell NumLines Text)]
-> [NonEmpty (Cell NumLines Text)]
forall a. [a] -> [a] -> [a]
++ [NonEmpty (Cell NumLines Text)]
totals
    where (NonEmpty (Cell NumLines Text)
header, [NonEmpty (Cell NumLines Text)]
body, [NonEmpty (Cell NumLines Text)]
totals) =
            AmountFormat
-> ReportOpts
-> BalanceReport
-> (NonEmpty (Cell NumLines Text), [NonEmpty (Cell NumLines Text)],
    [NonEmpty (Cell NumLines Text)])
balanceReportAsSpreadsheetParts AmountFormat
fmt ReportOpts
opts BalanceReport
mbr

-- | Render the ODS table rows for a BalanceReport.
-- Returns the heading row, 0 or more body rows, and the totals row if enabled.
balanceReportAsSpreadsheetParts ::
    AmountFormat -> ReportOpts -> BalanceReport ->
    (NonEmpty (Ods.Cell Ods.NumLines Text),
     [NonEmpty (Ods.Cell Ods.NumLines Text)],
     [NonEmpty (Ods.Cell Ods.NumLines Text)])
balanceReportAsSpreadsheetParts :: AmountFormat
-> ReportOpts
-> BalanceReport
-> (NonEmpty (Cell NumLines Text), [NonEmpty (Cell NumLines Text)],
    [NonEmpty (Cell NumLines Text)])
balanceReportAsSpreadsheetParts AmountFormat
fmt ReportOpts
opts ([BalanceReportItem]
items, Change
total) =
    (NonEmpty (Cell NumLines Text)
headers,
     (BalanceReportItem -> [NonEmpty (Cell NumLines Text)])
-> [BalanceReportItem] -> [NonEmpty (Cell NumLines Text)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (RowClass -> BalanceReportItem -> [NonEmpty (Cell NumLines Text)]
rows RowClass
Value) [BalanceReportItem]
items,
      if ReportOpts -> Bool
no_total_ ReportOpts
opts
        then []
        else [NonEmpty (Cell NumLines Text)] -> [NonEmpty (Cell NumLines Text)]
forall (f :: * -> *) border text.
Functor f =>
[f (Cell border text)] -> [f (Cell NumLines text)]
addTotalBorders ([NonEmpty (Cell NumLines Text)]
 -> [NonEmpty (Cell NumLines Text)])
-> [NonEmpty (Cell NumLines Text)]
-> [NonEmpty (Cell NumLines Text)]
forall a b. (a -> b) -> a -> b
$
          RowClass -> BalanceReportItem -> [NonEmpty (Cell NumLines Text)]
rows RowClass
Total (Text
totalRowHeadingSpreadsheet, Text
totalRowHeadingSpreadsheet, Int
0, Change
total))
  where
    cell :: text -> Cell NumLines text
cell = text -> Cell NumLines text
forall border text. Lines border => text -> Cell border text
Ods.defaultCell
    hCell :: Text -> Text -> Cell border Text
hCell Text
cls Text
label = (Text -> Cell border Text
forall borders. Lines borders => Text -> Cell borders Text
headerCell Text
label) {Ods.cellClass = Ods.Class cls}
    headers :: NonEmpty (Cell NumLines Text)
headers =
      NonEmpty (Cell () Text) -> NonEmpty (Cell NumLines Text)
forall (f :: * -> *) text.
Functor f =>
f (Cell () text) -> f (Cell NumLines text)
addHeaderBorders (NonEmpty (Cell () Text) -> NonEmpty (Cell NumLines Text))
-> NonEmpty (Cell () Text) -> NonEmpty (Cell NumLines Text)
forall a b. (a -> b) -> a -> b
$
      Text -> Text -> Cell () Text
forall {border}. Lines border => Text -> Text -> Cell border Text
hCell Text
"account" Text
"account" Cell () Text -> [Cell () Text] -> NonEmpty (Cell () Text)
forall a. a -> [a] -> NonEmpty a
:| case ReportOpts -> Layout
layout_ ReportOpts
opts of
        Layout
LayoutBareWide -> (Text -> Cell () Text) -> [Text] -> [Cell () Text]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> Text -> Cell () Text
forall {border}. Lines border => Text -> Text -> Cell border Text
hCell Text
"amount") [Text]
allCommodities
        Layout
LayoutBare -> [Text -> Cell () Text
forall borders. Lines borders => Text -> Cell borders Text
headerCell Text
"commodity", Text -> Text -> Cell () Text
forall {border}. Lines border => Text -> Text -> Cell border Text
hCell Text
"amount" Text
"balance"]
        Layout
_          -> [Text -> Text -> Cell () Text
forall {border}. Lines border => Text -> Text -> Cell border Text
hCell Text
"amount" Text
"balance"]
    allCommodities :: [Text]
allCommodities =
        Set Text -> [Text]
forall a. Set a -> [a]
S.toAscList (Set Text -> [Text]) -> Set Text -> [Text]
forall a b. (a -> b) -> a -> b
$ (BalanceReportItem -> Set Text) -> [BalanceReportItem] -> Set Text
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (\(Text
_,Text
_,Int
_,Change
ma) -> Change -> Set Text
maCommodities Change
ma) [BalanceReportItem]
items
    shownParents :: Set Text
shownParents = [Text] -> Set Text
shownParentsOf [Text
name | (Text
name, Text
_, Int
_, Change
_) <- [BalanceReportItem]
items]
    -- The report's period, from -b/-e/-p. A date: query term is in the
    -- query string already, and the report intersected the two.
    periodterms :: [Text]
periodterms = if ReportOpts -> Period
period_ ReportOpts
opts Period -> Period -> Bool
forall a. Eq a => a -> a -> Bool
== Period
PeriodAll then [] else Bool -> DateSpan -> [Text]
dateTerm (ReportOpts -> Bool
date2_ ReportOpts
opts) (DateSpan -> [Text]) -> DateSpan -> [Text]
forall a b. (a -> b) -> a -> b
$ Period -> DateSpan
periodAsDateSpan (Period -> DateSpan) -> Period -> DateSpan
forall a b. (a -> b) -> a -> b
$ ReportOpts -> Period
period_ ReportOpts
opts
    linkquery :: [Text]
linkquery = [Text]
periodterms [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ ReportOpts -> [Text]
querystring_ ReportOpts
opts
    rows ::
        RowClass -> BalanceReportItem ->
        [NonEmpty (Ods.Cell Ods.NumLines Text)]
    rows :: RowClass -> BalanceReportItem -> [NonEmpty (Cell NumLines Text)]
rows RowClass
rc (Text
name, Text
dispName, Int
dep, Change
ma) =
      let lo :: LinkOpts
lo = ReportOpts -> [DateSpan] -> Set Text -> Text -> LinkOpts
rowLinkOpts ReportOpts
opts [] Set Text
shownParents Text
name
          accountCell :: Cell NumLines Text
accountCell =
              LinkOpts
-> Maybe Text
-> [Text]
-> Text
-> Cell NumLines Text
-> Cell NumLines Text
forall border text.
LinkOpts
-> Maybe Text
-> [Text]
-> Text
-> Cell border text
-> Cell border text
setAccountAnchorWith LinkOpts
lo
                  (Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (RowClass
rcRowClass -> RowClass -> Bool
forall a. Eq a => a -> a -> Bool
==RowClass
Value) Maybe () -> Maybe Text -> Maybe Text
forall a b. Maybe a -> Maybe b -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ReportOpts -> Maybe Text
balance_base_url_ ReportOpts
opts)
                  [Text]
linkquery Text
name (Cell NumLines Text -> Cell NumLines Text)
-> Cell NumLines Text -> Cell NumLines Text
forall a b. (a -> b) -> a -> b
$
              (\Cell NumLines Text
c -> Cell NumLines Text
c{Ods.cellClass = case rc of RowClass
Value -> ReportOpts -> Int -> Text -> Class
treeAccountClass ReportOpts
opts Int
dep Text
name
                                                  RowClass
Total -> Class
accountClass}) (Cell NumLines Text -> Cell NumLines Text)
-> Cell NumLines Text -> Cell NumLines Text
forall a b. (a -> b) -> a -> b
$
              Text -> Cell NumLines Text
forall {text}. text -> Cell NumLines text
cell (Text -> Cell NumLines Text) -> Text -> Cell NumLines Text
forall a b. (a -> b) -> a -> b
$ case RowClass
rc of
                RowClass
Total -> Text
dispName  -- show the total row heading as is; --drop etc. don't apply (#2688)
                RowClass
Value -> ReportOpts -> Text -> (Text, Text, Int) -> Text
renderBalanceAcct ReportOpts
opts Text
nbsp (Text
name, Text
dispName, Int
dep) in
      -- The figure links where the account name does, or, in the total
      -- row, to the register of everything in the report; a figure that
      -- looks zero has an empty register, and no link.
      let linkFigure :: Cell border text -> Cell border text
linkFigure
            | Bool -> Bool
not (ReportOpts -> Bool
figuresLink ReportOpts
opts) Bool -> Bool -> Bool
|| Change -> Bool
mixedAmountLooksZero Change
ma = Cell border text -> Cell border text
forall a. a -> a
id
            | RowClass
rc RowClass -> RowClass -> Bool
forall a. Eq a => a -> a -> Bool
== RowClass
Value = Text -> Text -> Cell border text -> Cell border text
forall border text.
Text -> Text -> Cell border text -> Cell border text
withLink (Cell NumLines Text -> Text
forall border text. Cell border text -> Text
Ods.cellAnchor Cell NumLines Text
accountCell) (LinkOpts -> Bool -> Text
amountPhrase LinkOpts
lo Bool
False)
            | Bool
otherwise =
                Text -> Text -> Cell border text -> Cell border text
forall border text.
Text -> Text -> Cell border text -> Cell border text
withLink
                    ([(Text, Text)] -> Maybe Text -> [Text] -> Text
composeAnchorWith (LinkOpts -> [(Text, Text)]
linkParams LinkOpts
lo) (ReportOpts -> Maybe Text
balance_base_url_ ReportOpts
opts) ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$
                        [Text]
periodterms [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Text] -> [Text]
removeInacct (ReportOpts -> [Text]
querystring_ ReportOpts
opts))
                    (LinkOpts -> Bool -> Text
amountPhrase LinkOpts
lo Bool
True) in
      Cell NumLines Text
-> [[Cell NumLines Text]] -> [NonEmpty (Cell NumLines Text)]
forall border text.
Cell border text
-> [[Cell border text]] -> [NonEmpty (Cell border text)]
addRowSpanHeaderNE Cell NumLines Text
accountCell ([[Cell NumLines Text]] -> [NonEmpty (Cell NumLines Text)])
-> [[Cell NumLines Text]] -> [NonEmpty (Cell NumLines Text)]
forall a b. (a -> b) -> a -> b
$
      case ReportOpts -> Layout
layout_ ReportOpts
opts of
      Layout
LayoutBareWide ->
          let bopts :: AmountFormat
bopts =
                AmountFormat
machineFmt {
                    displayCommodity = False,
                    displayCommodityOrder = Just allCommodities
                } in
          [((WideBuilder, Amount) -> Cell NumLines Text)
-> [(WideBuilder, Amount)] -> [Cell NumLines Text]
forall a b. (a -> b) -> [a] -> [b]
map (\(WideBuilder, Amount)
bldAmt ->
                (WideBuilder -> Text)
-> Cell NumLines WideBuilder -> Cell NumLines Text
forall a b. (a -> b) -> Cell NumLines a -> Cell NumLines b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap WideBuilder -> Text
wbToText (Cell NumLines WideBuilder -> Cell NumLines Text)
-> Cell NumLines WideBuilder -> Cell NumLines Text
forall a b. (a -> b) -> a -> b
$ AmountFormat
-> (Class, (WideBuilder, Amount)) -> Cell NumLines WideBuilder
forall border wb.
Lines border =>
AmountFormat -> (Class, (wb, Amount)) -> Cell border wb
cellFromAmount AmountFormat
bopts (RowClass -> Class
amountClass RowClass
rc, (WideBuilder, Amount)
bldAmt)) ([(WideBuilder, Amount)] -> [Cell NumLines Text])
-> [(WideBuilder, Amount)] -> [Cell NumLines Text]
forall a b. (a -> b) -> a -> b
$
              AmountFormat -> Change -> [(WideBuilder, Amount)]
showMixedAmountLinesPartsB AmountFormat
bopts Change
ma]
      Layout
LayoutBare ->
          (Amount -> [Cell NumLines Text])
-> [Amount] -> [[Cell NumLines Text]]
forall a b. (a -> b) -> [a] -> [b]
map (\Amount
a -> [Text -> Cell NumLines Text
forall {text}. text -> Cell NumLines text
cell (Text -> Cell NumLines Text) -> Text -> Cell NumLines Text
forall a b. (a -> b) -> a -> b
$ Amount -> Text
acommodity Amount
a, RowClass -> Change -> Cell NumLines Text
forall {border}.
Lines border =>
RowClass -> Change -> Cell border Text
renderAmount RowClass
rc (Change -> Cell NumLines Text) -> Change -> Cell NumLines Text
forall a b. (a -> b) -> a -> b
$ Amount -> Change
mixedAmount Amount
a])
          ([Amount] -> [[Cell NumLines Text]])
-> (Change -> [Amount]) -> Change -> [[Cell NumLines Text]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Change -> [Amount]
amounts (Change -> [[Cell NumLines Text]])
-> Change -> [[Cell NumLines Text]]
forall a b. (a -> b) -> a -> b
$ Change -> Change
mixedAmountStripCosts Change
ma
      Layout
_ -> [[Cell NumLines Text -> Cell NumLines Text
forall {border} {text}. Cell border text -> Cell border text
linkFigure (Cell NumLines Text -> Cell NumLines Text)
-> Cell NumLines Text -> Cell NumLines Text
forall a b. (a -> b) -> a -> b
$ RowClass -> Change -> Cell NumLines Text
forall {border}.
Lines border =>
RowClass -> Change -> Cell border Text
renderAmount RowClass
rc Change
ma]]

    renderAmount :: RowClass -> Change -> Cell border Text
renderAmount RowClass
rc Change
mixedAmt =
        WideBuilder -> Text
wbToText (WideBuilder -> Text)
-> Cell border WideBuilder -> Cell border Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AmountFormat -> (Class, Change) -> Cell border WideBuilder
forall border.
Lines border =>
AmountFormat -> (Class, Change) -> Cell border WideBuilder
cellFromMixedAmount AmountFormat
bopts (RowClass -> Class
amountClass RowClass
rc, Change
mixedAmt)
      where
        bopts :: AmountFormat
bopts = AmountFormat
fmt{displayCommodity=showcomm, displayCommodityOrder = commorder}
        (Bool
showcomm, Maybe [Text]
commorder)
          | ReportOpts -> Layout
layout_ ReportOpts
opts Layout -> Layout -> Bool
forall a. Eq a => a -> a -> Bool
== Layout
LayoutBare = (Bool
False, [Text] -> Maybe [Text]
forall a. a -> Maybe a
Just ([Text] -> Maybe [Text]) -> [Text] -> Maybe [Text]
forall a b. (a -> b) -> a -> b
$ Set Text -> [Text]
forall a. Set a -> [a]
S.toList (Set Text -> [Text]) -> Set Text -> [Text]
forall a b. (a -> b) -> a -> b
$ Change -> Set Text
maCommodities Change
mixedAmt)
          | Bool
otherwise                  = (Bool
True, Maybe [Text]
forall a. Maybe a
Nothing)



-- Multi-column balance reports

-- | Render a multi-column balance report as CSV.
-- The CSV will always include the initial headings row,
-- and will include the final totals row unless --no-total is set.
multiBalanceReportAsCsv :: ReportOpts -> MultiBalanceReport -> CSV
multiBalanceReportAsCsv :: ReportOpts -> MultiBalanceReport -> CSV
multiBalanceReportAsCsv opts :: ReportOpts
opts@ReportOpts{Bool
Int
[Text]
[Status]
SortSpec
Maybe Int
Maybe Text
Maybe NormalSign
Maybe ConversionOp
Maybe ValuationType
Interval
Period
DepthSpec
AccountListMode
BalanceAccumulation
BalanceCalculation
Layout
PeriodTitles
Translations
StringFormat
balancecalc_ :: ReportOpts -> BalanceCalculation
translations_ :: ReportOpts -> Translations
no_total_ :: ReportOpts -> Bool
layout_ :: ReportOpts -> Layout
interval_ :: ReportOpts -> Interval
format_ :: ReportOpts -> StringFormat
color_ :: ReportOpts -> Bool
pretty_ :: ReportOpts -> Bool
accountlistmode_ :: ReportOpts -> AccountListMode
full_names_ :: ReportOpts -> Bool
drop_ :: ReportOpts -> Int
transpose_ :: ReportOpts -> Bool
period_ :: ReportOpts -> Period
date2_ :: ReportOpts -> Bool
querystring_ :: ReportOpts -> [Text]
balance_base_url_ :: ReportOpts -> Maybe Text
period_ :: Period
interval_ :: Interval
statuses_ :: [Status]
conversionop_ :: Maybe ConversionOp
value_ :: Maybe ValuationType
infer_prices_ :: Bool
depth_ :: DepthSpec
fold_depth_ :: Maybe Int
no_elide_ :: Bool
date2_ :: Bool
empty_ :: Bool
full_names_ :: Bool
real_ :: Bool
format_ :: StringFormat
balance_base_url_ :: Maybe Text
pretty_ :: Bool
querystring_ :: [Text]
average_ :: Bool
related_ :: Bool
sortspec_ :: SortSpec
txn_dates_ :: Bool
balancecalc_ :: BalanceCalculation
balanceaccum_ :: BalanceAccumulation
budgetpat_ :: Maybe Text
accountlistmode_ :: AccountListMode
drop_ :: Int
declared_ :: Bool
row_total_ :: Bool
no_total_ :: Bool
summary_only_ :: Bool
show_costs_ :: Bool
sort_amount_ :: Bool
percent_ :: Bool
invert_ :: Bool
normalbalance_ :: Maybe NormalSign
color_ :: Bool
transpose_ :: Bool
layout_ :: Layout
period_titles_ :: PeriodTitles
title_ :: Maybe Text
subreport_titles_ :: Maybe Text
translations_ :: Translations
average_ :: ReportOpts -> Bool
balanceaccum_ :: ReportOpts -> BalanceAccumulation
budgetpat_ :: ReportOpts -> Maybe Text
conversionop_ :: ReportOpts -> Maybe ConversionOp
declared_ :: ReportOpts -> Bool
depth_ :: ReportOpts -> DepthSpec
empty_ :: ReportOpts -> Bool
fold_depth_ :: ReportOpts -> Maybe Int
infer_prices_ :: ReportOpts -> Bool
invert_ :: ReportOpts -> Bool
no_elide_ :: ReportOpts -> Bool
normalbalance_ :: ReportOpts -> Maybe NormalSign
percent_ :: ReportOpts -> Bool
period_titles_ :: ReportOpts -> PeriodTitles
real_ :: ReportOpts -> Bool
related_ :: ReportOpts -> Bool
row_total_ :: ReportOpts -> Bool
show_costs_ :: ReportOpts -> Bool
sort_amount_ :: ReportOpts -> Bool
sortspec_ :: ReportOpts -> SortSpec
statuses_ :: ReportOpts -> [Status]
subreport_titles_ :: ReportOpts -> Maybe Text
summary_only_ :: ReportOpts -> Bool
title_ :: ReportOpts -> Maybe Text
txn_dates_ :: ReportOpts -> Bool
value_ :: ReportOpts -> Maybe ValuationType
..} MultiBalanceReport
report =
    (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
$
    [[Cell NumLines Text]] -> CSV
forall (f :: * -> *) border text.
Functor f =>
[f (Cell border text)] -> [f text]
rawTableContent ([[Cell NumLines Text]] -> CSV) -> [[Cell NumLines Text]] -> CSV
forall a b. (a -> b) -> a -> b
$ [[Cell NumLines Text]]
header [[Cell NumLines Text]]
-> [[Cell NumLines Text]] -> [[Cell NumLines Text]]
forall a. [a] -> [a] -> [a]
++ [[Cell NumLines Text]]
body [[Cell NumLines Text]]
-> [[Cell NumLines Text]] -> [[Cell NumLines Text]]
forall a. [a] -> [a] -> [a]
++ [[Cell NumLines Text]]
totals
  where
    ([[Cell NumLines Text]]
header, [[Cell NumLines Text]]
body, [[Cell NumLines Text]]
totals) =
        AmountFormat
-> ReportOpts
-> MultiBalanceReport
-> ([[Cell NumLines Text]], [[Cell NumLines Text]],
    [[Cell NumLines Text]])
multiBalanceReportAsSpreadsheetParts AmountFormat
machineFmt ReportOpts
opts MultiBalanceReport
report


multiBalanceReportNumHeaderColumns :: Layout -> Int
multiBalanceReportNumHeaderColumns :: Layout -> Int
multiBalanceReportNumHeaderColumns Layout
lay =
    case Layout
lay of
        Layout
LayoutBare -> Int
2
        Layout
LayoutTidy -> Int
0
        Layout
_ -> Int
1

-- | Render the Spreadsheet table rows (CSV, ODS, HTML) for a MultiBalanceReport.
-- Returns the heading rows, 0 or more body rows, and the totals row if enabled.
multiBalanceReportAsSpreadsheetParts ::
    AmountFormat -> ReportOpts -> MultiBalanceReport ->
    ([[Ods.Cell Ods.NumLines Text]],
     [[Ods.Cell Ods.NumLines Text]],
     [[Ods.Cell Ods.NumLines Text]])
multiBalanceReportAsSpreadsheetParts :: AmountFormat
-> ReportOpts
-> MultiBalanceReport
-> ([[Cell NumLines Text]], [[Cell NumLines Text]],
    [[Cell NumLines Text]])
multiBalanceReportAsSpreadsheetParts AmountFormat
fmt ReportOpts
opts MultiBalanceReport
mbr =
  AmountFormat
-> ReportOpts
-> [Text]
-> MultiBalanceReport
-> ([[Cell NumLines Text]], [[Cell NumLines Text]],
    [[Cell NumLines Text]])
balanceSubReportAsSpreadsheetParts AmountFormat
fmt ReportOpts
opts
    ([PeriodicReportRow DisplayName Change] -> [Text]
forall a. [PeriodicReportRow a Change] -> [Text]
allCommoditiesFromPeriodicReport ([PeriodicReportRow DisplayName Change] -> [Text])
-> [PeriodicReportRow DisplayName Change] -> [Text]
forall a b. (a -> b) -> a -> b
$ MultiBalanceReport -> [PeriodicReportRow DisplayName Change]
forall a b. PeriodicReport a b -> [PeriodicReportRow a b]
prRows MultiBalanceReport
mbr) MultiBalanceReport
mbr


-- | Render a multi-column balance report as HTML.
multiBalanceReportAsHtml :: ReportOpts -> MultiBalanceReport -> Html
multiBalanceReportAsHtml :: ReportOpts -> MultiBalanceReport -> Html
multiBalanceReportAsHtml ReportOpts
ropts MultiBalanceReport
mbr =
  Text -> [[Cell NumLines Html]] -> Html
forall border. Lines border => Text -> [[Cell border Html]] -> Html
titledTableHtml (ReportOpts -> MultiBalanceReport -> Text
multiBalanceReportTitle ReportOpts
ropts MultiBalanceReport
mbr) ([[Cell NumLines Html]] -> Html)
-> ([[Cell NumLines Text]] -> [[Cell NumLines Html]])
-> [[Cell NumLines Text]]
-> Html
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Cell NumLines Text] -> [Cell NumLines Html])
-> [[Cell NumLines Text]] -> [[Cell NumLines Html]]
forall a b. (a -> b) -> [a] -> [b]
map ((Cell NumLines Text -> Cell NumLines Html)
-> [Cell NumLines Text] -> [Cell NumLines Html]
forall a b. (a -> b) -> [a] -> [b]
map ((Text -> Html) -> Cell NumLines Text -> Cell NumLines Html
forall a b. (a -> b) -> Cell NumLines a -> Cell NumLines b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Html
forall a. ToMarkup a => a -> Html
toHtml)) ([[Cell NumLines Text]] -> Html) -> [[Cell NumLines Text]] -> Html
forall a b. (a -> b) -> a -> b
$
    ((Int, Int), [[Cell NumLines Text]]) -> [[Cell NumLines Text]]
forall a b. (a, b) -> b
snd (((Int, Int), [[Cell NumLines Text]]) -> [[Cell NumLines Text]])
-> ((Int, Int), [[Cell NumLines Text]]) -> [[Cell NumLines Text]]
forall a b. (a -> b) -> a -> b
$ ReportOpts
-> MultiBalanceReport -> ((Int, Int), [[Cell NumLines Text]])
multiBalanceReportAsSpreadsheet ReportOpts
ropts MultiBalanceReport
mbr

-- | Render the ODS table rows for a MultiBalanceReport.
-- Returns the heading row, 0 or more body rows, and the totals row if enabled.
multiBalanceReportAsSpreadsheet ::
  ReportOpts -> MultiBalanceReport ->
  ((Int, Int), [[Ods.Cell Ods.NumLines Text]])
multiBalanceReportAsSpreadsheet :: ReportOpts
-> MultiBalanceReport -> ((Int, Int), [[Cell NumLines Text]])
multiBalanceReportAsSpreadsheet ReportOpts
ropts MultiBalanceReport
mbr =
  let ([[Cell NumLines Text]]
header,[[Cell NumLines Text]]
body,[[Cell NumLines Text]]
total) =
            AmountFormat
-> ReportOpts
-> MultiBalanceReport
-> ([[Cell NumLines Text]], [[Cell NumLines Text]],
    [[Cell NumLines Text]])
multiBalanceReportAsSpreadsheetParts AmountFormat
oneLineNoCostFmt ReportOpts
ropts MultiBalanceReport
mbr
  in  (if ReportOpts -> Bool
transpose_ ReportOpts
ropts then (Int, Int) -> (Int, Int)
forall a b. (a, b) -> (b, a)
swap ((Int, Int) -> (Int, Int))
-> ([[Cell NumLines Text]] -> [[Cell NumLines Text]])
-> ((Int, Int), [[Cell NumLines Text]])
-> ((Int, Int), [[Cell NumLines Text]])
forall b c b' c'. (b -> c) -> (b' -> c') -> (b, b') -> (c, c')
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** [[Cell NumLines Text]] -> [[Cell NumLines Text]]
forall border text. [[Cell border text]] -> [[Cell border text]]
Ods.transpose else ((Int, Int), [[Cell NumLines Text]])
-> ((Int, Int), [[Cell NumLines Text]])
forall a. a -> a
id) (((Int, Int), [[Cell NumLines Text]])
 -> ((Int, Int), [[Cell NumLines Text]]))
-> ((Int, Int), [[Cell NumLines Text]])
-> ((Int, Int), [[Cell NumLines Text]])
forall a b. (a -> b) -> a -> b
$
      (([[Cell NumLines Text]] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[Cell NumLines Text]]
header, Layout -> Int
multiBalanceReportNumHeaderColumns (ReportOpts -> Layout
layout_ ReportOpts
ropts)),
       [[Cell NumLines Text]]
header [[Cell NumLines Text]]
-> [[Cell NumLines Text]] -> [[Cell NumLines Text]]
forall a. [a] -> [a] -> [a]
++ [[Cell NumLines Text]]
body [[Cell NumLines Text]]
-> [[Cell NumLines Text]] -> [[Cell NumLines Text]]
forall a. [a] -> [a] -> [a]
++ [[Cell NumLines Text]]
total)


-- | Render a report title as a text block to prefix to a text report,
-- or nothing if the title is empty.
titleAsTextBuilder :: Text -> TB.Builder
titleAsTextBuilder :: Text -> Builder
titleAsTextBuilder Text
title
  | Text -> Bool
T.null Text
title = Builder
forall a. Monoid a => a
mempty
  | Bool
otherwise    = Text -> Builder
TB.fromText Text
title Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Text -> Builder
TB.fromText Text
"\n\n"

-- | Render a multi-column balance report as plain text suitable for console output.
multiBalanceReportAsText :: ReportOpts -> MultiBalanceReport -> TL.Text
multiBalanceReportAsText :: ReportOpts -> MultiBalanceReport -> Text
multiBalanceReportAsText ReportOpts
ropts MultiBalanceReport
r = Builder -> Text
TB.toLazyText (Builder -> Text) -> Builder -> Text
forall a b. (a -> b) -> a -> b
$
    Text -> Builder
titleAsTextBuilder (ReportOpts -> MultiBalanceReport -> Text
multiBalanceReportTitle ReportOpts
ropts MultiBalanceReport
r)
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ReportOpts -> Table Text Text WideBuilder -> Builder
multiBalanceReportTableAsText ReportOpts
ropts (ReportOpts -> MultiBalanceReport -> Table Text Text WideBuilder
multiBalanceReportAsTable ReportOpts
ropts MultiBalanceReport
r)

-- | The heading for a multi-column balance report: a description of the
-- report, or --title's value if that was provided (possibly empty).
multiBalanceReportTitle :: ReportOpts -> MultiBalanceReport -> Text
multiBalanceReportTitle :: ReportOpts -> MultiBalanceReport -> Text
multiBalanceReportTitle ropts :: ReportOpts
ropts@ReportOpts{Bool
Int
[Text]
[Status]
SortSpec
Maybe Int
Maybe Text
Maybe NormalSign
Maybe ConversionOp
Maybe ValuationType
Interval
Period
DepthSpec
AccountListMode
BalanceAccumulation
BalanceCalculation
Layout
PeriodTitles
Translations
StringFormat
balancecalc_ :: ReportOpts -> BalanceCalculation
translations_ :: ReportOpts -> Translations
no_total_ :: ReportOpts -> Bool
layout_ :: ReportOpts -> Layout
interval_ :: ReportOpts -> Interval
format_ :: ReportOpts -> StringFormat
color_ :: ReportOpts -> Bool
pretty_ :: ReportOpts -> Bool
accountlistmode_ :: ReportOpts -> AccountListMode
full_names_ :: ReportOpts -> Bool
drop_ :: ReportOpts -> Int
transpose_ :: ReportOpts -> Bool
period_ :: ReportOpts -> Period
date2_ :: ReportOpts -> Bool
querystring_ :: ReportOpts -> [Text]
balance_base_url_ :: ReportOpts -> Maybe Text
average_ :: ReportOpts -> Bool
balanceaccum_ :: ReportOpts -> BalanceAccumulation
budgetpat_ :: ReportOpts -> Maybe Text
conversionop_ :: ReportOpts -> Maybe ConversionOp
declared_ :: ReportOpts -> Bool
depth_ :: ReportOpts -> DepthSpec
empty_ :: ReportOpts -> Bool
fold_depth_ :: ReportOpts -> Maybe Int
infer_prices_ :: ReportOpts -> Bool
invert_ :: ReportOpts -> Bool
no_elide_ :: ReportOpts -> Bool
normalbalance_ :: ReportOpts -> Maybe NormalSign
percent_ :: ReportOpts -> Bool
period_titles_ :: ReportOpts -> PeriodTitles
real_ :: ReportOpts -> Bool
related_ :: ReportOpts -> Bool
row_total_ :: ReportOpts -> Bool
show_costs_ :: ReportOpts -> Bool
sort_amount_ :: ReportOpts -> Bool
sortspec_ :: ReportOpts -> SortSpec
statuses_ :: ReportOpts -> [Status]
subreport_titles_ :: ReportOpts -> Maybe Text
summary_only_ :: ReportOpts -> Bool
title_ :: ReportOpts -> Maybe Text
txn_dates_ :: ReportOpts -> Bool
value_ :: ReportOpts -> Maybe ValuationType
period_ :: Period
interval_ :: Interval
statuses_ :: [Status]
conversionop_ :: Maybe ConversionOp
value_ :: Maybe ValuationType
infer_prices_ :: Bool
depth_ :: DepthSpec
fold_depth_ :: Maybe Int
no_elide_ :: Bool
date2_ :: Bool
empty_ :: Bool
full_names_ :: Bool
real_ :: Bool
format_ :: StringFormat
balance_base_url_ :: Maybe Text
pretty_ :: Bool
querystring_ :: [Text]
average_ :: Bool
related_ :: Bool
sortspec_ :: SortSpec
txn_dates_ :: Bool
balancecalc_ :: BalanceCalculation
balanceaccum_ :: BalanceAccumulation
budgetpat_ :: Maybe Text
accountlistmode_ :: AccountListMode
drop_ :: Int
declared_ :: Bool
row_total_ :: Bool
no_total_ :: Bool
summary_only_ :: Bool
show_costs_ :: Bool
sort_amount_ :: Bool
percent_ :: Bool
invert_ :: Bool
normalbalance_ :: Maybe NormalSign
color_ :: Bool
transpose_ :: Bool
layout_ :: Layout
period_titles_ :: PeriodTitles
title_ :: Maybe Text
subreport_titles_ :: Maybe Text
translations_ :: Translations
..} MultiBalanceReport
r = ReportOpts -> Text -> Text
effectiveTitle ReportOpts
ropts Text
defaultTitle
  where
    tr :: Text -> Text
tr  = Translations -> Text -> Text
I18n.tr  Translations
translations_
    trf :: Text -> [(Text, Text)] -> Text
trf = Translations -> Text -> [(Text, Text)] -> Text
I18n.trf Translations
translations_
    -- TRANSLATORS: the multi-period balance report title, eg "Balance changes in 2024, valued at period ends:".
    defaultTitle :: Text
defaultTitle = Text -> [(Text, Text)] -> Text
trf Text
"{report} in {dates}{valuation}:"
      [ (Text
"report", Text
mtitle), (Text
"dates", DateSpan -> Text
showDateSpan (MultiBalanceReport -> DateSpan
forall a b. PeriodicReport a b -> DateSpan
periodicReportSpan MultiBalanceReport
r)), (Text
"valuation", Text
valuationdesc) ]

    mtitle :: Text
mtitle = case (BalanceCalculation
balancecalc_, BalanceAccumulation
balanceaccum_) of
        (BalanceCalculation
CalcValueChange, BalanceAccumulation
PerPeriod  ) -> Text -> Text
tr Text
"Period-end value changes"
        (BalanceCalculation
CalcValueChange, BalanceAccumulation
Cumulative ) -> Text -> Text
tr Text
"Cumulative period-end value changes"
        (BalanceCalculation
CalcGain,        BalanceAccumulation
PerPeriod  ) -> Text -> Text
tr Text
"Incremental gain"
        (BalanceCalculation
CalcGain,        BalanceAccumulation
Cumulative ) -> Text -> Text
tr Text
"Cumulative gain"
        (BalanceCalculation
CalcGain,        BalanceAccumulation
Historical ) -> Text -> Text
tr Text
"Historical gain"
        (BalanceCalculation
CalcPostingsCount, BalanceAccumulation
PerPeriod  ) -> Text -> Text
tr Text
"Posting counts"
        (BalanceCalculation
CalcPostingsCount, BalanceAccumulation
Cumulative ) -> Text -> Text
tr Text
"Cumulative posting counts"
        (BalanceCalculation
CalcPostingsCount, BalanceAccumulation
Historical ) -> Text -> Text
tr Text
"Historical posting counts"
        (BalanceCalculation
CalcTransactionsCount, BalanceAccumulation
_      ) -> Text -> Text
tr Text
"Transaction counts"
        (BalanceCalculation
_,               BalanceAccumulation
PerPeriod  ) -> Text -> Text
tr Text
"Balance changes"
        (BalanceCalculation
_,               BalanceAccumulation
Cumulative ) -> Text -> Text
tr Text
"Ending balances (cumulative)"
        (BalanceCalculation
_,               BalanceAccumulation
Historical)  -> Text -> Text
tr Text
"Ending balances (historical)"
    valuationdesc :: Text
valuationdesc =
        (case Maybe ConversionOp
conversionop_ of
            Just ConversionOp
ToCost -> Text -> Text
tr Text
", converted to cost"
            Maybe ConversionOp
_           -> Text
"")
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (case Maybe ValuationType
value_ of
            Just (AtThen Maybe Text
_mc)    -> Text -> Text
tr Text
", valued at posting date"
            Just (AtEnd Maybe Text
_mc) | Bool
changingValuation -> Text
""
            Just (AtEnd Maybe Text
_mc)     -> Text -> Text
tr Text
", valued at period ends"
            Just (AtNow Maybe Text
_mc)     -> Text -> Text
tr Text
", current value"
            Just (AtDate Day
d Maybe Text
_mc)  -> Text -> [(Text, Text)] -> Text
trf Text
", valued at {date}" [(Text
"date", Day -> Text
showDate Day
d)]
            Maybe ValuationType
Nothing              -> Text
"")

    changingValuation :: Bool
changingValuation = case (BalanceCalculation
balancecalc_, BalanceAccumulation
balanceaccum_) of
        (BalanceCalculation
CalcValueChange, BalanceAccumulation
PerPeriod)  -> Bool
True
        (BalanceCalculation
CalcValueChange, BalanceAccumulation
Cumulative) -> Bool
True
        (BalanceCalculation, BalanceAccumulation)
_                                     -> Bool
False

-- | Given a table representing a multi-column balance report,
-- render it in a format suitable for console output.
-- Amounts with more than two commodities will be elided unless --no-elide is used.
multiBalanceReportTableAsText :: ReportOpts -> Table T.Text T.Text WideBuilder -> TB.Builder
multiBalanceReportTableAsText :: ReportOpts -> Table Text Text WideBuilder -> Builder
multiBalanceReportTableAsText ReportOpts{Bool
Int
[Text]
[Status]
SortSpec
Maybe Int
Maybe Text
Maybe NormalSign
Maybe ConversionOp
Maybe ValuationType
Interval
Period
DepthSpec
AccountListMode
BalanceAccumulation
BalanceCalculation
Layout
PeriodTitles
Translations
StringFormat
balancecalc_ :: ReportOpts -> BalanceCalculation
translations_ :: ReportOpts -> Translations
no_total_ :: ReportOpts -> Bool
layout_ :: ReportOpts -> Layout
interval_ :: ReportOpts -> Interval
format_ :: ReportOpts -> StringFormat
color_ :: ReportOpts -> Bool
pretty_ :: ReportOpts -> Bool
accountlistmode_ :: ReportOpts -> AccountListMode
full_names_ :: ReportOpts -> Bool
drop_ :: ReportOpts -> Int
transpose_ :: ReportOpts -> Bool
period_ :: ReportOpts -> Period
date2_ :: ReportOpts -> Bool
querystring_ :: ReportOpts -> [Text]
balance_base_url_ :: ReportOpts -> Maybe Text
average_ :: ReportOpts -> Bool
balanceaccum_ :: ReportOpts -> BalanceAccumulation
budgetpat_ :: ReportOpts -> Maybe Text
conversionop_ :: ReportOpts -> Maybe ConversionOp
declared_ :: ReportOpts -> Bool
depth_ :: ReportOpts -> DepthSpec
empty_ :: ReportOpts -> Bool
fold_depth_ :: ReportOpts -> Maybe Int
infer_prices_ :: ReportOpts -> Bool
invert_ :: ReportOpts -> Bool
no_elide_ :: ReportOpts -> Bool
normalbalance_ :: ReportOpts -> Maybe NormalSign
percent_ :: ReportOpts -> Bool
period_titles_ :: ReportOpts -> PeriodTitles
real_ :: ReportOpts -> Bool
related_ :: ReportOpts -> Bool
row_total_ :: ReportOpts -> Bool
show_costs_ :: ReportOpts -> Bool
sort_amount_ :: ReportOpts -> Bool
sortspec_ :: ReportOpts -> SortSpec
statuses_ :: ReportOpts -> [Status]
subreport_titles_ :: ReportOpts -> Maybe Text
summary_only_ :: ReportOpts -> Bool
title_ :: ReportOpts -> Maybe Text
txn_dates_ :: ReportOpts -> Bool
value_ :: ReportOpts -> Maybe ValuationType
period_ :: Period
interval_ :: Interval
statuses_ :: [Status]
conversionop_ :: Maybe ConversionOp
value_ :: Maybe ValuationType
infer_prices_ :: Bool
depth_ :: DepthSpec
fold_depth_ :: Maybe Int
no_elide_ :: Bool
date2_ :: Bool
empty_ :: Bool
full_names_ :: Bool
real_ :: Bool
format_ :: StringFormat
balance_base_url_ :: Maybe Text
pretty_ :: Bool
querystring_ :: [Text]
average_ :: Bool
related_ :: Bool
sortspec_ :: SortSpec
txn_dates_ :: Bool
balancecalc_ :: BalanceCalculation
balanceaccum_ :: BalanceAccumulation
budgetpat_ :: Maybe Text
accountlistmode_ :: AccountListMode
drop_ :: Int
declared_ :: Bool
row_total_ :: Bool
no_total_ :: Bool
summary_only_ :: Bool
show_costs_ :: Bool
sort_amount_ :: Bool
percent_ :: Bool
invert_ :: Bool
normalbalance_ :: Maybe NormalSign
color_ :: Bool
transpose_ :: Bool
layout_ :: Layout
period_titles_ :: PeriodTitles
title_ :: Maybe Text
subreport_titles_ :: Maybe Text
translations_ :: Translations
..} = TableOpts
-> ([Text] -> [Cell])
-> ((Text, [WideBuilder]) -> (Cell, [Cell]))
-> Table Text Text WideBuilder
-> Builder
forall a ch rh.
Show a =>
TableOpts
-> ([ch] -> [Cell])
-> ((rh, [a]) -> (Cell, [Cell]))
-> Table rh ch a
-> Builder
renderTableByRowsB TableOpts
tableopts [Text] -> [Cell]
renderCh (Text, [WideBuilder]) -> (Cell, [Cell])
renderRow
  where
    tableopts :: TableOpts
tableopts = TableOpts
forall a. Default a => a
def{tableBorders=multiColumnTableOuterBorder, prettyTable=pretty_}
    multiColumnTableOuterBorder :: Bool
multiColumnTableOuterBorder = Bool
pretty_

    renderCh :: [Text] -> [Cell]
    renderCh :: [Text] -> [Cell]
renderCh
      | Layout
layout_ Layout -> Layout -> Bool
forall a. Eq a => a -> a -> Bool
/= Layout
LayoutBare Bool -> Bool -> Bool
|| Bool
transpose_ = (Text -> Cell) -> [Text] -> [Cell]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Align -> Text -> Cell
textCell Align
TopRight)
      | Bool
otherwise = ((Text -> Cell) -> Text -> Cell)
-> [Text -> Cell] -> [Text] -> [Cell]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (Text -> Cell) -> Text -> Cell
forall a b. (a -> b) -> a -> b
($) (Align -> Text -> Cell
textCell Align
TopLeft (Text -> Cell) -> [Text -> Cell] -> [Text -> Cell]
forall a. a -> [a] -> [a]
: (Text -> Cell) -> [Text -> Cell]
forall a. a -> [a]
repeat (Align -> Text -> Cell
textCell Align
TopRight))

    renderRow :: (Text, [WideBuilder]) -> (Cell, [Cell])
    renderRow :: (Text, [WideBuilder]) -> (Cell, [Cell])
renderRow (Text
rh, [WideBuilder]
row)
      | Layout
layout_ Layout -> Layout -> Bool
forall a. Eq a => a -> a -> Bool
/= Layout
LayoutBare Bool -> Bool -> Bool
|| Bool
transpose_ =
          (Align -> Text -> Cell
textCell Align
TopLeft Text
rh, (WideBuilder -> Cell) -> [WideBuilder] -> [Cell]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Align -> [WideBuilder] -> Cell
Cell Align
TopRight ([WideBuilder] -> Cell)
-> (WideBuilder -> [WideBuilder]) -> WideBuilder -> Cell
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WideBuilder -> [WideBuilder]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure) [WideBuilder]
row)
      | Bool
otherwise =
          (Align -> Text -> Cell
textCell Align
TopLeft Text
rh, (([WideBuilder] -> Cell) -> [WideBuilder] -> Cell)
-> [[WideBuilder] -> Cell] -> [[WideBuilder]] -> [Cell]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith ([WideBuilder] -> Cell) -> [WideBuilder] -> Cell
forall a b. (a -> b) -> a -> b
($) (Align -> [WideBuilder] -> Cell
Cell Align
TopLeft ([WideBuilder] -> Cell)
-> [[WideBuilder] -> Cell] -> [[WideBuilder] -> Cell]
forall a. a -> [a] -> [a]
: ([WideBuilder] -> Cell) -> [[WideBuilder] -> Cell]
forall a. a -> [a]
repeat (Align -> [WideBuilder] -> Cell
Cell Align
TopRight)) ((WideBuilder -> [WideBuilder]) -> [WideBuilder] -> [[WideBuilder]]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap WideBuilder -> [WideBuilder]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [WideBuilder]
row))

-- | Build a 'Table' from a multi-column balance report.
multiBalanceReportAsTable :: ReportOpts -> MultiBalanceReport -> Table T.Text T.Text WideBuilder
multiBalanceReportAsTable :: ReportOpts -> MultiBalanceReport -> Table Text Text WideBuilder
multiBalanceReportAsTable ReportOpts
opts report :: MultiBalanceReport
report@(PeriodicReport [DateSpan]
_spans [PeriodicReportRow DisplayName Change]
items PeriodicReportRow () Change
_tr) =
    ReportOpts
-> [Text] -> MultiBalanceReport -> Table Text Text WideBuilder
multiBalanceReportAsPartTable ReportOpts
opts
        ([PeriodicReportRow DisplayName Change] -> [Text]
forall a. [PeriodicReportRow a Change] -> [Text]
allCommoditiesFromPeriodicReport [PeriodicReportRow DisplayName Change]
items)
        MultiBalanceReport
report


multiBalanceRowAsCsvText ::
    ReportOpts -> [DateSpan] -> [CommoditySymbol] ->
    PeriodicReportRow a MixedAmount -> [[T.Text]]
multiBalanceRowAsCsvText :: forall a.
ReportOpts
-> [DateSpan] -> [Text] -> PeriodicReportRow a Change -> CSV
multiBalanceRowAsCsvText ReportOpts
opts [DateSpan]
colspans [Text]
allCommodities =
    ([Cell NumLines WideBuilder] -> [Text])
-> [[Cell NumLines WideBuilder]] -> CSV
forall a b. (a -> b) -> [a] -> [b]
map ((Cell NumLines WideBuilder -> Text)
-> [Cell NumLines WideBuilder] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (WideBuilder -> Text
wbToText (WideBuilder -> Text)
-> (Cell NumLines WideBuilder -> WideBuilder)
-> Cell NumLines WideBuilder
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Cell NumLines WideBuilder -> WideBuilder
forall border text. Cell border text -> text
Ods.cellContent)) ([[Cell NumLines WideBuilder]] -> CSV)
-> (PeriodicReportRow a Change -> [[Cell NumLines WideBuilder]])
-> PeriodicReportRow a Change
-> CSV
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    AmountFormat
-> ReportOpts
-> [DateSpan]
-> [Text]
-> RowClass
-> (DateSpan -> Cell NumLines Text)
-> PeriodicReportRow a Change
-> [[Cell NumLines WideBuilder]]
forall a.
AmountFormat
-> ReportOpts
-> [DateSpan]
-> [Text]
-> RowClass
-> (DateSpan -> Cell NumLines Text)
-> PeriodicReportRow a Change
-> [[Cell NumLines WideBuilder]]
multiBalanceRowAsCellBuilders AmountFormat
machineFmt ReportOpts
opts [DateSpan]
colspans [Text]
allCommodities
        RowClass
Value (PeriodTitles -> DateSpan -> Cell NumLines Text
simpleDateSpanCell (PeriodTitles -> DateSpan -> Cell NumLines Text)
-> PeriodTitles -> DateSpan -> Cell NumLines Text
forall a b. (a -> b) -> a -> b
$ ReportOpts -> PeriodTitles
period_titles_ ReportOpts
opts)


-- Budget reports

-- A BudgetCell's data values rendered for display - the actual change amount,
-- the budget goal amount if any, and the corresponding goal percentage if possible.
type BudgetDisplayCell = (WideBuilder, Maybe (WideBuilder, Maybe WideBuilder))

-- | A row of rendered budget data cells.
type BudgetDisplayRow  = [BudgetDisplayCell]

-- | An amount render helper for the budget report. Renders each commodity separately.
type BudgetShowAmountsFn   = MixedAmount -> [WideBuilder]

-- | A goal percentage calculating helper for the budget report.
type BudgetCalcPercentagesFn  = Change -> BudgetGoal -> [Maybe Percentage]

-- | Render a budget report as plain text suitable for console output.
budgetReportAsText :: ReportOpts -> BudgetReport -> TL.Text
budgetReportAsText :: ReportOpts -> BudgetReport -> Text
budgetReportAsText ReportOpts
ropts BudgetReport
budgetr = Builder -> Text
TB.toLazyText (Builder -> Text) -> Builder -> Text
forall a b. (a -> b) -> a -> b
$
    Text -> Builder
titleAsTextBuilder (ReportOpts -> BudgetReport -> Text
budgetReportTitle ReportOpts
ropts BudgetReport
budgetr) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
      ReportOpts -> Table Text Text WideBuilder -> Builder
multiBalanceReportTableAsText ReportOpts
ropts (ReportOpts -> BudgetReport -> Table Text Text WideBuilder
budgetReportAsTable ReportOpts
ropts BudgetReport
budgetr)

-- | The heading for a budget report: a description of the report,
-- or --title's value if that was provided (possibly empty).
budgetReportTitle :: ReportOpts -> BudgetReport -> Text
budgetReportTitle :: ReportOpts -> BudgetReport -> Text
budgetReportTitle ropts :: ReportOpts
ropts@ReportOpts{Bool
Int
[Text]
[Status]
SortSpec
Maybe Int
Maybe Text
Maybe NormalSign
Maybe ConversionOp
Maybe ValuationType
Interval
Period
DepthSpec
AccountListMode
BalanceAccumulation
BalanceCalculation
Layout
PeriodTitles
Translations
StringFormat
balancecalc_ :: ReportOpts -> BalanceCalculation
translations_ :: ReportOpts -> Translations
no_total_ :: ReportOpts -> Bool
layout_ :: ReportOpts -> Layout
interval_ :: ReportOpts -> Interval
format_ :: ReportOpts -> StringFormat
color_ :: ReportOpts -> Bool
pretty_ :: ReportOpts -> Bool
accountlistmode_ :: ReportOpts -> AccountListMode
full_names_ :: ReportOpts -> Bool
drop_ :: ReportOpts -> Int
transpose_ :: ReportOpts -> Bool
period_ :: ReportOpts -> Period
date2_ :: ReportOpts -> Bool
querystring_ :: ReportOpts -> [Text]
balance_base_url_ :: ReportOpts -> Maybe Text
average_ :: ReportOpts -> Bool
balanceaccum_ :: ReportOpts -> BalanceAccumulation
budgetpat_ :: ReportOpts -> Maybe Text
conversionop_ :: ReportOpts -> Maybe ConversionOp
declared_ :: ReportOpts -> Bool
depth_ :: ReportOpts -> DepthSpec
empty_ :: ReportOpts -> Bool
fold_depth_ :: ReportOpts -> Maybe Int
infer_prices_ :: ReportOpts -> Bool
invert_ :: ReportOpts -> Bool
no_elide_ :: ReportOpts -> Bool
normalbalance_ :: ReportOpts -> Maybe NormalSign
percent_ :: ReportOpts -> Bool
period_titles_ :: ReportOpts -> PeriodTitles
real_ :: ReportOpts -> Bool
related_ :: ReportOpts -> Bool
row_total_ :: ReportOpts -> Bool
show_costs_ :: ReportOpts -> Bool
sort_amount_ :: ReportOpts -> Bool
sortspec_ :: ReportOpts -> SortSpec
statuses_ :: ReportOpts -> [Status]
subreport_titles_ :: ReportOpts -> Maybe Text
summary_only_ :: ReportOpts -> Bool
title_ :: ReportOpts -> Maybe Text
txn_dates_ :: ReportOpts -> Bool
value_ :: ReportOpts -> Maybe ValuationType
period_ :: Period
interval_ :: Interval
statuses_ :: [Status]
conversionop_ :: Maybe ConversionOp
value_ :: Maybe ValuationType
infer_prices_ :: Bool
depth_ :: DepthSpec
fold_depth_ :: Maybe Int
no_elide_ :: Bool
date2_ :: Bool
empty_ :: Bool
full_names_ :: Bool
real_ :: Bool
format_ :: StringFormat
balance_base_url_ :: Maybe Text
pretty_ :: Bool
querystring_ :: [Text]
average_ :: Bool
related_ :: Bool
sortspec_ :: SortSpec
txn_dates_ :: Bool
balancecalc_ :: BalanceCalculation
balanceaccum_ :: BalanceAccumulation
budgetpat_ :: Maybe Text
accountlistmode_ :: AccountListMode
drop_ :: Int
declared_ :: Bool
row_total_ :: Bool
no_total_ :: Bool
summary_only_ :: Bool
show_costs_ :: Bool
sort_amount_ :: Bool
percent_ :: Bool
invert_ :: Bool
normalbalance_ :: Maybe NormalSign
color_ :: Bool
transpose_ :: Bool
layout_ :: Layout
period_titles_ :: PeriodTitles
title_ :: Maybe Text
subreport_titles_ :: Maybe Text
translations_ :: Translations
..} BudgetReport
budgetr = ReportOpts -> Text -> Text
effectiveTitle ReportOpts
ropts Text
defaultTitle
  where
    tr :: Text -> Text
tr  = Translations -> Text -> Text
I18n.tr  Translations
translations_
    trf :: Text -> [(Text, Text)] -> Text
trf = Translations -> Text -> [(Text, Text)] -> Text
I18n.trf Translations
translations_
    -- TRANSLATORS: the budget report title, eg "Budget performance in 2024, valued at period ends:".
    defaultTitle :: Text
defaultTitle = Text -> [(Text, Text)] -> Text
trf Text
"Budget performance in {dates}{valuation}:"
      [ (Text
"dates", DateSpan -> Text
showDateSpan (BudgetReport -> DateSpan
forall a b. PeriodicReport a b -> DateSpan
periodicReportSpan BudgetReport
budgetr)), (Text
"valuation", Text
valuationdesc) ]
    valuationdesc :: Text
valuationdesc =
              (case Maybe ConversionOp
conversionop_ of
                 Just ConversionOp
ToCost -> Text -> Text
tr Text
", converted to cost"
                 Maybe ConversionOp
_           -> Text
"")
           Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (case Maybe ValuationType
value_ of
                 Just (AtThen Maybe Text
_mc)   -> Text -> Text
tr Text
", valued at posting date"
                 Just (AtEnd Maybe Text
_mc)    -> Text -> Text
tr Text
", valued at period ends"
                 Just (AtNow Maybe Text
_mc)    -> Text -> Text
tr Text
", current value"
                 Just (AtDate Day
d Maybe Text
_mc) -> Text -> [(Text, Text)] -> Text
trf Text
", valued at {date}" [(Text
"date", Day -> Text
showDate Day
d)]
                 Maybe ValuationType
Nothing             -> Text
"")

-- | Build a 'Table' from a multi-column balance report.
budgetReportAsTable :: ReportOpts -> BudgetReport -> Table Text Text WideBuilder
budgetReportAsTable :: ReportOpts -> BudgetReport -> Table Text Text WideBuilder
budgetReportAsTable ropts :: ReportOpts
ropts@ReportOpts{Bool
Int
[Text]
[Status]
SortSpec
Maybe Int
Maybe Text
Maybe NormalSign
Maybe ConversionOp
Maybe ValuationType
Interval
Period
DepthSpec
AccountListMode
BalanceAccumulation
BalanceCalculation
Layout
PeriodTitles
Translations
StringFormat
balancecalc_ :: ReportOpts -> BalanceCalculation
translations_ :: ReportOpts -> Translations
no_total_ :: ReportOpts -> Bool
layout_ :: ReportOpts -> Layout
interval_ :: ReportOpts -> Interval
format_ :: ReportOpts -> StringFormat
color_ :: ReportOpts -> Bool
pretty_ :: ReportOpts -> Bool
accountlistmode_ :: ReportOpts -> AccountListMode
full_names_ :: ReportOpts -> Bool
drop_ :: ReportOpts -> Int
transpose_ :: ReportOpts -> Bool
period_ :: ReportOpts -> Period
date2_ :: ReportOpts -> Bool
querystring_ :: ReportOpts -> [Text]
balance_base_url_ :: ReportOpts -> Maybe Text
average_ :: ReportOpts -> Bool
balanceaccum_ :: ReportOpts -> BalanceAccumulation
budgetpat_ :: ReportOpts -> Maybe Text
conversionop_ :: ReportOpts -> Maybe ConversionOp
declared_ :: ReportOpts -> Bool
depth_ :: ReportOpts -> DepthSpec
empty_ :: ReportOpts -> Bool
fold_depth_ :: ReportOpts -> Maybe Int
infer_prices_ :: ReportOpts -> Bool
invert_ :: ReportOpts -> Bool
no_elide_ :: ReportOpts -> Bool
normalbalance_ :: ReportOpts -> Maybe NormalSign
percent_ :: ReportOpts -> Bool
period_titles_ :: ReportOpts -> PeriodTitles
real_ :: ReportOpts -> Bool
related_ :: ReportOpts -> Bool
row_total_ :: ReportOpts -> Bool
show_costs_ :: ReportOpts -> Bool
sort_amount_ :: ReportOpts -> Bool
sortspec_ :: ReportOpts -> SortSpec
statuses_ :: ReportOpts -> [Status]
subreport_titles_ :: ReportOpts -> Maybe Text
summary_only_ :: ReportOpts -> Bool
title_ :: ReportOpts -> Maybe Text
txn_dates_ :: ReportOpts -> Bool
value_ :: ReportOpts -> Maybe ValuationType
period_ :: Period
interval_ :: Interval
statuses_ :: [Status]
conversionop_ :: Maybe ConversionOp
value_ :: Maybe ValuationType
infer_prices_ :: Bool
depth_ :: DepthSpec
fold_depth_ :: Maybe Int
no_elide_ :: Bool
date2_ :: Bool
empty_ :: Bool
full_names_ :: Bool
real_ :: Bool
format_ :: StringFormat
balance_base_url_ :: Maybe Text
pretty_ :: Bool
querystring_ :: [Text]
average_ :: Bool
related_ :: Bool
sortspec_ :: SortSpec
txn_dates_ :: Bool
balancecalc_ :: BalanceCalculation
balanceaccum_ :: BalanceAccumulation
budgetpat_ :: Maybe Text
accountlistmode_ :: AccountListMode
drop_ :: Int
declared_ :: Bool
row_total_ :: Bool
no_total_ :: Bool
summary_only_ :: Bool
show_costs_ :: Bool
sort_amount_ :: Bool
percent_ :: Bool
invert_ :: Bool
normalbalance_ :: Maybe NormalSign
color_ :: Bool
transpose_ :: Bool
layout_ :: Layout
period_titles_ :: PeriodTitles
title_ :: Maybe Text
subreport_titles_ :: Maybe Text
translations_ :: Translations
..} (PeriodicReport [DateSpan]
spans [PeriodicReportRow DisplayName (Maybe Change, Maybe Change)]
items PeriodicReportRow () (Maybe Change, Maybe Change)
totrow) =
  (case Layout
layout_ of
      Layout
LayoutTall -> Table Text Text WideBuilder -> Table Text Text WideBuilder
forall a. a -> a
id
      LayoutWide Maybe Int
_ -> Table Text Text WideBuilder -> Table Text Text WideBuilder
forall a. a -> a
id
      Layout
LayoutBare -> Table Text Text WideBuilder -> Table Text Text WideBuilder
forall a. a -> a
id
      Layout
_ -> Layout
-> Table Text Text WideBuilder -> Table Text Text WideBuilder
forall a. Layout -> a -> a
unsupportedLayout Layout
layout_) (Table Text Text WideBuilder -> Table Text Text WideBuilder)
-> Table Text Text WideBuilder -> Table Text Text WideBuilder
forall a b. (a -> b) -> a -> b
$
  Table Text Text WideBuilder -> Table Text Text WideBuilder
forall {rh} {a}. Table rh rh a -> Table rh rh a
maybetransposetable (Table Text Text WideBuilder -> Table Text Text WideBuilder)
-> Table Text Text WideBuilder -> Table Text Text WideBuilder
forall a b. (a -> b) -> a -> b
$
  Table Text Text WideBuilder -> Table Text Text WideBuilder
forall {ch}. Table Text ch WideBuilder -> Table Text ch WideBuilder
addtotalrow (Table Text Text WideBuilder -> Table Text Text WideBuilder)
-> Table Text Text WideBuilder -> Table Text Text WideBuilder
forall a b. (a -> b) -> a -> b
$
    Header Text
-> Header Text -> [[WideBuilder]] -> Table Text Text WideBuilder
forall rh ch a. Header rh -> Header ch -> [[a]] -> Table rh ch a
Table
      (Properties -> [Header Text] -> Header Text
forall h. Properties -> [Header h] -> Header h
Group Properties
budgetTableInterRowBorder    ([Header Text] -> Header Text) -> [Header Text] -> Header Text
forall a b. (a -> b) -> a -> b
$ (Text -> Header Text) -> [Text] -> [Header Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Header Text
forall h. h -> Header h
Header [Text]
accts)
      (Properties -> [Header Text] -> Header Text
forall h. Properties -> [Header h] -> Header h
Group Properties
budgetTableInterColumnBorder ([Header Text] -> Header Text) -> [Header Text] -> Header Text
forall a b. (a -> b) -> a -> b
$ (Text -> Header Text) -> [Text] -> [Header Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Header Text
forall h. h -> Header h
Header [Text]
colheadings)
      [[WideBuilder]]
rows
  where
    trc :: Text -> Text -> Text
trc = Translations -> Text -> Text -> Text
I18n.trc Translations
translations_
    budgetTableInterRowBorder :: Properties
budgetTableInterRowBorder    = Properties
NoLine
    budgetTableInterColumnBorder :: Properties
budgetTableInterColumnBorder = if Bool
pretty_ then Properties
SingleLine else Properties
NoLine

    maybetransposetable :: Table rh rh a -> Table rh rh a
maybetransposetable
      | 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

    addtotalrow :: Table Text ch WideBuilder -> Table Text ch WideBuilder
addtotalrow
      | Bool
no_total_ = Table Text ch WideBuilder -> Table Text ch WideBuilder
forall a. a -> a
id
      | Bool
otherwise =
        let
          rowhdrs :: Header Text
rowhdrs = Properties -> [Header Text] -> Header Text
forall h. Properties -> [Header h] -> Header h
Group Properties
NoLine ([Header Text] -> Header Text) -> [Header Text] -> Header Text
forall a b. (a -> b) -> a -> b
$ (Text -> Header Text) -> [Text] -> [Header Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Header Text
forall h. h -> Header h
Header ([Text] -> [Header Text]) -> [Text] -> [Header Text]
forall a b. (a -> b) -> a -> b
$ Text
totalRowHeadingBudgetText Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: Int -> Text -> [Text]
forall a. Int -> a -> [a]
replicate ([[WideBuilder]] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[WideBuilder]]
totalrows Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Text
""
          colhdrs :: Header [a]
colhdrs = [a] -> Header [a]
forall h. h -> Header h
Header [] -- ignored by concatTables
        in
          ((Table Text ch WideBuilder
 -> Table Text [ZonkAny 0] WideBuilder -> Table Text ch WideBuilder)
-> Table Text [ZonkAny 0] WideBuilder
-> Table Text ch WideBuilder
-> Table Text ch WideBuilder
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Properties
-> Table Text ch WideBuilder
-> Table Text [ZonkAny 0] WideBuilder
-> Table Text ch WideBuilder
forall rh ch a ch2.
Properties -> Table rh ch a -> Table rh ch2 a -> Table rh ch a
concatTables Properties
SingleLine) (Table Text [ZonkAny 0] WideBuilder
 -> Table Text ch WideBuilder -> Table Text ch WideBuilder)
-> Table Text [ZonkAny 0] WideBuilder
-> Table Text ch WideBuilder
-> Table Text ch WideBuilder
forall a b. (a -> b) -> a -> b
$ Header Text
-> Header [ZonkAny 0]
-> [[WideBuilder]]
-> Table Text [ZonkAny 0] WideBuilder
forall rh ch a. Header rh -> Header ch -> [[a]] -> Table rh ch a
Table Header Text
rowhdrs Header [ZonkAny 0]
forall {a}. Header [a]
colhdrs [[WideBuilder]]
totalrows)  -- XXX ?

    colheadings :: [Text]
colheadings = [Text -> Text -> Text
trc Text
"column heading" Text
"Commodity" | Layout
layout_ Layout -> Layout -> Bool
forall a. Eq a => a -> a -> Bool
== Layout
LayoutBare]
                  [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ (if Bool -> Bool
not Bool
summary_only_ then (DateSpan -> Text) -> [DateSpan] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (ReportOpts -> [DateSpan] -> DateSpan -> Text
reportPeriodName ReportOpts
ropts [DateSpan]
spans) [DateSpan]
spans else [])
                  [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Text
"  " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text -> Text
trc Text
"column heading" Text
"Total" | Bool
row_total_]
                  [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Text -> Text -> Text
trc Text
"column heading" Text
"Average" | Bool
average_]

    ([Text]
accts, [[WideBuilder]]
rows, [[WideBuilder]]
totalrows) =
      ([Text]
accts'
      ,[WideBuilder] -> [[WideBuilder]] -> [[WideBuilder]]
maybecommcol [WideBuilder]
itemscs  ([[WideBuilder]] -> [[WideBuilder]])
-> [[WideBuilder]] -> [[WideBuilder]]
forall a b. (a -> b) -> a -> b
$ [[BudgetDisplayCell]] -> [[WideBuilder]]
showcells  [[BudgetDisplayCell]]
texts
      ,[WideBuilder] -> [[WideBuilder]] -> [[WideBuilder]]
maybecommcol [WideBuilder]
totrowcs ([[WideBuilder]] -> [[WideBuilder]])
-> [[WideBuilder]] -> [[WideBuilder]]
forall a b. (a -> b) -> a -> b
$ [[BudgetDisplayCell]] -> [[WideBuilder]]
showtotrow [[BudgetDisplayCell]]
totrowtexts)
      where
        -- If --layout=bare, prepend a commodities column.
        maybecommcol :: [WideBuilder] -> [[WideBuilder]] -> [[WideBuilder]]
        maybecommcol :: [WideBuilder] -> [[WideBuilder]] -> [[WideBuilder]]
maybecommcol [WideBuilder]
cs
          | Layout
layout_ Layout -> Layout -> Bool
forall a. Eq a => a -> a -> Bool
== Layout
LayoutBare = (WideBuilder -> [WideBuilder] -> [WideBuilder])
-> [WideBuilder] -> [[WideBuilder]] -> [[WideBuilder]]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (:) [WideBuilder]
cs
          | Bool
otherwise             = [[WideBuilder]] -> [[WideBuilder]]
forall a. a -> a
id

        showcells, showtotrow :: [[BudgetDisplayCell]] -> [[WideBuilder]]
        ([[BudgetDisplayCell]] -> [[WideBuilder]]
showcells, [[BudgetDisplayCell]] -> [[WideBuilder]]
showtotrow) =
          ([[WideBuilder]] -> [[WideBuilder]]
forall a. [[a]] -> [[a]]
maybetranspose ([[WideBuilder]] -> [[WideBuilder]])
-> ([[BudgetDisplayCell]] -> [[WideBuilder]])
-> [[BudgetDisplayCell]]
-> [[WideBuilder]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([BudgetDisplayCell] -> [WideBuilder])
-> [[BudgetDisplayCell]] -> [[WideBuilder]]
forall a b. (a -> b) -> [a] -> [b]
map (((Int, Int, Int) -> BudgetDisplayCell -> WideBuilder)
-> [(Int, Int, Int)] -> [BudgetDisplayCell] -> [WideBuilder]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (Int, Int, Int) -> BudgetDisplayCell -> WideBuilder
showBudgetDisplayCell [(Int, Int, Int)]
widths)       ([[BudgetDisplayCell]] -> [[WideBuilder]])
-> ([[BudgetDisplayCell]] -> [[BudgetDisplayCell]])
-> [[BudgetDisplayCell]]
-> [[WideBuilder]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[BudgetDisplayCell]] -> [[BudgetDisplayCell]]
forall a. [[a]] -> [[a]]
maybetranspose
          ,[[WideBuilder]] -> [[WideBuilder]]
forall a. [[a]] -> [[a]]
maybetranspose ([[WideBuilder]] -> [[WideBuilder]])
-> ([[BudgetDisplayCell]] -> [[WideBuilder]])
-> [[BudgetDisplayCell]]
-> [[WideBuilder]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([BudgetDisplayCell] -> [WideBuilder])
-> [[BudgetDisplayCell]] -> [[WideBuilder]]
forall a b. (a -> b) -> [a] -> [b]
map (((Int, Int, Int) -> BudgetDisplayCell -> WideBuilder)
-> [(Int, Int, Int)] -> [BudgetDisplayCell] -> [WideBuilder]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (Int, Int, Int) -> BudgetDisplayCell -> WideBuilder
showBudgetDisplayCell [(Int, Int, Int)]
totrowwidths) ([[BudgetDisplayCell]] -> [[WideBuilder]])
-> ([[BudgetDisplayCell]] -> [[BudgetDisplayCell]])
-> [[BudgetDisplayCell]]
-> [[WideBuilder]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[BudgetDisplayCell]] -> [[BudgetDisplayCell]]
forall a. [[a]] -> [[a]]
maybetranspose)
          where
            -- | Combine a BudgetDisplayCell's rendered values into a "[PERCENT of GOAL]" rendering,
            -- respecting the given widths.
            showBudgetDisplayCell :: (Int, Int, Int) -> BudgetDisplayCell -> WideBuilder
            showBudgetDisplayCell :: (Int, Int, Int) -> BudgetDisplayCell -> WideBuilder
showBudgetDisplayCell (Int
actualwidth, Int
budgetwidth, Int
percentwidth) (WideBuilder
actual, Maybe (WideBuilder, Maybe WideBuilder)
mbudget) =
              (Builder -> Int -> WideBuilder) -> Int -> Builder -> WideBuilder
forall a b c. (a -> b -> c) -> b -> a -> c
flip Builder -> Int -> WideBuilder
WideBuilder (Int
actualwidth Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
totalbudgetwidth) (Builder -> WideBuilder) -> Builder -> WideBuilder
forall a b. (a -> b) -> a -> b
$
                WideBuilder -> Builder
toPadded WideBuilder
actual Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
-> ((WideBuilder, Maybe WideBuilder) -> Builder)
-> Maybe (WideBuilder, Maybe WideBuilder)
-> Builder
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Builder
emptycell (WideBuilder, Maybe WideBuilder) -> Builder
showBudgetGoalAndPercentage Maybe (WideBuilder, Maybe WideBuilder)
mbudget

              where
                toPadded :: WideBuilder -> Builder
toPadded (WideBuilder Builder
b Int
w) = (Text -> Builder
TB.fromText (Text -> Builder) -> (Int -> Text) -> Int -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Text -> Text) -> Text -> Int -> Text
forall a b c. (a -> b -> c) -> b -> a -> c
flip Int -> Text -> Text
T.replicate Text
" " (Int -> Builder) -> Int -> Builder
forall a b. (a -> b) -> a -> b
$ Int
actualwidth Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
w) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
b

                (Int
totalpercentwidth, Int
totalbudgetwidth) =
                  let 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
                   in ( Int
totalpercentwidth'
                      , 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
                      )

                emptycell :: TB.Builder
                emptycell :: Builder
emptycell = Text -> Builder
TB.fromText (Text -> Builder) -> Text -> Builder
forall a b. (a -> b) -> a -> b
$ Int -> Text -> Text
T.replicate Int
totalbudgetwidth Text
" "

                showBudgetGoalAndPercentage :: (WideBuilder, Maybe WideBuilder) -> TB.Builder
                showBudgetGoalAndPercentage :: (WideBuilder, Maybe WideBuilder) -> Builder
showBudgetGoalAndPercentage (WideBuilder
goal, Maybe WideBuilder
perc) =
                  let perct :: Text
perct = case Maybe WideBuilder
perc of
                        Maybe WideBuilder
Nothing  -> Int -> Text -> Text
T.replicate Int
totalpercentwidth Text
" "
                        Just WideBuilder
pct -> Int -> Text -> Text
T.replicate (Int
percentwidth Int -> Int -> Int
forall a. Num a => a -> a -> a
- WideBuilder -> Int
wbWidth WideBuilder
pct) Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> WideBuilder -> Text
wbToText WideBuilder
pct Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"% of "
                   in Text -> Builder
TB.fromText (Text -> Builder) -> Text -> Builder
forall a b. (a -> b) -> a -> b
$ Text
" [" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
perct Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text -> Text
T.replicate (Int
budgetwidth Int -> Int -> Int
forall a. Num a => a -> a -> a
- WideBuilder -> Int
wbWidth WideBuilder
goal) Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> WideBuilder -> Text
wbToText WideBuilder
goal Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"]"

            -- | Build a list of widths for each column.
            -- When --transpose is used, the totals row must be included in this list.
            widths :: [(Int, Int, Int)]
            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
              where
                actualwidths :: [Int]
actualwidths  = ([(Int, Int, Int)] -> Int) -> [[(Int, Int, Int)]] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ([Int] -> Int
forall a. Integral a => [a] -> a
maximum' ([Int] -> Int)
-> ([(Int, Int, Int)] -> [Int]) -> [(Int, Int, Int)] -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int, Int, Int) -> Int) -> [(Int, Int, Int)] -> [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]) -> [[(Int, Int, Int)]] -> [Int]
forall a b. (a -> b) -> a -> b
$ [[(Int, Int, Int)]]
cols
                budgetwidths :: [Int]
budgetwidths  = ([(Int, Int, Int)] -> Int) -> [[(Int, Int, Int)]] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ([Int] -> Int
forall a. Integral a => [a] -> a
maximum' ([Int] -> Int)
-> ([(Int, Int, Int)] -> [Int]) -> [(Int, Int, Int)] -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int, Int, Int) -> Int) -> [(Int, Int, Int)] -> [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]) -> [[(Int, Int, Int)]] -> [Int]
forall a b. (a -> b) -> a -> b
$ [[(Int, Int, Int)]]
cols
                percentwidths :: [Int]
percentwidths = ([(Int, Int, Int)] -> Int) -> [[(Int, Int, Int)]] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ([Int] -> Int
forall a. Integral a => [a] -> a
maximum' ([Int] -> Int)
-> ([(Int, Int, Int)] -> [Int]) -> [(Int, Int, Int)] -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int, Int, Int) -> Int) -> [(Int, Int, Int)] -> [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]) -> [[(Int, Int, Int)]] -> [Int]
forall a b. (a -> b) -> a -> b
$ [[(Int, Int, Int)]]
cols
                catcolumnwidths :: [[[a]]] -> [[a]]
catcolumnwidths = ([[a]] -> [[a]] -> [[a]]) -> [[a]] -> [[[a]]] -> [[a]]
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (([a] -> [a] -> [a]) -> [[a]] -> [[a]] -> [[a]]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
(++)) ([[a]] -> [[[a]]] -> [[a]]) -> [[a]] -> [[[a]]] -> [[a]]
forall a b. (a -> b) -> a -> b
$ [a] -> [[a]]
forall a. a -> [a]
repeat []
                cols :: [[(Int, Int, Int)]]
cols = [[(Int, Int, Int)]] -> [[(Int, Int, Int)]]
forall a. [[a]] -> [[a]]
maybetranspose ([[(Int, Int, Int)]] -> [[(Int, Int, Int)]])
-> [[(Int, Int, Int)]] -> [[(Int, Int, Int)]]
forall a b. (a -> b) -> a -> b
$ [[[(Int, Int, Int)]]] -> [[(Int, Int, Int)]]
forall {a}. [[[a]]] -> [[a]]
catcolumnwidths ([[[(Int, Int, Int)]]] -> [[(Int, Int, Int)]])
-> [[[(Int, Int, Int)]]] -> [[(Int, Int, Int)]]
forall a b. (a -> b) -> a -> b
$ (PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
 -> [[(Int, Int, Int)]])
-> [PeriodicReportRow DisplayName (Maybe Change, Maybe Change)]
-> [[[(Int, Int, Int)]]]
forall a b. (a -> b) -> [a] -> [b]
map ([(Maybe Change, Maybe Change)] -> [[(Int, Int, Int)]]
cellswidth ([(Maybe Change, Maybe Change)] -> [[(Int, Int, Int)]])
-> (PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
    -> [(Maybe Change, Maybe Change)])
-> PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
-> [[(Int, Int, Int)]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
-> [(Maybe Change, Maybe Change)]
forall a.
PeriodicReportRow a (Maybe Change, Maybe Change)
-> [(Maybe Change, Maybe Change)]
rowToBudgetCells) [PeriodicReportRow DisplayName (Maybe Change, Maybe Change)]
items [[[(Int, Int, Int)]]]
-> [[[(Int, Int, Int)]]] -> [[[(Int, Int, Int)]]]
forall a. [a] -> [a] -> [a]
++ [[(Maybe Change, Maybe Change)] -> [[(Int, Int, Int)]]
cellswidth ([(Maybe Change, Maybe Change)] -> [[(Int, Int, Int)]])
-> [(Maybe Change, Maybe Change)] -> [[(Int, Int, Int)]]
forall a b. (a -> b) -> a -> b
$ PeriodicReportRow () (Maybe Change, Maybe Change)
-> [(Maybe Change, Maybe Change)]
forall a.
PeriodicReportRow a (Maybe Change, Maybe Change)
-> [(Maybe Change, Maybe Change)]
rowToBudgetCells PeriodicReportRow () (Maybe Change, Maybe Change)
totrow]

                cellswidth :: [BudgetCell] -> [[(Int, Int, Int)]]
                cellswidth :: [(Maybe Change, Maybe Change)] -> [[(Int, Int, Int)]]
cellswidth [(Maybe Change, Maybe Change)]
row =
                  let cs :: [Text]
cs = [(Maybe Change, Maybe Change)] -> [Text]
budgetCellsCommodities [(Maybe Change, Maybe Change)]
row
                      (Change -> [WideBuilder]
showmixed, BudgetCalcPercentagesFn
percbudget) = [Text] -> (Change -> [WideBuilder], BudgetCalcPercentagesFn)
mkBudgetDisplayFns [Text]
cs
                      disp :: (Maybe Change, Maybe Change) -> [BudgetDisplayCell]
disp = (Change -> [WideBuilder])
-> BudgetCalcPercentagesFn
-> (Maybe Change, Maybe Change)
-> [BudgetDisplayCell]
showcell Change -> [WideBuilder]
showmixed BudgetCalcPercentagesFn
percbudget
                      budgetpercwidth :: (WideBuilder, Maybe WideBuilder) -> (Int, Int)
budgetpercwidth = WideBuilder -> Int
wbWidth (WideBuilder -> Int)
-> (Maybe WideBuilder -> Int)
-> (WideBuilder, Maybe WideBuilder)
-> (Int, Int)
forall b c b' c'. (b -> c) -> (b' -> c') -> (b, b') -> (c, c')
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** Int -> (WideBuilder -> Int) -> Maybe WideBuilder -> Int
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int
0 WideBuilder -> Int
wbWidth
                      cellwidth :: BudgetDisplayCell -> (Int, Int, Int)
cellwidth (WideBuilder
am, Maybe (WideBuilder, Maybe WideBuilder)
bm) = let (Int
bw, Int
pw) = (Int, Int)
-> ((WideBuilder, Maybe WideBuilder) -> (Int, Int))
-> Maybe (WideBuilder, Maybe WideBuilder)
-> (Int, Int)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Int
0, Int
0) (WideBuilder, Maybe WideBuilder) -> (Int, Int)
budgetpercwidth Maybe (WideBuilder, Maybe WideBuilder)
bm in (WideBuilder -> Int
wbWidth WideBuilder
am, Int
bw, Int
pw)
                   in ((Maybe Change, Maybe Change) -> [(Int, Int, Int)])
-> [(Maybe Change, Maybe Change)] -> [[(Int, Int, Int)]]
forall a b. (a -> b) -> [a] -> [b]
map ((BudgetDisplayCell -> (Int, Int, Int))
-> [BudgetDisplayCell] -> [(Int, Int, Int)]
forall a b. (a -> b) -> [a] -> [b]
map BudgetDisplayCell -> (Int, Int, Int)
cellwidth ([BudgetDisplayCell] -> [(Int, Int, Int)])
-> ((Maybe Change, Maybe Change) -> [BudgetDisplayCell])
-> (Maybe Change, Maybe Change)
-> [(Int, Int, Int)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe Change, Maybe Change) -> [BudgetDisplayCell]
disp) [(Maybe Change, Maybe Change)]
row

            totrowwidths :: [(Int, Int, Int)]
            totrowwidths :: [(Int, Int, Int)]
totrowwidths
              | Bool
transpose_ = Int -> [(Int, Int, Int)] -> [(Int, Int, Int)]
forall a. Int -> [a] -> [a]
drop ([[BudgetDisplayCell]] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[BudgetDisplayCell]]
texts) [(Int, Int, Int)]
widths
              | Bool
otherwise = [(Int, Int, Int)]
widths

            maybetranspose :: [[a]] -> [[a]]
maybetranspose
              | Bool
transpose_ = [[a]] -> [[a]]
forall a. [[a]] -> [[a]]
transpose
              | Bool
otherwise  = [[a]] -> [[a]]
forall a. a -> a
id

        ([Text]
accts', [WideBuilder]
itemscs, [[BudgetDisplayCell]]
texts) = [(Text, WideBuilder, [BudgetDisplayCell])]
-> ([Text], [WideBuilder], [[BudgetDisplayCell]])
forall a b c. [(a, b, c)] -> ([a], [b], [c])
unzip3 ([(Text, WideBuilder, [BudgetDisplayCell])]
 -> ([Text], [WideBuilder], [[BudgetDisplayCell]]))
-> [(Text, WideBuilder, [BudgetDisplayCell])]
-> ([Text], [WideBuilder], [[BudgetDisplayCell]])
forall a b. (a -> b) -> a -> b
$ [[(Text, WideBuilder, [BudgetDisplayCell])]]
-> [(Text, WideBuilder, [BudgetDisplayCell])]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[(Text, WideBuilder, [BudgetDisplayCell])]]
shownitems
          where
            shownitems :: [[(AccountName, WideBuilder, BudgetDisplayRow)]]
            shownitems :: [[(Text, WideBuilder, [BudgetDisplayCell])]]
shownitems =
              (PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
 -> [(Text, WideBuilder, [BudgetDisplayCell])])
-> [PeriodicReportRow DisplayName (Maybe Change, Maybe Change)]
-> [[(Text, WideBuilder, [BudgetDisplayCell])]]
forall a b. (a -> b) -> [a] -> [b]
map (\PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
i ->
                let
                  addacctcolumn :: [(b, c)] -> [(Text, b, c)]
addacctcolumn = ((b, c) -> (Text, b, c)) -> [(b, c)] -> [(Text, b, c)]
forall a b. (a -> b) -> [a] -> [b]
map (\(b
cs, c
cvals) -> (ReportOpts
-> Text
-> PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
-> Text
forall a.
ReportOpts -> Text -> PeriodicReportRow DisplayName a -> Text
renderPeriodicAcct ReportOpts
ropts Text
" " PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
i, b
cs, c
cvals))
                  isunbudgetedrow :: Bool
isunbudgetedrow = DisplayName -> Text
displayFull (PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
-> DisplayName
forall a b. PeriodicReportRow a b -> a
prrName PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
i) Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
unbudgetedAccountName
                in [(WideBuilder, [BudgetDisplayCell])]
-> [(Text, WideBuilder, [BudgetDisplayCell])]
forall {b} {c}. [(b, c)] -> [(Text, b, c)]
addacctcolumn ([(WideBuilder, [BudgetDisplayCell])]
 -> [(Text, WideBuilder, [BudgetDisplayCell])])
-> [(WideBuilder, [BudgetDisplayCell])]
-> [(Text, WideBuilder, [BudgetDisplayCell])]
forall a b. (a -> b) -> a -> b
$ Bool
-> [(Maybe Change, Maybe Change)]
-> [(WideBuilder, [BudgetDisplayCell])]
showrow Bool
isunbudgetedrow ([(Maybe Change, Maybe Change)]
 -> [(WideBuilder, [BudgetDisplayCell])])
-> [(Maybe Change, Maybe Change)]
-> [(WideBuilder, [BudgetDisplayCell])]
forall a b. (a -> b) -> a -> b
$ PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
-> [(Maybe Change, Maybe Change)]
forall a.
PeriodicReportRow a (Maybe Change, Maybe Change)
-> [(Maybe Change, Maybe Change)]
rowToBudgetCells PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
i)
              [PeriodicReportRow DisplayName (Maybe Change, Maybe Change)]
items

        ([WideBuilder]
totrowcs, [[BudgetDisplayCell]]
totrowtexts)  = [(WideBuilder, [BudgetDisplayCell])]
-> ([WideBuilder], [[BudgetDisplayCell]])
forall a b. [(a, b)] -> ([a], [b])
unzip  ([(WideBuilder, [BudgetDisplayCell])]
 -> ([WideBuilder], [[BudgetDisplayCell]]))
-> [(WideBuilder, [BudgetDisplayCell])]
-> ([WideBuilder], [[BudgetDisplayCell]])
forall a b. (a -> b) -> a -> b
$ [[(WideBuilder, [BudgetDisplayCell])]]
-> [(WideBuilder, [BudgetDisplayCell])]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[(WideBuilder, [BudgetDisplayCell])]]
showntotrow
          where
            showntotrow :: [[(WideBuilder, BudgetDisplayRow)]]
            showntotrow :: [[(WideBuilder, [BudgetDisplayCell])]]
showntotrow = [Bool
-> [(Maybe Change, Maybe Change)]
-> [(WideBuilder, [BudgetDisplayCell])]
showrow Bool
False ([(Maybe Change, Maybe Change)]
 -> [(WideBuilder, [BudgetDisplayCell])])
-> [(Maybe Change, Maybe Change)]
-> [(WideBuilder, [BudgetDisplayCell])]
forall a b. (a -> b) -> a -> b
$ PeriodicReportRow () (Maybe Change, Maybe Change)
-> [(Maybe Change, Maybe Change)]
forall a.
PeriodicReportRow a (Maybe Change, Maybe Change)
-> [(Maybe Change, Maybe Change)]
rowToBudgetCells PeriodicReportRow () (Maybe Change, Maybe Change)
totrow]

        -- | Get the data cells from a row or totals row, maybe adding
        -- the row total and/or row average depending on options.
        rowToBudgetCells :: PeriodicReportRow a BudgetCell -> [BudgetCell]
        rowToBudgetCells :: forall a.
PeriodicReportRow a (Maybe Change, Maybe Change)
-> [(Maybe Change, Maybe Change)]
rowToBudgetCells (PeriodicReportRow a
_ [(Maybe Change, Maybe Change)]
as (Maybe Change, Maybe Change)
rowtot (Maybe Change, Maybe Change)
rowavg) =
            (if Bool -> Bool
not Bool
summary_only_ then [(Maybe Change, Maybe Change)]
as else [])
            [(Maybe Change, Maybe Change)]
-> [(Maybe Change, Maybe Change)] -> [(Maybe Change, Maybe Change)]
forall a. [a] -> [a] -> [a]
++ [(Maybe Change, Maybe Change)
rowtot | Bool
row_total_ Bool -> Bool -> Bool
&& Bool -> Bool
not ([(Maybe Change, Maybe Change)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Maybe Change, Maybe Change)]
as)]
            [(Maybe Change, Maybe Change)]
-> [(Maybe Change, Maybe Change)] -> [(Maybe Change, Maybe Change)]
forall a. [a] -> [a] -> [a]
++ [(Maybe Change, Maybe Change)
rowavg | Bool
average_   Bool -> Bool -> Bool
&& Bool -> Bool
not ([(Maybe Change, Maybe Change)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Maybe Change, Maybe Change)]
as)]

        -- | Render a row's data cells as "BudgetDisplayCell"s, and a rendered list of commodity symbols.
        -- Also requires a flag indicating whether this is the special <unbudgeted> row.
        -- (The types make that hard to check here.)
        showrow :: Bool -> [BudgetCell] -> [(WideBuilder, BudgetDisplayRow)]
        showrow :: Bool
-> [(Maybe Change, Maybe Change)]
-> [(WideBuilder, [BudgetDisplayCell])]
showrow Bool
isunbudgetedrow [(Maybe Change, Maybe Change)]
cells =
          let
            cs :: [Text]
cs = [(Maybe Change, Maybe Change)] -> [Text]
budgetCellsCommodities [(Maybe Change, Maybe Change)]
cells
            -- #2071 If there are no commodities - because there are no actual or goal amounts -
            -- the zipped list would be empty, causing this row not to be shown.
            -- But rows like this sometimes need to be shown to preserve the account tree structure.
            -- So, ensure 0 will be shown as actual amount(s).
            -- Unfortunately this disables boring parent eliding, as if --no-elide had been used.
            -- (Just turning on --no-elide higher up doesn't work right.)
            -- Note, no goal amount will be shown for these rows,
            -- whereas --no-elide is likely to show a goal amount aggregated from children.
            cs1 :: [Text]
cs1 = if [Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Text]
cs Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
isunbudgetedrow then [Text
""] else [Text]
cs
            (Change -> [WideBuilder]
showmixed, BudgetCalcPercentagesFn
percbudget) = [Text] -> (Change -> [WideBuilder], BudgetCalcPercentagesFn)
mkBudgetDisplayFns [Text]
cs1
          in
            [WideBuilder]
-> [[BudgetDisplayCell]] -> [(WideBuilder, [BudgetDisplayCell])]
forall a b. [a] -> [b] -> [(a, b)]
zip ((Text -> WideBuilder) -> [Text] -> [WideBuilder]
forall a b. (a -> b) -> [a] -> [b]
map Text -> WideBuilder
wbFromText [Text]
cs1) ([[BudgetDisplayCell]] -> [(WideBuilder, [BudgetDisplayCell])])
-> [[BudgetDisplayCell]] -> [(WideBuilder, [BudgetDisplayCell])]
forall a b. (a -> b) -> a -> b
$
            [[BudgetDisplayCell]] -> [[BudgetDisplayCell]]
forall a. [[a]] -> [[a]]
transpose ([[BudgetDisplayCell]] -> [[BudgetDisplayCell]])
-> [[BudgetDisplayCell]] -> [[BudgetDisplayCell]]
forall a b. (a -> b) -> a -> b
$
            ((Maybe Change, Maybe Change) -> [BudgetDisplayCell])
-> [(Maybe Change, Maybe Change)] -> [[BudgetDisplayCell]]
forall a b. (a -> b) -> [a] -> [b]
map ((Change -> [WideBuilder])
-> BudgetCalcPercentagesFn
-> (Maybe Change, Maybe Change)
-> [BudgetDisplayCell]
showcell Change -> [WideBuilder]
showmixed BudgetCalcPercentagesFn
percbudget)
            [(Maybe Change, Maybe Change)]
cells

        budgetCellsCommodities :: [BudgetCell] -> [CommoditySymbol]
        budgetCellsCommodities :: [(Maybe Change, Maybe Change)] -> [Text]
budgetCellsCommodities = Set Text -> [Text]
forall a. Set a -> [a]
S.toList (Set Text -> [Text])
-> ([(Maybe Change, Maybe Change)] -> Set Text)
-> [(Maybe Change, Maybe Change)]
-> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Set Text -> Set Text -> Set Text)
-> Set Text -> [Set Text] -> Set Text
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Set Text -> Set Text -> Set Text
forall a. Ord a => Set a -> Set a -> Set a
S.union Set Text
forall a. Monoid a => a
mempty ([Set Text] -> Set Text)
-> ([(Maybe Change, Maybe Change)] -> [Set Text])
-> [(Maybe Change, Maybe Change)]
-> Set Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Maybe Change, Maybe Change) -> Set Text)
-> [(Maybe Change, Maybe Change)] -> [Set Text]
forall a b. (a -> b) -> [a] -> [b]
map (Maybe Change, Maybe Change) -> Set Text
budgetCellCommodities
          where
            budgetCellCommodities :: BudgetCell -> S.Set CommoditySymbol
            budgetCellCommodities :: (Maybe Change, Maybe Change) -> Set Text
budgetCellCommodities (Maybe Change
am, Maybe Change
bm) = Maybe Change -> Set Text
f Maybe Change
am Set Text -> Set Text -> Set Text
forall a. Ord a => Set a -> Set a -> Set a
`S.union` Maybe Change -> Set Text
f Maybe Change
bm
              where f :: Maybe Change -> Set Text
f = Set Text -> (Change -> Set Text) -> Maybe Change -> Set Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Set Text
forall a. Monoid a => a
mempty Change -> Set Text
maCommodities

        -- | Render a "BudgetCell"'s amounts as "BudgetDisplayCell"s (one per commodity).
        showcell :: BudgetShowAmountsFn -> BudgetCalcPercentagesFn -> BudgetCell -> BudgetDisplayRow
        showcell :: (Change -> [WideBuilder])
-> BudgetCalcPercentagesFn
-> (Maybe Change, Maybe Change)
-> [BudgetDisplayCell]
showcell Change -> [WideBuilder]
showCommodityAmounts BudgetCalcPercentagesFn
calcCommodityPercentages (Maybe Change
mactual, Maybe Change
mbudget) =
          [WideBuilder]
-> [Maybe (WideBuilder, Maybe WideBuilder)] -> [BudgetDisplayCell]
forall a b. [a] -> [b] -> [(a, b)]
zip [WideBuilder]
actualamts [Maybe (WideBuilder, Maybe WideBuilder)]
budgetinfos
          where
            actual :: Change
actual = Change -> Maybe Change -> Change
forall a. a -> Maybe a -> a
fromMaybe Change
nullmixedamt Maybe Change
mactual
            actualamts :: [WideBuilder]
actualamts = Change -> [WideBuilder]
showCommodityAmounts Change
actual
            budgetinfos :: [Maybe (WideBuilder, Maybe WideBuilder)]
budgetinfos =
              case Maybe Change
mbudget of
                Maybe Change
Nothing   -> Maybe (WideBuilder, Maybe WideBuilder)
-> [Maybe (WideBuilder, Maybe WideBuilder)]
forall a. a -> [a]
repeat Maybe (WideBuilder, Maybe WideBuilder)
forall a. Maybe a
Nothing
                Just Change
goal -> ((WideBuilder, Maybe WideBuilder)
 -> Maybe (WideBuilder, Maybe WideBuilder))
-> [(WideBuilder, Maybe WideBuilder)]
-> [Maybe (WideBuilder, Maybe WideBuilder)]
forall a b. (a -> b) -> [a] -> [b]
map (WideBuilder, Maybe WideBuilder)
-> Maybe (WideBuilder, Maybe WideBuilder)
forall a. a -> Maybe a
Just ([(WideBuilder, Maybe WideBuilder)]
 -> [Maybe (WideBuilder, Maybe WideBuilder)])
-> [(WideBuilder, Maybe WideBuilder)]
-> [Maybe (WideBuilder, Maybe WideBuilder)]
forall a b. (a -> b) -> a -> b
$ Change -> [(WideBuilder, Maybe WideBuilder)]
showGoalAmountsAndPercentages Change
goal
                where
                  showGoalAmountsAndPercentages :: MixedAmount -> [(WideBuilder, Maybe WideBuilder)]
                  showGoalAmountsAndPercentages :: Change -> [(WideBuilder, Maybe WideBuilder)]
showGoalAmountsAndPercentages Change
goal = [WideBuilder]
-> [Maybe WideBuilder] -> [(WideBuilder, Maybe WideBuilder)]
forall a b. [a] -> [b] -> [(a, b)]
zip [WideBuilder]
amts [Maybe WideBuilder]
mpcts
                    where
                      amts :: [WideBuilder]
amts  = Change -> [WideBuilder]
showCommodityAmounts Change
goal
                      mpcts :: [Maybe WideBuilder]
mpcts = (Maybe Percentage -> Maybe WideBuilder)
-> [Maybe Percentage] -> [Maybe WideBuilder]
forall a b. (a -> b) -> [a] -> [b]
map (Percentage -> WideBuilder
showrounded (Percentage -> WideBuilder)
-> Maybe Percentage -> Maybe WideBuilder
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) ([Maybe Percentage] -> [Maybe WideBuilder])
-> [Maybe Percentage] -> [Maybe WideBuilder]
forall a b. (a -> b) -> a -> b
$ BudgetCalcPercentagesFn
calcCommodityPercentages Change
actual Change
goal
                        where showrounded :: Percentage -> WideBuilder
showrounded = Text -> WideBuilder
wbFromText (Text -> WideBuilder)
-> (Percentage -> Text) -> Percentage -> WideBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack (String -> Text) -> (Percentage -> String) -> Percentage -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Percentage -> String
forall a. Show a => a -> String
show (Percentage -> String)
-> (Percentage -> Percentage) -> Percentage -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Percentage -> Percentage
forall i. Integral i => Word8 -> DecimalRaw i -> DecimalRaw i
roundTo Word8
0

        -- | Make budget info display helpers that adapt to --layout=wide.
        mkBudgetDisplayFns :: [CommoditySymbol] -> (BudgetShowAmountsFn, BudgetCalcPercentagesFn)
        mkBudgetDisplayFns :: [Text] -> (Change -> [WideBuilder], BudgetCalcPercentagesFn)
mkBudgetDisplayFns [Text]
cs = case Layout
layout_ of
          LayoutWide Maybe Int
width ->
               ( WideBuilder -> [WideBuilder]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (WideBuilder -> [WideBuilder])
-> (Change -> WideBuilder) -> Change -> [WideBuilder]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountFormat -> Change -> WideBuilder
showMixedAmountB AmountFormat
oneLineNoCostFmt{displayMaxWidth=width, displayColour=color_}
               , \Change
a -> Maybe Percentage -> [Maybe Percentage]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Percentage -> [Maybe Percentage])
-> (Change -> Maybe Percentage) -> Change -> [Maybe Percentage]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Change -> Change -> Maybe Percentage
percentage Change
a)
          Layout
_ -> ( AmountFormat -> Change -> [WideBuilder]
showMixedAmountLinesB ([Text] -> AmountFormat -> AmountFormat
setDisplayCommodityBare [Text]
cs AmountFormat
noCostFmt){displayCommodity=layout_/=LayoutBare, displayColour=color_}
               , \Change
a Change
b -> (Text -> Maybe Percentage) -> [Text] -> [Maybe Percentage]
forall a b. (a -> b) -> [a] -> [b]
map (Change -> Change -> Text -> Maybe Percentage
percentage' Change
a Change
b) [Text]
cs)
          where
            -- | 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 :: Change -> Change -> Maybe Percentage
percentage Change
actual Change
budget =
              case (Change -> [Amount]
costedAmounts Change
actual, Change -> [Amount]
costedAmounts Change
budget) of
                ([Amount
a], [Amount
b]) | (Amount -> Text
acommodity Amount
a Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Amount -> Text
acommodity Amount
b Bool -> Bool -> Bool
|| Amount -> Bool
amountLooksZero Amount
a) Bool -> Bool -> Bool
&& Bool -> Bool
not (Amount -> Bool
amountLooksZero Amount
b)
                    -> Percentage -> Maybe Percentage
forall a. a -> Maybe a
Just (Percentage -> Maybe Percentage) -> Percentage -> Maybe Percentage
forall a b. (a -> b) -> a -> b
$ Percentage
100 Percentage -> Percentage -> Percentage
forall a. Num a => a -> a -> a
* Amount -> Percentage
aquantity Amount
a Percentage -> Percentage -> Percentage
forall a. Fractional a => a -> a -> a
/ Amount -> Percentage
aquantity Amount
b
                ([Amount], [Amount])
_   -> Maybe Percentage
forall a. Maybe a
Nothing
              where
                costedAmounts :: Change -> [Amount]
costedAmounts = case Maybe ConversionOp
conversionop_ of
                    Just ConversionOp
ToCost           -> Change -> [Amount]
amounts (Change -> [Amount]) -> (Change -> Change) -> Change -> [Amount]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Change -> Change
mixedAmountCostBasis
                    Just ConversionOp
ToTransactedCost -> Change -> [Amount]
amounts (Change -> [Amount]) -> (Change -> Change) -> Change -> [Amount]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Change -> Change
mixedAmountCost
                    Maybe ConversionOp
_                     -> Change -> [Amount]
amounts (Change -> [Amount]) -> (Change -> Change) -> Change -> [Amount]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Change -> Change
mixedAmountStripCosts  -- strip any lingering cost info that would prevent unification

            -- | Like percentage, but accept multicommodity actual and budget amounts,
            -- and extract the specified commodity from both.
            percentage' :: Change -> BudgetGoal -> CommoditySymbol -> Maybe Percentage
            percentage' :: Change -> Change -> Text -> Maybe Percentage
percentage' Change
am Change
bm Text
c = case ((,) (Maybe Amount -> Maybe Amount -> (Maybe Amount, Maybe Amount))
-> (Change -> Maybe Amount)
-> Change
-> Change
-> (Maybe Amount, Maybe Amount)
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` (Amount -> Bool) -> [Amount] -> Maybe Amount
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
(==) Text
c (Text -> Bool) -> (Amount -> Text) -> Amount -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Amount -> Text
acommodity) ([Amount] -> Maybe Amount)
-> (Change -> [Amount]) -> Change -> Maybe Amount
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Change -> [Amount]
amounts) Change
am Change
bm of
                (Just Amount
a, Just Amount
b) -> Change -> Change -> Maybe Percentage
percentage (Amount -> Change
mixedAmount Amount
a) (Amount -> Change
mixedAmount Amount
b)
                (Maybe Amount, Maybe Amount)
_                -> Maybe Percentage
forall a. Maybe a
Nothing

-- XXX generalise this with multiBalanceReportAsCsv ?
-- | Render a budget report as CSV. Like multiBalanceReportAsCsv,
-- but includes alternating actual and budget amount columns.
budgetReportAsCsv :: ReportOpts -> BudgetReport -> [[Text]]
budgetReportAsCsv :: ReportOpts -> BudgetReport -> CSV
budgetReportAsCsv ReportOpts
ropts BudgetReport
report
  = [[Cell NumLines Text]] -> CSV
forall (f :: * -> *) border text.
Functor f =>
[f (Cell border text)] -> [f text]
rawTableContent ([[Cell NumLines Text]] -> CSV) -> [[Cell NumLines Text]] -> CSV
forall a b. (a -> b) -> a -> b
$ ((Int, Int), [[Cell NumLines Text]]) -> [[Cell NumLines Text]]
forall a b. (a, b) -> b
snd (((Int, Int), [[Cell NumLines Text]]) -> [[Cell NumLines Text]])
-> ((Int, Int), [[Cell NumLines Text]]) -> [[Cell NumLines Text]]
forall a b. (a -> b) -> a -> b
$
    AmountFormat
-> ReportOpts
-> BudgetReport
-> ((Int, Int), [[Cell NumLines Text]])
budgetReportAsSpreadsheet AmountFormat
machineFmt ReportOpts
ropts BudgetReport
report

-- | Render a budget report as HTML.
budgetReportAsHtml :: ReportOpts -> BudgetReport -> Html
budgetReportAsHtml :: ReportOpts -> BudgetReport -> Html
budgetReportAsHtml ReportOpts
ropts BudgetReport
budgetr =
  Text -> [[Cell NumLines Html]] -> Html
forall border. Lines border => Text -> [[Cell border Html]] -> Html
titledTableHtml (ReportOpts -> BudgetReport -> Text
budgetReportTitle ReportOpts
ropts BudgetReport
budgetr) ([[Cell NumLines Html]] -> Html)
-> (((Int, Int), [[Cell NumLines Text]]) -> [[Cell NumLines Html]])
-> ((Int, Int), [[Cell NumLines Text]])
-> Html
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Cell NumLines Text] -> [Cell NumLines Html])
-> [[Cell NumLines Text]] -> [[Cell NumLines Html]]
forall a b. (a -> b) -> [a] -> [b]
map ((Cell NumLines Text -> Cell NumLines Html)
-> [Cell NumLines Text] -> [Cell NumLines Html]
forall a b. (a -> b) -> [a] -> [b]
map ((Text -> Html) -> Cell NumLines Text -> Cell NumLines Html
forall a b. (a -> b) -> Cell NumLines a -> Cell NumLines b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Html
forall a. ToMarkup a => a -> Html
toHtml)) ([[Cell NumLines Text]] -> [[Cell NumLines Html]])
-> (((Int, Int), [[Cell NumLines Text]]) -> [[Cell NumLines Text]])
-> ((Int, Int), [[Cell NumLines Text]])
-> [[Cell NumLines Html]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int, Int), [[Cell NumLines Text]]) -> [[Cell NumLines Text]]
forall a b. (a, b) -> b
snd (((Int, Int), [[Cell NumLines Text]]) -> Html)
-> ((Int, Int), [[Cell NumLines Text]]) -> Html
forall a b. (a -> b) -> a -> b
$
    AmountFormat
-> ReportOpts
-> BudgetReport
-> ((Int, Int), [[Cell NumLines Text]])
budgetReportAsSpreadsheet AmountFormat
oneLineNoCostFmt ReportOpts
ropts BudgetReport
budgetr

budgetReportAsSpreadsheet ::
  AmountFormat -> ReportOpts -> BudgetReport ->
  ((Int,Int), [[Ods.Cell Ods.NumLines Text]])
budgetReportAsSpreadsheet :: AmountFormat
-> ReportOpts
-> BudgetReport
-> ((Int, Int), [[Cell NumLines Text]])
budgetReportAsSpreadsheet
  AmountFormat
fmt
  ropts :: ReportOpts
ropts@ReportOpts{Bool
Int
[Text]
[Status]
SortSpec
Maybe Int
Maybe Text
Maybe NormalSign
Maybe ConversionOp
Maybe ValuationType
Interval
Period
DepthSpec
AccountListMode
BalanceAccumulation
BalanceCalculation
Layout
PeriodTitles
Translations
StringFormat
balancecalc_ :: ReportOpts -> BalanceCalculation
translations_ :: ReportOpts -> Translations
no_total_ :: ReportOpts -> Bool
layout_ :: ReportOpts -> Layout
interval_ :: ReportOpts -> Interval
format_ :: ReportOpts -> StringFormat
color_ :: ReportOpts -> Bool
pretty_ :: ReportOpts -> Bool
accountlistmode_ :: ReportOpts -> AccountListMode
full_names_ :: ReportOpts -> Bool
drop_ :: ReportOpts -> Int
transpose_ :: ReportOpts -> Bool
period_ :: ReportOpts -> Period
date2_ :: ReportOpts -> Bool
querystring_ :: ReportOpts -> [Text]
balance_base_url_ :: ReportOpts -> Maybe Text
average_ :: ReportOpts -> Bool
balanceaccum_ :: ReportOpts -> BalanceAccumulation
budgetpat_ :: ReportOpts -> Maybe Text
conversionop_ :: ReportOpts -> Maybe ConversionOp
declared_ :: ReportOpts -> Bool
depth_ :: ReportOpts -> DepthSpec
empty_ :: ReportOpts -> Bool
fold_depth_ :: ReportOpts -> Maybe Int
infer_prices_ :: ReportOpts -> Bool
invert_ :: ReportOpts -> Bool
no_elide_ :: ReportOpts -> Bool
normalbalance_ :: ReportOpts -> Maybe NormalSign
percent_ :: ReportOpts -> Bool
period_titles_ :: ReportOpts -> PeriodTitles
real_ :: ReportOpts -> Bool
related_ :: ReportOpts -> Bool
row_total_ :: ReportOpts -> Bool
show_costs_ :: ReportOpts -> Bool
sort_amount_ :: ReportOpts -> Bool
sortspec_ :: ReportOpts -> SortSpec
statuses_ :: ReportOpts -> [Status]
subreport_titles_ :: ReportOpts -> Maybe Text
summary_only_ :: ReportOpts -> Bool
title_ :: ReportOpts -> Maybe Text
txn_dates_ :: ReportOpts -> Bool
value_ :: ReportOpts -> Maybe ValuationType
period_ :: Period
interval_ :: Interval
statuses_ :: [Status]
conversionop_ :: Maybe ConversionOp
value_ :: Maybe ValuationType
infer_prices_ :: Bool
depth_ :: DepthSpec
fold_depth_ :: Maybe Int
no_elide_ :: Bool
date2_ :: Bool
empty_ :: Bool
full_names_ :: Bool
real_ :: Bool
format_ :: StringFormat
balance_base_url_ :: Maybe Text
pretty_ :: Bool
querystring_ :: [Text]
average_ :: Bool
related_ :: Bool
sortspec_ :: SortSpec
txn_dates_ :: Bool
balancecalc_ :: BalanceCalculation
balanceaccum_ :: BalanceAccumulation
budgetpat_ :: Maybe Text
accountlistmode_ :: AccountListMode
drop_ :: Int
declared_ :: Bool
row_total_ :: Bool
no_total_ :: Bool
summary_only_ :: Bool
show_costs_ :: Bool
sort_amount_ :: Bool
percent_ :: Bool
invert_ :: Bool
normalbalance_ :: Maybe NormalSign
color_ :: Bool
transpose_ :: Bool
layout_ :: Layout
period_titles_ :: PeriodTitles
title_ :: Maybe Text
subreport_titles_ :: Maybe Text
translations_ :: Translations
..}
  (PeriodicReport [DateSpan]
colspans [PeriodicReportRow DisplayName (Maybe Change, Maybe Change)]
items PeriodicReportRow () (Maybe Change, Maybe Change)
totrow)
  =
  (case Layout
layout_ of
      LayoutWide Maybe Int
_ -> ((Int, Int), [[Cell NumLines Text]])
-> ((Int, Int), [[Cell NumLines Text]])
forall a. a -> a
id
      Layout
LayoutBare -> ((Int, Int), [[Cell NumLines Text]])
-> ((Int, Int), [[Cell NumLines Text]])
forall a. a -> a
id
      Layout
LayoutBareWide -> ((Int, Int), [[Cell NumLines Text]])
-> ((Int, Int), [[Cell NumLines Text]])
forall a. a -> a
id
      Layout
_ -> Layout
-> ((Int, Int), [[Cell NumLines Text]])
-> ((Int, Int), [[Cell NumLines Text]])
forall a. Layout -> a -> a
unsupportedLayout Layout
layout_) (((Int, Int), [[Cell NumLines Text]])
 -> ((Int, Int), [[Cell NumLines Text]]))
-> ((Int, Int), [[Cell NumLines Text]])
-> ((Int, Int), [[Cell NumLines Text]])
forall a b. (a -> b) -> a -> b
$
  (if Bool
transpose_ then (Int, Int) -> (Int, Int)
forall a b. (a, b) -> (b, a)
swap ((Int, Int) -> (Int, Int))
-> ([[Cell NumLines Text]] -> [[Cell NumLines Text]])
-> ((Int, Int), [[Cell NumLines Text]])
-> ((Int, Int), [[Cell NumLines Text]])
forall b c b' c'. (b -> c) -> (b' -> c') -> (b, b') -> (c, c')
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** [[Cell NumLines Text]] -> [[Cell NumLines Text]]
forall border text. [[Cell border text]] -> [[Cell border text]]
Ods.transpose else ((Int, Int), [[Cell NumLines Text]])
-> ((Int, Int), [[Cell NumLines Text]])
forall a. a -> a
id) (((Int, Int), [[Cell NumLines Text]])
 -> ((Int, Int), [[Cell NumLines Text]]))
-> ((Int, Int), [[Cell NumLines Text]])
-> ((Int, Int), [[Cell NumLines Text]])
forall a b. (a -> b) -> a -> b
$
  (([[Cell NumLines Text]] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[Cell NumLines Text]]
allHeaders, [Text] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Text]
leadingHeaders)
   ,
    -- heading row
    [[Cell NumLines Text]]
allHeaders [[Cell NumLines Text]]
-> [[Cell NumLines Text]] -> [[Cell NumLines Text]]
forall a. [a] -> [a] -> [a]
++

    -- account rows
    (PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
 -> [[Cell NumLines Text]])
-> [PeriodicReportRow DisplayName (Maybe Change, Maybe Change)]
-> [[Cell NumLines Text]]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
row -> RowClass
-> Cell NumLines Text
-> PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
-> [[Cell NumLines Text]]
forall a.
RowClass
-> Cell NumLines Text
-> PeriodicReportRow a (Maybe Change, Maybe Change)
-> [[Cell NumLines Text]]
rowAsTexts RowClass
Value (PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
-> Cell NumLines Text
forall {a}. PeriodicReportRow DisplayName a -> Cell NumLines Text
accountCell PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
row) PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
row) [PeriodicReportRow DisplayName (Maybe Change, Maybe Change)]
items

    -- totals row
    [[Cell NumLines Text]]
-> [[Cell NumLines Text]] -> [[Cell NumLines Text]]
forall a. [a] -> [a] -> [a]
++ [[Cell NumLines Text]] -> [[Cell NumLines Text]]
forall (f :: * -> *) border text.
Functor f =>
[f (Cell border text)] -> [f (Cell NumLines text)]
addTotalBorders
          ([[[Cell NumLines Text]]] -> [[Cell NumLines Text]]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ RowClass
-> Cell NumLines Text
-> PeriodicReportRow () (Maybe Change, Maybe Change)
-> [[Cell NumLines Text]]
forall a.
RowClass
-> Cell NumLines Text
-> PeriodicReportRow a (Maybe Change, Maybe Change)
-> [[Cell NumLines Text]]
rowAsTexts RowClass
Total (Text -> Cell NumLines Text
forall {text}. text -> Cell NumLines text
cell Text
totalRowHeadingBudgetCsv) PeriodicReportRow () (Maybe Change, Maybe Change)
totrow | Bool -> Bool
not Bool
no_total_ ])
    )

  where
    cell :: text -> Cell NumLines text
cell = text -> Cell NumLines text
forall border text. Lines border => text -> Cell border text
Ods.defaultCell
    accountCell :: PeriodicReportRow DisplayName a -> Cell NumLines Text
accountCell PeriodicReportRow DisplayName a
row =
        let name :: Text
name = PeriodicReportRow DisplayName a -> Text
forall a. PeriodicReportRow DisplayName a -> Text
prrFullName PeriodicReportRow DisplayName a
row in
        Maybe Text
-> [Text] -> Text -> Cell NumLines Text -> Cell NumLines Text
forall border text.
Maybe Text
-> [Text] -> Text -> Cell border text -> Cell border text
setAccountAnchor (Maybe Text
balance_base_url_) [Text]
querystring_ Text
name (Cell NumLines Text -> Cell NumLines Text)
-> Cell NumLines Text -> Cell NumLines Text
forall a b. (a -> b) -> a -> b
$
        Text -> Cell NumLines Text
forall {text}. text -> Cell NumLines text
cell (Text -> Cell NumLines Text) -> Text -> Cell NumLines Text
forall a b. (a -> b) -> a -> b
$ ReportOpts -> Text -> PeriodicReportRow DisplayName a -> Text
forall a.
ReportOpts -> Text -> PeriodicReportRow DisplayName a -> Text
renderPeriodicAcct ReportOpts
ropts Text
nbsp PeriodicReportRow DisplayName a
row
    allHeaders :: [[Cell NumLines Text]]
allHeaders =
      case Layout
layout_ of
      Layout
LayoutBareWide ->
          [[Cell () Text] -> [Cell NumLines Text]
forall text. [Cell () text] -> [Cell NumLines text]
headerWithoutBorders ([Cell () Text] -> [Cell NumLines Text])
-> [Cell () Text] -> [Cell NumLines Text]
forall a b. (a -> b) -> a -> b
$
              Cell () Text
forall border text. (Lines border, Monoid text) => Cell border text
Ods.emptyCell Cell () Text -> [Cell () Text] -> [Cell () Text]
forall a. a -> [a] -> [a]
:
              (Text -> [Cell () Text]) -> [Text] -> [Cell () Text]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap
                  ([Text] -> Cell () Text -> [Cell () Text]
forall border text a.
(Lines border, Monoid text) =>
[a] -> Cell border text -> [Cell border text]
Ods.horizontalSpan [Text]
allCommodities (Cell () Text -> [Cell () Text])
-> (Text -> Cell () Text) -> Text -> [Cell () Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Cell () Text
forall borders. Lines borders => Text -> Cell borders Text
headerCell)
                  [Text]
dateHeaders,
           [Cell () Text] -> [Cell NumLines Text]
forall (f :: * -> *) text.
Functor f =>
f (Cell () text) -> f (Cell NumLines text)
addHeaderBorders ([Cell () Text] -> [Cell NumLines Text])
-> [Cell () Text] -> [Cell NumLines Text]
forall a b. (a -> b) -> a -> b
$ (Text -> Cell () Text) -> [Text] -> [Cell () Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Cell () Text
forall borders. Lines borders => Text -> Cell borders Text
headerCell ([Text] -> [Cell () Text]) -> [Text] -> [Cell () Text]
forall a b. (a -> b) -> a -> b
$
              [Text]
leadingHeaders [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ ([Text]
dateHeaders [Text] -> [Text] -> [Text]
forall a b. [a] -> [b] -> [b]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [Text]
allCommodities)]
      Layout
_ -> [[Cell () Text] -> [Cell NumLines Text]
forall (f :: * -> *) text.
Functor f =>
f (Cell () text) -> f (Cell NumLines text)
addHeaderBorders ([Cell () Text] -> [Cell NumLines Text])
-> [Cell () Text] -> [Cell NumLines Text]
forall a b. (a -> b) -> a -> b
$ (Text -> Cell () Text) -> [Text] -> [Cell () Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Cell () Text
forall borders. Lines borders => Text -> Cell borders Text
headerCell ([Text] -> [Cell () Text]) -> [Text] -> [Cell () Text]
forall a b. (a -> b) -> a -> b
$ [Text]
leadingHeaders [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Text]
dateHeaders]
    leadingHeaders :: [Text]
leadingHeaders =
      Text
"Account" Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: [Text
"Commodity" | Layout
layout_ Layout -> Layout -> Bool
forall a. Eq a => a -> a -> Bool
== Layout
LayoutBare ]
    dateHeaders :: [Text]
dateHeaders =
      (if Bool -> Bool
not Bool
summary_only_
            then (DateSpan -> [Text]) -> [DateSpan] -> [Text]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\DateSpan
spn -> [PeriodTitles -> DateSpan -> Text
renderPeriodHeading PeriodTitles
period_titles_ DateSpan
spn, Text
"budget"]) [DateSpan]
colspans
            else [])
       [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ CSV -> [Text]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Text
"Total"  ,Text
"budget"] | Bool
row_total_]
       [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ CSV -> [Text]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Text
"Average",Text
"budget"] | Bool
average_]
    allCommodities :: [Text]
allCommodities =
       Set Text -> [Text]
forall a. Set a -> [a]
S.toAscList (Set Text -> [Text]) -> Set Text -> [Text]
forall a b. (a -> b) -> a -> b
$
       (PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
 -> Set Text)
-> [PeriodicReportRow DisplayName (Maybe Change, Maybe Change)]
-> Set Text
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ((Change -> Set Text) -> [Change] -> Set Text
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Change -> Set Text
maCommodities ([Change] -> Set Text)
-> (PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
    -> [Change])
-> PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
-> Set Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Maybe Change, Maybe Change) -> [Change])
-> [(Maybe Change, Maybe Change)] -> [Change]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\(Maybe Change
change,Maybe Change
goal) -> Maybe Change -> [Change]
forall a. Maybe a -> [a]
maybeToList Maybe Change
change [Change] -> [Change] -> [Change]
forall a. [a] -> [a] -> [a]
++ Maybe Change -> [Change]
forall a. Maybe a -> [a]
maybeToList Maybe Change
goal) ([(Maybe Change, Maybe Change)] -> [Change])
-> (PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
    -> [(Maybe Change, Maybe Change)])
-> PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
-> [Change]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PeriodicReportRow DisplayName (Maybe Change, Maybe Change)
-> [(Maybe Change, Maybe Change)]
forall a b. PeriodicReportRow a b -> [b]
prrAmounts) [PeriodicReportRow DisplayName (Maybe Change, Maybe Change)]
items

    {-
    ToDo: The chosen HTML cell class names are not put in stone.
    If you find you need more systematic names,
    feel free to develop a more sophisticated scheme.
    -}
    flattentuples :: RowClass -> [(b, b)] -> [(Class, b)]
flattentuples RowClass
rc [(b, b)]
tups =
        [[(Class, b)]] -> [(Class, b)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[(RowClass -> Class
amountClass RowClass
rc, b
a),(RowClass -> Class
budgetClass RowClass
rc, b
b)] | (b
a,b
b) <- [(b, b)]
tups]
    showNorm :: (Class, Maybe Change) -> Cell border Text
showNorm (Class
cls,Maybe Change
mval) =
        Cell border Text
-> (Change -> Cell border Text) -> Maybe Change -> Cell border Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Cell border Text
forall border text. (Lines border, Monoid text) => Cell border text
Ods.emptyCell ((WideBuilder -> Text)
-> Cell border WideBuilder -> Cell border Text
forall a b. (a -> b) -> Cell border a -> Cell border b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap WideBuilder -> Text
wbToText (Cell border WideBuilder -> Cell border Text)
-> (Change -> Cell border WideBuilder)
-> Change
-> Cell border Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Class, Change) -> Cell border WideBuilder)
-> Class -> Change -> Cell border WideBuilder
forall a b c. ((a, b) -> c) -> a -> b -> c
curry (AmountFormat -> (Class, Change) -> Cell border WideBuilder
forall border.
Lines border =>
AmountFormat -> (Class, Change) -> Cell border WideBuilder
cellFromMixedAmount AmountFormat
fmt) Class
cls) Maybe Change
mval

    rowAsTexts :: RowClass
               -> Ods.Cell Ods.NumLines Text
               -> PeriodicReportRow a BudgetCell
               -> [[Ods.Cell Ods.NumLines Text]]
    rowAsTexts :: forall a.
RowClass
-> Cell NumLines Text
-> PeriodicReportRow a (Maybe Change, Maybe Change)
-> [[Cell NumLines Text]]
rowAsTexts RowClass
rc Cell NumLines Text
acctCell (PeriodicReportRow a
_ [(Maybe Change, Maybe Change)]
as (Maybe Change
rowtot,Maybe Change
budgettot) (Maybe Change
rowavg, Maybe Change
budgetavg)) =
      Cell NumLines Text
-> [[Cell NumLines Text]] -> [[Cell NumLines Text]]
forall border text.
Cell border text -> [[Cell border text]] -> [[Cell border text]]
addRowSpanHeader Cell NumLines Text
acctCell ([[Cell NumLines Text]] -> [[Cell NumLines Text]])
-> [[Cell NumLines Text]] -> [[Cell NumLines Text]]
forall a b. (a -> b) -> a -> b
$
      case Layout
layout_ of
        Layout
LayoutBare ->
            (Cell NumLines Text
 -> [Cell NumLines Text] -> [Cell NumLines Text])
-> [Cell NumLines Text]
-> [[Cell NumLines Text]]
-> [[Cell NumLines Text]]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (:) ((Text -> Cell NumLines Text) -> [Text] -> [Cell NumLines Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Cell NumLines Text
forall {text}. text -> Cell NumLines text
cell [Text]
cs)   -- add symbols
          ([[Cell NumLines Text]] -> [[Cell NumLines Text]])
-> ([(Class, Maybe Change)] -> [[Cell NumLines Text]])
-> [(Class, Maybe Change)]
-> [[Cell NumLines Text]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Cell NumLines Text]] -> [[Cell NumLines Text]]
forall a. [[a]] -> [[a]]
transpose                   -- each row becomes a list of Text quantities
          ([[Cell NumLines Text]] -> [[Cell NumLines Text]])
-> ([(Class, Maybe Change)] -> [[Cell NumLines Text]])
-> [(Class, Maybe Change)]
-> [[Cell NumLines Text]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> [(Class, Maybe Change)] -> [[Cell NumLines Text]]
forall {border}.
Lines border =>
[Text] -> [(Class, Maybe Change)] -> [[Cell border Text]]
bareCells [Text]
cs
          ([(Class, Maybe Change)] -> [[Cell NumLines Text]])
-> [(Class, Maybe Change)] -> [[Cell NumLines Text]]
forall a b. (a -> b) -> a -> b
$ [(Class, Maybe Change)]
vals
        Layout
LayoutBareWide -> [[[Cell NumLines Text]] -> [Cell NumLines Text]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Cell NumLines Text]] -> [Cell NumLines Text])
-> ([(Class, Maybe Change)] -> [[Cell NumLines Text]])
-> [(Class, Maybe Change)]
-> [Cell NumLines Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> [(Class, Maybe Change)] -> [[Cell NumLines Text]]
forall {border}.
Lines border =>
[Text] -> [(Class, Maybe Change)] -> [[Cell border Text]]
bareCells [Text]
allCommodities ([(Class, Maybe Change)] -> [Cell NumLines Text])
-> [(Class, Maybe Change)] -> [Cell NumLines Text]
forall a b. (a -> b) -> a -> b
$ [(Class, Maybe Change)]
vals]
        Layout
_ -> [((Class, Maybe Change) -> Cell NumLines Text)
-> [(Class, Maybe Change)] -> [Cell NumLines Text]
forall a b. (a -> b) -> [a] -> [b]
map (Class, Maybe Change) -> Cell NumLines Text
forall {border}.
Lines border =>
(Class, Maybe Change) -> Cell border Text
showNorm [(Class, Maybe Change)]
vals]
      where
        bareCells :: [Text] -> [(Class, Maybe Change)] -> [[Cell border Text]]
bareCells [Text]
cs_ =
          ((Class, Maybe Change) -> [Cell border Text])
-> [(Class, Maybe Change)] -> [[Cell border Text]]
forall a b. (a -> b) -> [a] -> [b]
map ((Cell border WideBuilder -> Cell border Text)
-> [Cell border WideBuilder] -> [Cell border Text]
forall a b. (a -> b) -> [a] -> [b]
map ((WideBuilder -> Text)
-> Cell border WideBuilder -> Cell border Text
forall a b. (a -> b) -> Cell border a -> Cell border b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap WideBuilder -> Text
wbToText) ([Cell border WideBuilder] -> [Cell border Text])
-> ((Class, Maybe Change) -> [Cell border WideBuilder])
-> (Class, Maybe Change)
-> [Cell border Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
          AmountFormat -> (Class, Change) -> [Cell border WideBuilder]
forall border.
Lines border =>
AmountFormat -> (Class, Change) -> [Cell border WideBuilder]
cellsFromMixedAmount ([Text] -> AmountFormat -> AmountFormat
setDisplayCommodityBare [Text]
cs_ AmountFormat
fmt) ((Class, Change) -> [Cell border WideBuilder])
-> ((Class, Maybe Change) -> (Class, Change))
-> (Class, Maybe Change)
-> [Cell border WideBuilder]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
          (Maybe Change -> Change)
-> (Class, Maybe Change) -> (Class, Change)
forall b c d. (b -> c) -> (d, b) -> (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second (Change -> Maybe Change -> Change
forall a. a -> Maybe a -> a
fromMaybe Change
nullmixedamt))
        cs :: [Text]
cs = Set Text -> [Text]
forall a. Set a -> [a]
S.toList (Set Text -> [Text])
-> ([Change] -> Set Text) -> [Change] -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Set Text] -> Set Text
forall a. Monoid a => [a] -> a
mconcat ([Set Text] -> Set Text)
-> ([Change] -> [Set Text]) -> [Change] -> Set Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Change -> Set Text) -> [Change] -> [Set Text]
forall a b. (a -> b) -> [a] -> [b]
map Change -> Set Text
maCommodities ([Change] -> [Text]) -> [Change] -> [Text]
forall a b. (a -> b) -> a -> b
$ ((Class, Maybe Change) -> Maybe Change)
-> [(Class, Maybe Change)] -> [Change]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Class, Maybe Change) -> Maybe Change
forall a b. (a, b) -> b
snd [(Class, Maybe Change)]
vals
        vals :: [(Class, Maybe Change)]
vals = RowClass
-> [(Maybe Change, Maybe Change)] -> [(Class, Maybe Change)]
forall {b}. RowClass -> [(b, b)] -> [(Class, b)]
flattentuples RowClass
rc (if Bool -> Bool
not Bool
summary_only_ then [(Maybe Change, Maybe Change)]
as else [])
            [(Class, Maybe Change)]
-> [(Class, Maybe Change)] -> [(Class, Maybe Change)]
forall a. [a] -> [a] -> [a]
++ [[(Class, Maybe Change)]] -> [(Class, Maybe Change)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[(RowClass -> Class
rowTotalClass RowClass
rc, Maybe Change
rowtot),
                        (RowClass -> Class
budgetTotalClass RowClass
rc, Maybe Change
budgettot)]
                            | Bool
row_total_]
            [(Class, Maybe Change)]
-> [(Class, Maybe Change)] -> [(Class, Maybe Change)]
forall a. [a] -> [a] -> [a]
++ [[(Class, Maybe Change)]] -> [(Class, Maybe Change)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[(RowClass -> Class
rowAverageClass RowClass
rc, Maybe Change
rowavg),
                        (RowClass -> Class
budgetAverageClass RowClass
rc, Maybe Change
budgetavg)]
                            | Bool
average_]

unsupportedLayout :: Layout -> a -> a
unsupportedLayout :: forall a. Layout -> a -> a
unsupportedLayout Layout
lay =
    String -> a -> a
forall a. String -> a
error' (String -> a -> a) -> String -> a -> a
forall a b. (a -> b) -> a -> b
$ Layout -> String
forall a. Show a => a -> String
show Layout
lay String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" not supported for the chosen output format."


-- tests

tests_Balance :: TestTree
tests_Balance = String -> [TestTree] -> TestTree
testGroup String
"Balance" [

   String -> [TestTree] -> TestTree
testGroup String
"balanceReportAsText" [
    String -> IO () -> TestTree
testCase String
"unicode in balance layout" (IO () -> TestTree) -> IO () -> TestTree
forall a b. (a -> b) -> a -> b
$ do
      j <- Text -> IO Journal
readJournal'' Text
"2009/01/01 * медвежья шкура\n  расходы:покупки  100\n  актив:наличные\n"
      let rspec = ReportSpec
defreportspec{_rsReportOpts=defreportopts{no_total_=True}}
      balanceReportAsText (_rsReportOpts rspec) (balanceReport rspec{_rsDay=fromGregorian 2008 11 26} j)
        @?=
        TL.unlines
        ["                -100  актив:наличные"
        ,"                 100  расходы:покупки"
        ]
    ]

  ]