]> Git — Sourcephile - comptalang.git/blob - lib/Hcompta/Model/Transaction/Posting.hs
Ajout : Format.Ledger.Read.journal
[comptalang.git] / lib / Hcompta / Model / Transaction / Posting.hs
1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE ScopedTypeVariables #-}
3 {-# OPTIONS_GHC -fno-warn-orphans #-}
4 module Hcompta.Model.Transaction.Posting where
5
6 import Data.Data
7 import qualified Data.Foldable
8 import qualified Data.List
9 import qualified Data.Map.Strict as Data.Map
10 import Data.Text (Text)
11 import Data.Typeable ()
12 import Text.Parsec.Pos (SourcePos, initialPos)
13
14 import qualified Hcompta.Model.Account as Account ()
15 import Hcompta.Model.Account (Account)
16 import qualified Hcompta.Model.Amount as Amount
17 import Hcompta.Model.Date (Date)
18 import qualified Hcompta.Model.Transaction.Tag as Tag
19
20 -- * The 'Posting' type
21
22 data Posting
23 = Posting
24 { account :: Account
25 , amounts :: Amount.By_Unit
26 , comments :: [Comment]
27 , dates :: [Date]
28 , sourcepos :: SourcePos
29 , status :: Bool
30 , tags :: Tag.By_Name
31 , type_ :: Type
32 } deriving (Data, Eq, Read, Show, Typeable)
33
34 type Comment = Text
35
36 instance Read SourcePos where
37 readsPrec _ s = [(initialPos s, "")]
38
39 data Type
40 = Type_Regular
41 | Type_Virtual
42 | Type_Virtual_Balanced
43 deriving (Data, Eq, Read, Show, Typeable)
44
45 -- ** Convenient constructors
46
47 nil :: Posting
48 nil =
49 Posting
50 { account = []
51 , amounts = Data.Map.empty
52 , comments = []
53 , dates = []
54 , status = False
55 , sourcepos = initialPos ""
56 , tags = Data.Map.empty
57 , type_ = Type_Regular
58 }
59
60 -- * The 'By_Account' mapping
61
62 type By_Account
63 = Data.Map.Map Account [Posting]
64
65 -- ** Convenient constructors
66
67 -- | Return a tuple associating the given 'Posting' with its 'Account'.
68 by_account :: Posting -> (Account, Posting)
69 by_account posting = (account posting, posting)
70
71 -- | Return a Data.'Data.Map.Map' associating the given 'Posting's with their respective 'Account'.
72 from_List :: [Posting] -> By_Account
73 from_List postings =
74 Data.Map.fromListWith (++) $
75 Data.List.map
76 (\posting -> (account posting, [posting]))
77 postings
78
79 -- * Collectors
80
81 -- | Return the 'Unit's in use within the given 'Posting's
82 units
83 :: Data.Foldable.Foldable m
84 => m [Posting]
85 -> [Amount.Unit]
86 units =
87 Data.Foldable.foldl
88 (\acc ->
89 Data.List.union acc .
90 Data.List.concatMap
91 (Data.Map.keys . amounts))
92 []