]> Git — Sourcephile - comptalang.git/blob - lib/Hcompta/Model/Transaction.hs
Ajout : Format.Ledger.Read.journal
[comptalang.git] / lib / Hcompta / Model / Transaction.hs
1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# OPTIONS_GHC -fno-warn-orphans #-}
4 module Hcompta.Model.Transaction where
5
6 import Data.Data
7 import qualified Data.List
8 import qualified Data.Map.Strict as Data.Map
9 import Data.Text (Text)
10 import Data.Typeable ()
11 import Text.Parsec.Pos (SourcePos, initialPos)
12
13 import qualified Hcompta.Model.Date as Date
14 import Hcompta.Model.Date (Date)
15 import qualified Hcompta.Model.Transaction.Posting as Posting
16 import qualified Hcompta.Model.Transaction.Tag as Tag
17
18 -- * The 'Transaction' type
19
20 data Transaction
21 = Transaction
22 { code :: Code
23 , comments_before :: [Comment]
24 , comments_after :: [Comment]
25 , dates :: (Date, [Date])
26 , description :: Description
27 , postings :: Posting.By_Account
28 , sourcepos :: SourcePos
29 , status :: Bool
30 , tags :: Tag.By_Name
31 } deriving (Data, Eq, Read, Show, Typeable)
32
33 type Code = Text
34 type Comment = Posting.Comment
35 type Description = Text
36
37 nil :: Transaction
38 nil =
39 Transaction
40 { code = ""
41 , comments_before = []
42 , comments_after = []
43 , dates = (Date.nil, [])
44 , description = ""
45 , postings = Data.Map.empty
46 , sourcepos = initialPos ""
47 , status = False
48 , tags = Data.Map.empty
49 }
50
51 -- * Types to submodules
52
53 type Posting = Posting.Posting
54 type Tag = Tag.Tag
55
56 -- * The 'By_Date' mapping
57
58 type By_Date
59 = Data.Map.Map Date.UTC [Transaction]
60
61 -- ** Convenient constructors
62
63 -- | Return a tuple associating the given 'Transaction' with its 'Date'.
64 by_date :: Transaction -> (Date.UTC, Transaction)
65 by_date t = (Date.to_UTC $ fst $ dates t, t)
66
67 -- | Return a Data.'Data.Map.Map' associating the given 'Transaction's with their respective 'Date'.
68 from_List :: [Transaction] -> By_Date
69 from_List =
70 Data.Map.fromListWith (++) . -- TODO: check order is preserved on equality, or flip (++)
71 Data.List.map (\t -> (Date.to_UTC $ fst $ dates t, [t]))