diff --git a/src/Main.hs b/src/Main.hs index 7d74f63..23a9ab3 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,12 +1,15 @@ +{-# LANGUAGE NamedFieldPuns #-} module Main where import CLI (Invocation(..), invoked) import CSV (Row, parse) -import Data.Time (UTCTime(..), getCurrentTime) +import Data.List (minimum) +import Data.Map (elems, mergeWithKey) +import Data.Time (UTCTime(..), getCurrentTime, addDays) import Event (Event) import Medicine (Medicine, Pharmacy, pharmacy) import System.Exit (exitFailure, exitSuccess) -import Timeline (State, currentState) +import Timeline (State(..), currentState) readCSV :: Row a => FilePath -> IO [a] readCSV filePath = do @@ -22,10 +25,20 @@ getCurrentState = <*> (pharmacy <$> readCSV "medicine.csv") <*> readCSV "timeline.csv" +schedule :: State -> String +schedule (State {day, stock, consumptionRate}) = unlines [ + "Days left: " ++ show deltaDays + , "Provision on: " ++ show (deltaDays `addDays` day ) + ] + where + daysLeftByMedicine = + mergeWithKey (\k a b -> Just $ a / b) id id stock consumptionRate + deltaDays = truncate . minimum $ elems daysLeftByMedicine + main :: IO () main = do invocation <- invoked case invocation of Inventory -> show <$> getCurrentState >>= putStrLn - Schedule -> putStrLn "schedule" + Schedule -> schedule <$> getCurrentState >>= putStrLn Version version -> putStrLn version >> exitSuccess diff --git a/src/Timeline.hs b/src/Timeline.hs index 1f11146..5e400a9 100644 --- a/src/Timeline.hs +++ b/src/Timeline.hs @@ -1,6 +1,6 @@ {-# LANGUAGE NamedFieldPuns #-} module Timeline ( - State + State(..) , currentState ) where