1 {-# LANGUAGE FlexibleContexts #-}
2 -- {-# LANGUAGE DeriveDataTypeable #-}
3 {-# LANGUAGE NamedFieldPuns #-}
4 -- {-# LANGUAGE StandaloneDeriving #-}
5 {-# LANGUAGE TypeFamilies #-}
6 -- {-# OPTIONS_GHC -fno-warn-deprecations #-}
7 -- FIXME: to be removed when dropping GHC-7.6 support
8 module Hcompta.Chart where
12 import Text.Show (Show(..))
14 import Hcompta.Account
17 chart_account_tags :: c -> account -> Account_Tags
18 chart_anchor_account :: c -> Account_Anchor -> account
21 import Control.DeepSeq (NFData(..))
22 import Data.Data (Data)
23 import Data.Eq (Eq(..))
24 import Data.Function (($), (.), on)
25 import Data.Functor (Functor(..))
26 -- import Data.List (concat)
27 -- import Data.List.NonEmpty (NonEmpty(..))
28 import Data.Map.Strict (Map)
29 import qualified Data.Map.Strict as Map
30 import Data.Monoid (Monoid(..))
31 import Data.Ord (Ord(..))
32 import Data.TreeMap.Strict (TreeMap)
33 import qualified Data.TreeMap.Strict as TreeMap
36 import Text.Show (Show(..))
38 import qualified Hcompta.Account as Account
39 import Hcompta.Account (Account(..), Account_Anchor, Account_Tags(..))
40 -- import qualified Hcompta.Anchor as Anchor
41 import Hcompta.Tag (Tags(..))
46 => Chart account account_data
48 { chart_accounts :: TreeMap (Account_Section account) account_data
49 , chart_anchors :: Map Account_Anchor account
52 deriving instance ( Account account
55 ) => Data (Chart account account_data)
56 deriving instance ( Account account
59 ) => Show (Chart account account_data)
60 deriving instance Typeable2 Chart
65 ) => Monoid (Chart account account_data) where
68 { chart_accounts = mempty
69 , chart_anchors = mempty
73 { chart_accounts = chart_accounts x `mappend` chart_accounts y
74 , chart_anchors = chart_anchors x `mappend` chart_anchors y
76 data Charted account_section account account_data a
78 { chart :: Chart account_section account account_data
81 deriving (Data, Show, Typeable)
86 class Chart_Account_Data account_data where
87 chart_account_data_tags :: account_data -> Account_Tags
91 , Chart_Account_Data account_data
92 ) => Chart account account_data
94 { chart_accounts :: TreeMap (Account_Section account) account_data
95 , chart_anchors :: Map Account_Anchor account
96 -- , chart_tags :: Account_Tags
98 deriving instance ( Account account
99 , Chart_Account_Data account_data
102 ) => Data (Chart account account_data)
103 deriving instance ( Account account
106 ) => Eq (Chart account account_data)
107 deriving instance ( Account account
109 ) => Show (Chart account account_data)
110 deriving instance Typeable2 Chart
111 -- FIXME: use 'Typeable' when dropping GHC-7.6 support
113 instance Account account
114 => Monoid (Chart account_data account) where
117 { chart_accounts = mempty
118 -- , chart_tags = mempty
119 , chart_anchors = mempty
123 { chart_accounts = chart_accounts x `mappend` chart_accounts y
124 -- , chart_tags = chart_tags x `mappend` chart_tags y
125 , chart_anchors = chart_anchors x `mappend` chart_anchors y
131 ) => NFData (Chart account_data account) where
132 rnf Chart{chart_accounts, chart_anchors} =
133 rnf chart_accounts `seq`
136 -- | Return the 'Account_Tags'
137 -- associated with the given 'Account_Path'
138 -- in the given 'Chart'.
139 account_tags :: Account account => account -> Chart account account_data -> Account_Tags
140 account_tags acct chart =
141 Account_Tags $ Tags $
142 Map.unionsWith mappend $
143 fmap ((\(Account_Tags (Tags tags)) -> tags) . chart_account_data_tags) $
144 TreeMap.find_along (account_path acct) $
147 data Charted account_data account x
149 { charted_chart :: Chart account account_data
152 deriving (Data, Show, Typeable)
155 ( Account.Account account
157 ) => Account.Account (Charted account account_data x) where
158 type Account_Section (Charted account account_data x) = Account.Account_Section x
159 account_path = Account.account_path . charted
163 ) => Eq (Charted account account_data x) where
164 (==) = (==) `on` charted
168 ) => Ord (Charted account account_data x) where
169 compare = compare `on` charted
170 instance Functor (Charted account account_data) where
171 fmap f (Charted a x) = Charted a $ f x
175 ) => Monoid (Charted account account_data x) where
176 mempty = Charted mempty mempty
177 mappend (Charted xc xt) (Charted yc yt) =
178 Charted (mappend xc yc) (mappend xt yt)
181 , NFData account, NFData x
182 ) => NFData (Charted account account_data x) where
183 rnf (Charted c x) = rnf c `seq` rnf x
187 -- | Return the 'Account's associated with
189 -- in the given 'Chart'.
190 tag_accounts :: Tag -> Chart -> [Account]
191 tag_accounts (p:|ps, value) chart =
193 TreeMap.find_along (p:|ps `mappend` [value]) $