{-# 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)
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
isAssetType :: AccountType -> Bool
isAssetType :: AccountType -> Bool
isAssetType = (AccountType -> AccountType -> Bool
`isAccountSubtypeOf` AccountType
Asset)
isLiabilityType :: AccountType -> Bool
isLiabilityType :: AccountType -> Bool
isLiabilityType = (AccountType -> AccountType -> Bool
`isAccountSubtypeOf` AccountType
Liability)
isEquityType :: AccountType -> Bool
isEquityType :: AccountType -> Bool
isEquityType = (AccountType -> AccountType -> Bool
`isAccountSubtypeOf` AccountType
Equity)
isRevenueType :: AccountType -> Bool
isRevenueType :: AccountType -> Bool
isRevenueType = (AccountType -> AccountType -> Bool
`isAccountSubtypeOf` AccountType
Revenue)
isExpenseType :: AccountType -> Bool
isExpenseType :: AccountType -> Bool
isExpenseType = (AccountType -> AccountType -> Bool
`isAccountSubtypeOf` AccountType
Expense)
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"
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"]]
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]
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)
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
lotDirectionSign :: LotDirection -> Quantity
lotDirectionSign :: LotDirection -> Quantity
lotDirectionSign LotDirection
Long = Quantity
1
lotDirectionSign LotDirection
Short = -Quantity
1