{- |
New common report types, used by the BudgetReport for now, perhaps all reports later.
-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}

module Hledger.Reports.ReportTypes
( PeriodicReport(..)
, PeriodicReportRow(..)

, Percentage
, Change
, Balance
, Total
, Average

, periodicReportSpan
, prNegate
, prNormaliseSign
) where

import Data.Aeson
import Data.Decimal
import GHC.Generics (Generic)
import Hledger.Data

type Percentage = Decimal

type Change  = MixedAmount  -- ^ A change in balance during a certain period.
type Balance = MixedAmount  -- ^ An ending balance as of some date.
type Total   = MixedAmount  -- ^ The sum of 'Change's in a report or a report row. Does not make sense for 'Balance's.
type Average = MixedAmount  -- ^ The average of 'Change's or 'Balance's in a report or report row.

-- | A periodic report is a generic tabular report, where each row corresponds
-- to some label (usually an account name) and each column to a date period.
-- The column periods are usually consecutive subperiods formed by splitting
-- the overall report period by some report interval (daily, weekly, etc.).
-- It has:
--
-- 1. a list of each column's period (date span)
--
-- 2. a list of rows, each containing:
--
--   * an account label
--
--   * the account's depth
--
--   * A list of amounts, one for each column. Depending on the value type,
--     these can represent balance changes, ending balances, budget
--     performance, etc. (for example, see 'BalanceType' and
--     "Hledger.Cli.Commands.Balance").
--
--   * the total of the row's amounts for a periodic report,
--     or zero for cumulative/historical reports (since summing
--     end balances generally doesn't make sense).
--
--   * the average of the row's amounts
--
-- 3. the column totals, and the overall grand total (or zero for
-- cumulative/historical reports) and grand average.

data PeriodicReport a b =
  PeriodicReport
  { PeriodicReport a b -> [DateSpan]
prDates  :: [DateSpan]               -- The subperiods formed by splitting the overall
                                         -- report period by the report interval. For
                                         -- ending-balance reports, only the end date is
                                         -- significant. Usually displayed as report columns.
  , PeriodicReport a b -> [PeriodicReportRow a b]
prRows   :: [PeriodicReportRow a b]  -- One row per account in the report.
  , PeriodicReport a b -> PeriodicReportRow () b
prTotals :: PeriodicReportRow () b   -- The grand totals row.
  } deriving (Int -> PeriodicReport a b -> ShowS
[PeriodicReport a b] -> ShowS
PeriodicReport a b -> String
(Int -> PeriodicReport a b -> ShowS)
-> (PeriodicReport a b -> String)
-> ([PeriodicReport a b] -> ShowS)
-> Show (PeriodicReport a b)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall a b. (Show a, Show b) => Int -> PeriodicReport a b -> ShowS
forall a b. (Show a, Show b) => [PeriodicReport a b] -> ShowS
forall a b. (Show a, Show b) => PeriodicReport a b -> String
showList :: [PeriodicReport a b] -> ShowS
$cshowList :: forall a b. (Show a, Show b) => [PeriodicReport a b] -> ShowS
show :: PeriodicReport a b -> String
$cshow :: forall a b. (Show a, Show b) => PeriodicReport a b -> String
showsPrec :: Int -> PeriodicReport a b -> ShowS
$cshowsPrec :: forall a b. (Show a, Show b) => Int -> PeriodicReport a b -> ShowS
Show, (forall x. PeriodicReport a b -> Rep (PeriodicReport a b) x)
-> (forall x. Rep (PeriodicReport a b) x -> PeriodicReport a b)
-> Generic (PeriodicReport a b)
forall x. Rep (PeriodicReport a b) x -> PeriodicReport a b
forall x. PeriodicReport a b -> Rep (PeriodicReport a b) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a b x. Rep (PeriodicReport a b) x -> PeriodicReport a b
forall a b x. PeriodicReport a b -> Rep (PeriodicReport a b) x
$cto :: forall a b x. Rep (PeriodicReport a b) x -> PeriodicReport a b
$cfrom :: forall a b x. PeriodicReport a b -> Rep (PeriodicReport a b) x
Generic, [PeriodicReport a b] -> Encoding
[PeriodicReport a b] -> Value
PeriodicReport a b -> Encoding
PeriodicReport a b -> Value
(PeriodicReport a b -> Value)
-> (PeriodicReport a b -> Encoding)
-> ([PeriodicReport a b] -> Value)
-> ([PeriodicReport a b] -> Encoding)
-> ToJSON (PeriodicReport a b)
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
forall a b.
(ToJSON a, ToJSON b) =>
[PeriodicReport a b] -> Encoding
forall a b. (ToJSON a, ToJSON b) => [PeriodicReport a b] -> Value
forall a b. (ToJSON a, ToJSON b) => PeriodicReport a b -> Encoding
forall a b. (ToJSON a, ToJSON b) => PeriodicReport a b -> Value
toEncodingList :: [PeriodicReport a b] -> Encoding
$ctoEncodingList :: forall a b.
(ToJSON a, ToJSON b) =>
[PeriodicReport a b] -> Encoding
toJSONList :: [PeriodicReport a b] -> Value
$ctoJSONList :: forall a b. (ToJSON a, ToJSON b) => [PeriodicReport a b] -> Value
toEncoding :: PeriodicReport a b -> Encoding
$ctoEncoding :: forall a b. (ToJSON a, ToJSON b) => PeriodicReport a b -> Encoding
toJSON :: PeriodicReport a b -> Value
$ctoJSON :: forall a b. (ToJSON a, ToJSON b) => PeriodicReport a b -> Value
ToJSON)

data PeriodicReportRow a b =
  PeriodicReportRow
  { PeriodicReportRow a b -> a
prrName    :: a    -- An account name.
  , PeriodicReportRow a b -> Int
prrDepth   :: Int  -- Indent level for displaying this account name in tree mode. 0, 1, 2...
  , PeriodicReportRow a b -> [b]
prrAmounts :: [b]  -- The data value for each subperiod.
  , PeriodicReportRow a b -> b
prrTotal   :: b    -- The total of this row's values.
  , PeriodicReportRow a b -> b
prrAverage :: b    -- The average of this row's values.
  } deriving (Int -> PeriodicReportRow a b -> ShowS
[PeriodicReportRow a b] -> ShowS
PeriodicReportRow a b -> String
(Int -> PeriodicReportRow a b -> ShowS)
-> (PeriodicReportRow a b -> String)
-> ([PeriodicReportRow a b] -> ShowS)
-> Show (PeriodicReportRow a b)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall a b.
(Show a, Show b) =>
Int -> PeriodicReportRow a b -> ShowS
forall a b. (Show a, Show b) => [PeriodicReportRow a b] -> ShowS
forall a b. (Show a, Show b) => PeriodicReportRow a b -> String
showList :: [PeriodicReportRow a b] -> ShowS
$cshowList :: forall a b. (Show a, Show b) => [PeriodicReportRow a b] -> ShowS
show :: PeriodicReportRow a b -> String
$cshow :: forall a b. (Show a, Show b) => PeriodicReportRow a b -> String
showsPrec :: Int -> PeriodicReportRow a b -> ShowS
$cshowsPrec :: forall a b.
(Show a, Show b) =>
Int -> PeriodicReportRow a b -> ShowS
Show, (forall x. PeriodicReportRow a b -> Rep (PeriodicReportRow a b) x)
-> (forall x.
    Rep (PeriodicReportRow a b) x -> PeriodicReportRow a b)
-> Generic (PeriodicReportRow a b)
forall x. Rep (PeriodicReportRow a b) x -> PeriodicReportRow a b
forall x. PeriodicReportRow a b -> Rep (PeriodicReportRow a b) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a b x.
Rep (PeriodicReportRow a b) x -> PeriodicReportRow a b
forall a b x.
PeriodicReportRow a b -> Rep (PeriodicReportRow a b) x
$cto :: forall a b x.
Rep (PeriodicReportRow a b) x -> PeriodicReportRow a b
$cfrom :: forall a b x.
PeriodicReportRow a b -> Rep (PeriodicReportRow a b) x
Generic, [PeriodicReportRow a b] -> Encoding
[PeriodicReportRow a b] -> Value
PeriodicReportRow a b -> Encoding
PeriodicReportRow a b -> Value
(PeriodicReportRow a b -> Value)
-> (PeriodicReportRow a b -> Encoding)
-> ([PeriodicReportRow a b] -> Value)
-> ([PeriodicReportRow a b] -> Encoding)
-> ToJSON (PeriodicReportRow a b)
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
forall a b.
(ToJSON b, ToJSON a) =>
[PeriodicReportRow a b] -> Encoding
forall a b.
(ToJSON b, ToJSON a) =>
[PeriodicReportRow a b] -> Value
forall a b.
(ToJSON b, ToJSON a) =>
PeriodicReportRow a b -> Encoding
forall a b. (ToJSON b, ToJSON a) => PeriodicReportRow a b -> Value
toEncodingList :: [PeriodicReportRow a b] -> Encoding
$ctoEncodingList :: forall a b.
(ToJSON b, ToJSON a) =>
[PeriodicReportRow a b] -> Encoding
toJSONList :: [PeriodicReportRow a b] -> Value
$ctoJSONList :: forall a b.
(ToJSON b, ToJSON a) =>
[PeriodicReportRow a b] -> Value
toEncoding :: PeriodicReportRow a b -> Encoding
$ctoEncoding :: forall a b.
(ToJSON b, ToJSON a) =>
PeriodicReportRow a b -> Encoding
toJSON :: PeriodicReportRow a b -> Value
$ctoJSON :: forall a b. (ToJSON b, ToJSON a) => PeriodicReportRow a b -> Value
ToJSON)

-- | Figure out the overall date span of a PeridicReport
periodicReportSpan :: PeriodicReport a b -> DateSpan
periodicReportSpan :: PeriodicReport a b -> DateSpan
periodicReportSpan (PeriodicReport [] _ _)       = Maybe Day -> Maybe Day -> DateSpan
DateSpan Maybe Day
forall a. Maybe a
Nothing Maybe Day
forall a. Maybe a
Nothing
periodicReportSpan (PeriodicReport colspans :: [DateSpan]
colspans _ _) = Maybe Day -> Maybe Day -> DateSpan
DateSpan (DateSpan -> Maybe Day
spanStart (DateSpan -> Maybe Day) -> DateSpan -> Maybe Day
forall a b. (a -> b) -> a -> b
$ [DateSpan] -> DateSpan
forall a. [a] -> a
head [DateSpan]
colspans) (DateSpan -> Maybe Day
spanEnd (DateSpan -> Maybe Day) -> DateSpan -> Maybe Day
forall a b. (a -> b) -> a -> b
$ [DateSpan] -> DateSpan
forall a. [a] -> a
last [DateSpan]
colspans)

-- | Given a PeriodicReport and its normal balance sign,
-- if it is known to be normally negative, convert it to normally positive.
prNormaliseSign :: Num b => NormalSign -> PeriodicReport a b -> PeriodicReport a b
prNormaliseSign :: NormalSign -> PeriodicReport a b -> PeriodicReport a b
prNormaliseSign NormallyNegative = PeriodicReport a b -> PeriodicReport a b
forall b a. Num b => PeriodicReport a b -> PeriodicReport a b
prNegate
prNormaliseSign _ = PeriodicReport a b -> PeriodicReport a b
forall a. a -> a
id

-- | Flip the sign of all amounts in a PeriodicReport.
prNegate :: Num b => PeriodicReport a b -> PeriodicReport a b
prNegate :: PeriodicReport a b -> PeriodicReport a b
prNegate (PeriodicReport colspans :: [DateSpan]
colspans rows :: [PeriodicReportRow a b]
rows totalsrow :: PeriodicReportRow () b
totalsrow) =
    [DateSpan]
-> [PeriodicReportRow a b]
-> PeriodicReportRow () b
-> PeriodicReport a b
forall a b.
[DateSpan]
-> [PeriodicReportRow a b]
-> PeriodicReportRow () b
-> PeriodicReport a b
PeriodicReport [DateSpan]
colspans ((PeriodicReportRow a b -> PeriodicReportRow a b)
-> [PeriodicReportRow a b] -> [PeriodicReportRow a b]
forall a b. (a -> b) -> [a] -> [b]
map PeriodicReportRow a b -> PeriodicReportRow a b
forall b a. Num b => PeriodicReportRow a b -> PeriodicReportRow a b
rowNegate [PeriodicReportRow a b]
rows) (PeriodicReportRow () b -> PeriodicReportRow () b
forall b a. Num b => PeriodicReportRow a b -> PeriodicReportRow a b
rowNegate PeriodicReportRow () b
totalsrow)
  where
    rowNegate :: PeriodicReportRow a b -> PeriodicReportRow a b
rowNegate (PeriodicReportRow name :: a
name indent :: Int
indent amts :: [b]
amts tot :: b
tot avg :: b
avg) =
        a -> Int -> [b] -> b -> b -> PeriodicReportRow a b
forall a b. a -> Int -> [b] -> b -> b -> PeriodicReportRow a b
PeriodicReportRow a
name Int
indent ((b -> b) -> [b] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map b -> b
forall a. Num a => a -> a
negate [b]
amts) (-b
tot) (-b
avg)