From 1f8f5262fa1620a6ff6b9b98947047aaab9624e1 Mon Sep 17 00:00:00 2001 From: zhouzhou <123@qq.com> Date: Sun, 8 Dec 2024 19:28:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yuthon_hr_attendance/models/hr_attendance.py | 81 ++++++++++++++----- .../models/yuthon_run_planning.py | 48 ++++------- 2 files changed, 80 insertions(+), 49 deletions(-) diff --git a/yuthon_hr_attendance/models/hr_attendance.py b/yuthon_hr_attendance/models/hr_attendance.py index ec9bc051f..177f21f02 100644 --- a/yuthon_hr_attendance/models/hr_attendance.py +++ b/yuthon_hr_attendance/models/hr_attendance.py @@ -62,25 +62,54 @@ class HrAttendance(models.Model): record.is_late_card = False record.is_early_card = False if not record.employee_id.resource_id.calendar_id.attendance_ids: - continue - min_hour_from = record.employee_id.resource_id.calendar_id.attendance_ids[0].hour_from - min_hour = int(min_hour_from) - min_minute = int(min_hour_from * 10 % min_hour_from) - if record.check_in: - check_in = record.check_in + timedelta(hours=8) - will_datetime_start = check_in.replace(hour=min_hour, minute=min_minute) - record.is_late_card = check_in > will_datetime_start + department_ids = self.env['hr.department'].search([('name', 'in', ['保安部', '班组A', '班组B', '班组C'])]).ids + employee_ids = list(set(self.env['hr.attendance'].search([ + ('employee_id.department_id', 'in', department_ids) + ]).mapped('employee_id').ids)) + for employee_id in employee_ids: + planning_ids = self.env['planning.slot'].search([('resource_id', '=', employee_id.resource_id.id)]) + for planning_id in planning_ids: + start_date = planning_id.start_datetime.date() + attendance_id = self.env['hr.attendance'].search([('employee_id', '=', employee_id.id), + ('current_data', '=', start_date)]) + if attendance_id: + if attendance_id.check_in > planning_id.start_datetime: + attendance_id.write({'is_early_card': True}) + elif attendance_id.check_out < planning_id.end_datetime: + attendance_id.write({'is_late_card': True}) + department_ids = self.env['hr.department'].search([('name', 'in',['工程部', '运行班', '高压班','综合班'])]).ids + employee_ids = list(set(self.env['hr.attendance'].search([('employee_id.department_id', 'in', department_ids) + ]).mapped('employee_id').ids)) + for employee_id in employee_ids: + planning_ids = self.env['planning.slot'].search([('resource_id', '=', employee_id.resource_id.id)]) + for planning_id in planning_ids: + start_date = planning_id.start_datetime.date() + attendance_id = self.env['hr.attendance'].search([('employee_id', '=', employee_id.id), + ('current_data', '=', start_date)]) + if attendance_id: + if attendance_id.check_in > planning_id.start_datetime: + attendance_id.write({'is_early_card': True}) + elif attendance_id.check_out < planning_id.end_datetime: + attendance_id.write({'is_late_card': True}) else: - record.is_no_card = True - max_hour_to = record.employee_id.resource_id.calendar_id.attendance_ids[-1].hour_to - max_hour = int(max_hour_to) - max_minute = int(max_hour_to * 10 % max_hour_to) - if record.check_out: - check_out = record.check_out + timedelta(hours=8) - will_datetime_end = check_out.replace(hour=max_hour, minute=max_minute) - record.is_early_card = will_datetime_end > check_out - else: - record.is_no_card = True + min_hour_from = record.employee_id.resource_id.calendar_id.attendance_ids[0].hour_from + min_hour = int(min_hour_from) + min_minute = int(min_hour_from * 10 % min_hour_from) + if record.check_in: + check_in = record.check_in + timedelta(hours=8) + will_datetime_start = check_in.replace(hour=min_hour, minute=min_minute) + record.is_late_card = check_in > will_datetime_start + else: + record.is_no_card = True + max_hour_to = record.employee_id.resource_id.calendar_id.attendance_ids[-1].hour_to + max_hour = int(max_hour_to) + max_minute = int(max_hour_to * 10 % max_hour_to) + if record.check_out: + check_out = record.check_out + timedelta(hours=8) + will_datetime_end = check_out.replace(hour=max_hour, minute=max_minute) + record.is_early_card = will_datetime_end > check_out + else: + record.is_no_card = True def add_comment(self): self.ensure_one() @@ -311,3 +340,19 @@ class HrAttendance(models.Model): self.card_in_comment(resource.employee_id) elif datetime.now() >= today_slots_id.end_datetime+timedelta(hours=8): self.card_out_comment(resource.employee_id) + + @api.depends('check_in', 'check_out') + def _compute_shift_attendance_state(self): + employee_ids = list(set(self.env['hr.attendance'].search([]).mapped('employee_id').ids)) + for employee_id in employee_ids: + planning_ids = self.env['planning.slot'].search([('resource_id', '=', employee_id.resource_id.id)]) + for planning_id in planning_ids: + start_date = planning_id.start_datetime.date() + attendance_id = self.env['hr.attendance'].search([('employee_id', '=', employee_id.id), + ('current_data', '=', start_date)]) + if attendance_id: + if attendance_id.check_in > planning_id.start_datetime: + attendance_id.write({'is_early_card': True}) + elif attendance_id.check_out < planning_id.end_datetime: + attendance_id.write({'is_late_card': True}) + diff --git a/yuthon_hr_attendance/models/yuthon_run_planning.py b/yuthon_hr_attendance/models/yuthon_run_planning.py index cb0a66e98..fdfcf04b1 100644 --- a/yuthon_hr_attendance/models/yuthon_run_planning.py +++ b/yuthon_hr_attendance/models/yuthon_run_planning.py @@ -114,50 +114,40 @@ class YuthonRunPlanning(models.Model): else: last_day_of_month = (today.replace(year=today.year + 1, month=1, day=1) - timedelta(days=1)) role_a = self.env['planning.role'].search([('name', '=', '工程部')], limit=1) - start_time = int(self.env['planning.slot.template'].search( - [('role_id', '=', role_a.id), ('planning_name', '=', '白班')]).start_time) - end_time = int(self.env['planning.slot.template'].search( - [('role_id', '=', role_a.id), ('planning_name', '=', '白班')]).end_time) + start_time = int(self.env['planning.slot.template'].search([('role_id', '=', role_a.id), ('planning_name', '=', '白班')]).start_time) + end_time = int(self.env['planning.slot.template'].search([('role_id', '=', role_a.id), ('planning_name', '=', '白班')]).end_time) for employee in self.high_run_ids: current_date = first_day_of_month while current_date <= last_day_of_month: resource_id = employee.employee_id.resource_id self.env['planning.slot'].create({ - 'template_id': self.env['planning.slot.template'].search( - [('role_id', '=', role_a.id), ('planning_name', '=', '白班')]).id, + 'template_id': self.env['planning.slot.template'].search([('role_id', '=', role_a.id), ('planning_name', '=', '白班')]).id, 'resource_id': resource_id.id, - 'start_datetime': datetime(current_date.year, current_date.month, current_date.day, - start_time) + timedelta(hours=16), - 'end_datetime': datetime(current_date.year, current_date.month, current_date.day, - end_time) + timedelta(hours=16), + 'start_datetime': datetime(current_date.year, current_date.month, current_date.day,start_time) + timedelta(hours=16)- timedelta(days=1), + 'end_datetime': datetime(current_date.year, current_date.month, current_date.day,end_time) + timedelta(hours=16)- timedelta(days=1), 'role_id': role_a.id, 'allocated_hours': 8, }) current_date += timedelta(days=1) if current_date > last_day_of_month: break - start_time2 = int(self.env['planning.slot.template'].search( - [('planning_name', '=', '中值夜班'), ('role_id', '=', role_a.id)]).start_time) - end_time2 = int(self.env['planning.slot.template'].search( - [('planning_name', '=', '中值夜班'), ('role_id', '=', role_a.id)]).end_time) - + start_time2 = int(self.env['planning.slot.template'].search( [('planning_name', '=', '中值夜班'), ('role_id', '=', role_a.id)]).start_time) + end_time2 = int(self.env['planning.slot.template'].search([('planning_name', '=', '中值夜班'), ('role_id', '=', role_a.id)]).end_time) all_slot = self.env['planning.slot'].search([('department_id.name', '=', '高压班')]) current_date = first_day_of_month i = -1 while current_date <= last_day_of_month: - for employee in range(0, len(self.engineering_run_ids)): + for employee in range(0, len(self.high_run_ids)): i = i + 1 slot_id = all_slot.search( - [('resource_id', '=', self.engineering_run_ids[employee].employee_id.resource_id.id)]).sorted( + [('resource_id', '=', self.high_run_ids[employee].employee_id.resource_id.id)]).sorted( 'start_datetime') if i < 31: slot_id[i].write({ 'template_id': self.env['planning.slot.template'].search( [('role_id', '=', role_a.id), ('planning_name', '=', '中值夜班')]).id, - 'start_datetime': datetime(current_date.year, current_date.month, current_date.day, - start_time2) + timedelta(hours=16) - timedelta(days=1), - 'end_datetime': datetime(current_date.year, current_date.month, current_date.day, - end_time2) + timedelta(days=1, hours=16) - timedelta(days=1), + 'start_datetime': datetime(current_date.year, current_date.month, current_date.day,start_time2) + timedelta(hours=16) - timedelta(days=1), + 'end_datetime': datetime(current_date.year, current_date.month, current_date.day,end_time2) + timedelta(days=1, hours=16) - timedelta(days=1), 'role_id': role_a.id, 'allocated_hours': 8, }) @@ -165,10 +155,8 @@ class YuthonRunPlanning(models.Model): slot_id[i + 1].write({ 'template_id': self.env['planning.slot.template'].search( [('role_id', '=', role_a.id), ('planning_name', '=', '白班中午值班')]).id, - 'start_datetime': datetime(current_date.year, current_date.month, current_date.day, - start_time) + timedelta(days=1, hours=16) - timedelta(days=1), - 'end_datetime': datetime(current_date.year, current_date.month, current_date.day, - end_time) + timedelta(days=1, hours=16) - timedelta(days=1), + 'start_datetime': datetime(current_date.year, current_date.month, current_date.day,start_time) + timedelta(days=1, hours=16) - timedelta(days=1), + 'end_datetime': datetime(current_date.year, current_date.month, current_date.day,end_time) + timedelta(days=1, hours=16) - timedelta(days=1), 'role_id': role_a.id, 'allocated_hours': 8, }) @@ -222,19 +210,17 @@ class YuthonRunPlanning(models.Model): current_date = first_day_of_month i = -1 while current_date <= last_day_of_month: - for employee in range(0, len(self.engineering_run_ids)): + for employee in range(0, len(self.all_run_ids)): i = i + 1 slot_id = all_slot.search( - [('resource_id', '=', self.engineering_run_ids[employee].employee_id.resource_id.id)]).sorted( + [('resource_id', '=', self.all_run_ids[employee].employee_id.resource_id.id)]).sorted( 'start_datetime') if i < 31: slot_id[i].write({ 'template_id': self.env['planning.slot.template'].search( [('role_id', '=', role_a.id), ('planning_name', '=', '中值夜班')]).id, - 'start_datetime': datetime(current_date.year, current_date.month, current_date.day, - start_time2) + timedelta(hours=16) - timedelta(days=1), - 'end_datetime': datetime(current_date.year, current_date.month, current_date.day, - end_time2) + timedelta(days=1, hours=16) - timedelta(days=1), + 'start_datetime': datetime(current_date.year, current_date.month, current_date.day,start_time2) + timedelta(hours=16) - timedelta(days=1), + 'end_datetime': datetime(current_date.year, current_date.month, current_date.day,end_time2) + timedelta(days=1, hours=16) - timedelta(days=1), 'role_id': role_a.id, 'allocated_hours': 8, })