yammat/Handler/Select.hs

89 lines
2.9 KiB
Haskell
Raw Normal View History

2015-08-09 21:16:33 +02:00
-- yammat - Yet Another MateMAT
-- Copyright (C) 2015 Amedeo Molnár
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as published
-- by the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
2018-10-02 20:25:32 +02:00
{-# LANGUAGE DoAndIfThenElse #-}
2015-04-04 06:46:33 +02:00
module Handler.Select where
import Import
import Handler.Common
import qualified Data.Text as T
2015-04-16 02:51:08 +02:00
import Data.Maybe
2015-04-04 06:46:33 +02:00
getSelectR :: UserId -> Handler Html
2016-05-23 20:35:14 +02:00
getSelectR uId =
isUser uId HomeR >>= (\user -> do
master <- getYesod
2018-10-02 20:25:32 +02:00
mpin <- lookupSession "pinentry"
case mpin of
Nothing -> redirect $ PinentryR uId
2018-10-02 20:51:56 +02:00
Just ppin ->
2018-10-02 20:25:32 +02:00
if ppin == T.pack (show uId)
then do
beverages <- runDB $ selectList [BeverageAmount >. 0] [Asc BeverageIdent]
defaultLayout $ do
addScript $ StaticR js_barcode_js
setTitleI MsgSelectItem
$(widgetFile "select")
else do
deleteSession "pinentry"
setMessageI MsgWrongPinAuth
redirect HomeR
2016-05-23 20:35:14 +02:00
)
2015-04-04 06:46:33 +02:00
getSelectCashR :: Handler Html
getSelectCashR = do
2015-04-09 13:10:23 +02:00
beverages <- runDB $ selectList [BeverageAmount >. 0] [Asc BeverageIdent]
2017-02-12 11:58:54 +01:00
defaultLayout $ do
addScript $ StaticR js_barcode_js
2018-09-04 18:06:12 +02:00
setTitleI MsgSelectItem
2015-04-04 06:46:33 +02:00
$(widgetFile "selectCash")
getRechargeR :: UserId -> Handler Html
2016-05-23 20:35:14 +02:00
getRechargeR uId =
isUser uId HomeR >>= (\user -> do
(rechargeWidget, enctype) <- generateFormPost
$ renderBootstrap3 BootstrapBasicForm rechargeForm
currency <- appCurrency <$> appSettings <$> getYesod
2018-09-04 18:06:12 +02:00
defaultLayout $ do
setTitleI MsgRecharge
2016-05-23 20:35:14 +02:00
$(widgetFile "recharge")
)
2015-04-04 06:46:33 +02:00
postRechargeR :: UserId -> Handler Html
2016-05-23 20:35:14 +02:00
postRechargeR uId =
isUser uId HomeR >>= (\user -> do
((res, _), _) <- runFormPost
$ renderBootstrap3 BootstrapBasicForm rechargeForm
case res of
FormSuccess amount ->
2017-08-13 19:51:20 +02:00
if amount <= 0
2016-05-23 20:35:14 +02:00
then do
setMessageI MsgNegativeRecharge
redirect $ RechargeR uId
else do
updateCashier amount ("Guthaben: " `T.append` userIdent user)
today <- liftIO $ return . utctDay =<< getCurrentTime
runDB $ update uId [UserBalance +=. amount, UserTimestamp =. today]
setMessageI MsgRecharged
redirect HomeR
_ -> do
setMessageI MsgRechargeError
redirect HomeR
)
2015-04-04 06:46:33 +02:00
2015-10-22 23:57:27 +02:00
rechargeForm :: AForm Handler Int
rechargeForm = areq currencyField (bfs MsgValue) (Just 0)
<* bootstrapSubmit (msgToBSSubmit MsgRecharge)