yammat/Handler/Payout.hs

60 lines
1.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/>.
2015-04-04 06:46:33 +02:00
module Handler.Payout where
import Import
import Handler.Common
import qualified Data.Text as T
data Payment = Payment
{ paymentAmount :: Int
, paymentDesc :: T.Text
}
getPayoutR :: Handler Html
getPayoutR = do
2015-10-22 23:57:27 +02:00
(payoutWidget, enctype) <- generateFormPost
$ renderBootstrap3 BootstrapBasicForm payoutForm
2018-09-04 18:06:12 +02:00
defaultLayout $ do
setTitleI MsgPayoutFromCash
2015-04-04 06:46:33 +02:00
$(widgetFile "payout")
postPayoutR :: Handler Html
postPayoutR = do
2015-10-22 23:57:27 +02:00
((res, _), _) <- runFormPost
$ renderBootstrap3 BootstrapBasicForm payoutForm
2015-04-04 06:46:33 +02:00
case res of
FormSuccess payment -> do
2017-10-19 17:11:33 +02:00
balance <- getCashierBalance
2017-10-19 22:47:37 +02:00
if balance >= paymentAmount payment
then do
msg <- renderMessage' $ MsgPayout $ paymentDesc payment
updateCashier (- (paymentAmount payment)) msg
setMessageI MsgPaidOut
redirect HomeR
else do
setMessageI MsgNotEnoughFunds
redirect HomeR
2015-04-04 06:46:33 +02:00
_ -> do
2015-04-10 00:40:58 +02:00
setMessageI MsgNotPaidOut
2015-09-15 00:49:13 +02:00
redirect JournalR
2015-04-04 06:46:33 +02:00
2015-10-22 23:57:27 +02:00
payoutForm :: AForm Handler Payment
payoutForm = Payment
<$> areq currencyField (bfs MsgValue) Nothing
<*> areq textField (bfs MsgDescription) Nothing
<* bootstrapSubmit (msgToBSSubmit MsgDoPayout)