2015-04-26 20:18:24 +00:00
|
|
|
module Handler.Wallet where
|
|
|
|
|
|
|
|
import Import
|
|
|
|
|
2015-08-07 21:41:37 +00:00
|
|
|
import Data.List (unfoldr)
|
2015-08-06 22:07:48 +00:00
|
|
|
import Data.Time.Clock
|
2015-08-07 21:41:37 +00:00
|
|
|
import Text.Printf
|
2015-07-24 19:12:18 +00:00
|
|
|
|
2015-08-09 00:06:32 +00:00
|
|
|
buttonIntervals :: [(Int64,String)]
|
|
|
|
buttonIntervals = [ (2,"2 hours")
|
|
|
|
, (6,"6 hours")
|
|
|
|
, (12,"12 hours")
|
|
|
|
, (24,"1 day")
|
|
|
|
, (48,"2 days")
|
|
|
|
, (7*24,"7 days")
|
|
|
|
, (31*24,"31 days")
|
|
|
|
]
|
|
|
|
|
2015-04-26 20:18:24 +00:00
|
|
|
getWalletR :: Handler Html
|
2015-08-06 22:07:48 +00:00
|
|
|
getWalletR = getWalletDetailsR 6 7
|
|
|
|
|
|
|
|
getWalletDetailsR :: Int64 -> Int64 -> Handler Html
|
|
|
|
getWalletDetailsR hrs days = loginOrDo (\(uid,user) -> do
|
|
|
|
now <- liftIO getCurrentTime
|
|
|
|
trans <- runDB $ selectList [TransactionDateTime >. (addUTCTime ((fromIntegral $ -(hrs*3600)) :: NominalDiffTime) now)] [Desc TransactionDateTime]
|
2015-07-20 20:10:30 +00:00
|
|
|
defaultLayout $ [whamlet|
|
2015-08-09 00:06:32 +00:00
|
|
|
<div .panel .panel-default>
|
|
|
|
<div .panel-heading>Transactions in the last #{hrs} hours:
|
|
|
|
<div .btn-group .btn-group-justified role="group">
|
|
|
|
$forall (hrs',cap) <- buttonIntervals
|
|
|
|
$if hrs == hrs'
|
|
|
|
<a href="@{WalletDetailsR hrs' days}" .btn .active role="button">#{cap}
|
2015-08-07 21:41:37 +00:00
|
|
|
$else
|
2015-08-09 00:06:32 +00:00
|
|
|
<a href="@{WalletDetailsR hrs' days}" .btn role="button">#{cap}
|
2015-08-09 01:17:36 +00:00
|
|
|
<table .table .table-condensed .small>
|
2015-08-09 00:06:32 +00:00
|
|
|
<tr>
|
|
|
|
<th .text-center>Time
|
|
|
|
<th .text-center>P/C
|
|
|
|
<th .text-center>B/S
|
|
|
|
<th .text-center>Item
|
2015-08-09 01:17:36 +00:00
|
|
|
<th .text-center>##
|
2015-08-09 00:06:32 +00:00
|
|
|
<th .text-center>ISK/Item
|
|
|
|
<th .text-center>ISK total
|
|
|
|
<th .text-center>ISK profit
|
|
|
|
<th .text-center>%
|
|
|
|
<th .text-center>Time
|
|
|
|
<th .text-center>Client
|
|
|
|
<th .text-center>Station
|
|
|
|
<th .text-center>?
|
|
|
|
<th .text-center>
|
|
|
|
$forall Entity _ t <- trans
|
|
|
|
<tr>
|
|
|
|
<td>#{show $ utctDay $ transactionDateTime $ t} #{showTime $ round $ utctDayTime $ transactionDateTime $ t}
|
|
|
|
$if transactionTransForCorp t
|
|
|
|
<td .corpTransaction .text-center>C
|
|
|
|
$else
|
|
|
|
<td .personalTransaction .text-center>P
|
|
|
|
$if transactionTransIsSell t
|
|
|
|
<td .sellTransaction .text-center>S
|
|
|
|
$else
|
|
|
|
<td .buyTransaction .text-center>B
|
|
|
|
<td>#{transactionTypeName t}
|
2015-08-09 01:17:36 +00:00
|
|
|
<td .numeric>#{transactionQuantity t}
|
|
|
|
<td .numeric>#{prettyISK $ transactionPriceCents t}
|
|
|
|
<td .numeric>#{prettyISK $ transactionQuantity t * transactionPriceCents t}
|
2015-08-09 00:06:32 +00:00
|
|
|
$maybe profit <- transRealProfit t
|
|
|
|
$if (&&) (transactionTransIsSell t) (profit > 0)
|
2015-08-09 01:17:36 +00:00
|
|
|
<td .numeric .profit>
|
2015-08-09 00:06:32 +00:00
|
|
|
#{prettyISK $ profit}
|
|
|
|
$elseif (&&) (transactionTransIsSell t) (profit < 0)
|
2015-08-09 01:17:36 +00:00
|
|
|
<td .numeric .loss>
|
2015-08-09 00:06:32 +00:00
|
|
|
#{prettyISK $ profit}
|
2015-08-09 01:17:36 +00:00
|
|
|
$elseif not (transactionTransIsSell t)
|
|
|
|
<td .numeric .buyfee>
|
2015-08-09 00:06:32 +00:00
|
|
|
#{prettyISK $ profit}
|
|
|
|
$else
|
2015-08-09 01:17:36 +00:00
|
|
|
<td ..numeric>
|
2015-08-09 00:06:32 +00:00
|
|
|
#{prettyISK $ profit}
|
2015-08-09 01:17:36 +00:00
|
|
|
<td .numeric>
|
2015-08-09 00:06:32 +00:00
|
|
|
#{profitPercent profit t}%
|
|
|
|
$nothing
|
|
|
|
<td>
|
|
|
|
-
|
|
|
|
<td>
|
2015-08-09 01:17:36 +00:00
|
|
|
<td .duration>
|
2015-08-09 00:06:32 +00:00
|
|
|
$maybe secs <- transactionSecondsToSell t
|
|
|
|
#{showSecsToSell secs}
|
|
|
|
$nothing
|
|
|
|
|
|
|
|
<td>#{transactionClientName t}
|
|
|
|
<td>#{transactionStationName t}
|
2015-08-07 21:41:37 +00:00
|
|
|
<td>
|
|
|
|
<td>
|
2015-07-20 20:10:30 +00:00
|
|
|
|
2015-08-09 00:06:32 +00:00
|
|
|
<div .panel .panel-default>
|
|
|
|
<div .panel-heading>Statistices for the last #{days} days:
|
|
|
|
<table .table .table-striped .table-condensed .small>
|
|
|
|
<tr>
|
|
|
|
<th .text-center>TODO
|
2015-07-24 19:12:18 +00:00
|
|
|
|]
|
|
|
|
)
|
2015-07-20 20:10:30 +00:00
|
|
|
|
2015-08-07 00:26:16 +00:00
|
|
|
transRealProfit :: Transaction -> Maybe Int64
|
2015-08-09 00:06:32 +00:00
|
|
|
transRealProfit t = if transactionTransIsSell t then
|
|
|
|
(\a b c -> a - b - c) <$> transactionProfit t <*> transactionFee t <*> transactionTax t
|
|
|
|
else
|
|
|
|
negate <$> ((+) <$> transactionFee t <*> transactionTax t)
|
2015-08-07 21:41:37 +00:00
|
|
|
|
|
|
|
profitPercent :: Int64 -> Transaction -> String
|
|
|
|
profitPercent p t = printf "%.2f" $ (100*(fromIntegral p) / (fromIntegral (transactionQuantity t * transactionPriceCents t)) :: Double)
|
|
|
|
|
|
|
|
prettyISK :: Int64 -> String
|
2015-08-09 00:06:32 +00:00
|
|
|
prettyISK isk = signIsk++pretty++","++ printf "%02u" cents
|
2015-08-07 21:41:37 +00:00
|
|
|
where
|
2015-08-09 00:06:32 +00:00
|
|
|
signIsk = if isk > 0 then "" else "-"
|
|
|
|
(isk',cents) = divMod (abs isk) 100
|
2015-08-07 21:41:37 +00:00
|
|
|
thousands = unfoldr (\b -> if b == 0 then Nothing else Just (b `mod` 1000, b `div` 1000)) isk'
|
|
|
|
(ht:t) = reverse thousands
|
|
|
|
pretty = intercalate "." $ [show ht] ++ (printf "%03u" <$> t)
|
|
|
|
|
2015-08-09 00:06:32 +00:00
|
|
|
showTime :: Int64 -> String
|
|
|
|
showTime t = printf "%2u" hours ++ ":" ++ printf "%02u" minutes ++ ":" ++ printf "%02u" seconds
|
|
|
|
where
|
|
|
|
(hours, minutes') = divMod t 3600
|
|
|
|
(minutes, seconds) = divMod minutes' 60
|
2015-08-07 21:41:37 +00:00
|
|
|
|
2015-08-09 00:06:32 +00:00
|
|
|
showSecsToSell :: Int64 -> String
|
|
|
|
showSecsToSell t
|
|
|
|
| t > 4*7*86400 = pp (fromIntegral t / (7*86400) :: Double) ++ "w"
|
|
|
|
| t > 86400 = pp (fromIntegral t / 86400 :: Double) ++ "d"
|
|
|
|
| t > 3600 = pp (fromIntegral t / 3600 :: Double) ++ "h"
|
|
|
|
| t > 60 = pp (fromIntegral t / 60 :: Double) ++ "m"
|
|
|
|
| otherwise = pp (fromIntegral t :: Double) ++ "s"
|
|
|
|
where
|
|
|
|
pp x = printf "%.2f" x
|