diff --git a/Account.hs b/Account.hs index 2ce693d2b..2e374cb72 100644 --- a/Account.hs +++ b/Account.hs @@ -5,6 +5,7 @@ import qualified Data.Map as Map import Utils import BasicTypes import AccountName +import Amount import Entry import Transaction import EntryTransaction diff --git a/BasicTypes.hs b/BasicTypes.hs index 3b88495e0..f49509761 100644 --- a/BasicTypes.hs +++ b/BasicTypes.hs @@ -5,76 +5,3 @@ import Utils type Date = String type DateTime = String - --- amounts -{- a simple amount is a currency, quantity pair: - 0 - $1 - £-50 - EUR 3.44 - HRS 1.5 - DAYS 3 - GOOG 500 - - a mixed amount is one or more simple amounts: - $50, EUR 3, AAPL 500 - HRS 16, $13.55, oranges 6 - - arithmetic: - $1 - $5 = $-4 - $1 + EUR 0.76 = $2 - EUR0.76 + $1 = EUR 1.52 - EUR0.76 - $1 = 0 - ($5, HRS 2) + $1 = ($6, HRS 2) - ($50, EUR 3, AAPL 500) + ($13.55, oranges 6) = $67.51, AAPL 500, oranges 6 - ($50, EUR 3) * $-1 = $-53.96 - ($50, AAPL 500) * $-1 = error - --} - -type Currency = String - -data Amount = Amount { - currency :: Currency, - quantity :: Double - } deriving (Eq,Ord) - -instance Show Amount where show = showAmountRoundedOrZero - -nullamt = Amount "" 0 - -showAmountRoundedOrZero :: Amount -> String -showAmountRoundedOrZero (Amount cur qty) = - let rounded = printf "%.2f" qty in - case rounded of - "0.00" -> "0" - "-0.00" -> "0" - otherwise -> cur ++ rounded - -instance Num Amount where - abs (Amount c q) = Amount c (abs q) - signum (Amount c q) = Amount c (signum q) - fromInteger i = Amount "$" (fromInteger i) - (+) = amountAdd - (-) = amountSub - (*) = amountMul -Amount ac aq `amountAdd` b = Amount ac (aq + (quantity $ toCurrency ac b)) -Amount ac aq `amountSub` b = Amount ac (aq - (quantity $ toCurrency ac b)) -Amount ac aq `amountMul` b = Amount ac (aq * (quantity $ toCurrency ac b)) - -toCurrency :: Currency -> Amount -> Amount -toCurrency newc (Amount oldc q) = - Amount newc (q * (conversionRate oldc newc)) - -conversionRate :: Currency -> Currency -> Double -conversionRate oldc newc = (rate newc) / (rate oldc) - -rate :: Currency -> Double -rate "$" = 1.0 -rate "EUR" = 0.760383 -rate "£" = 0.512527 -rate _ = 1 - - -data MixedAmount = MixedAmount [Amount] deriving (Eq,Ord) - diff --git a/EntryTransaction.hs b/EntryTransaction.hs index a365c88bf..55a69507e 100644 --- a/EntryTransaction.hs +++ b/EntryTransaction.hs @@ -5,6 +5,7 @@ import Utils import BasicTypes import Entry import Transaction +import Amount -- We convert Transactions into EntryTransactions, which are (entry, diff --git a/Models.hs b/Models.hs index 1890e587d..cafd7d7fc 100644 --- a/Models.hs +++ b/Models.hs @@ -1,6 +1,7 @@ -- data types & behaviours module Models ( module BasicTypes, + module Amount, module AccountName, module Transaction, module Entry, @@ -13,6 +14,7 @@ where import qualified Data.Map as Map import BasicTypes +import Amount import AccountName import Transaction import Entry diff --git a/TimeLog.hs b/TimeLog.hs index 40d10befe..6b9732a5c 100644 --- a/TimeLog.hs +++ b/TimeLog.hs @@ -2,6 +2,7 @@ module TimeLog where import Utils import BasicTypes +import Amount import Transaction import Entry import Ledger @@ -44,11 +45,11 @@ entriesFromTimeLogEntries [clockin,clockout] = ]} ] where - accountname = (tcomment clockin) - intime = tdatetime clockin - indate = dateFrom $ tdatetime clockin - outtime = tdatetime clockout - amount = timeAmount $ 0 -- read $ outtime - intime + accountname = tcomment clockin + intime = tdatetime clockin + indate = dateFrom $ tdatetime clockin + outtime = tdatetime clockout + amount = timeAmount $ 0 -- read $ outtime - intime entriesFromTimeLogEntries many = (entriesFromTimeLogEntries $ take 2 many) ++ diff --git a/Transaction.hs b/Transaction.hs index 9a143c80e..dd0c66323 100644 --- a/Transaction.hs +++ b/Transaction.hs @@ -4,6 +4,7 @@ where import Utils import BasicTypes import AccountName +import Amount data Transaction = Transaction {