{-|

Helpers for working with 'AccountType's.
Subtypes (Cash, Conversion, Gain, UnrealisedGain, Imbalance) are recognised as their parent type
where appropriate.

-}

{-# LANGUAGE OverloadedStrings #-}

module Hledger.Data.AccountType (
  isAccountSubtypeOf,
  isAssetType,
  isLiabilityType,
  isEquityType,
  isRevenueType,
  isExpenseType,
  accountTypeName,
  parseAccountType,
  accountTypeChoices,
  LotDirection(..),
  accountTypeLotDirection,
  lotDirectionSign,
) where

import Data.List (intercalate)
import Data.Text (Text)
import Data.Text qualified as T

import Hledger.Data.Types (AccountType(..), Quantity)

-- | Check whether the first argument is a subtype of the second: either equal
-- or one of the defined subtypes.
isAccountSubtypeOf :: AccountType -> AccountType -> Bool
isAccountSubtypeOf :: AccountType -> AccountType -> Bool
isAccountSubtypeOf AccountType
Asset          AccountType
Asset          = Bool
True
isAccountSubtypeOf AccountType
Liability      AccountType
Liability      = Bool
True
isAccountSubtypeOf AccountType
Equity         AccountType
Equity         = Bool
True
isAccountSubtypeOf AccountType
Revenue        AccountType
Revenue        = Bool
True
isAccountSubtypeOf AccountType
Expense        AccountType
Expense        = Bool
True
isAccountSubtypeOf AccountType
Cash           AccountType
Cash           = Bool
True
isAccountSubtypeOf AccountType
Cash           AccountType
Asset          = Bool
True
isAccountSubtypeOf AccountType
Conversion     AccountType
Conversion     = Bool
True
isAccountSubtypeOf AccountType
Conversion     AccountType
Equity         = Bool
True
isAccountSubtypeOf AccountType
Gain           AccountType
Gain           = Bool
True
isAccountSubtypeOf AccountType
Gain           AccountType
Revenue        = Bool
True
isAccountSubtypeOf AccountType
UnrealisedGain AccountType
UnrealisedGain = Bool
True
isAccountSubtypeOf AccountType
UnrealisedGain AccountType
Equity         = Bool
True
isAccountSubtypeOf AccountType
Imbalance      AccountType
Imbalance      = Bool
True
isAccountSubtypeOf AccountType
Imbalance      AccountType
Equity         = Bool
True
isAccountSubtypeOf AccountType
_              AccountType
_              = Bool
False

-- | Is this an Asset or Cash (subtype of Asset) account type ?
isAssetType :: AccountType -> Bool
isAssetType :: AccountType -> Bool
isAssetType = (AccountType -> AccountType -> Bool
`isAccountSubtypeOf` AccountType
Asset)

-- | Is this a Liability account type ?
isLiabilityType :: AccountType -> Bool
isLiabilityType :: AccountType -> Bool
isLiabilityType = (AccountType -> AccountType -> Bool
`isAccountSubtypeOf` AccountType
Liability)

-- | Is this an Equity or Conversion (subtype of Equity) account type ?
isEquityType :: AccountType -> Bool
isEquityType :: AccountType -> Bool
isEquityType = (AccountType -> AccountType -> Bool
`isAccountSubtypeOf` AccountType
Equity)

-- | Is this a Revenue or Gain (subtype of Revenue) account type ?
isRevenueType :: AccountType -> Bool
isRevenueType :: AccountType -> Bool
isRevenueType = (AccountType -> AccountType -> Bool
`isAccountSubtypeOf` AccountType
Revenue)

-- | Is this an Expense account type ?
isExpenseType :: AccountType -> Bool
isExpenseType :: AccountType -> Bool
isExpenseType = (AccountType -> AccountType -> Bool
`isAccountSubtypeOf` AccountType
Expense)

-- | An account type's long-form name (its one-letter code is its 'show' value).
accountTypeName :: AccountType -> Text
accountTypeName :: AccountType -> Text
accountTypeName AccountType
Asset          = Text
"Asset"
accountTypeName AccountType
Liability      = Text
"Liability"
accountTypeName AccountType
Equity         = Text
"Equity"
accountTypeName AccountType
Revenue        = Text
"Revenue"
accountTypeName AccountType
Expense        = Text
"Expense"
accountTypeName AccountType
Cash           = Text
"Cash"
accountTypeName AccountType
Conversion     = Text
"Conversion"
accountTypeName AccountType
Gain           = Text
"Gain"
accountTypeName AccountType
UnrealisedGain = Text
"UnrealisedGain"
accountTypeName AccountType
Imbalance      = Text
"Imbalance"

-- | Case-insensitively parse an account type's one-letter code,
-- or if permitted, its long-form name (or another accepted spelling of that).
-- On failure, returns the unparseable text.
parseAccountType :: Bool -> Text -> Either String AccountType
parseAccountType :: Bool -> Text -> Either String AccountType
parseAccountType Bool
allowlongform Text
s =
  Either String AccountType
-> (AccountType -> Either String AccountType)
-> Maybe AccountType
-> Either String AccountType
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> Either String AccountType
forall a b. a -> Either a b
Left (String -> Either String AccountType)
-> String -> Either String AccountType
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack Text
s) AccountType -> Either String AccountType
forall a b. b -> Either a b
Right (Maybe AccountType -> Either String AccountType)
-> Maybe AccountType -> Either String AccountType
forall a b. (a -> b) -> a -> b
$ Text -> [(Text, AccountType)] -> Maybe AccountType
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup (Text -> Text
T.toLower Text
s) ([(Text, AccountType)] -> Maybe AccountType)
-> [(Text, AccountType)] -> Maybe AccountType
forall a b. (a -> b) -> a -> b
$
    [(Text -> Text
T.toLower (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ AccountType -> String
forall a. Show a => a -> String
show AccountType
t, AccountType
t) | AccountType
t <- [AccountType
forall a. Bounded a => a
minBound..AccountType
forall a. Bounded a => a
maxBound]]
    [(Text, AccountType)]
-> [(Text, AccountType)] -> [(Text, AccountType)]
forall a. [a] -> [a] -> [a]
++ if Bool
allowlongform then [(Text -> Text
T.toLower (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ AccountType -> Text
accountTypeName AccountType
t, AccountType
t) | AccountType
t <- [AccountType
forall a. Bounded a => a
minBound..AccountType
forall a. Bounded a => a
maxBound]] [(Text, AccountType)]
-> [(Text, AccountType)] -> [(Text, AccountType)]
forall a. [a] -> [a] -> [a]
++ [(Text, AccountType)]
otherspellings else []
  where
    otherspellings :: [(Text, AccountType)]
otherspellings =
      (Text
"gains", AccountType
Gain) (Text, AccountType)
-> [(Text, AccountType)] -> [(Text, AccountType)]
forall a. a -> [a] -> [a]
:
      [(Text
n, AccountType
UnrealisedGain) | Text
n <- [Text
"unrealised", Text
"unrealised-gain", Text
"unrealised-gains", Text
"unrealized", Text
"unrealizedgain", Text
"unrealized-gain", Text
"unrealized-gains"]]

-- | The account type codes, and if permitted their long-form names, for showing in messages.
accountTypeChoices :: Bool -> String
accountTypeChoices :: Bool -> String
accountTypeChoices Bool
allowlongform = String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$
  (AccountType -> String) -> [AccountType] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map AccountType -> String
forall a. Show a => a -> String
show [AccountType]
types [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ if Bool
allowlongform then (AccountType -> String) -> [AccountType] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> String
T.unpack (Text -> String) -> (AccountType -> Text) -> AccountType -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AccountType -> Text
accountTypeName) [AccountType]
types else []
  where types :: [AccountType]
types = [AccountType
forall a. Bounded a => a
minBound..AccountType
forall a. Bounded a => a
maxBound] :: [AccountType]

-- | Which way lots flow in a lot-tracking account. In a Long account
-- (assets) a positive posting opens a lot and a negative one closes it; in
-- a Short account (liabilities, holding short positions) it is the reverse:
-- a negative posting opens a short lot and a positive one closes (covers) it.
data LotDirection = Long | Short
  deriving (LotDirection -> LotDirection -> Bool
(LotDirection -> LotDirection -> Bool)
-> (LotDirection -> LotDirection -> Bool) -> Eq LotDirection
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LotDirection -> LotDirection -> Bool
== :: LotDirection -> LotDirection -> Bool
$c/= :: LotDirection -> LotDirection -> Bool
/= :: LotDirection -> LotDirection -> Bool
Eq, Eq LotDirection
Eq LotDirection =>
(LotDirection -> LotDirection -> Ordering)
-> (LotDirection -> LotDirection -> Bool)
-> (LotDirection -> LotDirection -> Bool)
-> (LotDirection -> LotDirection -> Bool)
-> (LotDirection -> LotDirection -> Bool)
-> (LotDirection -> LotDirection -> LotDirection)
-> (LotDirection -> LotDirection -> LotDirection)
-> Ord LotDirection
LotDirection -> LotDirection -> Bool
LotDirection -> LotDirection -> Ordering
LotDirection -> LotDirection -> LotDirection
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: LotDirection -> LotDirection -> Ordering
compare :: LotDirection -> LotDirection -> Ordering
$c< :: LotDirection -> LotDirection -> Bool
< :: LotDirection -> LotDirection -> Bool
$c<= :: LotDirection -> LotDirection -> Bool
<= :: LotDirection -> LotDirection -> Bool
$c> :: LotDirection -> LotDirection -> Bool
> :: LotDirection -> LotDirection -> Bool
$c>= :: LotDirection -> LotDirection -> Bool
>= :: LotDirection -> LotDirection -> Bool
$cmax :: LotDirection -> LotDirection -> LotDirection
max :: LotDirection -> LotDirection -> LotDirection
$cmin :: LotDirection -> LotDirection -> LotDirection
min :: LotDirection -> LotDirection -> LotDirection
Ord, Int -> LotDirection -> ShowS
[LotDirection] -> ShowS
LotDirection -> String
(Int -> LotDirection -> ShowS)
-> (LotDirection -> String)
-> ([LotDirection] -> ShowS)
-> Show LotDirection
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LotDirection -> ShowS
showsPrec :: Int -> LotDirection -> ShowS
$cshow :: LotDirection -> String
show :: LotDirection -> String
$cshowList :: [LotDirection] -> ShowS
showList :: [LotDirection] -> ShowS
Show)

-- | The lot direction of an account type: Long for assets (and Cash),
-- Short for liabilities, none for other types, which don't hold lots.
accountTypeLotDirection :: AccountType -> Maybe LotDirection
accountTypeLotDirection :: AccountType -> Maybe LotDirection
accountTypeLotDirection AccountType
t
  | AccountType -> Bool
isAssetType AccountType
t     = LotDirection -> Maybe LotDirection
forall a. a -> Maybe a
Just LotDirection
Long
  | AccountType -> Bool
isLiabilityType AccountType
t = LotDirection -> Maybe LotDirection
forall a. a -> Maybe a
Just LotDirection
Short
  | Bool
otherwise         = Maybe LotDirection
forall a. Maybe a
Nothing

-- | The sign of a posting that opens a lot in this direction: 1 or -1.
-- Multiplying a posting quantity by this gives its lot flow: positive
-- when it opens or receives a lot, negative when it closes or sends one.
lotDirectionSign :: LotDirection -> Quantity
lotDirectionSign :: LotDirection -> Quantity
lotDirectionSign LotDirection
Long  = Quantity
1
lotDirectionSign LotDirection
Short = -Quantity
1