yammat/Handler/Journal.hs

64 lines
2.7 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.Journal where
import Import as I
import Data.List as L
import Handler.Common
getJournalR :: Handler Html
getJournalR = do
2015-04-04 08:25:10 +02:00
master <- getYesod
2015-04-10 17:00:50 +02:00
rawEntries <- runDB $ selectList [] [Desc TransactionId]
next <- runDB $ selectList [] [Desc TransactionId, OffsetBy 30]
2015-09-15 00:49:13 +02:00
let entries = L.reverse $ L.take 30 rawEntries
let total = L.sum $ I.map (transactionAmount . entityVal) rawEntries
timeLimit <- if L.null entries
then liftIO getCurrentTime
else return $ transactionTime $ entityVal $ L.head entries
2015-04-10 14:50:44 +02:00
cashChecks <- runDB $ selectList [CashCheckTime >=. timeLimit] [Asc CashCheckId]
2015-09-15 00:49:13 +02:00
let list = merge entries cashChecks
2015-04-04 06:46:33 +02:00
cashBalance <- getCashierBalance
2018-09-04 18:06:12 +02:00
defaultLayout $ do
setTitleI MsgJournal
2015-04-04 06:46:33 +02:00
$(widgetFile "journal")
2015-04-09 21:39:39 +02:00
2015-04-10 14:50:44 +02:00
merge :: [Entity Transaction] -> [Entity CashCheck] -> [Either Transaction CashCheck]
2015-04-09 21:39:39 +02:00
merge [] [] = []
2015-04-10 14:50:44 +02:00
merge [] (c:cs) = (Right $ entityVal c) : merge [] cs
2015-04-09 21:39:39 +02:00
merge (t:ts) [] = (Left $ entityVal t) : merge ts []
merge (t:ts) (c:cs)
2015-04-10 14:50:44 +02:00
| transactionTime (entityVal t) < cashCheckTime (entityVal c) = (Left $ entityVal t) : merge ts (c:cs)
2016-01-21 22:06:14 +01:00
| otherwise = (Right $ entityVal c) : merge (t:ts) cs
getJournalPageR :: Int -> Handler Html
getJournalPageR p = do
master <- getYesod
rawEntries <- runDB $ selectList [] [Desc TransactionId, OffsetBy (p * 30)]
next <- runDB $ selectList [] [Desc TransactionId, OffsetBy ((p + 1) * 30)]
2015-09-15 00:49:13 +02:00
let entries = L.reverse $ L.take 30 rawEntries
lTimeLimit <- if L.null entries
then liftIO getCurrentTime
else return $ transactionTime $ entityVal $ L.head entries
uTimeLimit <- if L.null entries
then liftIO getCurrentTime
else return $ transactionTime $ entityVal $ L.last entries
cashChecks <- runDB $ selectList [CashCheckTime >=. lTimeLimit, CashCheckTime <. uTimeLimit] [Asc CashCheckId]
2015-09-15 00:49:13 +02:00
let list = merge entries cashChecks
2018-09-04 18:06:12 +02:00
defaultLayout $ do
setTitleI MsgJournal
$(widgetFile "journalPage")