From 172dab89ba45b1c021edf33da33ccd23eb46403c Mon Sep 17 00:00:00 2001 From: Tissevert Date: Wed, 10 Apr 2019 19:28:53 +0200 Subject: [PATCH] Issue warning when stocks are lower than required --- src/Schedule.hs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) 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