1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
5 {-# LANGUAGE OverloadedStrings #-}
6 {-# LANGUAGE StandaloneDeriving #-}
7 {-# LANGUAGE TypeFamilies #-}
8 {-# OPTIONS_GHC -fno-warn-orphans #-}
9 module Hcompta.Format.JCC.Amount where
11 import Control.DeepSeq
14 import Data.Eq (Eq(..))
15 -- import qualified Data.Foldable
16 -- import qualified Data.List
17 -- import Data.Map.Strict (Map)
18 import Data.Ord (Ord(..), Ordering(..))
19 -- import Data.String (IsString)
20 -- import Data.Text (Text)
21 -- import qualified Data.Text as Text
22 import Data.Typeable ()
23 import Prelude ((.), seq)
24 -- import Prelude (($), (.), Bounded(..), Int, Num(..), flip, seq, error)
25 import Text.Show (Show(..))
27 import qualified Hcompta.Amount as Amount
28 import qualified Hcompta.Filter as Filter
29 import qualified Hcompta.Polarize as Polarize
30 import qualified Hcompta.Quantity as Quantity
31 import qualified Hcompta.Unit as Unit
32 import qualified Hcompta.Format.JCC.Amount.Style as Style
33 import Hcompta.Format.JCC.Quantity (Quantity)
34 import Hcompta.Format.JCC.Unit (Unit(..))
38 type Style = Style.Style
39 type Styles = Style.Styles
40 type Styled t = Style.Styled t
46 { amount_unit :: !Unit
47 , amount_quantity :: !Quantity
48 } deriving (Data, Show, Typeable)
49 instance Amount.Amount Amount where
50 type Amount_Quantity Amount = Quantity
51 type Amount_Unit Amount = Unit
52 amount_quantity = amount_quantity
53 amount_unit = amount_unit
54 instance Filter.Amount Amount where
55 type Amount_Quantity Amount = Quantity
56 type Amount_Unit Amount = Unit
57 amount_quantity = Polarize.polarize . amount_quantity
58 amount_unit = amount_unit
59 instance NFData Amount where
60 rnf (Amount q u) = rnf q `seq` rnf u
61 instance Quantity.Zero Amount where
62 quantity_zero = Amount Unit.unit_empty Quantity.quantity_zero
63 quantity_null = (==) Quantity.quantity_zero . amount_quantity
65 amount_style :: Styles -> Amount -> Style
66 amount_style styles = Style.style styles . amount_unit
68 style :: Styles -> Amount -> Styled Amount
69 style styles amt = (amount_style styles amt, amt)
71 sign :: Amount -> Ordering
73 case amount_quantity a of
83 { amount_quantity = Quantity.quantity_zero
89 -- | Return 'True' if and only if 'Amount'’s 'Quantity' is zero.
91 -- NOTE: the 'Amount'’s 'amount_quantity' MUST have been rounded
92 -- at 'Amount'’s 'amount_style'’s 'Style.precision'.
93 null :: Amount -> Bool
94 null = Quantity.quantity_null . amount_quantity