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.Journal where
|
|
|
|
|
|
|
|
import Import as I
|
|
|
|
import Data.List as L
|
|
|
|
import Handler.Common
|
|
|
|
|
|
|
|
getJournalR :: Handler Html
|
|
|
|
getJournalR = do
|
2015-04-04 06:25:10 +00:00
|
|
|
master <- getYesod
|
2015-04-10 15:00:50 +00:00
|
|
|
rawEntries <- runDB $ selectList [] [Desc TransactionId]
|
2015-09-07 16:15:30 +00:00
|
|
|
next <- runDB $ selectList [] [Desc TransactionId, OffsetBy 30]
|
2015-09-14 22:49:13 +00: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 12:50:44 +00:00
|
|
|
cashChecks <- runDB $ selectList [CashCheckTime >=. timeLimit] [Asc CashCheckId]
|
2015-09-14 22:49:13 +00:00
|
|
|
let list = merge entries cashChecks
|
2015-04-04 04:46:33 +00:00
|
|
|
cashBalance <- getCashierBalance
|
2015-09-14 22:49:13 +00:00
|
|
|
defaultLayout $
|
2015-04-04 04:46:33 +00:00
|
|
|
$(widgetFile "journal")
|
2015-04-09 19:39:39 +00:00
|
|
|
|
2015-04-10 12:50:44 +00:00
|
|
|
merge :: [Entity Transaction] -> [Entity CashCheck] -> [Either Transaction CashCheck]
|
2015-04-09 19:39:39 +00:00
|
|
|
merge [] [] = []
|
2015-04-10 12:50:44 +00:00
|
|
|
merge [] (c:cs) = (Right $ entityVal c) : merge [] cs
|
2015-04-09 19:39:39 +00:00
|
|
|
merge (t:ts) [] = (Left $ entityVal t) : merge ts []
|
|
|
|
merge (t:ts) (c:cs)
|
2015-04-10 12:50:44 +00:00
|
|
|
| transactionTime (entityVal t) < cashCheckTime (entityVal c) = (Left $ entityVal t) : merge ts (c:cs)
|
2016-01-21 21:06:14 +00:00
|
|
|
| otherwise = (Right $ entityVal c) : merge (t:ts) cs
|
2015-09-07 16:15:30 +00:00
|
|
|
|
|
|
|
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-14 22:49:13 +00: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
|
2015-09-07 16:15:30 +00:00
|
|
|
cashChecks <- runDB $ selectList [CashCheckTime >=. lTimeLimit, CashCheckTime <. uTimeLimit] [Asc CashCheckId]
|
2015-09-14 22:49:13 +00:00
|
|
|
let list = merge entries cashChecks
|
|
|
|
defaultLayout $
|
2015-09-07 16:15:30 +00:00
|
|
|
$(widgetFile "journalPage")
|