1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE DeriveDataTypeable #-}
3 {-# LANGUAGE NamedFieldPuns #-}
4 {-# LANGUAGE StandaloneDeriving #-}
5 {-# LANGUAGE TypeFamilies #-}
6 {-# OPTIONS_GHC -fno-warn-deprecations #-} -- FIXME: to be removed when dropping GHC-7.6 support
7 module Hcompta.Chart where
9 import Control.DeepSeq (NFData(..))
10 import Data.Data (Data)
11 import Data.Eq (Eq(..))
12 import Data.Function (on)
13 import Data.Functor (Functor(..))
14 -- import Data.List (concat)
15 -- import Data.List.NonEmpty (NonEmpty(..))
16 import qualified Data.Map.Strict as Map
17 import Data.Map.Strict (Map)
18 import Data.Monoid (Monoid(..))
19 import Data.Ord (Ord(..))
21 import Text.Show (Show(..))
22 import Prelude (($), (.), seq)
24 import qualified Hcompta.Account as Account
25 import Hcompta.Account (Account(..), Account_Anchor, Account_Tags(..))
26 import Hcompta.Lib.TreeMap (TreeMap)
27 import qualified Hcompta.Lib.TreeMap as TreeMap
28 -- import qualified Hcompta.Anchor as Anchor
29 import Hcompta.Tag (Tags(..))
32 chart_account_tags :: c -> account -> Account_Tags
33 chart_anchor_account :: c -> Account_Anchor -> account
37 => Chart account account_data
39 { chart_accounts :: TreeMap (Account_Section account) account_data
40 , chart_anchors :: Map Account_Anchor account
43 deriving instance ( Account account
46 ) => Data (Chart account account_data)
47 deriving instance ( Account account
50 ) => Show (Chart account account_data)
51 deriving instance Typeable2 Chart
56 ) => Monoid (Chart account account_data) where
59 { chart_accounts = mempty
60 , chart_anchors = mempty
64 { chart_accounts = chart_accounts x `mappend` chart_accounts y
65 , chart_anchors = chart_anchors x `mappend` chart_anchors y
69 data Charted account_section account account_data x
71 { chart :: Chart account_section account account_data
74 deriving (Data, Show, Typeable)
77 class Chart_Account_Data account_data where
78 chart_account_data_tags :: account_data -> Account_Tags
82 , Chart_Account_Data account_data
83 ) => Chart account account_data
85 { chart_accounts :: TreeMap (Account_Section account) account_data
86 , chart_anchors :: Map Account_Anchor account
87 -- , chart_tags :: Account_Tags
89 deriving instance ( Account account
90 , Chart_Account_Data account_data
93 ) => Data (Chart account account_data)
94 deriving instance ( Account account
97 ) => Eq (Chart account account_data)
98 deriving instance ( Account account
100 ) => Show (Chart account account_data)
101 deriving instance Typeable2 Chart
102 -- FIXME: use 'Typeable' when dropping GHC-7.6 support
104 instance Account account
105 => Monoid (Chart account_data account) where
108 { chart_accounts = mempty
109 -- , chart_tags = mempty
110 , chart_anchors = mempty
114 { chart_accounts = chart_accounts x `mappend` chart_accounts y
115 -- , chart_tags = chart_tags x `mappend` chart_tags y
116 , chart_anchors = chart_anchors x `mappend` chart_anchors y
122 ) => NFData (Chart account_data account) where
123 rnf Chart{chart_accounts, chart_anchors} =
124 rnf chart_accounts `seq`
127 -- | Return the 'Account_Tags'
128 -- associated with the given 'Account_Path'
129 -- in the given 'Chart'.
130 account_tags :: Account account => account -> Chart account account_data -> Account_Tags
131 account_tags acct chart =
132 Account_Tags $ Tags $
133 Map.unionsWith mappend $
134 fmap ((\(Account_Tags (Tags tags)) -> tags) . chart_account_data_tags) $
135 TreeMap.find_along (account_path acct) $
138 data Charted account_data account x
140 { charted_chart :: Chart account account_data
143 deriving (Data, Show, Typeable)
146 ( Account.Account account
148 ) => Account.Account (Charted account account_data x) where
149 type Account_Section (Charted account account_data x) = Account.Account_Section x
150 account_path = Account.account_path . charted
154 ) => Eq (Charted account account_data x) where
155 (==) = (==) `on` charted
159 ) => Ord (Charted account account_data x) where
160 compare = compare `on` charted
161 instance Functor (Charted account account_data) where
162 fmap f (Charted a x) = Charted a $ f x
166 ) => Monoid (Charted account account_data x) where
167 mempty = Charted mempty mempty
168 mappend (Charted xc xt) (Charted yc yt) =
169 Charted (mappend xc yc) (mappend xt yt)
172 , NFData account, NFData x
173 ) => NFData (Charted account account_data x) where
174 rnf (Charted c x) = rnf c `seq` rnf x
178 -- | Return the 'Account's associated with
180 -- in the given 'Chart'.
181 tag_accounts :: Tag -> Chart -> [Account]
182 tag_accounts (p:|ps, value) chart =
184 TreeMap.find_along (p:|ps `mappend` [value]) $