Issue warning when stocks are lower than required

This commit is contained in:
Tissevert 2019-04-10 19:28:53 +02:00
parent d38daf6a7b
commit 172dab89ba

View file

@ -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