From 0eaa0ecb31ef91d786fd495d343909a52ac06b32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B9=8F=E5=AE=87?= Date: Sun, 22 Dec 2024 20:24:30 +0800 Subject: [PATCH] =?UTF-8?q?ljm=20=E4=BF=AE=E6=94=B9=E5=A4=87=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yuthon_wechat/models/hr_attendance_detail.py | 127 ++++++++++++------- 1 file changed, 79 insertions(+), 48 deletions(-) diff --git a/yuthon_wechat/models/hr_attendance_detail.py b/yuthon_wechat/models/hr_attendance_detail.py index 142ad39de..ffb50e9c5 100644 --- a/yuthon_wechat/models/hr_attendance_detail.py +++ b/yuthon_wechat/models/hr_attendance_detail.py @@ -599,7 +599,7 @@ class HrAttendanceDetail(models.Model): :return: """ # 同步整理当天出勤数据 - attendance_obj = self.env['hr.attendance'].search([("userid", "=", user.id), ("current_data", "=", current_date)], limit=1) + attendance_obj = self.env['hr.attendance'].search([("userid", "=", user.oauth_uid), ("current_data", "=", current_date)], limit=1) hr_employee_obj = self.env['hr.employee'].sudo().search([("user_id", "=", user.id)], limit=1) current_datetime_start = datetime.combine(datetime.strptime(current_date, '%Y-%m-%d'), datetime.min.time()) current_datetime_end = datetime.combine(datetime.strptime(current_date, '%Y-%m-%d'), datetime.max.time()) @@ -608,15 +608,13 @@ class HrAttendanceDetail(models.Model): if not hr_employee_obj.default_planning_role_id: attendance_obj = self.env['hr.attendance'].sudo().create({ 'employee_id': hr_employee_obj.id, - "userid": user.id, + "userid": user.oauth_uid, "current_data": current_date, "attendance_type": "wx_work" }) - if attendance_obj.is_approval: - # 已经审批修复无需再次进行更新数据 - return True - self._do_not_planning_role(user, current_date, attendance_obj) + else: + return True # 暂时跳过 planning_slot_id = self.env['planning.slot'].search( [('resource_id', '=', hr_employee_obj.resource_id.id), ('start_datetime', '>=', current_datetime_start), @@ -625,28 +623,36 @@ class HrAttendanceDetail(models.Model): if planning_slot_id: attendance_obj = self.env['hr.attendance'].sudo().create({ 'employee_id': hr_employee_obj.id, - "userid": user.id, + "userid": user.oauth_uid, "current_data": current_date, "shift_id": planning_slot_id.id, "attendance_type": "wx_work" }) - if attendance_obj.is_approval: - # 已经审批修复无需再次进行更新数据 - return True - self._do_planning_role(user, current_date, attendance_obj) else: # 没有排班 return True + if not hr_employee_obj.default_planning_role_id: + if attendance_obj.is_approval: + # 已经审批修复无需再次进行更新数据 + return True + self._do_not_planning_role(user, current_date, attendance_obj) + else: + return True # 暂时跳过 + if planning_slot_id: + if attendance_obj.is_approval: + # 已经审批修复无需再次进行更新数据 + return True + self._do_planning_role(user, current_date, attendance_obj) + # 查询出当前抓取微信的当前时间的所有数据 attendance_detail_obj = self.env['hr.attendance.detail'].sudo().search( - [("userid", "=", user.id), - ("checkin_time", ">=", current_datetime_start), - ("checkin_time", "<=", current_datetime_end)]) + [("userid", "=", user.oauth_uid), + ("checkin_time", ">=", current_datetime_start - timedelta(hours=8)), + ("checkin_time", "<=", current_datetime_end - timedelta(hours=8))]) # 关联 打卡详情 attendance_detail_obj.hr_attendance_id = attendance_obj.id return True - def _do_not_planning_role(self, user, current_date, attendance_obj): """ 正常班次的 上班下班判断 @@ -667,9 +673,9 @@ class HrAttendanceDetail(models.Model): # 判断打卡情况排除未打卡情况 attendance_detail_obj = self.env['hr.attendance.detail'].sudo().search( [("exception_type", "!=", "未打卡"), - ("userid", "=", user.id), - ("checkin_time", ">=", current_datetime_start), - ("checkin_time", "<=", current_datetime_end)]) + ("userid", "=", user.oauth_uid), + ("checkin_time", ">=", current_datetime_start - timedelta(hours=8)), + ("checkin_time", "<=", current_datetime_end - timedelta(hours=8))]) # 没有打卡记录说明缺卡 if len(attendance_detail_obj) <= 0: attendance_obj.sudo().write({"is_no_card": True, "is_abnormal": True}) @@ -679,50 +685,75 @@ class HrAttendanceDetail(models.Model): checkin_time = attendance_detail_obj.checkin_time will_datetime_start = checkin_time.replace(hour=min_hour, minute=min_minute) will_datetime_end = checkin_time.replace(hour=max_hour, minute=max_minute) - - if attendance_detail_obj.checkin_type == "上班打卡": + check_in = attendance_obj.check_in if attendance_obj.check_in else None + check_out = attendance_obj.check_out if attendance_obj.check_out else None + # 如果原记录有上班打卡时间 和 下班打卡时间 + checkin_time_list = [checkin_time] + if check_in: + checkin_time_list.append(check_in) + if check_out: + checkin_time_list.append(check_out) + if len(checkin_time_list) > 1: + # 最早 和 最晚 时间 + min_time = min(checkin_time_list) + max_time = max(checkin_time_list) attendance_obj.sudo().write({ "sch_check_in_time": will_datetime_start, - "check_in": checkin_time, - "is_late_card": checkin_time > will_datetime_start, - "is_no_card": True, - }) - elif attendance_detail_obj.checkin_type == "下班打卡": - attendance_obj.sudo().write({ + "check_in": min_time, + "is_late_card": min_time > will_datetime_start, + "sch_check_out_time": will_datetime_end, - "check_out": checkin_time, - "is_early_card": will_datetime_end > checkin_time, - "is_no_card": True, + "check_out": max_time, + "is_early_card": will_datetime_end > max_time, }) + return True else: - # 如果是 打卡时间 早于 上班时间 判定为 上班打卡 缺下班卡 - if will_datetime_start > checkin_time: + if attendance_detail_obj.checkin_type == "上班打卡": attendance_obj.sudo().write({ "sch_check_in_time": will_datetime_start, "check_in": checkin_time, "is_late_card": checkin_time > will_datetime_start, "is_no_card": True, }) - # 如果是 打卡时间 晚于 下班时间 判定为 下班打卡 缺上班卡 - elif will_datetime_end < checkin_time: + elif attendance_detail_obj.checkin_type == "下班打卡": attendance_obj.sudo().write({ - "sch_check_out_time": will_datetime_end, + "sch_check_out_time": will_datetime_end, + "check_in": None, "check_out": checkin_time, "is_early_card": will_datetime_end > checkin_time, "is_no_card": True, }) - # 既不是上班也不是下班,默认为上班打卡 else: - attendance_obj.sudo().write({ - "sch_check_in_time": will_datetime_start, - "check_in": checkin_time, - "is_late_card": checkin_time > will_datetime_start, - "is_no_card": True, - }) - return True + # 如果是 打卡时间 早于 上班时间 判定为 上班打卡 缺下班卡 + if will_datetime_start > checkin_time: + attendance_obj.sudo().write({ + "sch_check_in_time": will_datetime_start, + "check_in": checkin_time, + "check_out": None, + "is_late_card": checkin_time > will_datetime_start, + "is_no_card": True, + }) + # 如果是 打卡时间 晚于 下班时间 判定为 下班打卡 缺上班卡 + elif will_datetime_end < checkin_time: + attendance_obj.sudo().write({ + "sch_check_out_time": will_datetime_end, + "check_in": None, + "check_out": checkin_time, + "is_early_card": will_datetime_end > checkin_time, + "is_no_card": True, + }) + # 既不是上班也不是下班,默认为上班打卡 + else: + attendance_obj.sudo().write({ + "sch_check_in_time": will_datetime_start, + "check_in": checkin_time, + "is_late_card": checkin_time > will_datetime_start, + "is_no_card": True, + }) + return True else: # 多次打卡时间的集合 - checkin_time_list = [obj.checkin_time for obj in attendance_detail_obj] + checkin_time_list = [obj.checkin_time for obj in attendance_detail_obj] # 最早 和 最晚 时间 min_time = min(checkin_time_list) max_time = max(checkin_time_list) @@ -758,8 +789,8 @@ class HrAttendanceDetail(models.Model): hr_employee_obj = self.env['hr.employee'].sudo().search([("user_id", "=", user.id)], limit=1) planning_id = self.env['planning.slot'].search( [('resource_id', '=', hr_employee_obj.resource_id.id), - ('start_datetime', '>=', current_datetime_start), - ('start_datetime', '<=', current_datetime_end)]) + ('start_datetime', '>=', current_datetime_start - timedelta(hours=8)), + ('start_datetime', '<=', current_datetime_end - timedelta(hours=8))]) if planning_id: planning_start_time = planning_id.start_datetime + timedelta(hours=8) @@ -768,9 +799,9 @@ class HrAttendanceDetail(models.Model): # 判断打卡情况排除未打卡情况 attendance_detail_obj = self.env['hr.attendance.detail'].sudo().search( [("exception_type", "!=", "未打卡"), - ("userid", "=", user.id), - ("checkin_time", ">=", planning_start_time), - ("checkin_time", "<=", planning_end_time)]) + ("userid", "=", user.oauth_uid), + ("checkin_time", ">=", planning_start_time - timedelta(hours=8)), + ("checkin_time", "<=", planning_end_time - timedelta(hours=8))])