diff --git a/src/Schedule.hs b/src/Schedule.hs index 16c4bab..1fdad6d 100644 --- a/src/Schedule.hs +++ b/src/Schedule.hs @@ -3,28 +3,33 @@ module Schedule ( schedule ) where -import Data.Map (elems, mergeWithKey) +import Data.Map ((!), elems, mergeWithKey) import Data.Time (Day, addDays) -import Medicine (Pharmacy) +import Medicine (Medicine(..), Pharmacy) import Timeline (State(..)) import YAML ((.:), Value(..), YAML(..)) data Schedule = Schedule { daysLeft :: Integer - , provisionDate :: Day + , provisionDate :: Maybe Day } instance YAML Schedule where - toYAML (Schedule {daysLeft, provisionDate}) = Object [ + toYAML (Schedule {daysLeft, provisionDate = Nothing}) = Object [ + "Warning" .: "Stock lower than the minimum security amount defined. Go now !" + , "Days left" .: daysLeft + ] + toYAML (Schedule {daysLeft, provisionDate = Just day}) = Object [ "Days left" .: daysLeft - , "Provision on" .: provisionDate + , "Provision on" .: day ] schedule :: Pharmacy -> State -> Schedule -schedule _ (State {day, stock, consumptionRate}) = - Schedule {daysLeft , provisionDate = daysLeft `addDays` day} +schedule pharmacy (State {day, stock, consumptionRate}) = + Schedule {daysLeft , provisionDate = if daysLeft > 0 then Just $ daysLeft `addDays` day else Nothing} where - daysLeftByMedicine = - mergeWithKey (\_ a b -> Just $ a / b) id id stock consumptionRate + stockInDays medicineName stockLeft rate = + Just $ (stockLeft - minStock (pharmacy ! medicineName)) / rate + daysLeftByMedicine = mergeWithKey stockInDays id id stock consumptionRate daysLeft = truncate . minimum $ elems daysLeftByMedicine