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 schedule
) where ) where
import Data.Map (elems, mergeWithKey) import Data.Map ((!), elems, mergeWithKey)
import Data.Time (Day, addDays) import Data.Time (Day, addDays)
import Medicine (Pharmacy) import Medicine (Medicine(..), Pharmacy)
import Timeline (State(..)) import Timeline (State(..))
import YAML ((.:), Value(..), YAML(..)) import YAML ((.:), Value(..), YAML(..))
data Schedule = Schedule { data Schedule = Schedule {
daysLeft :: Integer daysLeft :: Integer
, provisionDate :: Day , provisionDate :: Maybe Day
} }
instance YAML Schedule where 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 "Days left" .: daysLeft
, "Provision on" .: provisionDate , "Provision on" .: day
] ]
schedule :: Pharmacy -> State -> Schedule schedule :: Pharmacy -> State -> Schedule
schedule _ (State {day, stock, consumptionRate}) = schedule pharmacy (State {day, stock, consumptionRate}) =
Schedule {daysLeft , provisionDate = daysLeft `addDays` day} Schedule {daysLeft , provisionDate = if daysLeft > 0 then Just $ daysLeft `addDays` day else Nothing}
where where
daysLeftByMedicine = stockInDays medicineName stockLeft rate =
mergeWithKey (\_ a b -> Just $ a / b) id id stock consumptionRate Just $ (stockLeft - minStock (pharmacy ! medicineName)) / rate
daysLeftByMedicine = mergeWithKey stockInDays id id stock consumptionRate
daysLeft = truncate . minimum $ elems daysLeftByMedicine daysLeft = truncate . minimum $ elems daysLeftByMedicine