From 24232dcb3d40b2a5dd2a8cd7874248f60f3a83c6 Mon Sep 17 00:00:00 2001 From: zakhy Date: Tue, 13 Aug 2024 13:00:01 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BE=D1=81=D1=82=D1=8B=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D0=B0=D1=8F=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=B4=D0=BB=D1=8F=20=D0=B2=D1=80=D0=B5=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20?= =?UTF-8?q?=D1=84=D0=B0=D0=B1=D1=80=D0=B8=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/apps/main/services/worktime/service.py | 35 ++++++++++++++++------ 1 file changed, 26 insertions(+), 9 deletions(-) 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,