From 3e6701b12e4f3d8765d8fc5529b6a485065dbd73 Mon Sep 17 00:00:00 2001 From: nek0 Date: Fri, 10 Apr 2015 14:50:44 +0200 Subject: [PATCH] resolved cashCheck issue --- Handler/CashCheck.hs | 9 +++++---- Handler/Common.hs | 7 +++---- Handler/Journal.hs | 13 +++++++------ config/models | 5 ++++- templates/journal.hamlet | 4 ++-- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Handler/CashCheck.hs b/Handler/CashCheck.hs index f495de5..10faa62 100644 --- a/Handler/CashCheck.hs +++ b/Handler/CashCheck.hs @@ -5,23 +5,24 @@ import Handler.Common getCashCheckR :: Handler Html getCashCheckR = do - (cashCheckWidget, enctype) <- generateFormPost createCashierForm + (cashCheckWidget, enctype) <- generateFormPost createCashCheckForm defaultLayout $ do $(widgetFile "cashCheck") postCashCheckR :: Handler Html postCashCheckR = do - ((res, _), _) <- runFormPost createCashierForm + ((res, _), _) <- runFormPost createCashCheckForm case res of FormSuccess c -> do runDB $ insert_ c + runDB $ insert_ $ Cashier (cashCheckBalance c) setMessage "Kassensturz durchgeführt. Kasse aktualisiert" redirect $ HomeR _ -> do setMessage "Fehler im Kassensturz" redirect $ CashCheckR -createCashierForm :: Form Cashier -createCashierForm = renderDivs $ Cashier +createCashCheckForm :: Form CashCheck +createCashCheckForm = renderDivs $ CashCheck <$> areq currencyField "Gezählter Betrag" Nothing <*> lift (liftIO getCurrentTime) diff --git a/Handler/Common.hs b/Handler/Common.hs index eee3dd8..e18156f 100644 --- a/Handler/Common.hs +++ b/Handler/Common.hs @@ -26,20 +26,19 @@ getRobotsR = return $ TypedContent typePlain updateCashier :: Int -> Text -> Handler () updateCashier amount desc = do - mCashier <- runDB $ selectFirst [] [Desc CashierCreated] + mCashier <- runDB $ selectFirst [] [Desc CashierId] trans <- liftIO $ (\time -> return $ Transaction desc amount time) =<< getCurrentTime case mCashier of Just entCash -> do runDB $ update (entityKey entCash) [CashierBalance +=. amount] runDB $ insert_ trans Nothing -> do - currentTime <- liftIO getCurrentTime - runDB $ insert_ $ Cashier amount currentTime + runDB $ insert_ $ Cashier amount runDB $ insert_ trans getCashierBalance :: Handler Int getCashierBalance = do - mCashier <- runDB $ selectFirst [] [Desc CashierCreated] + mCashier <- runDB $ selectFirst [] [Desc CashierId] case mCashier of Just cashier -> do return $ cashierBalance $ entityVal cashier diff --git a/Handler/Journal.hs b/Handler/Journal.hs index 4603a48..557d0cb 100644 --- a/Handler/Journal.hs +++ b/Handler/Journal.hs @@ -10,16 +10,17 @@ getJournalR = do rawEntries <- runDB $ selectList [] [Desc TransactionId, LimitTo 30] entries <- return $ L.reverse rawEntries total <- return $ L.sum $ I.map (transactionAmount . entityVal) entries - timeLimit <- return $ transactionTime $ entityVal $ L.last $ entries - cashiers <- runDB $ selectList [CashierCreated <=. timeLimit] [Asc CashierId] - list <- return $ merge entries cashiers + timeLimit <- return $ transactionTime $ entityVal $ L.head $ entries + cashChecks <- runDB $ selectList [CashCheckTime >=. timeLimit] [Asc CashCheckId] + list <- return $ merge entries cashChecks cashBalance <- getCashierBalance defaultLayout $ do $(widgetFile "journal") -merge :: [Entity Transaction] -> [Entity Cashier] -> [Either Transaction Cashier] +merge :: [Entity Transaction] -> [Entity CashCheck] -> [Either Transaction CashCheck] merge [] [] = [] +merge [] (c:cs) = (Right $ entityVal c) : merge [] cs merge (t:ts) [] = (Left $ entityVal t) : merge ts [] merge (t:ts) (c:cs) - | transactionTime (entityVal t) < cashierCreated (entityVal c) = (Left $ entityVal t) : merge ts (c:cs) - | transactionTime (entityVal t) > cashierCreated (entityVal c) = (Right $ entityVal c) : merge (t:ts) cs + | 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 diff --git a/config/models b/config/models index 4442ad4..f9dcb17 100644 --- a/config/models +++ b/config/models @@ -20,7 +20,10 @@ Transaction deriving Typeable Show Cashier balance Int - created UTCTime default=now() + deriving Typeable Show +CashCheck + balance Int + time UTCTime deriving Typeable Show -- By default this file is used in Model.hs (which is imported by Foundation.hs) diff --git a/templates/journal.hamlet b/templates/journal.hamlet index bbc43c1..02d9d17 100644 --- a/templates/journal.hamlet +++ b/templates/journal.hamlet @@ -19,8 +19,8 @@ $if not $ L.null entries $of Right cash - Kassensturz vom #{formatTime defaultTimeLocale "%A %F %H:%M" $ cashierCreated cash}: - #{formatIntCurrency (cashierBalance cash)} #{appCurrency $ appSettings master} + Kassensturz vom #{formatTime defaultTimeLocale "%A %F %H:%M" $ cashCheckTime cash}: + #{formatIntCurrency (cashCheckBalance cash)} #{appCurrency $ appSettings master} Gesamtbetrag: