mirror of
https://github.com/simonmichael/hledger.git
synced 2024-12-27 12:24:43 +03:00
Amount module
This commit is contained in:
parent
510d39095c
commit
ea98a9798f
@ -5,6 +5,7 @@ import qualified Data.Map as Map
|
|||||||
import Utils
|
import Utils
|
||||||
import BasicTypes
|
import BasicTypes
|
||||||
import AccountName
|
import AccountName
|
||||||
|
import Amount
|
||||||
import Entry
|
import Entry
|
||||||
import Transaction
|
import Transaction
|
||||||
import EntryTransaction
|
import EntryTransaction
|
||||||
|
@ -5,76 +5,3 @@ import Utils
|
|||||||
|
|
||||||
type Date = String
|
type Date = String
|
||||||
type DateTime = 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)
|
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import Utils
|
|||||||
import BasicTypes
|
import BasicTypes
|
||||||
import Entry
|
import Entry
|
||||||
import Transaction
|
import Transaction
|
||||||
|
import Amount
|
||||||
|
|
||||||
|
|
||||||
-- We convert Transactions into EntryTransactions, which are (entry,
|
-- We convert Transactions into EntryTransactions, which are (entry,
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
-- data types & behaviours
|
-- data types & behaviours
|
||||||
module Models (
|
module Models (
|
||||||
module BasicTypes,
|
module BasicTypes,
|
||||||
|
module Amount,
|
||||||
module AccountName,
|
module AccountName,
|
||||||
module Transaction,
|
module Transaction,
|
||||||
module Entry,
|
module Entry,
|
||||||
@ -13,6 +14,7 @@ where
|
|||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
|
|
||||||
import BasicTypes
|
import BasicTypes
|
||||||
|
import Amount
|
||||||
import AccountName
|
import AccountName
|
||||||
import Transaction
|
import Transaction
|
||||||
import Entry
|
import Entry
|
||||||
|
11
TimeLog.hs
11
TimeLog.hs
@ -2,6 +2,7 @@ module TimeLog
|
|||||||
where
|
where
|
||||||
import Utils
|
import Utils
|
||||||
import BasicTypes
|
import BasicTypes
|
||||||
|
import Amount
|
||||||
import Transaction
|
import Transaction
|
||||||
import Entry
|
import Entry
|
||||||
import Ledger
|
import Ledger
|
||||||
@ -44,11 +45,11 @@ entriesFromTimeLogEntries [clockin,clockout] =
|
|||||||
]}
|
]}
|
||||||
]
|
]
|
||||||
where
|
where
|
||||||
accountname = (tcomment clockin)
|
accountname = tcomment clockin
|
||||||
intime = tdatetime clockin
|
intime = tdatetime clockin
|
||||||
indate = dateFrom $ tdatetime clockin
|
indate = dateFrom $ tdatetime clockin
|
||||||
outtime = tdatetime clockout
|
outtime = tdatetime clockout
|
||||||
amount = timeAmount $ 0 -- read $ outtime - intime
|
amount = timeAmount $ 0 -- read $ outtime - intime
|
||||||
|
|
||||||
entriesFromTimeLogEntries many =
|
entriesFromTimeLogEntries many =
|
||||||
(entriesFromTimeLogEntries $ take 2 many) ++
|
(entriesFromTimeLogEntries $ take 2 many) ++
|
||||||
|
@ -4,6 +4,7 @@ where
|
|||||||
import Utils
|
import Utils
|
||||||
import BasicTypes
|
import BasicTypes
|
||||||
import AccountName
|
import AccountName
|
||||||
|
import Amount
|
||||||
|
|
||||||
|
|
||||||
data Transaction = Transaction {
|
data Transaction = Transaction {
|
||||||
|
Loading…
Reference in New Issue
Block a user