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/>.
|
2015-04-04 04:46:33 +00:00
|
|
|
module Handler.Restock where
|
|
|
|
|
|
|
|
import Import
|
|
|
|
import Handler.Common
|
2016-07-21 15:09:03 +00:00
|
|
|
import Handler.Modify
|
2015-09-16 12:40:06 +00:00
|
|
|
import Data.Maybe (fromJust)
|
2015-04-04 04:46:33 +00:00
|
|
|
|
|
|
|
getRestockR :: Handler Html
|
|
|
|
getRestockR = do
|
2015-04-09 11:47:27 +00:00
|
|
|
beverages <- runDB $ selectList [] [Asc BeverageIdent]
|
2015-09-14 22:49:13 +00:00
|
|
|
defaultLayout $
|
2015-04-04 04:46:33 +00:00
|
|
|
$(widgetFile "restock")
|
|
|
|
|
|
|
|
getUpstockR :: BeverageId -> Handler Html
|
2016-05-23 18:35:14 +00:00
|
|
|
getUpstockR bId =
|
|
|
|
isBeverage bId RestockR >>= (\bev -> do
|
|
|
|
(upstockWidget, enctype) <- generateFormPost
|
|
|
|
$ renderBootstrap3 BootstrapBasicForm upstockForm
|
|
|
|
defaultLayout $
|
|
|
|
$(widgetFile "upstock")
|
|
|
|
)
|
2015-04-04 04:46:33 +00:00
|
|
|
|
|
|
|
postUpstockR :: BeverageId -> Handler Html
|
2016-05-23 18:35:14 +00:00
|
|
|
postUpstockR bId =
|
|
|
|
isBeverage bId RestockR >>= (\bev -> do
|
|
|
|
((res, _), _) <- runFormPost
|
|
|
|
$ renderBootstrap3 BootstrapBasicForm upstockForm
|
|
|
|
case res of
|
|
|
|
FormSuccess c ->
|
|
|
|
if upstockSingles c >= 0 && upstockCrates c >= 0
|
|
|
|
then do
|
|
|
|
let total = upstockSingles c + (upstockCrates c * (fromMaybe 0 $ beveragePerCrate bev))
|
|
|
|
runDB $ update bId [BeverageAmount +=. total]
|
|
|
|
setMessageI MsgStockedUp
|
|
|
|
redirect RestockR
|
|
|
|
else do
|
|
|
|
setMessageI MsgNotStockedUp
|
|
|
|
redirect $ UpstockR bId
|
|
|
|
|
|
|
|
_ -> do
|
|
|
|
setMessageI MsgStockupError
|
|
|
|
redirect $ UpstockR bId
|
|
|
|
)
|
2015-04-04 04:46:33 +00:00
|
|
|
|
2016-01-30 21:57:45 +00:00
|
|
|
data UpstockAmount = UpstockAmount
|
|
|
|
{ upstockSingles :: Int
|
|
|
|
, upstockCrates :: Int
|
|
|
|
}
|
|
|
|
|
|
|
|
upstockForm :: AForm Handler UpstockAmount
|
|
|
|
upstockForm = UpstockAmount
|
|
|
|
<$> areq amountField (bfs MsgAmountAdded) (Just 1)
|
|
|
|
<*> areq amountField (bfs MsgCrateAmountAdded) (Just 0)
|
|
|
|
<* bootstrapSubmit (msgToBSSubmit MsgFillup)
|
2015-04-04 04:46:33 +00:00
|
|
|
|
|
|
|
getNewArticleR :: Handler Html
|
|
|
|
getNewArticleR = do
|
2015-10-22 21:57:27 +00:00
|
|
|
(newArticleWidget, enctype) <- generateFormPost
|
2016-07-21 15:09:03 +00:00
|
|
|
$ renderBootstrap3 BootstrapBasicForm $ modifyForm Nothing []
|
2015-09-14 22:49:13 +00:00
|
|
|
defaultLayout $
|
2015-04-04 04:46:33 +00:00
|
|
|
$(widgetFile "newArticle")
|
|
|
|
|
|
|
|
postNewArticleR :: Handler Html
|
|
|
|
postNewArticleR = do
|
2015-10-22 21:57:27 +00:00
|
|
|
((result, _), _) <- runFormPost
|
2016-07-21 15:09:03 +00:00
|
|
|
$ renderBootstrap3 BootstrapBasicForm $ modifyForm Nothing []
|
2015-04-04 04:46:33 +00:00
|
|
|
case result of
|
2016-07-21 15:09:03 +00:00
|
|
|
FormSuccess nBev -> do
|
|
|
|
bId <- runDB $ insert $ Beverage
|
|
|
|
(modBevIdent nBev)
|
|
|
|
(modBevPrice nBev)
|
|
|
|
(modBevAmount nBev)
|
|
|
|
(modBevAlertAmount nBev)
|
|
|
|
0
|
|
|
|
(modBevMl nBev)
|
|
|
|
(modBevAvatar nBev)
|
|
|
|
(modBevSupp nBev)
|
|
|
|
(modBevMaxAmount nBev)
|
|
|
|
(modBevPC nBev)
|
|
|
|
(modBevArtNr nBev)
|
|
|
|
(modBevPricePC nBev)
|
|
|
|
handleBarcodes (Right bId) (fromMaybe [] $ modBevBarcodes nBev)
|
2015-04-09 22:40:58 +00:00
|
|
|
setMessageI MsgItemAdded
|
2016-01-30 17:36:03 +00:00
|
|
|
redirect RestockR
|
2015-04-04 04:46:33 +00:00
|
|
|
_ -> do
|
2015-04-09 22:40:58 +00:00
|
|
|
setMessageI MsgItemNotAdded
|
2016-01-30 17:36:03 +00:00
|
|
|
redirect RestockR
|