yammat/Handler/Journal.hs

29 lines
1.1 KiB
Haskell
Raw Normal View History

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]
entries <- return $ L.reverse $ L.take 30 rawEntries
total <- return $ L.sum $ I.map (transactionAmount . entityVal) rawEntries
timeLimit <- case L.null entries of
False -> return $ transactionTime $ entityVal $ L.head $ entries
True -> liftIO getCurrentTime
2015-04-10 14:50:44 +02:00
cashChecks <- runDB $ selectList [CashCheckTime >=. timeLimit] [Asc CashCheckId]
list <- return $ merge entries cashChecks
2015-04-04 06:46:33 +02:00
cashBalance <- getCashierBalance
defaultLayout $ do
$(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)
| transactionTime (entityVal t) > cashCheckTime (entityVal c) = (Right $ entityVal c) : merge (t:ts) cs