diff --git a/app/apps/main/services/worktime/service.py b/app/apps/main/services/worktime/service.py index 56d2cf3..e407085 100644 --- a/app/apps/main/services/worktime/service.py +++ b/app/apps/main/services/worktime/service.py @@ -11,12 +11,8 @@ class FabricWorkTimeService(DashboardBaseService): @classmethod def calculate_work_time(cls, qs: list, return_duration: bool = False) -> dict: status_durations = {"WO": timedelta(0), "WA": timedelta(0), "NW": timedelta(0)} - time_now = cls.get_current_time() if not qs: - return {"WO": 0, "WA": 0, "NW": 0} if not return_duration else {"WO": "P0DT00H00M00S", - "WA": "P0DT00H00M00S", - "NW": time_now - cls.get_shift_start( - time_now)} + return previous_time = qs[0]["time"] previous_status = qs[0]["status"] @@ -34,7 +30,7 @@ class FabricWorkTimeService(DashboardBaseService): now_date = cls.get_current_time() duration = now_date - previous_time if duration.seconds // 60 > 5: - status_durations['NW'] += duration + status_durations["NW"] += duration else: status_durations[previous_status] += duration if return_duration: @@ -59,13 +55,34 @@ class FabricWorkTimeService(DashboardBaseService): start_week = now_date - relativedelta(hours=168) start_shift = self.get_shift_start(now_date) monthly_qs = self.get_qs_by_range(start_month, now_date) - monthly = self.calculate_work_time(list(monthly_qs), return_durations) + if not monthly_qs: + monthly = {"WO": 0, "WA": 0, "NW": 0} if not return_durations else { + "WO": "P0DT00H00M00S", + "WA": "P0DT00H00M00S", + "NW": now_date - start_month, + } + else: + monthly = self.calculate_work_time(list(monthly_qs), return_durations) weekly_qs = self.get_qs_by_range(start_week, now_date) - weekly = self.calculate_work_time(list(weekly_qs), return_durations) + if not weekly_qs: + weekly = {"WO": 0, "WA": 0, "NW": 0} if not return_durations else { + "WO": "P0DT00H00M00S", + "WA": "P0DT00H00M00S", + "NW": now_date - start_week, + } + else: + weekly = self.calculate_work_time(list(monthly_qs), return_durations) shift_qs = self.get_qs_by_range(start_shift, now_date) - daily = self.calculate_work_time(list(shift_qs), return_durations) + if not shift_qs: + daily = {"WO": 0, "WA": 0, "NW": 0} if not return_durations else { + "WO": "P0DT00H00M00S", + "WA": "P0DT00H00M00S", + "NW": now_date - self.get_shift_start(now_date), + } + else: + daily = self.calculate_work_time(list(shift_qs), return_durations) return { "monthly": monthly, "weekly": weekly,