refactor
This commit is contained in:
parent
7e38481f8b
commit
41fa72cbeb
38
Models.hs
38
Models.hs
@ -11,12 +11,14 @@ type Date = String
|
|||||||
type Status = Bool
|
type Status = Bool
|
||||||
type Account = String
|
type Account = String
|
||||||
|
|
||||||
|
-- amounts
|
||||||
|
-- amount arithmetic currently ignores currency conversion
|
||||||
|
|
||||||
data Amount = Amount {
|
data Amount = Amount {
|
||||||
currency :: String,
|
currency :: String,
|
||||||
quantity :: Double
|
quantity :: Double
|
||||||
} deriving (Eq)
|
} deriving (Eq)
|
||||||
|
|
||||||
-- amount arithmetic, ignores currency conversion
|
|
||||||
instance Num Amount where
|
instance Num Amount where
|
||||||
abs (Amount c q) = Amount c (abs q)
|
abs (Amount c q) = Amount c (abs q)
|
||||||
signum (Amount c q) = Amount c (signum q)
|
signum (Amount c q) = Amount c (signum q)
|
||||||
@ -73,9 +75,9 @@ data Entry = Entry {
|
|||||||
etransactions :: [Transaction]
|
etransactions :: [Transaction]
|
||||||
} deriving (Eq)
|
} deriving (Eq)
|
||||||
|
|
||||||
instance Show Entry where show = showEntryDetails
|
instance Show Entry where show = showEntry
|
||||||
|
|
||||||
showEntryDetails e = printf "%-10s %-20s " (edate e) (take 20 $ edescription e)
|
showEntry e = printf "%-10s %-20s " (edate e) (take 20 $ edescription e)
|
||||||
|
|
||||||
isEntryBalanced :: Entry -> Bool
|
isEntryBalanced :: Entry -> Bool
|
||||||
isEntryBalanced e = (sumTransactions . etransactions) e == 0
|
isEntryBalanced e = (sumTransactions . etransactions) e == 0
|
||||||
@ -92,31 +94,28 @@ data Transaction = Transaction {
|
|||||||
tamount :: Amount
|
tamount :: Amount
|
||||||
} deriving (Eq)
|
} deriving (Eq)
|
||||||
|
|
||||||
instance Show Transaction where
|
instance Show Transaction where show = showTransaction
|
||||||
show t = printf "%-25s %10s" (take 25 $ taccount t) (show $ tamount t)
|
|
||||||
|
showTransaction t = printf "%-25s %10s" (take 25 $ taccount t) (show $ tamount t)
|
||||||
|
|
||||||
autofillTransactions :: [Transaction] -> [Transaction]
|
autofillTransactions :: [Transaction] -> [Transaction]
|
||||||
autofillTransactions ts =
|
autofillTransactions ts =
|
||||||
let (ns, as) = normalAndAutoTransactions ts in
|
let (ns, as) = partition isNormal ts
|
||||||
|
where isNormal t = (currency $ tamount t) /= "AUTO" in
|
||||||
case (length as) of
|
case (length as) of
|
||||||
0 -> ns
|
0 -> ns
|
||||||
1 -> ns ++ [balanceTransaction $ head as]
|
1 -> ns ++ [balanceTransaction $ head as]
|
||||||
where balanceTransaction t = t{tamount = -(sumTransactions ns)}
|
where balanceTransaction t = t{tamount = -(sumTransactions ns)}
|
||||||
otherwise -> error "too many blank transactions in this entry"
|
otherwise -> error "too many blank transactions in this entry"
|
||||||
|
|
||||||
normalAndAutoTransactions :: [Transaction] -> ([Transaction], [Transaction])
|
|
||||||
normalAndAutoTransactions ts =
|
|
||||||
partition isNormal ts
|
|
||||||
where isNormal t = (currency $ tamount t) /= "AUTO"
|
|
||||||
|
|
||||||
sumTransactions :: [Transaction] -> Amount
|
sumTransactions :: [Transaction] -> Amount
|
||||||
sumTransactions ts = sum [tamount t | t <- ts]
|
sumTransactions ts = sum [tamount t | t <- ts]
|
||||||
|
|
||||||
-- entrytransactions
|
-- entrytransactions
|
||||||
-- the entry/transaction types used in app-level functions have morphed
|
-- We parse Entries containing Transactions and flatten them into
|
||||||
-- through E->T; (T,E); ET; E<->T; (E,T). Currently, we parse Entries
|
-- (entry,transaction) pairs (entrytransactions, hereafter referred to as
|
||||||
-- containing Transactions and flatten them into (Entry,Transaction) pairs
|
-- "transactions") for easier processing. (So far, these types have
|
||||||
-- (hereafter referred to as "transactions") for processing
|
-- morphed through E->T; (T,E); ET; E<->T; (E,T)).
|
||||||
|
|
||||||
type EntryTransaction = (Entry,Transaction)
|
type EntryTransaction = (Entry,Transaction)
|
||||||
|
|
||||||
@ -157,16 +156,11 @@ showTransactionsWithBalances ts b =
|
|||||||
|
|
||||||
showTransactionDescriptionAndBalance :: EntryTransaction -> Amount -> String
|
showTransactionDescriptionAndBalance :: EntryTransaction -> Amount -> String
|
||||||
showTransactionDescriptionAndBalance t b =
|
showTransactionDescriptionAndBalance t b =
|
||||||
(showTransactionEntryDetails t) ++ (showTransactionDetails t) ++ (showBalance b)
|
(showEntry $ entry t) ++ (showTransaction $ transaction t) ++ (showBalance b)
|
||||||
|
|
||||||
showTransactionAndBalance :: EntryTransaction -> Amount -> String
|
showTransactionAndBalance :: EntryTransaction -> Amount -> String
|
||||||
showTransactionAndBalance t b =
|
showTransactionAndBalance t b =
|
||||||
(replicate 32 ' ') ++ (showTransactionDetails t) ++ (showBalance b)
|
(replicate 32 ' ') ++ (showTransaction $ transaction t) ++ (showBalance b)
|
||||||
|
|
||||||
-- like showEntryDetails
|
|
||||||
showTransactionEntryDetails t = printf "%-10s %-20s " (date t) (take 20 $ description t)
|
|
||||||
|
|
||||||
showTransactionDetails t = printf "%-25s %10s" (take 25 $ account t) (show $ amount t)
|
|
||||||
|
|
||||||
showBalance b = printf " %10.2s" (show b)
|
showBalance b = printf " %10.2s" (show b)
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user