1 {-|
    2 
    3 Most data types are defined here to avoid import cycles. See the
    4 corresponding modules for each type's documentation.
    5 
    6 A note about entry\/transaction\/posting terminology:
    7 
    8   - ledger 2 had Entrys containing Transactions.
    9   
   10   - hledger 0.4 had Entrys containing RawTransactions, plus Transactions
   11     which were a RawTransaction with its parent Entry's info added.
   12     The latter are what we most work with when reporting and are
   13     ubiquitous in the code and docs.
   14   
   15   - ledger 3 has Transactions containing Postings.
   16   
   17 
   18   - hledger 0.5 has LedgerTransactions containing Postings, plus
   19     Transactions as before (a Posting plus it's parent's info).  The
   20     \"transaction\" term is pretty ingrained in the code, docs and with
   21     users, so we've kept it. 
   22 
   23 -}
   24 
   25 module Ledger.Types 
   26 where
   27 import Ledger.Utils
   28 import qualified Data.Map as Map
   29 
   30 
   31 type SmartDate = (String,String,String)
   32 
   33 data DateSpan = DateSpan (Maybe Day) (Maybe Day) deriving (Eq,Show,Ord)
   34 
   35 data Interval = NoInterval | Daily | Weekly | Monthly | Quarterly | Yearly 
   36                 deriving (Eq,Show,Ord)
   37 
   38 type AccountName = String
   39 
   40 data Side = L | R deriving (Eq,Show,Ord) 
   41 
   42 data Commodity = Commodity {
   43       symbol :: String,  -- ^ the commodity's symbol
   44       -- display preferences for amounts of this commodity
   45       side :: Side,      -- ^ should the symbol appear on the left or the right
   46       spaced :: Bool,    -- ^ should there be a space between symbol and quantity
   47       comma :: Bool,     -- ^ should thousands be comma-separated
   48       precision :: Int   -- ^ number of decimal places to display
   49     } deriving (Eq,Show,Ord)
   50 
   51 data Amount = Amount {
   52       commodity :: Commodity,
   53       quantity :: Double,
   54       price :: Maybe MixedAmount  -- ^ optional per-unit price for this amount at the time of entry
   55     } deriving (Eq)
   56 
   57 newtype MixedAmount = Mixed [Amount] deriving (Eq)
   58 
   59 data PostingType = RegularPosting | VirtualPosting | BalancedVirtualPosting
   60                    deriving (Eq,Show)
   61 
   62 data Posting = Posting {
   63       pstatus :: Bool,
   64       paccount :: AccountName,
   65       pamount :: MixedAmount,
   66       pcomment :: String,
   67       ptype :: PostingType
   68     } deriving (Eq)
   69 
   70 data ModifierTransaction = ModifierTransaction {
   71       mtvalueexpr :: String,
   72       mtpostings :: [Posting]
   73     } deriving (Eq)
   74 
   75 data PeriodicTransaction = PeriodicTransaction {
   76       ptperiodicexpr :: String,
   77       ptpostings :: [Posting]
   78     } deriving (Eq)
   79 
   80 data LedgerTransaction = LedgerTransaction {
   81       ltdate :: Day,
   82       ltstatus :: Bool,
   83       ltcode :: String,
   84       ltdescription :: String,
   85       ltcomment :: String,
   86       ltpostings :: [Posting],
   87       ltpreceding_comment_lines :: String
   88     } deriving (Eq)
   89 
   90 data TimeLogCode = SetBalance | SetRequiredHours | In | Out | FinalOut deriving (Eq,Ord) 
   91 
   92 data TimeLogEntry = TimeLogEntry {
   93       tlcode :: TimeLogCode,
   94       tldatetime :: LocalTime,
   95       tlcomment :: String
   96     } deriving (Eq,Ord)
   97 
   98 data HistoricalPrice = HistoricalPrice {
   99       hdate :: Day,
  100       hsymbol1 :: String,
  101       hsymbol2 :: String,
  102       hprice :: Double
  103     } deriving (Eq,Show)
  104 
  105 data RawLedger = RawLedger {
  106       modifier_txns :: [ModifierTransaction],
  107       periodic_txns :: [PeriodicTransaction],
  108       ledger_txns :: [LedgerTransaction],
  109       open_timelog_entries :: [TimeLogEntry],
  110       historical_prices :: [HistoricalPrice],
  111       final_comment_lines :: String,
  112       filepath :: FilePath
  113     } deriving (Eq)
  114 
  115 data Transaction = Transaction {
  116       tnum :: Int,
  117       tstatus :: Bool,           -- ^ posting status
  118       tdate :: Day,              -- ^ ledger transaction date
  119       tdescription :: String,    -- ^ ledger transaction description
  120       taccount :: AccountName,   -- ^ posting account
  121       tamount :: MixedAmount,    -- ^ posting amount
  122       ttype :: PostingType       -- ^ posting type
  123     } deriving (Eq)
  124 
  125 data Account = Account {
  126       aname :: AccountName,
  127       atransactions :: [Transaction], -- ^ transactions in this account
  128       abalance :: MixedAmount         -- ^ sum of transactions in this account and subaccounts
  129     }
  130 
  131 data Ledger = Ledger {
  132       rawledgertext :: String,
  133       rawledger :: RawLedger,
  134       accountnametree :: Tree AccountName,
  135       accountmap :: Map.Map AccountName Account
  136     }
  137