1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE ScopedTypeVariables #-}
3 {-# OPTIONS_GHC -fno-warn-orphans #-}
4 module Hcompta.Model.Transaction.Posting where
7 import qualified Data.Foldable
8 import qualified Data.List
9 import qualified Data.Map
11 import Data.Typeable ()
12 import Text.Parsec.Pos (SourcePos, initialPos)
14 import qualified Hcompta.Model.Account as Account ()
15 import Hcompta.Model.Account (Account)
16 import qualified Hcompta.Model.Amount as Amount
17 import qualified Hcompta.Model.Date as Date
18 import Hcompta.Model.Date (Date)
19 import qualified Hcompta.Model.Transaction.Tag as Tag
21 -- * The 'Posting' type
26 , amounts :: Amount.By_Unit
31 , sourcepos :: SourcePos
34 } deriving (Data, Eq, Read, Show, Typeable)
36 instance Read SourcePos where
37 readsPrec _ s = [(initialPos s, "")]
42 | Type_Virtual_Balanced
43 deriving (Data, Eq, Read, Show, Typeable)
45 -- ** Convenient constructors
51 , amounts = Data.Map.empty
56 , sourcepos = initialPos ""
57 , tags = Data.Map.empty
58 , type_ = Type_Regular
61 -- * The 'By_Account' mapping
64 = Map Account [Posting]
66 -- ** Convenient constructors
68 -- | Return a tuple associating the given posting with its account.
69 by_account :: Posting -> (Account, Posting)
70 by_account posting = (account posting, posting)
72 -- | Return a 'Data.Map.Map' associating the given 'Posting's with their respective 'Unit'.
73 from_List :: [Posting] -> By_Account
75 Data.Map.fromListWith (++) $
77 (\posting -> (account posting, [posting]))
82 -- | Return the units in use within the given postings
84 :: Data.Foldable.Foldable m
92 (Data.Map.keys . amounts))