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 1/6] =?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))]) From 0aab917cedbc9c866d4f8e93f2bd48b6dec8c053 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:32:27 +0800 Subject: [PATCH 2/6] =?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 | 3 --- 1 file changed, 3 deletions(-) diff --git a/yuthon_wechat/models/hr_attendance_detail.py b/yuthon_wechat/models/hr_attendance_detail.py index ffb50e9c5..f8341635e 100644 --- a/yuthon_wechat/models/hr_attendance_detail.py +++ b/yuthon_wechat/models/hr_attendance_detail.py @@ -718,7 +718,6 @@ class HrAttendanceDetail(models.Model): elif attendance_detail_obj.checkin_type == "下班打卡": 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, @@ -729,7 +728,6 @@ class HrAttendanceDetail(models.Model): 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, }) @@ -737,7 +735,6 @@ class HrAttendanceDetail(models.Model): 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, From 3af7ad4815bc1cded60ba9029cc18cea48672378 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:36:25 +0800 Subject: [PATCH 3/6] =?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 | 46 +++----------------- 1 file changed, 7 insertions(+), 39 deletions(-) diff --git a/yuthon_wechat/models/hr_attendance_detail.py b/yuthon_wechat/models/hr_attendance_detail.py index f8341635e..d902b6549 100644 --- a/yuthon_wechat/models/hr_attendance_detail.py +++ b/yuthon_wechat/models/hr_attendance_detail.py @@ -708,45 +708,13 @@ class HrAttendanceDetail(models.Model): }) return True else: - 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 attendance_detail_obj.checkin_type == "下班打卡": - attendance_obj.sudo().write({ - "sch_check_out_time": will_datetime_end, - "check_out": checkin_time, - "is_early_card": will_datetime_end > checkin_time, - "is_no_card": True, - }) - else: - # 如果是 打卡时间 早于 上班时间 判定为 上班打卡 缺下班卡 - if will_datetime_start > checkin_time: - 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: - attendance_obj.sudo().write({ - "sch_check_out_time": will_datetime_end, - "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, - }) + # 既不是上班也不是下班,默认为上班打卡 + 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: # 多次打卡时间的集合 From 807276d49f67111aeba4c1070d341f5b299fa830 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:54:18 +0800 Subject: [PATCH 4/6] =?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 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/yuthon_wechat/models/hr_attendance_detail.py b/yuthon_wechat/models/hr_attendance_detail.py index d902b6549..bfb9b4694 100644 --- a/yuthon_wechat/models/hr_attendance_detail.py +++ b/yuthon_wechat/models/hr_attendance_detail.py @@ -683,8 +683,8 @@ class HrAttendanceDetail(models.Model): # 打了一次卡记录说明缺卡 elif len(attendance_detail_obj) == 1: 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) + will_datetime_start = checkin_time.replace(hour=min_hour, minute=min_minute) - timedelta(hours=8) + will_datetime_end = checkin_time.replace(hour=max_hour, minute=max_minute) - timedelta(hours=8) 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 # 如果原记录有上班打卡时间 和 下班打卡时间 @@ -724,8 +724,8 @@ class HrAttendanceDetail(models.Model): max_time = max(checkin_time_list) # 赋值企业微信打卡数据 - will_datetime_start = min_time.replace(hour=min_hour, minute=min_minute) - will_datetime_end = max_time.replace(hour=max_hour, minute=max_minute) + will_datetime_start = min_time.replace(hour=min_hour, minute=min_minute) - timedelta(hours=8) + will_datetime_end = max_time.replace(hour=max_hour, minute=max_minute) - timedelta(hours=8) # 查询最早时间上班时间 查询最晚下班时间 attendance_obj.sudo().write({ "sch_check_in_time": will_datetime_start, From 5f9a96dbf45d33db125bc880ac6d5d5fb925887e 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:57:42 +0800 Subject: [PATCH 5/6] =?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 | 39 ++++---------------- 1 file changed, 8 insertions(+), 31 deletions(-) diff --git a/yuthon_wechat/models/hr_attendance_detail.py b/yuthon_wechat/models/hr_attendance_detail.py index bfb9b4694..72c2e06fe 100644 --- a/yuthon_wechat/models/hr_attendance_detail.py +++ b/yuthon_wechat/models/hr_attendance_detail.py @@ -685,37 +685,14 @@ class HrAttendanceDetail(models.Model): checkin_time = attendance_detail_obj.checkin_time will_datetime_start = checkin_time.replace(hour=min_hour, minute=min_minute) - timedelta(hours=8) will_datetime_end = checkin_time.replace(hour=max_hour, minute=max_minute) - timedelta(hours=8) - 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": min_time, - "is_late_card": min_time > will_datetime_start, - - "sch_check_out_time": will_datetime_end, - "check_out": max_time, - "is_early_card": will_datetime_end > max_time, - }) - return 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 + # 既不是上班也不是下班,默认为上班打卡 + 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] From f32088cbd9a57d694ed898a54334e0a7e9a050cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B9=8F=E5=AE=87?= Date: Mon, 23 Dec 2024 08:53:50 +0800 Subject: [PATCH 6/6] =?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_hr_attendance/models/hr_attendance.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/yuthon_hr_attendance/models/hr_attendance.py b/yuthon_hr_attendance/models/hr_attendance.py index 804db2de6..33d3dbdaf 100644 --- a/yuthon_hr_attendance/models/hr_attendance.py +++ b/yuthon_hr_attendance/models/hr_attendance.py @@ -15,10 +15,10 @@ class HrAttendance(models.Model): _rec_name = 'current_data' is_clock_card = fields.Boolean(string="外勤") - is_no_card = fields.Boolean(string="缺卡", groups_operator='count_false') - is_early_card = fields.Boolean(string="早退", groups_operator='count_false') - is_late_card = fields.Boolean(string="迟到", groups_operator='count_false') - is_abnormal = fields.Boolean(string="异常", groups_operator='count_false') + is_no_card = fields.Boolean(string="缺卡", group_operator='count_false') + is_early_card = fields.Boolean(string="早退", group_operator='count_false') + is_late_card = fields.Boolean(string="迟到", group_operator='count_false') + is_abnormal = fields.Boolean(string="异常", group_operator='count_false') work_time_type = fields.Selection([('close_day', '休息日'), ('work_day', '工作日')], string="工作时间类型") company_id = fields.Many2one('res.company', string='公司', index=True, default=lambda self: self.env.company) on_card_location = fields.Char(string="上班打卡地点")