2007-07-04 13:51:37 +04:00
|
|
|
module LedgerEntry
|
2007-02-16 12:00:17 +03:00
|
|
|
where
|
|
|
|
import Utils
|
2007-07-02 18:54:36 +04:00
|
|
|
import Types
|
2007-07-04 13:51:37 +04:00
|
|
|
import LedgerTransaction
|
2007-07-04 13:28:07 +04:00
|
|
|
import Amount
|
2007-02-16 12:00:17 +03:00
|
|
|
|
|
|
|
|
2007-07-04 13:51:37 +04:00
|
|
|
instance Show LedgerEntry where show = showEntryDescription
|
2007-07-03 03:41:07 +04:00
|
|
|
|
2008-10-01 04:29:58 +04:00
|
|
|
-- | for register report
|
2007-07-04 13:28:07 +04:00
|
|
|
--
|
2007-02-16 12:00:17 +03:00
|
|
|
-- a register entry is displayed as two or more lines like this:
|
|
|
|
-- date description account amount balance
|
|
|
|
-- DDDDDDDDDD dddddddddddddddddddd aaaaaaaaaaaaaaaaaaaaaa AAAAAAAAAAA AAAAAAAAAAAA
|
|
|
|
-- aaaaaaaaaaaaaaaaaaaaaa AAAAAAAAAAA AAAAAAAAAAAA
|
|
|
|
-- ... ... ...
|
2007-07-04 13:28:07 +04:00
|
|
|
-- datewidth = 10
|
|
|
|
-- descwidth = 20
|
|
|
|
-- acctwidth = 22
|
|
|
|
-- amtwidth = 11
|
|
|
|
-- balwidth = 12
|
2007-02-16 12:00:17 +03:00
|
|
|
|
2007-07-04 13:28:07 +04:00
|
|
|
showEntryDescription e = (showDate $ edate e) ++ " " ++ (showDescription $ edescription e) ++ " "
|
2007-02-16 12:00:17 +03:00
|
|
|
showDate d = printf "%-10s" d
|
|
|
|
showDescription s = printf "%-20s" (elideRight 20 s)
|
|
|
|
|
2008-10-01 04:29:58 +04:00
|
|
|
-- | quick & dirty: checks entry's 0 balance only to 8 places
|
2007-07-04 13:51:37 +04:00
|
|
|
isEntryBalanced :: LedgerEntry -> Bool
|
2007-07-11 13:32:43 +04:00
|
|
|
isEntryBalanced = ((0::Double)==) . read . printf "%0.8f" . quantity . sumLedgerTransactions . etransactions
|
2007-02-16 12:00:17 +03:00
|
|
|
|
2007-07-04 13:51:37 +04:00
|
|
|
autofillEntry :: LedgerEntry -> LedgerEntry
|
2008-06-28 08:44:33 +04:00
|
|
|
autofillEntry e@(LedgerEntry _ _ _ _ _ ts _) =
|
2007-07-11 12:15:58 +04:00
|
|
|
let e' = e{etransactions=autofillTransactions ts} in
|
|
|
|
case (isEntryBalanced e') of
|
|
|
|
True -> e'
|
|
|
|
False -> (error $ "transactions don't balance in " ++ show e)
|
2007-02-16 12:00:17 +03:00
|
|
|
|
2008-10-01 04:29:58 +04:00
|
|
|
-- | the print command shows cleaned up ledger file entries, something like:
|
2007-07-04 13:28:07 +04:00
|
|
|
--
|
2007-07-04 16:40:26 +04:00
|
|
|
-- yyyy/mm/dd[ *][ CODE] description......... [ ; comment...............]
|
|
|
|
-- account name 1..................... ...$amount1[ ; comment...............]
|
|
|
|
-- account name 2..................... ..$-amount1[ ; comment...............]
|
2007-07-04 13:28:07 +04:00
|
|
|
--
|
2007-07-07 13:05:35 +04:00
|
|
|
-- pcodewidth = no limit -- 10
|
|
|
|
-- pdescwidth = no limit -- 20
|
|
|
|
-- pacctwidth = 35 minimum, no maximum
|
|
|
|
-- pamtwidth = 11
|
|
|
|
-- pcommentwidth = no limit -- 22
|
2007-07-04 13:28:07 +04:00
|
|
|
|
2007-07-04 13:51:37 +04:00
|
|
|
showEntry :: LedgerEntry -> String
|
2007-07-04 13:28:07 +04:00
|
|
|
showEntry e =
|
2008-06-28 09:07:09 +04:00
|
|
|
unlines $ [precedingcomment ++ description] ++ (showtxns $ etransactions e) ++ [""]
|
2007-07-04 13:28:07 +04:00
|
|
|
where
|
2008-06-28 08:44:33 +04:00
|
|
|
precedingcomment = epreceding_comment_lines e
|
2007-07-07 13:05:35 +04:00
|
|
|
description = concat [date, status, code, desc] -- , comment]
|
2007-07-04 13:28:07 +04:00
|
|
|
date = showDate $ edate e
|
|
|
|
status = if estatus e then " *" else ""
|
2007-07-04 16:40:26 +04:00
|
|
|
code = if (length $ ecode e) > 0 then (printf " (%s)" $ ecode e) else ""
|
2007-07-07 13:05:35 +04:00
|
|
|
desc = " " ++ edescription e
|
2007-07-04 16:40:26 +04:00
|
|
|
comment = if (length $ ecomment e) > 0 then " ; "++(ecomment e) else ""
|
2007-07-04 13:28:07 +04:00
|
|
|
showtxns (t1:t2:[]) = [showtxn t1, showtxnnoamt t2]
|
|
|
|
showtxns ts = map showtxn ts
|
2007-07-04 16:05:54 +04:00
|
|
|
showtxn t = showacct t ++ " " ++ (showamount $ tamount t) ++ (showcomment $ tcomment t)
|
2007-07-07 13:05:35 +04:00
|
|
|
showtxnnoamt t = showacct t ++ " " ++ (showcomment $ tcomment t)
|
2007-07-04 13:28:07 +04:00
|
|
|
showacct t = " " ++ (showaccountname $ taccount t)
|
2007-07-07 13:05:35 +04:00
|
|
|
showamount = printf "%12s" . showAmountRounded
|
|
|
|
showaccountname s = printf "%-34s" s
|
2007-07-04 16:40:26 +04:00
|
|
|
showcomment s = if (length s) > 0 then " ; "++s else ""
|
2007-07-04 13:28:07 +04:00
|
|
|
|
2007-07-04 13:51:37 +04:00
|
|
|
showEntries :: [LedgerEntry] -> String
|
2007-07-04 13:28:07 +04:00
|
|
|
showEntries = concatMap showEntry
|
|
|
|
|
2007-07-04 13:51:37 +04:00
|
|
|
entrySetPrecision :: Int -> LedgerEntry -> LedgerEntry
|
2008-06-28 08:44:33 +04:00
|
|
|
entrySetPrecision p (LedgerEntry d s c desc comm ts prec) =
|
|
|
|
LedgerEntry d s c desc comm (map (ledgerTransactionSetPrecision p) ts) prec
|
2007-07-04 13:28:07 +04:00
|
|
|
|
|
|
|
|
2007-02-16 12:00:17 +03:00
|
|
|
-- modifier & periodic entries
|
|
|
|
|
|
|
|
instance Show ModifierEntry where
|
|
|
|
show e = "= " ++ (valueexpr e) ++ "\n" ++ unlines (map show (m_transactions e))
|
|
|
|
|
|
|
|
instance Show PeriodicEntry where
|
|
|
|
show e = "~ " ++ (periodexpr e) ++ "\n" ++ unlines (map show (p_transactions e))
|
|
|
|
|