{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}

-- | This module provides definitions for acccounts and types of accounts as
-- they are used in accounting reporting.
module Haspara.Accounting.Account where

import qualified Data.Aeson as Aeson
import Data.Hashable (Hashable)
import qualified Data.Text as T
import GHC.Generics (Generic)
import Haspara.Internal.Aeson (aesonOptionsForSingleTag, commonAesonOptions)


-- * Account Kind


-- | Type encoding for ledger account type.
--
-- This type covers both balance sheet and income statement account types:
--
-- 1. For balance sheet accounts:
--     1. Asset ('AccountKindAsset')
--     2. Liability ('AccountKindLiability')
--     3. Equity ('AccountKindEquity')
-- 2. For income statement accounts:
--     1. Revenue ('AccountKindRevenue')
--     2. Expense ('AccountKindExpense')
--
-- 'Data.Aeson.FromJSON' and 'Data.Aeson.ToJSON' instances, too:
--
-- >>> :set -XTypeApplications
-- >>> Data.Aeson.decode @AccountKind "\"ASSET\""
-- Just AccountKindAsset
-- >>> Data.Aeson.decode @AccountKind "\"LIABILITY\""
-- Just AccountKindLiability
-- >>> Data.Aeson.decode @AccountKind "\"EQUITY\""
-- Just AccountKindEquity
-- >>> Data.Aeson.decode @AccountKind "\"REVENUE\""
-- Just AccountKindRevenue
-- >>> Data.Aeson.decode @AccountKind "\"EXPENSE\""
-- Just AccountKindExpense
-- >>> Data.Aeson.encode AccountKindAsset
-- "\"ASSET\""
-- >>> Data.Aeson.encode AccountKindLiability
-- "\"LIABILITY\""
-- >>> Data.Aeson.encode AccountKindEquity
-- "\"EQUITY\""
-- >>> Data.Aeson.encode AccountKindRevenue
-- "\"REVENUE\""
-- >>> Data.Aeson.encode AccountKindExpense
-- "\"EXPENSE\""
data AccountKind
  = AccountKindAsset
  | AccountKindLiability
  | AccountKindEquity
  | AccountKindRevenue
  | AccountKindExpense
  deriving (AccountKind
AccountKind -> AccountKind -> Bounded AccountKind
forall a. a -> a -> Bounded a
$cminBound :: AccountKind
minBound :: AccountKind
$cmaxBound :: AccountKind
maxBound :: AccountKind
Bounded, Int -> AccountKind
AccountKind -> Int
AccountKind -> [AccountKind]
AccountKind -> AccountKind
AccountKind -> AccountKind -> [AccountKind]
AccountKind -> AccountKind -> AccountKind -> [AccountKind]
(AccountKind -> AccountKind)
-> (AccountKind -> AccountKind)
-> (Int -> AccountKind)
-> (AccountKind -> Int)
-> (AccountKind -> [AccountKind])
-> (AccountKind -> AccountKind -> [AccountKind])
-> (AccountKind -> AccountKind -> [AccountKind])
-> (AccountKind -> AccountKind -> AccountKind -> [AccountKind])
-> Enum AccountKind
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: AccountKind -> AccountKind
succ :: AccountKind -> AccountKind
$cpred :: AccountKind -> AccountKind
pred :: AccountKind -> AccountKind
$ctoEnum :: Int -> AccountKind
toEnum :: Int -> AccountKind
$cfromEnum :: AccountKind -> Int
fromEnum :: AccountKind -> Int
$cenumFrom :: AccountKind -> [AccountKind]
enumFrom :: AccountKind -> [AccountKind]
$cenumFromThen :: AccountKind -> AccountKind -> [AccountKind]
enumFromThen :: AccountKind -> AccountKind -> [AccountKind]
$cenumFromTo :: AccountKind -> AccountKind -> [AccountKind]
enumFromTo :: AccountKind -> AccountKind -> [AccountKind]
$cenumFromThenTo :: AccountKind -> AccountKind -> AccountKind -> [AccountKind]
enumFromThenTo :: AccountKind -> AccountKind -> AccountKind -> [AccountKind]
Enum, AccountKind -> AccountKind -> Bool
(AccountKind -> AccountKind -> Bool)
-> (AccountKind -> AccountKind -> Bool) -> Eq AccountKind
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AccountKind -> AccountKind -> Bool
== :: AccountKind -> AccountKind -> Bool
$c/= :: AccountKind -> AccountKind -> Bool
/= :: AccountKind -> AccountKind -> Bool
Eq, (forall x. AccountKind -> Rep AccountKind x)
-> (forall x. Rep AccountKind x -> AccountKind)
-> Generic AccountKind
forall x. Rep AccountKind x -> AccountKind
forall x. AccountKind -> Rep AccountKind x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. AccountKind -> Rep AccountKind x
from :: forall x. AccountKind -> Rep AccountKind x
$cto :: forall x. Rep AccountKind x -> AccountKind
to :: forall x. Rep AccountKind x -> AccountKind
Generic, Eq AccountKind
Eq AccountKind =>
(AccountKind -> AccountKind -> Ordering)
-> (AccountKind -> AccountKind -> Bool)
-> (AccountKind -> AccountKind -> Bool)
-> (AccountKind -> AccountKind -> Bool)
-> (AccountKind -> AccountKind -> Bool)
-> (AccountKind -> AccountKind -> AccountKind)
-> (AccountKind -> AccountKind -> AccountKind)
-> Ord AccountKind
AccountKind -> AccountKind -> Bool
AccountKind -> AccountKind -> Ordering
AccountKind -> AccountKind -> AccountKind
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 :: AccountKind -> AccountKind -> Ordering
compare :: AccountKind -> AccountKind -> Ordering
$c< :: AccountKind -> AccountKind -> Bool
< :: AccountKind -> AccountKind -> Bool
$c<= :: AccountKind -> AccountKind -> Bool
<= :: AccountKind -> AccountKind -> Bool
$c> :: AccountKind -> AccountKind -> Bool
> :: AccountKind -> AccountKind -> Bool
$c>= :: AccountKind -> AccountKind -> Bool
>= :: AccountKind -> AccountKind -> Bool
$cmax :: AccountKind -> AccountKind -> AccountKind
max :: AccountKind -> AccountKind -> AccountKind
$cmin :: AccountKind -> AccountKind -> AccountKind
min :: AccountKind -> AccountKind -> AccountKind
Ord, Int -> AccountKind -> ShowS
[AccountKind] -> ShowS
AccountKind -> [Char]
(Int -> AccountKind -> ShowS)
-> (AccountKind -> [Char])
-> ([AccountKind] -> ShowS)
-> Show AccountKind
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AccountKind -> ShowS
showsPrec :: Int -> AccountKind -> ShowS
$cshow :: AccountKind -> [Char]
show :: AccountKind -> [Char]
$cshowList :: [AccountKind] -> ShowS
showList :: [AccountKind] -> ShowS
Show)


instance Hashable AccountKind


instance Aeson.FromJSON AccountKind where
  parseJSON :: Value -> Parser AccountKind
parseJSON = Options -> Value -> Parser AccountKind
forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
Aeson.genericParseJSON (Options -> Value -> Parser AccountKind)
-> Options -> Value -> Parser AccountKind
forall a b. (a -> b) -> a -> b
$ [Char] -> Options
aesonOptionsForSingleTag [Char]
"AccountKind"


instance Aeson.ToJSON AccountKind where
  toJSON :: AccountKind -> Value
toJSON = Options -> AccountKind -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
Aeson.genericToJSON (Options -> AccountKind -> Value)
-> Options -> AccountKind -> Value
forall a b. (a -> b) -> a -> b
$ [Char] -> Options
aesonOptionsForSingleTag [Char]
"AccountKind"
  toEncoding :: AccountKind -> Encoding
toEncoding = Options -> AccountKind -> Encoding
forall a.
(Generic a, GToJSON' Encoding Zero (Rep a)) =>
Options -> a -> Encoding
Aeson.genericToEncoding (Options -> AccountKind -> Encoding)
-> Options -> AccountKind -> Encoding
forall a b. (a -> b) -> a -> b
$ [Char] -> Options
aesonOptionsForSingleTag [Char]
"AccountKind"


-- | Provides textual representation of a given 'AccountKind'.
--
-- >>> accountKindText AccountKindAsset
-- "Asset"
-- >>> accountKindText AccountKindLiability
-- "Liability"
-- >>> accountKindText AccountKindEquity
-- "Equity"
-- >>> accountKindText AccountKindRevenue
-- "Revenue"
-- >>> accountKindText AccountKindExpense
-- "Expense"
accountKindText :: AccountKind -> T.Text
accountKindText :: AccountKind -> Text
accountKindText AccountKind
AccountKindAsset = Text
"Asset"
accountKindText AccountKind
AccountKindLiability = Text
"Liability"
accountKindText AccountKind
AccountKindEquity = Text
"Equity"
accountKindText AccountKind
AccountKindRevenue = Text
"Revenue"
accountKindText AccountKind
AccountKindExpense = Text
"Expense"


-- * Account


-- | Type encoding for account values.
--
-- This definition provides both the 'AccountKind' and an arbitrary object
-- identifying the account. This arbitrary nature provides flexibility to
-- use-site to use its own account identity and accompanying information when
-- required.
--
-- >>> :set -XTypeApplications
-- >>> let acc = Account AccountKindAsset (1 ::Int)
-- >>> Data.Aeson.encode acc
-- "{\"kind\":\"ASSET\",\"object\":1}"
-- >>> Data.Aeson.decode @(Account Int) (Data.Aeson.encode acc)
-- Just (Account {accountKind = AccountKindAsset, accountObject = 1})
-- >>> Data.Aeson.decode (Data.Aeson.encode acc) == Just acc
-- True
data Account o = Account
  { forall o. Account o -> AccountKind
accountKind :: !AccountKind
  , forall o. Account o -> o
accountObject :: !o
  }
  deriving (Account o -> Account o -> Bool
(Account o -> Account o -> Bool)
-> (Account o -> Account o -> Bool) -> Eq (Account o)
forall o. Eq o => Account o -> Account o -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall o. Eq o => Account o -> Account o -> Bool
== :: Account o -> Account o -> Bool
$c/= :: forall o. Eq o => Account o -> Account o -> Bool
/= :: Account o -> Account o -> Bool
Eq, (forall x. Account o -> Rep (Account o) x)
-> (forall x. Rep (Account o) x -> Account o)
-> Generic (Account o)
forall x. Rep (Account o) x -> Account o
forall x. Account o -> Rep (Account o) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall o x. Rep (Account o) x -> Account o
forall o x. Account o -> Rep (Account o) x
$cfrom :: forall o x. Account o -> Rep (Account o) x
from :: forall x. Account o -> Rep (Account o) x
$cto :: forall o x. Rep (Account o) x -> Account o
to :: forall x. Rep (Account o) x -> Account o
Generic, Eq (Account o)
Eq (Account o) =>
(Account o -> Account o -> Ordering)
-> (Account o -> Account o -> Bool)
-> (Account o -> Account o -> Bool)
-> (Account o -> Account o -> Bool)
-> (Account o -> Account o -> Bool)
-> (Account o -> Account o -> Account o)
-> (Account o -> Account o -> Account o)
-> Ord (Account o)
Account o -> Account o -> Bool
Account o -> Account o -> Ordering
Account o -> Account o -> Account o
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
forall o. Ord o => Eq (Account o)
forall o. Ord o => Account o -> Account o -> Bool
forall o. Ord o => Account o -> Account o -> Ordering
forall o. Ord o => Account o -> Account o -> Account o
$ccompare :: forall o. Ord o => Account o -> Account o -> Ordering
compare :: Account o -> Account o -> Ordering
$c< :: forall o. Ord o => Account o -> Account o -> Bool
< :: Account o -> Account o -> Bool
$c<= :: forall o. Ord o => Account o -> Account o -> Bool
<= :: Account o -> Account o -> Bool
$c> :: forall o. Ord o => Account o -> Account o -> Bool
> :: Account o -> Account o -> Bool
$c>= :: forall o. Ord o => Account o -> Account o -> Bool
>= :: Account o -> Account o -> Bool
$cmax :: forall o. Ord o => Account o -> Account o -> Account o
max :: Account o -> Account o -> Account o
$cmin :: forall o. Ord o => Account o -> Account o -> Account o
min :: Account o -> Account o -> Account o
Ord, Int -> Account o -> ShowS
[Account o] -> ShowS
Account o -> [Char]
(Int -> Account o -> ShowS)
-> (Account o -> [Char])
-> ([Account o] -> ShowS)
-> Show (Account o)
forall o. Show o => Int -> Account o -> ShowS
forall o. Show o => [Account o] -> ShowS
forall o. Show o => Account o -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall o. Show o => Int -> Account o -> ShowS
showsPrec :: Int -> Account o -> ShowS
$cshow :: forall o. Show o => Account o -> [Char]
show :: Account o -> [Char]
$cshowList :: forall o. Show o => [Account o] -> ShowS
showList :: [Account o] -> ShowS
Show)


instance Hashable o => Hashable (Account o)


instance Aeson.FromJSON o => Aeson.FromJSON (Account o) where
  parseJSON :: Value -> Parser (Account o)
parseJSON = Options -> Value -> Parser (Account o)
forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
Aeson.genericParseJSON (Options -> Value -> Parser (Account o))
-> Options -> Value -> Parser (Account o)
forall a b. (a -> b) -> a -> b
$ [Char] -> Options
commonAesonOptions [Char]
"account"


instance Aeson.ToJSON o => Aeson.ToJSON (Account o) where
  toJSON :: Account o -> Value
toJSON = Options -> Account o -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
Aeson.genericToJSON (Options -> Account o -> Value) -> Options -> Account o -> Value
forall a b. (a -> b) -> a -> b
$ [Char] -> Options
commonAesonOptions [Char]
"account"
  toEncoding :: Account o -> Encoding
toEncoding = Options -> Account o -> Encoding
forall a.
(Generic a, GToJSON' Encoding Zero (Rep a)) =>
Options -> a -> Encoding
Aeson.genericToEncoding (Options -> Account o -> Encoding)
-> Options -> Account o -> Encoding
forall a b. (a -> b) -> a -> b
$ [Char] -> Options
commonAesonOptions [Char]
"account"