1 {-|
    2 
    3 A compound data type for efficiency. A 'Transaction' is a 'Posting' with
    4 its parent 'LedgerTransaction' \'s date and description attached. The
    5 \"transaction\" term is pretty ingrained in the code, docs and with users,
    6 so we've kept it. These are what we work with most of the time when doing
    7 reports.
    8 
    9 -}
   10 
   11 module Ledger.Transaction
   12 where
   13 import Ledger.Dates
   14 import Ledger.Utils
   15 import Ledger.Types
   16 import Ledger.Dates
   17 import Ledger.LedgerTransaction (showAccountName)
   18 import Ledger.Posting
   19 import Ledger.Amount
   20 
   21 
   22 instance Show Transaction where show=showTransaction
   23 
   24 showTransaction :: Transaction -> String
   25 showTransaction (Transaction eno stat d desc a amt ttype) = 
   26     s ++ unwords [showDate d,desc,a',show amt,show ttype]
   27     where s = if stat then " *" else ""
   28           a' = showAccountName Nothing ttype a
   29 
   30 -- | Convert a 'LedgerTransaction' to two or more 'Transaction's. An id number
   31 -- is attached to the transactions to preserve their grouping - it should
   32 -- be unique per entry.
   33 flattenLedgerTransaction :: (LedgerTransaction, Int) -> [Transaction]
   34 flattenLedgerTransaction (LedgerTransaction d s _ desc _ ps _, n) = 
   35     [Transaction n s d desc (paccount p) (pamount p) (ptype p) | p <- ps]
   36 
   37 accountNamesFromTransactions :: [Transaction] -> [AccountName]
   38 accountNamesFromTransactions ts = nub $ map taccount ts
   39 
   40 sumTransactions :: [Transaction] -> MixedAmount
   41 sumTransactions = sum . map tamount
   42 
   43 nulltxn :: Transaction
   44 nulltxn = Transaction 0 False (parsedate "1900/1/1") "" "" nullmixedamt RegularPosting
   45 
   46 -- | Does the given transaction fall within the given date span ?
   47 isTransactionInDateSpan :: DateSpan -> Transaction -> Bool
   48 isTransactionInDateSpan (DateSpan Nothing Nothing)   _ = True
   49 isTransactionInDateSpan (DateSpan Nothing (Just e))  (Transaction{tdate=d}) = d<e
   50 isTransactionInDateSpan (DateSpan (Just b) Nothing)  (Transaction{tdate=d}) = d>=b
   51 isTransactionInDateSpan (DateSpan (Just b) (Just e)) (Transaction{tdate=d}) = d>=b && d<e
   52