2007-02-09 04:23:12 +03:00
|
|
|
-- a data model
|
|
|
|
module Types where
|
|
|
|
|
|
|
|
import Text.Printf
|
|
|
|
|
|
|
|
data Ledger = Ledger {
|
|
|
|
modifier_entries :: [ModifierEntry],
|
|
|
|
periodic_entries :: [PeriodicEntry],
|
|
|
|
entries :: [Entry]
|
2007-02-09 12:58:11 +03:00
|
|
|
} deriving (Eq)
|
2007-02-09 04:23:12 +03:00
|
|
|
data ModifierEntry = ModifierEntry { -- aka automated entry
|
|
|
|
valueexpr :: String,
|
|
|
|
m_transactions :: [Transaction]
|
|
|
|
} deriving (Eq)
|
|
|
|
data PeriodicEntry = PeriodicEntry {
|
|
|
|
periodexpr :: String,
|
|
|
|
p_transactions :: [Transaction]
|
|
|
|
} deriving (Eq)
|
|
|
|
data Entry = Entry {
|
|
|
|
date :: Date,
|
|
|
|
status :: Bool,
|
|
|
|
code :: String,
|
|
|
|
description :: String,
|
|
|
|
transactions :: [Transaction]
|
|
|
|
} deriving (Eq)
|
|
|
|
data Transaction = Transaction {
|
|
|
|
account :: Account,
|
|
|
|
amount :: Amount
|
|
|
|
} deriving (Eq)
|
|
|
|
data Amount = Amount {
|
|
|
|
currency :: String,
|
|
|
|
quantity :: Float
|
2007-02-09 12:58:11 +03:00
|
|
|
} deriving (Eq)
|
2007-02-09 04:23:12 +03:00
|
|
|
type Date = String
|
|
|
|
type Account = String
|
|
|
|
|
|
|
|
-- show methods
|
|
|
|
|
2007-02-09 12:58:11 +03:00
|
|
|
instance Show Ledger where
|
|
|
|
show l = "Ledger with " ++ m ++ " modifier, " ++ p ++ " periodic, " ++ e ++ " normal entries:\n"
|
|
|
|
++ (concat $ map show (modifier_entries l))
|
|
|
|
++ (concat $ map show (periodic_entries l))
|
|
|
|
++ (concat $ map show (entries l))
|
|
|
|
where
|
|
|
|
m = show $ length $ modifier_entries l
|
|
|
|
p = show $ length $ periodic_entries l
|
|
|
|
e = show $ length $ entries l
|
2007-02-09 04:23:12 +03:00
|
|
|
|
|
|
|
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))
|
|
|
|
|
2007-02-09 12:58:11 +03:00
|
|
|
instance Show Entry where show = showEntry2
|
|
|
|
|
|
|
|
showEntry1 e = date e ++ " " ++ s ++ c ++ d ++ "\n" ++ unlines (map show (transactions e))
|
2007-02-09 04:23:12 +03:00
|
|
|
where
|
|
|
|
d = description e
|
|
|
|
s = case (status e) of {True -> "* "; False -> ""}
|
|
|
|
c = case (length(code e) > 0) of {True -> (code e ++ " "); False -> ""}
|
|
|
|
|
2007-02-09 12:58:11 +03:00
|
|
|
dateWidth = 10
|
|
|
|
descWidth = 20
|
|
|
|
acctWidth = 25
|
|
|
|
amtWidth = 11
|
|
|
|
|
|
|
|
showEntry2 e =
|
|
|
|
unlines (
|
|
|
|
[printf "%-10s %-20s " (date e) (take 20 $ description e)
|
|
|
|
++ (show $ head $ transactions e)]
|
|
|
|
++ map ((printf (take 32 (repeat ' ')) ++) . show) (tail $ transactions e))
|
|
|
|
|
2007-02-09 04:23:12 +03:00
|
|
|
instance Show Transaction where
|
2007-02-09 12:58:11 +03:00
|
|
|
show t = printf "%-25s %8.2s %8.2s" (take 25 $ account t) (show $ amount t) (show 0)
|
2007-02-09 04:23:12 +03:00
|
|
|
|
|
|
|
instance Show Amount where show a = (currency a) ++ (show $ quantity a)
|
|
|
|
|
2007-02-09 12:58:11 +03:00
|
|
|
-- more display methods
|
|
|
|
|
|
|
|
printRegister :: Ledger -> IO ()
|
|
|
|
printRegister l = do
|
|
|
|
putStr $ concat $ map show $ entries l
|