diff --git a/src/Control/Buy.hs b/src/Control/Buy.hs index 6f4a5e9..e611a35 100644 --- a/src/Control/Buy.hs +++ b/src/Control/Buy.hs @@ -52,7 +52,8 @@ buy auth pds = do Nothing -> do void $ insertNewJournalEntry (JournalSubmit - ("Cash purchase") + Nothing + BuyCash price ) conn diff --git a/src/Control/User.hs b/src/Control/User.hs index b6c42e3..104dd71 100644 --- a/src/Control/User.hs +++ b/src/Control/User.hs @@ -92,8 +92,8 @@ userRecharge (Just (auid, _)) (UserRecharge amount) = do ud <- userDetailsSelect auid conn void $ insertNewJournalEntry (JournalSubmit - ("User \"" <> userDetailsIdent ud <> "\" recharged " <> - T.pack (show (fromIntegral amount / 100 :: Double))) + (Just $ userDetailsId ud) + Recharge amount ) conn diff --git a/src/Model/Journal.hs b/src/Model/Journal.hs index d918443..cb701f2 100644 --- a/src/Model/Journal.hs +++ b/src/Model/Journal.hs @@ -30,9 +30,10 @@ initJournal = mconcat [ "CREATE TABLE IF NOT EXISTS \"journal\" (" , "journal_id SERIAL NOT NULL," , "journal_timestamp TIMESTAMPTZ NOT NULL," - , "journal_description TEXT NOT NULL," + , "journal_user INTEGER," + , "journal_action INTEGER NOT NULL," , "journal_total_amount INTEGER NOT NULL," - , "journal_entry_is_check BOOLEAN NOT NULL," + -- , "journal_entry_is_check BOOLEAN NOT NULL," , "PRIMARY KEY (journal_id)" , ")" ] @@ -40,23 +41,26 @@ initJournal = mconcat journalTable :: Table ( Maybe (Field SqlInt4) , Field SqlTimestamptz - , Field SqlText + , FieldNullable SqlInt4 , Field SqlInt4 - , Field SqlBool + , Field SqlInt4 + -- , Field SqlBool ) ( Field SqlInt4 , Field SqlTimestamptz - , Field SqlText + , FieldNullable SqlInt4 , Field SqlInt4 - , Field SqlBool + , Field SqlInt4 + -- , Field SqlBool ) journalTable = table "journal" ( p5 ( tableField "journal_id" , tableField "journal_timestamp" - , tableField "journal_description" + , tableField "journal_user" + , tableField "journal_action" , tableField "journal_total_amount" - , tableField "journal_entry_is_check" + -- , tableField "journal_entry_is_check" ) ) @@ -81,14 +85,14 @@ selectJournalEntries mlimit moffset conn = liftIO $ do ) :: IO [ ( Int , UTCTime - , T.Text + , Maybe Int + , Int , Int - , Bool ) ] return $ fst $ foldr - (\(id_, ts, descr, tot, check) (fin, before)-> - (JournalEntry id_ descr ts (tot - before) tot check : fin, tot) + (\(id_, ts, user, action, tot) (fin, before)-> + (JournalEntry id_ ts user (toEnum action) (tot - before) tot : fin, tot) ) ( [] , if isJust mlimit && length entries > fromJust mlimit @@ -112,16 +116,16 @@ selectLatestJournalEntry conn = liftIO $ do ) :: IO [ ( Int , UTCTime - , T.Text + , Maybe Int + , Int , Int - , Bool ) ] if not (null lastTwoEntries) then do - let diff = foldl (\acc (_, _, _, tot, _) -> tot - acc) 0 lastTwoEntries - (jid, ts, descr, total, check) = head lastTwoEntries - return $ Just $ JournalEntry jid descr ts diff total check + let diff = foldl (\acc (_, _, _, _, tot) -> tot - acc) 0 lastTwoEntries + (jid, ts, user, action, total) = head lastTwoEntries + return $ Just $ JournalEntry jid ts user (toEnum action) diff total else return Nothing @@ -130,7 +134,7 @@ insertNewJournalEntry :: JournalSubmit -> PGS.Connection -> MateHandler Int -insertNewJournalEntry (JournalSubmit descr amount) conn = do +insertNewJournalEntry (JournalSubmit user action amount) conn = do lastTotal <- (\case Just j -> journalEntryTotalAmount j Nothing -> 0 @@ -143,9 +147,9 @@ insertNewJournalEntry (JournalSubmit descr amount) conn = do [ ( C.constant (Nothing :: Maybe Int) , C.constant now - , C.constant descr + , C.constant user + , C.constant (fromEnum action) , C.constant (lastTotal + amount) - , C.constant False ) ] , iReturning = rReturning (\(id_, _, _, _, _) -> id_) @@ -156,7 +160,7 @@ insertNewCashCheck :: JournalCashCheck -> PGS.Connection -> MateHandler Int -insertNewCashCheck (JournalCashCheck amount) conn = +insertNewCashCheck (JournalCashCheck user amount) conn = -- lastTotal <- (\case -- Just j -> journalEntryTotalAmount j -- Nothing -> 0 @@ -169,9 +173,9 @@ insertNewCashCheck (JournalCashCheck amount) conn = [ ( C.constant (Nothing :: Maybe Int) , C.constant now - , C.constant ("Cash check" :: String) + , C.constant (Just user) + , C.constant (fromEnum CashCheck) , C.constant amount - , C.constant True ) ] , iReturning = rReturning (\(id_, _, _, _, _) -> id_) diff --git a/src/Types/Journal.hs b/src/Types/Journal.hs index 3bfa4fc..2f55383 100644 --- a/src/Types/Journal.hs +++ b/src/Types/Journal.hs @@ -4,19 +4,18 @@ module Types.Journal where import GHC.Generics -import qualified Data.Text as T (Text) - import Data.Aeson import Data.Time (UTCTime) data JournalEntry = JournalEntry { journalEntryId :: Int - , journalEntryDescription :: T.Text , journalEntryTimestamp :: UTCTime + , journalEntryUser :: Maybe Int + , journalEntryAction :: JournalAction , journalEntryAmount :: Int , journalEntryTotalAmount :: Int - , journalEntryIsCheck :: Bool + -- , journalEntryIsCheck :: Bool } deriving (Generic, Show) @@ -27,8 +26,9 @@ instance FromJSON JournalEntry data JournalSubmit = JournalSubmit - { journalSubmitDescription :: T.Text - , journalSubmitAmount :: Int + { journalSubmitUser :: Maybe Int + , journalSubmitAction :: JournalAction + , journalSubmitAmount :: Int } deriving (Generic, Show) @@ -38,8 +38,9 @@ instance ToJSON JournalSubmit where instance FromJSON JournalSubmit -newtype JournalCashCheck = JournalCashCheck - { journalCashCheckTotalAmount :: Int +data JournalCashCheck = JournalCashCheck + { journalCashCheckUser :: Int + , journalCashCheckTotalAmount :: Int } deriving (Generic, Show) @@ -47,3 +48,16 @@ instance ToJSON JournalCashCheck where toEncoding = genericToEncoding defaultOptions instance FromJSON JournalCashCheck + +data JournalAction + = CashCheck + | Recharge + | BuyCash + | PayBill + | PayOut + deriving (Generic, Show, Enum, Eq) + +instance ToJSON JournalAction where + toEncoding = genericToEncoding defaultOptions + +instance FromJSON JournalAction