From 6235692988949dcaabc0915da24cd3e014860ff8 Mon Sep 17 00:00:00 2001 From: Stefan Dresselhaus Date: Fri, 18 Sep 2015 00:41:37 +0200 Subject: [PATCH] created history as stated in #5 - not paginated yet. --- Application.hs | 1 + Handler/Item.hs | 84 ++++++++++++++++++++++++++++++++++++++++ Handler/Orders.hs | 4 +- Handler/ProfitItems.hs | 10 ++--- Handler/Stock.hs | 2 +- Handler/Wallet.hs | 11 +----- Import.hs | 9 +++++ config/routes | 1 + neat.cabal | 1 + static/js/neat.js | 4 +- test/Handler/ItemSpec.hs | 10 +++++ 11 files changed, 117 insertions(+), 20 deletions(-) create mode 100644 Handler/Item.hs create mode 100644 test/Handler/ItemSpec.hs diff --git a/Application.hs b/Application.hs index 91ae1fd..b30cadd 100644 --- a/Application.hs +++ b/Application.hs @@ -38,6 +38,7 @@ import Handler.Update import Handler.Stock import Handler.ProfitItems import Handler.Orders +import Handler.Item -- This line actually creates our YesodDispatch instance. It is the second half -- of the call to mkYesodData which occurs in Foundation.hs. Please see the diff --git a/Handler/Item.hs b/Handler/Item.hs new file mode 100644 index 0000000..4e764d1 --- /dev/null +++ b/Handler/Item.hs @@ -0,0 +1,84 @@ +module Handler.Item where + +import Import + +itemsPerPage :: Int +itemsPerPage = 100 + +getItemR :: Int64 -> Handler Html +getItemR transactionTypeId = getItemPagedR transactionTypeId 0 + +getItemPagedR :: Int64 -> Int -> Handler Html +getItemPagedR tid page = + loginOrDo (\(uid,user) -> do + items <- runDB $ selectList [TransactionTypeId ==. tid] [Desc TransactionDateTime, LimitTo itemsPerPage, OffsetBy (itemsPerPage*page)] + total <- runDB $ count [TransactionTypeId ==. tid] + let offset = itemsPerPage * page + loginLayout user $ [whamlet| +
+ $if page > 0 +
#{itemsPerPage} Transactions (starting at #{offset}) + $else +
#{itemsPerPage} Transactions + + + + -
Time + P/C + B/S + Item + ## + ISK/Item + ISK total + ISK profit + % + Time + Client + Station + ? + + $forall Entity _ t <- items +
#{showDateTime $ transactionDateTime $ t} + $if transactionTransForCorp t + C + $else + P + $if transactionTransIsSell t + S + $else + B + #{transactionTypeName t} + #{transactionQuantity t} + #{prettyISK $ transactionPriceCents t} + #{prettyISK $ transactionQuantity t * transactionPriceCents t} + $maybe profit <- transRealProfit t + $if (&&) (transactionTransIsSell t) (profit > 0) + + #{prettyISK $ profit} + $elseif (&&) (transactionTransIsSell t) (profit < 0) + + #{prettyISK $ profit} + $elseif not (transactionTransIsSell t) + + #{prettyISK $ profit} + $else + + #{prettyISK $ profit} + + #{profitPercent profit t}% + $nothing + + - + + + $maybe secs <- transactionSecondsToSell t + #{showSecsToSell secs} + $nothing +   + #{transactionClientName t} + #{transactionStationName t} + + + |] + ) + diff --git a/Handler/Orders.hs b/Handler/Orders.hs index f6d064f..b2268e3 100644 --- a/Handler/Orders.hs +++ b/Handler/Orders.hs @@ -42,7 +42,7 @@ getOrdersR = loginOrDo (\(uid,user) -> do $forall (Entity _ o, Single name, Single stationname, Single regionid) <- sellorders
#{showDateTime $ orderIssued $ o} - #{name} + #{name} #{prettyISK $ orderPriceCents o} #{orderVolRemaining o}/#{orderVolEntered o} (#{orderMinVolume o}) #{prettyISK $ orderVolRemaining o * orderPriceCents o} @@ -76,7 +76,7 @@ getOrdersR = loginOrDo (\(uid,user) -> do $forall (Entity _ o, Single name, Single stationname, Single regionid) <- buyorders
#{showDateTime $ orderIssued $ o} - #{name} + #{name} #{prettyISK $ orderPriceCents o} #{orderVolRemaining o}/#{orderVolEntered o} (#{orderMinVolume o}) #{prettyISK $ orderPriceCents o * orderVolRemaining o} diff --git a/Handler/ProfitItems.hs b/Handler/ProfitItems.hs index ab431d4..291ede0 100644 --- a/Handler/ProfitItems.hs +++ b/Handler/ProfitItems.hs @@ -77,7 +77,7 @@ getProfitItemsDetailsR days = loginOrDo (\(uid,user) -> do %/Day $forall (Profit tn tid quant pc f t a sc) <- items
#{tn} + #{tn} #{prettyISK pc} #{prettyISK f} #{prettyISK t} @@ -85,7 +85,7 @@ getProfitItemsDetailsR days = loginOrDo (\(uid,user) -> do #{showSecsToSell a} #{quant} #{prettyISK $ profitPerDay pc f t a} - #{profitPercent pc f t sc} + #{profitPercent' pc f t sc} #{profitPercentDay pc f t sc a} |] ) @@ -98,9 +98,9 @@ profitPerDay :: Int64 -> Int64 -> Int64 -> Int64 -> Int64 profitPerDay _ _ _ 0 = 0 profitPerDay p f t a = ((p-f-t) * 86400) `div` a -profitPercent :: Int64 -> Int64 -> Int64 -> Int64 -> String -profitPercent _ _ _ 0 = printf "%.2f" $ (0 :: Double) -profitPercent p f t s = printf "%.2f" $ (100*(fromIntegral (p-f-t)) / (fromIntegral s) :: Double) +profitPercent' :: Int64 -> Int64 -> Int64 -> Int64 -> String +profitPercent' _ _ _ 0 = printf "%.2f" $ (0 :: Double) +profitPercent' p f t s = printf "%.2f" $ (100*(fromIntegral (p-f-t)) / (fromIntegral s) :: Double) profitPercentDay :: Int64 -> Int64 -> Int64 -> Int64 -> Int64 -> String profitPercentDay _ _ _ 0 _ = printf "%.2f" $ (0 :: Double) diff --git a/Handler/Stock.hs b/Handler/Stock.hs index 11b5c12..9b9a37b 100644 --- a/Handler/Stock.hs +++ b/Handler/Stock.hs @@ -96,7 +96,7 @@ getStockR = loginOrDo (\(uid,user) -> do $forall DisCols tid sid sn tn is wrth avg' dt taxed <- items'
#{showDateTime dt} - #{tn} + #{tn} #{is} #{prettyISK avg'} #{prettyISK taxed} diff --git a/Handler/Wallet.hs b/Handler/Wallet.hs index 7ea1c68..e8487a5 100644 --- a/Handler/Wallet.hs +++ b/Handler/Wallet.hs @@ -106,7 +106,7 @@ getWalletDetailsR hrs days = loginOrDo (\(uid,user) -> do S $else B - #{transactionTypeName t} + #{transactionTypeName t} #{transactionQuantity t} #{prettyISK $ transactionPriceCents t} #{prettyISK $ transactionQuantity t * transactionPriceCents t} @@ -188,12 +188,6 @@ getWalletDetailsR hrs days = loginOrDo (\(uid,user) -> do |] ) -transRealProfit :: Transaction -> Maybe Int64 -transRealProfit t = if transactionTransIsSell t then - (\a b c -> a - b - c) <$> transactionProfit t <*> transactionFee t <*> transactionTax t - else - negate <$> ((+) <$> transactionFee t <*> transactionTax t) - transRealProfit' :: Int64 -> Int64 -> Int64 -> String transRealProfit' p bf tt = prettyISK (p-bf-tt) @@ -201,8 +195,5 @@ profitPercent' :: Int64 -> Int64 -> Int64 -> Int64 -> Maybe String profitPercent' p bf tt s = if s == 0 then Nothing else Just . printf "%.2f" $ 100*(fromIntegral (p - bf - tt) / fromIntegral s :: Double) -profitPercent :: Int64 -> Transaction -> String -profitPercent p t = printf "%.2f" $ (100*(fromIntegral p) / (fromIntegral (transactionQuantity t * transactionPriceCents t)) :: Double) - addProfit :: ProfitSum -> Profit -> ProfitSum addProfit (ProfitSum b' s' p' bf' tt') (Profit _ b s p bf tt) = ProfitSum (b+b') (s+s') (p+p') (bf+bf') (tt+tt') diff --git a/Import.hs b/Import.hs index 2f362fe..ae0b1bf 100644 --- a/Import.hs +++ b/Import.hs @@ -76,3 +76,12 @@ showSecsToSell t | otherwise = pp (fromIntegral t :: Double) ++ "s" where pp = printf "%.2f" + +transRealProfit :: Transaction -> Maybe Int64 +transRealProfit t = if transactionTransIsSell t then + (\a b c -> a - b - c) <$> transactionProfit t <*> transactionFee t <*> transactionTax t + else + negate <$> ((+) <$> transactionFee t <*> transactionTax t) + +profitPercent :: Int64 -> Transaction -> String +profitPercent p t = printf "%.2f" $ (100*(fromIntegral p) / (fromIntegral (transactionQuantity t * transactionPriceCents t)) :: Double) diff --git a/config/routes b/config/routes index 5fd208b..09c71f9 100644 --- a/config/routes +++ b/config/routes @@ -14,3 +14,4 @@ /analysis/items ProfitItemsR GET /analysis/items/#Int64 ProfitItemsDetailsR GET /orders OrdersR GET +/history/#Int64 ItemR GET diff --git a/neat.cabal b/neat.cabal index 3c3371c..abbe85c 100644 --- a/neat.cabal +++ b/neat.cabal @@ -28,6 +28,7 @@ library Handler.Stock Handler.ProfitItems Handler.Orders + Handler.Item if flag(dev) || flag(library-only) cpp-options: -DDEVELOPMENT diff --git a/static/js/neat.js b/static/js/neat.js index a6bafd0..037e343 100644 --- a/static/js/neat.js +++ b/static/js/neat.js @@ -5,8 +5,8 @@ neat utility functions Number.prototype.formatMoney = function(c, d, t) { var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, - d = d == undefined ? "." : t, - t = t == undefined ? "," : d, + d = d == undefined ? "." : d, + t = t == undefined ? "," : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0; diff --git a/test/Handler/ItemSpec.hs b/test/Handler/ItemSpec.hs new file mode 100644 index 0000000..aebb3e4 --- /dev/null +++ b/test/Handler/ItemSpec.hs @@ -0,0 +1,10 @@ +module Handler.ItemSpec (spec) where + +import TestImport + +spec :: Spec +spec = withApp $ do + + describe "getItemR" $ do + error "Spec not implemented: getItemRR" +