2021-05-11 11:35:47 +10:00
|
|
|
import datetime
|
2021-04-20 17:11:06 +10:00
|
|
|
import ifcopenshell.util.date
|
2021-05-11 11:35:47 +10:00
|
|
|
import ifcopenshell.util.sequence
|
2021-04-20 17:11:06 +10:00
|
|
|
|
|
|
|
|
|
2021-04-20 05:12:13 +00:00
|
|
|
class Usecase:
|
|
|
|
|
def __init__(self, file, **settings):
|
|
|
|
|
self.file = file
|
2021-04-20 17:11:06 +10:00
|
|
|
self.settings = {"task_time": None, "attributes": {}}
|
2021-04-20 05:12:13 +00:00
|
|
|
for key, value in settings.items():
|
|
|
|
|
self.settings[key] = value
|
|
|
|
|
|
|
|
|
|
def execute(self):
|
2021-05-11 11:35:47 +10:00
|
|
|
# If the user specifies both an end date and a duration, the duration takes priority
|
2021-06-04 15:21:48 +10:00
|
|
|
if (
|
|
|
|
|
"ScheduleDuration" in self.settings["attributes"].keys()
|
|
|
|
|
and "ScheduleFinish" in self.settings["attributes"].keys()
|
|
|
|
|
):
|
2021-05-15 02:04:43 +00:00
|
|
|
del self.settings["attributes"]["ScheduleFinish"]
|
2021-06-04 15:21:48 +10:00
|
|
|
if (
|
|
|
|
|
"ActualDuration" in self.settings["attributes"].keys()
|
|
|
|
|
and "ActualFinish" in self.settings["attributes"].keys()
|
|
|
|
|
):
|
2021-05-11 11:35:47 +10:00
|
|
|
del self.settings["attributes"]["ActualFinish"]
|
|
|
|
|
|
2021-06-04 15:21:48 +10:00
|
|
|
duration_type = self.settings["attributes"].get("DurationType", self.settings["task_time"].DurationType)
|
|
|
|
|
if "ScheduleFinish" in self.settings["attributes"]:
|
|
|
|
|
self.settings["attributes"]["ScheduleFinish"] = ifcopenshell.util.sequence.get_soonest_working_day(
|
|
|
|
|
self.settings["attributes"]["ScheduleFinish"], duration_type, self.get_calendar()
|
|
|
|
|
)
|
|
|
|
|
if "ScheduleStart" in self.settings["attributes"]:
|
|
|
|
|
self.settings["attributes"]["ScheduleStart"] = ifcopenshell.util.sequence.get_soonest_working_day(
|
|
|
|
|
self.settings["attributes"]["ScheduleStart"], duration_type, self.get_calendar()
|
|
|
|
|
)
|
|
|
|
|
|
2021-04-20 05:12:13 +00:00
|
|
|
for name, value in self.settings["attributes"].items():
|
2021-05-06 17:14:28 +10:00
|
|
|
if value:
|
|
|
|
|
if "Start" in name or "Finish" in name or name == "StatusTime":
|
2021-04-20 17:11:06 +10:00
|
|
|
value = ifcopenshell.util.date.datetime2ifc(value, "IfcDateTime")
|
2021-05-11 11:35:47 +10:00
|
|
|
elif name == "ScheduleDuration" or name == "ActualDuration" or name == "RemainingTime":
|
2021-05-04 00:27:53 +00:00
|
|
|
value = ifcopenshell.util.date.datetime2ifc(value, "IfcDuration")
|
2021-04-20 17:11:06 +10:00
|
|
|
setattr(self.settings["task_time"], name, value)
|
2021-05-11 11:35:47 +10:00
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
"ScheduleDuration" in self.settings["attributes"].keys()
|
|
|
|
|
and self.settings["task_time"].ScheduleDuration
|
|
|
|
|
and self.settings["task_time"].ScheduleStart
|
|
|
|
|
):
|
2021-05-31 12:42:02 +10:00
|
|
|
self.calculate_finish()
|
|
|
|
|
elif "ScheduleStart" in self.settings["attributes"].keys() and self.settings["task_time"].ScheduleDuration:
|
|
|
|
|
self.calculate_finish()
|
|
|
|
|
elif "ScheduleFinish" in self.settings["attributes"].keys() and self.settings["task_time"].ScheduleStart:
|
|
|
|
|
self.calculate_duration()
|
|
|
|
|
|
|
|
|
|
def calculate_finish(self):
|
2021-06-04 15:21:48 +10:00
|
|
|
finish_date = ifcopenshell.util.sequence.get_finish_date(
|
|
|
|
|
ifcopenshell.util.date.ifc2datetime(self.settings["task_time"].ScheduleStart),
|
|
|
|
|
ifcopenshell.util.date.ifc2datetime(self.settings["task_time"].ScheduleDuration),
|
|
|
|
|
self.settings["task_time"].DurationType,
|
|
|
|
|
self.get_calendar(),
|
|
|
|
|
)
|
|
|
|
|
self.settings["task_time"].ScheduleFinish = ifcopenshell.util.date.datetime2ifc(finish_date, "IfcDateTime")
|
2021-05-31 12:42:02 +10:00
|
|
|
|
|
|
|
|
def calculate_duration(self):
|
|
|
|
|
start = ifcopenshell.util.date.ifc2datetime(self.settings["task_time"].ScheduleStart)
|
|
|
|
|
finish = ifcopenshell.util.date.ifc2datetime(self.settings["task_time"].ScheduleFinish)
|
|
|
|
|
current_date = datetime.date(start.year, start.month, start.day)
|
|
|
|
|
finish_date = datetime.date(finish.year, finish.month, finish.day)
|
|
|
|
|
calendar = self.get_calendar()
|
|
|
|
|
duration = datetime.timedelta()
|
|
|
|
|
while current_date < finish_date:
|
|
|
|
|
if self.settings["task_time"].DurationType == "ELAPSEDTIME" or not calendar:
|
|
|
|
|
duration += datetime.timedelta(days=1)
|
|
|
|
|
elif ifcopenshell.util.sequence.is_working_day(current_date, calendar):
|
|
|
|
|
duration += datetime.timedelta(days=1)
|
|
|
|
|
current_date += datetime.timedelta(days=1)
|
|
|
|
|
self.settings["task_time"].ScheduleDuration = ifcopenshell.util.date.datetime2ifc(duration, "IfcDuration")
|
|
|
|
|
|
|
|
|
|
def get_calendar(self):
|
|
|
|
|
task = [e for e in self.file.get_inverse(self.settings["task_time"]) if e.is_a("IfcTask")]
|
|
|
|
|
if not task:
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
task = task[0]
|
|
|
|
|
return ifcopenshell.util.sequence.derive_calendar(task)
|