From 76fe4aa9df27daf4c9187c6587e46c7ed4f52dec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B9=8F=E5=AE=87?= <9664676+pengyuthon@user.noreply.gitee.com> Date: Wed, 22 Apr 2026 17:46:59 +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 --- .../models/overtime_approval.py | 61 +++++++++++-------- .../views/overtime_approval.xml | 2 +- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/yuthon_hr_attendance/models/overtime_approval.py b/yuthon_hr_attendance/models/overtime_approval.py index 1aa76aae6..2fb4a4be3 100644 --- a/yuthon_hr_attendance/models/overtime_approval.py +++ b/yuthon_hr_attendance/models/overtime_approval.py @@ -66,25 +66,31 @@ class OvertimeApprovals(models.Model): @api.constrains('start_time') def _check_start_time(self): for rec in self: - workflow_records = self.env['soong.workflow.record'].search([ - ('workflow_id.name', '=', '加班'), - ('create_uid', '=', self.users_id.id), - ('reclaim_state', '=', False), - ]) - if not workflow_records: + if not rec.employee_ids or not rec.start_time: continue - for wf_rec in workflow_records: - if wf_rec.res_model == rec._name and wf_rec.res_id == rec.id: + current_date = (rec.start_time + timedelta(hours=8)).date() + for employee in rec.employee_ids: + if not employee.user_id: continue - - target_model = self.env[wf_rec.res_model] - target_record = target_model.browse(wf_rec.res_id) - - if not target_record.exists() or not target_record.start_time: - continue - if (target_record.start_time + timedelta(hours=8)).date() == ( - rec.start_time + timedelta(hours=8)).date(): - raise UserError('当天已有加班审批,请重新选择开始时间') + workflow_records = self.env['soong.workflow.record'].search([ + ('workflow_id.name', '=', '加班'), + ('create_uid', '=', employee.user_id.id), + ('reclaim_state', '=', False), + ]) + for wf_rec in workflow_records: + # 跳过自己这条记录 + if wf_rec.res_model == rec._name and wf_rec.res_id == rec.id: + continue + target_model = self.env[wf_rec.res_model] + target_record = target_model.browse(wf_rec.res_id) + if not target_record.exists() or not target_record.start_time: + continue + exist_date = (target_record.start_time + timedelta(hours=8)).date() + if exist_date == current_date: + raise UserError('员工【%s】在【%s】已有加班记录,不可重复提交!' % ( + employee.name, + current_date + )) for employee in rec.employee_ids: holiday_status_id = self.env['hr.leave.type'].search([('name', '=', '补休')]) start_year = rec.start_time.date().year @@ -175,13 +181,20 @@ class OvertimeApprovals(models.Model): corrected_end_time = max_allowed_end_time - timedelta(hours=8) rec.end_time = corrected_end_time - @api.depends('users_id', 'duration', 'start_time') + @api.depends('employee_ids', 'duration', 'start_time') def _compute_overtime_hours(self): for rec in self: rec.overtime_hours = 0.0 - if not rec.users_id or not rec.start_time: + if not rec.employee_ids or len(rec.employee_ids) != 1: continue - now = rec.start_time # 获取当前时间(datetime类型) + employee = rec.employee_ids[0] + if not employee.user_id: + continue + + if not rec.start_time: + continue + + now = rec.start_time first_datetime = datetime(now.year, now.month, 1, 0, 0, 0) if now.month == 12: next_month_datetime = datetime(now.year + 1, 1, 1, 0, 0, 0) @@ -190,20 +203,20 @@ class OvertimeApprovals(models.Model): last_datetime = next_month_datetime - timedelta(seconds=1) workflow_records = self.env['soong.workflow.record'].search([ ('workflow_id.name', '=', '加班'), - ('create_uid', '=', rec.users_id.id), + ('create_uid', '=', employee.user_id.id), ('approve_state', 'in', ['pending', 'end']), ('reclaim_state', '!=', True), ]) - overtime_name_list = [] total_overtime = 0.0 for workflow in workflow_records: related_model = self.env[workflow.res_model].browse(workflow.res_id) + if not related_model.exists(): + continue start_time = related_model.start_time if first_datetime <= start_time <= last_datetime: if related_model.duration: total_overtime += related_model.duration - overtime_name_list.append(related_model.name) - rec.overtime_hours = total_overtime + rec.overtime_hours = total_overtime @api.constrains('users_id') def _check_overtime_hours(self): diff --git a/yuthon_hr_attendance/views/overtime_approval.xml b/yuthon_hr_attendance/views/overtime_approval.xml index a6cdc07b1..1c2f06a43 100644 --- a/yuthon_hr_attendance/views/overtime_approval.xml +++ b/yuthon_hr_attendance/views/overtime_approval.xml @@ -113,7 +113,7 @@ - +