yammat/Handler/Restock.hs

108 lines
3.6 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/>.
2015-04-04 06:46:33 +02:00
module Handler.Restock where
import Import
import Handler.Common
import Data.Maybe (fromJust)
2015-04-04 06:46:33 +02:00
getRestockR :: Handler Html
getRestockR = do
2015-04-09 13:47:27 +02:00
beverages <- runDB $ selectList [] [Asc BeverageIdent]
2015-09-15 00:49:13 +02:00
defaultLayout $
2015-04-04 06:46:33 +02:00
$(widgetFile "restock")
getUpstockR :: BeverageId -> Handler Html
getUpstockR bId = do
mBev <- runDB $ get bId
case mBev of
Just bev -> do
2015-10-22 23:57:27 +02:00
(upstockWidget, enctype) <- generateFormPost
$ renderBootstrap3 BootstrapBasicForm upstockForm
2015-09-15 00:49:13 +02:00
defaultLayout $
2015-04-04 06:46:33 +02:00
$(widgetFile "upstock")
Nothing -> do
2015-04-10 00:40:58 +02:00
setMessageI MsgItemUnknown
2015-09-15 00:49:13 +02:00
redirect HomeR
2015-04-04 06:46:33 +02:00
postUpstockR :: BeverageId -> Handler Html
postUpstockR bId = do
mBev <- runDB $ get bId
case mBev of
2015-09-15 00:49:13 +02:00
Just _ -> do
2015-10-22 23:57:27 +02:00
((res, _), _) <- runFormPost
$ renderBootstrap3 BootstrapBasicForm upstockForm
2015-04-04 06:46:33 +02:00
case res of
2015-09-15 00:49:13 +02:00
FormSuccess c ->
if c > 0
then do
2015-04-04 06:46:33 +02:00
runDB $ update bId [BeverageAmount +=. c]
2015-04-10 00:40:58 +02:00
setMessageI MsgStockedUp
2015-09-15 00:49:13 +02:00
redirect HomeR
else do
2015-04-10 00:40:58 +02:00
setMessageI MsgNotStockedUp
2015-04-04 06:46:33 +02:00
redirect $ UpstockR bId
2015-10-22 23:57:27 +02:00
2015-04-04 06:46:33 +02:00
_ -> do
2015-04-10 00:40:58 +02:00
setMessageI MsgStockupError
2015-04-04 06:46:33 +02:00
redirect $ UpstockR bId
Nothing -> do
2015-04-10 00:40:58 +02:00
setMessageI MsgItemUnknown
2015-09-15 00:49:13 +02:00
redirect HomeR
2015-04-04 06:46:33 +02:00
2015-10-22 23:57:27 +02:00
upstockForm :: AForm Handler Int
upstockForm = areq amountField (bfs MsgAmountAdded) (Just 1)
<* bootstrapSubmit (msgToBSSubmit MsgFillup)
2015-04-04 06:46:33 +02:00
getNewArticleR :: Handler Html
getNewArticleR = do
2015-10-22 23:57:27 +02:00
(newArticleWidget, enctype) <- generateFormPost
$ renderBootstrap3 BootstrapBasicForm newArticleForm
2015-09-15 00:49:13 +02:00
defaultLayout $
2015-04-04 06:46:33 +02:00
$(widgetFile "newArticle")
postNewArticleR :: Handler Html
postNewArticleR = do
2015-10-22 23:57:27 +02:00
((result, _), _) <- runFormPost
$ renderBootstrap3 BootstrapBasicForm newArticleForm
2015-04-04 06:46:33 +02:00
case result of
FormSuccess bev -> do
runDB $ insert_ bev
2015-04-10 00:40:58 +02:00
setMessageI MsgItemAdded
2015-09-15 00:49:13 +02:00
redirect HomeR
2015-04-04 06:46:33 +02:00
_ -> do
2015-04-10 00:40:58 +02:00
setMessageI MsgItemNotAdded
2015-09-15 00:49:13 +02:00
redirect HomeR
2015-04-04 06:46:33 +02:00
2015-10-22 23:57:27 +02:00
newArticleForm :: AForm Handler Beverage
newArticleForm = (\a b c d e f g h i j k l -> Beverage a b c d e i j k f g l h)
<$> areq textField (bfs MsgName) Nothing
<*> areq currencyField (bfs MsgPrice) (Just 100)
<*> areq amountField (bfs MsgAmount) (Just 0)
<*> areq amountField (bfs MsgAmountWarning) (Just 0)
<*> pure 0
2015-10-22 23:57:27 +02:00
<*> areq amountField (bfs MsgMaxAmount) (Just 200)
<*> aopt amountField (bfs MsgAmountPerCrate) (Just $ Just 20)
<*> aopt currencyField (bfs MsgPricePerCrate) Nothing
<*> areq volumeField (bfs MsgVolume) (Just 500)
<*> aopt (selectField avatars) (bfs MsgSelectAvatar) Nothing
<*> aopt (selectField sups) (bfs MsgSelectSupplier) Nothing
<*> aopt textField (bfs MsgArtNr) Nothing
<* bootstrapSubmit (msgToBSSubmit MsgSubmit)
2015-04-16 02:12:03 +02:00
where
2015-10-11 20:07:12 +02:00
avatars = optionsPersistKey [] [Asc AvatarIdent] avatarIdent
sups = optionsPersistKey [] [Asc SupplierIdent] supplierIdent