1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE ScopedTypeVariables #-}
3 module Hcompta.Model.Transaction.Posting where
6 import qualified Data.Foldable
7 import qualified Data.List
8 import qualified Data.Map
10 import Data.Typeable ()
11 import Text.Parsec.Pos (SourcePos, initialPos)
13 import qualified Hcompta.Model.Account as Account
14 import Hcompta.Model.Account (Account)
15 import qualified Hcompta.Model.Amount as Amount
16 import Hcompta.Model.Amount (Amount)
17 import qualified Hcompta.Model.Date as Date
18 import Hcompta.Model.Date (Date)
19 import qualified Hcompta.Model.Transaction.Tag as Tag
20 import Hcompta.Model.Transaction.Tag (Tag)
22 -- * The 'Posting' type
27 , amounts :: Amount.By_Unit
32 , sourcepos :: SourcePos
35 } deriving (Data, Eq, Read, Show, Typeable)
37 instance Read SourcePos where
38 readsPrec _ s = [(initialPos s, "")]
43 | Type_Virtual_Balanced
44 deriving (Data, Eq, Read, Show, Typeable)
46 -- ** Convenient constructors
52 , amounts = Data.Map.empty
57 , sourcepos = initialPos ""
58 , tags = Data.Map.empty
59 , type_ = Type_Regular
62 -- * The 'By_Account' mapping
65 = Map Account [Posting]
67 -- ** Convenient constructors
69 -- | Return a tuple associating the given posting with its account.
70 by_account :: Posting -> (Account, Posting)
71 by_account posting = (account posting, posting)
73 -- | Return a 'Data.Map.Map' associating the given 'Posting's with their respective 'Unit'.
74 from_List :: [Posting] -> By_Account
76 Data.Map.fromListWith (++) $
78 (\posting -> (account posting, [posting]))
83 -- | Return the units in use within the given postings
85 :: Data.Foldable.Foldable m
93 (Data.Map.keys . amounts))