yammat/Handler/Select.hs

89 lines
2.9 KiB
Haskell
Raw Normal View History

2015-08-09 19:16:33 +00: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 18:25:32 +00:00
{-# LANGUAGE DoAndIfThenElse #-}
2015-04-04 04:46:33 +00:00
module Handler.Select where
import Import
import Handler.Common
import qualified Data.Text as T
2015-04-16 00:51:08 +00:00
import Data.Maybe
2015-04-04 04:46:33 +00:00
getSelectR :: UserId -> Handler Html
2016-05-23 18:35:14 +00:00
getSelectR uId =
isUser uId HomeR >>= (\user -> do
master <- getYesod
2018-10-02 18:25:32 +00:00
mpin <- lookupSession "pinentry"
case mpin of
Nothing -> redirect $ PinentryR uId
2018-10-02 18:51:56 +00:00
Just ppin ->
2018-10-02 18:25:32 +00: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 18:35:14 +00:00
)
2015-04-04 04:46:33 +00:00
getSelectCashR :: Handler Html
getSelectCashR = do
2015-04-09 11:10:23 +00:00
beverages <- runDB $ selectList [BeverageAmount >. 0] [Asc BeverageIdent]
2017-02-12 10:58:54 +00:00
defaultLayout $ do
addScript $ StaticR js_barcode_js
2018-09-04 16:06:12 +00:00
setTitleI MsgSelectItem
2015-04-04 04:46:33 +00:00
$(widgetFile "selectCash")
getRechargeR :: UserId -> Handler Html
2016-05-23 18:35:14 +00:00
getRechargeR uId =
isUser uId HomeR >>= (\user -> do
(rechargeWidget, enctype) <- generateFormPost
$ renderBootstrap3 BootstrapBasicForm rechargeForm
currency <- appCurrency <$> appSettings <$> getYesod
2018-09-04 16:06:12 +00:00
defaultLayout $ do
setTitleI MsgRecharge
2016-05-23 18:35:14 +00:00
$(widgetFile "recharge")
)
2015-04-04 04:46:33 +00:00
postRechargeR :: UserId -> Handler Html
2016-05-23 18:35:14 +00:00
postRechargeR uId =
isUser uId HomeR >>= (\user -> do
((res, _), _) <- runFormPost
$ renderBootstrap3 BootstrapBasicForm rechargeForm
case res of
FormSuccess amount ->
2017-08-13 17:51:20 +00:00
if amount <= 0
2016-05-23 18:35:14 +00: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 04:46:33 +00:00
2015-10-22 21:57:27 +00:00
rechargeForm :: AForm Handler Int
rechargeForm = areq currencyField (bfs MsgValue) (Just 0)
<* bootstrapSubmit (msgToBSSubmit MsgRecharge)