2015-04-04 04:46:33 +00:00
|
|
|
module Handler.Buy where
|
|
|
|
|
|
|
|
import Import
|
|
|
|
import Handler.Common
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import Text.Blaze.Internal
|
2015-04-07 20:03:21 +00:00
|
|
|
import Text.Shakespeare.Text
|
2015-04-04 04:46:33 +00:00
|
|
|
|
|
|
|
getBuyR :: UserId -> BeverageId -> Handler Html
|
|
|
|
getBuyR uId bId = do
|
|
|
|
mTup <- checkData uId bId
|
|
|
|
case mTup of
|
|
|
|
Just (user, bev) -> do
|
2015-04-04 06:18:15 +00:00
|
|
|
master <- getYesod
|
2015-04-04 04:46:33 +00:00
|
|
|
(buyWidget, enctype) <- generateFormPost buyForm
|
|
|
|
defaultLayout $ do
|
|
|
|
$(widgetFile "buy")
|
|
|
|
Nothing -> do
|
|
|
|
setMessage "Benutzer oder Artikel unbekannt"
|
|
|
|
redirect $ HomeR
|
|
|
|
|
|
|
|
postBuyR :: UserId -> BeverageId -> Handler Html
|
|
|
|
postBuyR uId bId = do
|
|
|
|
mTup <- checkData uId bId
|
|
|
|
case mTup of
|
|
|
|
Just (user, bev) -> do
|
|
|
|
((res, _), _) <- runFormPost buyForm
|
|
|
|
case res of
|
|
|
|
FormSuccess quant -> do
|
2015-04-05 09:05:35 +00:00
|
|
|
case quant > beverageAmount bev of
|
2015-04-04 04:46:33 +00:00
|
|
|
False -> do
|
2015-04-05 09:05:35 +00:00
|
|
|
price <- return $ quant * (beveragePrice bev)
|
|
|
|
sw <- return $ price > (userBalance user)
|
|
|
|
runDB $ update uId [UserBalance -=. price]
|
|
|
|
runDB $ update bId [BeverageAmount -=. quant]
|
|
|
|
checkAlert bId
|
2015-04-07 20:03:21 +00:00
|
|
|
master <- getYesod
|
|
|
|
liftIO $ notifyUser user bev price master
|
2015-04-05 09:05:35 +00:00
|
|
|
case sw of
|
|
|
|
False -> do
|
|
|
|
setMessage "Viel Vergnügen"
|
|
|
|
redirect $ HomeR
|
|
|
|
True -> do
|
|
|
|
setMessage "Achtung: Guthaben im negativen Bereich"
|
|
|
|
redirect $ HomeR
|
2015-04-04 04:46:33 +00:00
|
|
|
True -> do
|
2015-04-05 09:05:35 +00:00
|
|
|
setMessage "So viele Artikel sind nicht vorhanden"
|
|
|
|
redirect $ BuyR uId bId
|
2015-04-04 04:46:33 +00:00
|
|
|
_ -> do
|
|
|
|
setMessage "Etwas ist schief gelaufen"
|
|
|
|
redirect $ HomeR
|
|
|
|
Nothing -> do
|
|
|
|
setMessage "Benutzer oder Artikel unbekannt"
|
|
|
|
redirect $ HomeR
|
|
|
|
|
2015-04-07 20:03:21 +00:00
|
|
|
notifyUser :: User -> Beverage -> Int -> App -> IO ()
|
|
|
|
notifyUser user bev price master = do
|
2015-04-10 15:00:19 +00:00
|
|
|
case userEmail user of
|
|
|
|
Just email ->
|
|
|
|
liftIO $ sendMail email "Einkauf beim Matematen"
|
|
|
|
[stext|
|
2015-04-07 20:03:21 +00:00
|
|
|
Hallo #{userIdent user},
|
|
|
|
|
|
|
|
Du hast gerade beim Matematen für #{formatIntCurrency price}#{appCurrency $ appSettings master} #{beverageIdent bev} eingekauft.
|
|
|
|
|
|
|
|
Viele Grüsse,
|
|
|
|
|
|
|
|
Der Matemat
|
2015-04-10 15:00:19 +00:00
|
|
|
|]
|
|
|
|
Nothing ->
|
2015-04-07 20:03:21 +00:00
|
|
|
return ()
|
|
|
|
|
2015-04-04 04:46:33 +00:00
|
|
|
getBuyCashR :: BeverageId -> Handler Html
|
|
|
|
getBuyCashR bId = do
|
|
|
|
mBev <- runDB $ get bId
|
|
|
|
case mBev of
|
|
|
|
Just bev -> do
|
2015-04-04 06:18:15 +00:00
|
|
|
master <- getYesod
|
2015-04-04 04:46:33 +00:00
|
|
|
(buyCashWidget, enctype) <- generateFormPost buyForm
|
|
|
|
defaultLayout $ do
|
|
|
|
$(widgetFile "buyCash")
|
|
|
|
Nothing -> do
|
|
|
|
setMessage "Getrank unbekannt"
|
|
|
|
redirect $ HomeR
|
|
|
|
|
|
|
|
postBuyCashR :: BeverageId -> Handler Html
|
|
|
|
postBuyCashR bId = do
|
|
|
|
mBev <- runDB $ get bId
|
|
|
|
case mBev of
|
|
|
|
Just bev -> do
|
|
|
|
((res, _), _) <- runFormPost buyForm
|
|
|
|
case res of
|
|
|
|
FormSuccess quant -> do
|
2015-04-05 09:05:35 +00:00
|
|
|
case quant > beverageAmount bev of
|
|
|
|
False -> do
|
|
|
|
master <- getYesod
|
|
|
|
price <- return $ quant * (beveragePrice bev + 50)
|
|
|
|
runDB $ update bId [BeverageAmount -=. quant]
|
|
|
|
updateCashier price "Barzahlung"
|
|
|
|
checkAlert bId
|
|
|
|
setMessage $ Content $ Text $ "Viel Vergnügen. Bitte Zahle "
|
2015-04-07 14:34:01 +00:00
|
|
|
`T.append` (formatIntCurrency price)
|
2015-04-05 09:05:35 +00:00
|
|
|
`T.append` " "
|
|
|
|
`T.append` (appCurrency $ appSettings master)
|
|
|
|
`T.append` " in die Kasse ein"
|
|
|
|
redirect $ HomeR
|
|
|
|
True -> do
|
|
|
|
setMessage "So viele Artikel sind nicht vorhanden"
|
|
|
|
redirect $ BuyCashR bId
|
2015-04-04 04:46:33 +00:00
|
|
|
_ -> do
|
|
|
|
setMessage "Etwas ist schief gelaufen"
|
|
|
|
redirect $ HomeR
|
|
|
|
Nothing -> do
|
|
|
|
setMessage "Artikel unbekannt"
|
|
|
|
redirect $ HomeR
|
|
|
|
|
|
|
|
checkData :: UserId -> BeverageId -> Handler (Maybe (User, Beverage))
|
|
|
|
checkData uId bId = do
|
|
|
|
mUser <- runDB $ get uId
|
|
|
|
mBev <- runDB $ get bId
|
|
|
|
case mUser of
|
|
|
|
Just user -> do
|
|
|
|
case mBev of
|
|
|
|
Just bev -> return $ Just (user, bev)
|
|
|
|
Nothing -> return Nothing
|
|
|
|
Nothing -> return Nothing
|
|
|
|
|
|
|
|
buyForm :: Form Int
|
|
|
|
buyForm = renderDivs
|
|
|
|
$ areq amountField "Anzahl" (Just 1)
|