103 lines
3.7 KiB
Python
103 lines
3.7 KiB
Python
import datetime
|
|
from collections import defaultdict
|
|
|
|
#from shifts import RotaBuilder, days, sites
|
|
|
|
|
|
class Worker:
|
|
def __init__(self,
|
|
Rota,
|
|
id,
|
|
name,
|
|
site,
|
|
grade,
|
|
fte=100,
|
|
nwd=None,
|
|
end_date=None,
|
|
oop=None,
|
|
pref_not_to_work=None):
|
|
self.id = id
|
|
self.name = name
|
|
self.site = site
|
|
# Grade are equivalent to roles, by keeping them integer defining model
|
|
# rules is easier.
|
|
self.grade = grade
|
|
self.fte = fte
|
|
self.nwd = nwd
|
|
self.proportion_rota_to_work = 1
|
|
|
|
self.shift_target_number = defaultdict(int)
|
|
|
|
days_to_work = Rota.rota_days_length
|
|
|
|
if end_date is None:
|
|
self.end_date = None
|
|
else:
|
|
self.end_date = datetime.datetime.strptime(end_date,
|
|
"%Y/%m/%d").date()
|
|
|
|
if self.end_date > Rota.rota_end_date:
|
|
self.end_date = Rota.rota_end_date
|
|
else:
|
|
days_to_work = (self.end_date - Rota.start_date).days
|
|
|
|
# add unavalabilities
|
|
for weeks_days in Rota.weeks_days_product[days_to_work:]:
|
|
week, day = weeks_days
|
|
Rota.unavailable_to_work.add((self.id, week, day))
|
|
|
|
if oop is not None:
|
|
start_oop, end_oop = oop
|
|
start_oop_date = datetime.datetime.strptime(start_oop,
|
|
"%Y/%m/%d").date()
|
|
end_oop_date = datetime.datetime.strptime(end_oop,
|
|
"%Y/%m/%d").date()
|
|
|
|
print(start_oop_date, end_oop_date)
|
|
if end_oop_date > Rota.rota_end_date:
|
|
end_oop_date = Rota.rota_end_date
|
|
|
|
if start_oop_date < Rota.start_date:
|
|
start_oop_date = Rota.start_date
|
|
|
|
oop_length = (end_oop_date - start_oop_date).days
|
|
days_to_work = days_to_work - oop_length
|
|
|
|
days_until_oop = (start_oop_date - Rota.start_date).days
|
|
|
|
for weeks_days in Rota.weeks_days_product[
|
|
days_until_oop:days_until_oop + oop_length]:
|
|
week, day = weeks_days
|
|
Rota.unavailable_to_work.add((self.id, week, day))
|
|
|
|
if pref_not_to_work is not None:
|
|
# loop throught dates converting to week / day combination
|
|
for date in pref_not_to_work:
|
|
days_from_start = (datetime.datetime.strptime(date, "%Y/%m/%d").date() - Rota.start_date).days
|
|
week = days_from_start // 7 + 1
|
|
day = Rota.days[(days_from_start % 7)]
|
|
# Weight the value to take into account the number of preferences
|
|
# 1 is added to the total number of requests to ensure they do not outweight
|
|
# a single (or fewer) request(s)
|
|
Rota.pref_not_to_work[(self.id, week, day)] = 1 / (len(pref_not_to_work) + 1)
|
|
|
|
|
|
self.proportion_rota_to_work = days_to_work / Rota.rota_days_length
|
|
|
|
# We had to adjust the full time equivalent for people who CCT / leave the rota early
|
|
self.fte_adj = self.fte * self.proportion_rota_to_work
|
|
|
|
def __lt__(self, other):
|
|
return (self.site, self.grade, self.fte_adj, self.name) < (
|
|
other.site,
|
|
other.grade,
|
|
other.fte_adj,
|
|
other.name,
|
|
)
|
|
|
|
def get_details(self):
|
|
return "{} {} {}".format(self.id, self.site[0], self.grade)
|
|
|
|
def get_full_details(self):
|
|
return "{}/{}/{}".format(self.site, self.grade, self.name)
|