added order-loading. Fixes #3
This commit is contained in:
		@@ -153,9 +153,12 @@ instance YesodAuth App where
 | 
				
			|||||||
                                   , userStandingsTimeout = now
 | 
					                                   , userStandingsTimeout = now
 | 
				
			||||||
                                   , userSkillTimeout = now
 | 
					                                   , userSkillTimeout = now
 | 
				
			||||||
                                   , userBalanceTimeout = now
 | 
					                                   , userBalanceTimeout = now
 | 
				
			||||||
 | 
					                                   , userOrderTimeout = now
 | 
				
			||||||
                                   , userAcc = 0
 | 
					                                   , userAcc = 0
 | 
				
			||||||
                                   , userBr = 0
 | 
					                                   , userBr = 0
 | 
				
			||||||
                                   , userBalanceCents = 0
 | 
					                                   , userBalanceCents = 0
 | 
				
			||||||
 | 
					                                   , userStockCents = 0
 | 
				
			||||||
 | 
					                                   , userEscrowCents = 0
 | 
				
			||||||
                                   }
 | 
					                                   }
 | 
				
			||||||
                       Nothing -> return $ ServerError "Problems extracting Access-Token"
 | 
					                       Nothing -> return $ ServerError "Problems extracting Access-Token"
 | 
				
			||||||
        where
 | 
					        where
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,12 +19,13 @@ getHomeR = do
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
getLoggedIn :: (Key User, User) -> Handler Html
 | 
					getLoggedIn :: (Key User, User) -> Handler Html
 | 
				
			||||||
getLoggedIn (uid, user) = do
 | 
					getLoggedIn (uid, user) = do
 | 
				
			||||||
 | 
					    let totalworth = userBalanceCents user + userStockCents user + userEscrowCents user
 | 
				
			||||||
    loginLayout user $ [whamlet|
 | 
					    loginLayout user $ [whamlet|
 | 
				
			||||||
             <h1>Welcome back, #{userName user}.
 | 
					             <h1>Welcome back, #{userName user}.
 | 
				
			||||||
             <p>Current Balance: #{prettyISK $ userBalanceCents user} ISK.
 | 
					             <p>Current Balance: #{prettyISK $ userBalanceCents user} ISK.
 | 
				
			||||||
             <p>Current Stock Worth: ...
 | 
					             <p>Current Stock Worth: #{prettyISK $ userStockCents user} ISK.
 | 
				
			||||||
             <p>Current total Worth: ...
 | 
					             <p>Current Escrow Worth: #{prettyISK $ userEscrowCents user} ISK.
 | 
				
			||||||
             <p>Profit in the last 7 days: ...
 | 
					             <p>Current total Worth: #{prettyISK $ totalworth} ISK.
 | 
				
			||||||
             |]
 | 
					             |]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -30,7 +30,6 @@ data DisCols = DisCols
 | 
				
			|||||||
             , dResell :: Int64
 | 
					             , dResell :: Int64
 | 
				
			||||||
             } deriving (Show, Eq)
 | 
					             } deriving (Show, Eq)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
instance RawSql Stock where
 | 
					instance RawSql Stock where
 | 
				
			||||||
  rawSqlCols _ _ = (8,[])
 | 
					  rawSqlCols _ _ = (8,[])
 | 
				
			||||||
  rawSqlColCountReason _ = "typeId, stationId, stationName, typeName, inStock, worth, date, tax"
 | 
					  rawSqlColCountReason _ = "typeId, stationId, stationName, typeName, inStock, worth, date, tax"
 | 
				
			||||||
@@ -81,6 +80,7 @@ getStockR = loginOrDo (\(uid,user) -> do
 | 
				
			|||||||
                           order by t.type_name asc"
 | 
					                           order by t.type_name asc"
 | 
				
			||||||
              (items :: [Stock]) <- runDB $ rawSql sql [toPersistValue uid]
 | 
					              (items :: [Stock]) <- runDB $ rawSql sql [toPersistValue uid]
 | 
				
			||||||
              let items' = convertStock <$> items
 | 
					              let items' = convertStock <$> items
 | 
				
			||||||
 | 
					              let total = foldl' sumTotal 0 items'
 | 
				
			||||||
              loginLayout user $ [whamlet|
 | 
					              loginLayout user $ [whamlet|
 | 
				
			||||||
             <div .panel .panel-default>
 | 
					             <div .panel .panel-default>
 | 
				
			||||||
               <div .panel-heading>Current Stock:
 | 
					               <div .panel-heading>Current Stock:
 | 
				
			||||||
@@ -102,6 +102,14 @@ getStockR = loginOrDo (\(uid,user) -> do
 | 
				
			|||||||
                     <td .numeric>#{prettyISK taxed}
 | 
					                     <td .numeric>#{prettyISK taxed}
 | 
				
			||||||
                     <td .numeric>#{prettyISK wrth}
 | 
					                     <td .numeric>#{prettyISK wrth}
 | 
				
			||||||
                     <td>#{sn}
 | 
					                     <td>#{sn}
 | 
				
			||||||
 | 
					                 <tr .total>
 | 
				
			||||||
 | 
					                   <th .text-center>Total
 | 
				
			||||||
 | 
					                   <td>
 | 
				
			||||||
 | 
					                   <td .numeric>
 | 
				
			||||||
 | 
					                   <td .numeric>
 | 
				
			||||||
 | 
					                   <td .numeric>
 | 
				
			||||||
 | 
					                   <td .numeric>#{prettyISK total}
 | 
				
			||||||
 | 
					                   <td>
 | 
				
			||||||
             |]
 | 
					             |]
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -109,3 +117,7 @@ convertStock :: Stock -> DisCols
 | 
				
			|||||||
convertStock (Stock tid sid sn tn is wrth dt tax) = DisCols tid sid sn tn (floor is) (floor wrth) avgItem dt (floor $ (fromIntegral avgItem) * tax)
 | 
					convertStock (Stock tid sid sn tn is wrth dt tax) = DisCols tid sid sn tn (floor is) (floor wrth) avgItem dt (floor $ (fromIntegral avgItem) * tax)
 | 
				
			||||||
  where
 | 
					  where
 | 
				
			||||||
    avgItem = floor $ wrth / is
 | 
					    avgItem = floor $ wrth / is
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					sumTotal :: Int64 -> DisCols -> Int64
 | 
				
			||||||
 | 
					sumTotal t (DisCols _ _ _ _ _ t' _ _ _) = t + t'
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,6 +8,7 @@ import qualified Eve.Api.Types as T
 | 
				
			|||||||
import qualified Eve.Api.Char.Standings as ST
 | 
					import qualified Eve.Api.Char.Standings as ST
 | 
				
			||||||
import qualified Eve.Api.Char.Skills as SK
 | 
					import qualified Eve.Api.Char.Skills as SK
 | 
				
			||||||
import qualified Eve.Api.Char.AccountBalance as BA
 | 
					import qualified Eve.Api.Char.AccountBalance as BA
 | 
				
			||||||
 | 
					import qualified Eve.Api.Char.MarketOrders as MO
 | 
				
			||||||
import Database.Persist.Sql
 | 
					import Database.Persist.Sql
 | 
				
			||||||
import Data.Time.Clock
 | 
					import Data.Time.Clock
 | 
				
			||||||
import Control.Lens.Operators
 | 
					import Control.Lens.Operators
 | 
				
			||||||
@@ -91,6 +92,26 @@ getUpdateR = loginOrDo (\(uid,user) -> do
 | 
				
			|||||||
                                         update uid [UserBalanceCents =. fromIntegral (balance' ^. BA.centbalance)]
 | 
					                                         update uid [UserBalanceCents =. fromIntegral (balance' ^. BA.centbalance)]
 | 
				
			||||||
                                         update uid [UserBalanceTimeout =. time']
 | 
					                                         update uid [UserBalanceTimeout =. time']
 | 
				
			||||||
                       _ -> return ()
 | 
					                       _ -> return ()
 | 
				
			||||||
 | 
					                   --update stock-worth (cache)
 | 
				
			||||||
 | 
					                   let stocksql = "update \"user\" set \
 | 
				
			||||||
 | 
					                                   stock_cents = (select sum(in_stock*price_cents) from transaction where \"user\"=\"user\".id and price_cents > 0 and in_stock > 0 and not trans_is_sell)\
 | 
				
			||||||
 | 
					                                   where id=?"
 | 
				
			||||||
 | 
					                   runDB $ rawExecute stocksql [toPersistValue uid]
 | 
				
			||||||
 | 
					                   --get current Orders
 | 
				
			||||||
 | 
					                   when (userOrderTimeout user < now) $
 | 
				
			||||||
 | 
					                     do
 | 
				
			||||||
 | 
					                       orders <- liftIO $ MO.getMarketOrders man apidata
 | 
				
			||||||
 | 
					                       case orders of
 | 
				
			||||||
 | 
					                         T.QueryResult time' orders' -> runDB $ do
 | 
				
			||||||
 | 
					                                         deleteWhere [OrderUser ==. uid]
 | 
				
			||||||
 | 
					                                         insertMany_ (migrateOrders uid <$> orders')
 | 
				
			||||||
 | 
					                                         update uid [UserOrderTimeout =. time']
 | 
				
			||||||
 | 
					                                         --update escrow-worth (cache)
 | 
				
			||||||
 | 
					                                         let ordersql = "update \"user\" set \
 | 
				
			||||||
 | 
					                                                        escrow_cents = COALESCE((select sum(escrow_cents) from \"order\" where \"user\"=\"user\".id),0) \
 | 
				
			||||||
 | 
					                                                        where id=?"
 | 
				
			||||||
 | 
					                                         rawExecute ordersql [toPersistValue uid]
 | 
				
			||||||
 | 
					                         _ -> return ()
 | 
				
			||||||
               redirect WalletR
 | 
					               redirect WalletR
 | 
				
			||||||
             )
 | 
					             )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -152,3 +173,8 @@ migrateTransaction u (WT.Transaction dt tid q tn ti pc ci cn si sn tt tf jti) =
 | 
				
			|||||||
      tfc :: WT.TransactionFor -> Bool
 | 
					      tfc :: WT.TransactionFor -> Bool
 | 
				
			||||||
      tfc WT.Corporation = True
 | 
					      tfc WT.Corporation = True
 | 
				
			||||||
      tfc WT.Personal = False
 | 
					      tfc WT.Personal = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					migrateOrders :: UserId -> MO.Order -> Import.Order
 | 
				
			||||||
 | 
					migrateOrders uid (MO.Order oid cid sid ve vr mv os tid r ak dur esc pric bid iss) =
 | 
				
			||||||
 | 
					               Import.Order uid oid cid sid ve vr mv (fromIntegral . fromEnum $ os) tid (fromIntegral . fromEnum $ r) (fromIntegral ak) (fromIntegral dur) (fromInteger esc) (fromInteger pric) (bid == MO.Sell) iss
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,6 +18,14 @@ data Profit = Profit
 | 
				
			|||||||
              , tt :: Int64
 | 
					              , tt :: Int64
 | 
				
			||||||
              } deriving (Show, Eq)
 | 
					              } deriving (Show, Eq)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					data ProfitSum = ProfitSum
 | 
				
			||||||
 | 
					                 { psbuy :: Int64
 | 
				
			||||||
 | 
					                 , pssell :: Int64
 | 
				
			||||||
 | 
					                 , psprofit :: Int64
 | 
				
			||||||
 | 
					                 , psbf :: Int64
 | 
				
			||||||
 | 
					                 , pstt :: Int64
 | 
				
			||||||
 | 
					                 } deriving (Show, Eq)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
instance RawSql Profit where
 | 
					instance RawSql Profit where
 | 
				
			||||||
  rawSqlCols _ _ = (6,[])
 | 
					  rawSqlCols _ _ = (6,[])
 | 
				
			||||||
  rawSqlColCountReason _ = "date, buy, sell, profit, bf, tt"
 | 
					  rawSqlColCountReason _ = "date, buy, sell, profit, bf, tt"
 | 
				
			||||||
@@ -61,6 +69,7 @@ getWalletDetailsR hrs days = loginOrDo (\(uid,user) -> do
 | 
				
			|||||||
                                order by \
 | 
					                                order by \
 | 
				
			||||||
                                  extract(day from (now() at time zone 'utc')-date(date_time at time zone 'utc')) asc"
 | 
					                                  extract(day from (now() at time zone 'utc')-date(date_time at time zone 'utc')) asc"
 | 
				
			||||||
             (profits :: [Profit]) <- runDB $ rawSql profitquery [toPersistValue uid, toPersistValue days]
 | 
					             (profits :: [Profit]) <- runDB $ rawSql profitquery [toPersistValue uid, toPersistValue days]
 | 
				
			||||||
 | 
					             let profitssum = foldl' addProfit (ProfitSum 0 0 0 0 0) profits
 | 
				
			||||||
             loginLayout user $ [whamlet|
 | 
					             loginLayout user $ [whamlet|
 | 
				
			||||||
             <div .panel .panel-default>
 | 
					             <div .panel .panel-default>
 | 
				
			||||||
               <div .panel-heading>Transactions in the last #{hrs} hours:
 | 
					               <div .panel-heading>Transactions in the last #{hrs} hours:
 | 
				
			||||||
@@ -162,6 +171,20 @@ getWalletDetailsR hrs days = loginOrDo (\(uid,user) -> do
 | 
				
			|||||||
                         #{pp}
 | 
					                         #{pp}
 | 
				
			||||||
                       $nothing
 | 
					                       $nothing
 | 
				
			||||||
                          
 | 
					                          
 | 
				
			||||||
 | 
					                 $with (ProfitSum b s p bf tt) <- profitssum
 | 
				
			||||||
 | 
					                   <tr .total>
 | 
				
			||||||
 | 
					                     <th .text-center>Total
 | 
				
			||||||
 | 
					                     <td .numeric>#{prettyISK b}
 | 
				
			||||||
 | 
					                     <td .numeric>#{prettyISK s}
 | 
				
			||||||
 | 
					                     <td .numeric>#{prettyISK p}
 | 
				
			||||||
 | 
					                     <td .numeric>#{prettyISK bf}
 | 
				
			||||||
 | 
					                     <td .numeric>#{prettyISK tt}
 | 
				
			||||||
 | 
					                     <td .numeric>#{transRealProfit' p bf tt}
 | 
				
			||||||
 | 
					                     <td .numeric>
 | 
				
			||||||
 | 
					                       $maybe pp <- profitPercent' p bf tt s
 | 
				
			||||||
 | 
					                         #{pp}
 | 
				
			||||||
 | 
					                       $nothing
 | 
				
			||||||
 | 
					                          
 | 
				
			||||||
             |]
 | 
					             |]
 | 
				
			||||||
             )
 | 
					             )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -180,3 +203,6 @@ profitPercent' p bf tt s = if s == 0 then Nothing
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
profitPercent :: Int64 -> Transaction -> String
 | 
					profitPercent :: Int64 -> Transaction -> String
 | 
				
			||||||
profitPercent p t = printf "%.2f" $ (100*(fromIntegral p) / (fromIntegral (transactionQuantity t * transactionPriceCents t)) :: Double)
 | 
					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')
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,6 +9,8 @@ import Text.Printf
 | 
				
			|||||||
import Data.List (unfoldr)
 | 
					import Data.List (unfoldr)
 | 
				
			||||||
import Text.Hamlet
 | 
					import Text.Hamlet
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					{- CONVINIENCE FUNCTIONS -}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
loginOrDo :: ((Key User, User) -> Handler Html) -> Handler Html
 | 
					loginOrDo :: ((Key User, User) -> Handler Html) -> Handler Html
 | 
				
			||||||
loginOrDo cont = do
 | 
					loginOrDo cont = do
 | 
				
			||||||
                 maid <- maybeAuthId
 | 
					                 maid <- maybeAuthId
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,9 +10,12 @@ User
 | 
				
			|||||||
    standingsTimeout UTCTime
 | 
					    standingsTimeout UTCTime
 | 
				
			||||||
    balanceTimeout UTCTime
 | 
					    balanceTimeout UTCTime
 | 
				
			||||||
    skillTimeout UTCTime
 | 
					    skillTimeout UTCTime
 | 
				
			||||||
 | 
					    orderTimeout UTCTime
 | 
				
			||||||
    br Int -- Broker-Relations-Skill
 | 
					    br Int -- Broker-Relations-Skill
 | 
				
			||||||
    acc Int -- Accounting-Skill
 | 
					    acc Int -- Accounting-Skill
 | 
				
			||||||
    balanceCents Int64
 | 
					    balanceCents Int64
 | 
				
			||||||
 | 
					    stockCents Int64
 | 
				
			||||||
 | 
					    escrowCents Int64
 | 
				
			||||||
    UniqueUser ident
 | 
					    UniqueUser ident
 | 
				
			||||||
    deriving Typeable
 | 
					    deriving Typeable
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -63,5 +66,22 @@ FactionStandings
 | 
				
			|||||||
    factionName Text
 | 
					    factionName Text
 | 
				
			||||||
    factionStanding Double
 | 
					    factionStanding Double
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Order
 | 
				
			||||||
 | 
					    user UserId
 | 
				
			||||||
 | 
					    orderId Int64
 | 
				
			||||||
 | 
					    charId Int64
 | 
				
			||||||
 | 
					    stationId Int64
 | 
				
			||||||
 | 
					    volEntered Int64
 | 
				
			||||||
 | 
					    volRemaining Int64
 | 
				
			||||||
 | 
					    minVolume Int64
 | 
				
			||||||
 | 
					    orderState Int32 --no custom field as this forces string-comparisons. Use toEnum/fromEnum to get an Int
 | 
				
			||||||
 | 
					    typeId Int64
 | 
				
			||||||
 | 
					    range Int32 --same as orderState
 | 
				
			||||||
 | 
					    accountKey Int32
 | 
				
			||||||
 | 
					    duration Int32
 | 
				
			||||||
 | 
					    escrowCents Int64
 | 
				
			||||||
 | 
					    priceCents Int64
 | 
				
			||||||
 | 
					    isSell Bool
 | 
				
			||||||
 | 
					    issued UTCTime
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 -- By default this file is used in Model.hs (which is imported by Foundation.hs)
 | 
					 -- By default this file is used in Model.hs (which is imported by Foundation.hs)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -35,3 +35,7 @@
 | 
				
			|||||||
  text-align: right;
 | 
					  text-align: right;
 | 
				
			||||||
  font-family: monospace;
 | 
					  font-family: monospace;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					tr.total {
 | 
				
			||||||
 | 
					  border-top: 2px solid grey;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -53,7 +53,7 @@ $newline never
 | 
				
			|||||||
                  <li role="separator" class="divider">
 | 
					                  <li role="separator" class="divider">
 | 
				
			||||||
                  <li><a href="@SettingsR">Settings</a-->
 | 
					                  <li><a href="@SettingsR">Settings</a-->
 | 
				
			||||||
              <li><a href="@{SettingsR}">Settings</a>
 | 
					              <li><a href="@{SettingsR}">Settings</a>
 | 
				
			||||||
              <li><a href="#">#{prettyISK $ userBalanceCents user} ISK</a>
 | 
					              <li><a href="#">#{prettyISK $ foldl' (+) 0 [userBalanceCents user, userEscrowCents user, userStockCents user]} ISK</a>
 | 
				
			||||||
            <ul class="nav navbar-nav navbar-right">
 | 
					            <ul class="nav navbar-nav navbar-right">
 | 
				
			||||||
              <li><a href="@{UpdateR}">Update</a>
 | 
					              <li><a href="@{UpdateR}">Update</a>
 | 
				
			||||||
              <li><a href="@{AuthR LogoutR}">Logout</a>
 | 
					              <li><a href="@{AuthR LogoutR}">Logout</a>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user