diff --git a/soong_workflow/models/soong_workflow_record.py b/soong_workflow/models/soong_workflow_record.py index bce7f00a4..26c6b8f38 100644 --- a/soong_workflow/models/soong_workflow_record.py +++ b/soong_workflow/models/soong_workflow_record.py @@ -657,22 +657,78 @@ class SoongWorkflowRecord(models.Model): @api.model def get_view(self, view_id=None, view_type='form', **options): - current_user_id = self.env.user.id - current_employee = self.env['hr.employee'].search([('user_id', '=', current_user_id)], limit=1) - view = super().get_view(view_id, view_type, **options) - if not current_employee: - return view - if view_type == 'list': - all_records = self.search([ - '|', - ('employee_ids', 'in', [current_employee.id]), - ('employee2_ids', 'in', [current_employee.id]), - ('reclaim_state', '=', False) # 补回丢失的过滤条件 - ]) # 限制最大记录数,避免性能问题 - if all_records: - all_records._compute_is_done() - all_records._compute_is_to_do() - return view + if view_type != 'list': + return super(SoongWorkflowRecord, self).get_view(view_id, view_type, **options) + current_user = self.env.user + all_records = self.search([ + ('users_ids', 'in', current_user.id), + ('reclaim_state', '=', False) + ]) + if not all_records: + return super(SoongWorkflowRecord, self).get_view(view_id, view_type, **options) + model_res_ids = {} + for record in all_records: + if not record.res_model or not record.res_id: + continue + if record.res_model not in model_res_ids: + model_res_ids[record.res_model] = [] + model_res_ids[record.res_model].append(record.res_id) + + model_records = {} + for model, res_ids in model_res_ids.items(): + model_records[model] = self.env[model].browse(res_ids).sudo().prefetch(['log_ids', 'log_ids2']) + + record_state_map = {} + for record in all_records: + if not record.res_model or not record.res_id: + continue + record_id = model_records.get(record.res_model, self.env[model]).browse(record.res_id) + if not record_id or not hasattr(record_id, 'log_ids'): + continue + + new_state = None + # 处理log_ids(批量过滤,无数据库查询) + current_user_logs = record_id.log_ids.filtered(lambda l: l.done_by.id == current_user.id) + if current_user_logs: + approval_states = current_user_logs.mapped('approval_state') + # 优先级:归档日志 > 普通日志 + archive_log = current_user_logs.filtered(lambda l: l.name == '归档') + if archive_log: + if archive_log[0].approval_state in ['0', '1']: + new_state = 'to_active' + elif archive_log[0].approval_state == '2': + new_state = 'active' + else: + if any(state in ['0', '1'] for state in approval_states): + new_state = 'to_do' + elif all(state == '2' for state in approval_states): + new_state = 'done' + + # 处理log_ids2(批量过滤,用ID匹配而非name) + if not new_state and hasattr(record_id, 'log_ids2'): + user_logs2 = record_id.log_ids2.filtered(lambda l: l.done_by.id == current_user.id) + if user_logs2: + log2_states = user_logs2.mapped('approval_state') + if '2' in log2_states: + new_state = 'done' + elif any(s in ['0', '1'] for s in log2_states): + new_state = 'to_do' + if new_state: + record_state_map[record.id] = new_state + + if record_state_map: + state_groups = {} + for rec_id, state in record_state_map.items(): + if state not in state_groups: + state_groups[state] = [] + state_groups[state].append(rec_id) + for state, rec_ids in state_groups.items(): + self.browse(rec_ids).write({'soong_state': state}) + + if all_records: + all_records._compute_is_done() + all_records._compute_is_to_do() + return super(SoongWorkflowRecord, self).get_view(view_id, view_type, **options) def to_do_process_reminders(self): workflow_records = self.env['soong.workflow.record'].search([ diff --git a/yuthon_hr_attendance/models/hr_attendance.py b/yuthon_hr_attendance/models/hr_attendance.py index 28455fa6c..3940de0b7 100644 --- a/yuthon_hr_attendance/models/hr_attendance.py +++ b/yuthon_hr_attendance/models/hr_attendance.py @@ -278,7 +278,6 @@ class HrAttendance(models.Model): def attendance_abnormal_remind(self): current_utc_time = fields.Datetime.now() target_time = current_utc_time + timedelta(hours=8) - target_hour = target_time.hour target_minute = target_time.minute @@ -293,6 +292,9 @@ class HrAttendance(models.Model): return today = date.today() + is_workday = self._process_shift_days(today) + if not is_workday: + return current_year = today.year current_month = today.month if current_month == 1: @@ -364,7 +366,7 @@ class HrAttendance(models.Model): url = f'https://phone.thtzjt.com/phone/AttendanceSummary/{last_month.year}/{last_month.month}/{emp.user_id.id}' self.sudo().env["wecom.apps"].sync_send_message_textcard(category='att', user_id=emp.wecom_userid, - title=f'【{title}】', + title=f'{title}', description='考勤异常请尽快处理', exception_alerts = True, url=url, btntxt="查看详情") diff --git a/yuthon_hr_attendance/models/yuthon_vacation_reports.py b/yuthon_hr_attendance/models/yuthon_vacation_reports.py index 5012908fc..acfdf4d8d 100644 --- a/yuthon_hr_attendance/models/yuthon_vacation_reports.py +++ b/yuthon_hr_attendance/models/yuthon_vacation_reports.py @@ -98,6 +98,7 @@ class YuthonVacationReports(models.Model): should_attendance = fields.Float(string="应到", store=True, group_operator=None, compute='compute_attendance_fields') actual_attendance = fields.Float(string="实到", compute='compute_attendance_fields', store=True,group_operator=None) expatriation = fields.Float(string='外派', compute='compute_attendance_fields', store=True, group_operator=None) + go_out = fields.Float(string='外出', compute='compute_attendance_fields', store=True, group_operator=None) is_late_card = fields.Integer(string="迟到", compute='compute_attendance_fields', store=True, group_operator=None) is_early_card = fields.Integer(string="早退", compute='compute_attendance_fields', store=True, group_operator=None) @@ -822,191 +823,154 @@ class YuthonVacationReports(models.Model): @api.depends('employee_id', 'vacation_date') def compute_attendance_fields(self): for rec in self: - is_late = 0 - is_early = 0 - is_no = 0 - is_work = 0 - is_abnormal = 0 - probation_days = 0 # 试用期天数 - regular_days = 0 # 转正天数 - expatriation_days = 0 # 初始化外派天数为0 - rec.test_days = 0 - rec.right_days = 0 - actual_attendance = 0 # 实到天数 = 应到天数 - 请假总天数 - 异常天数 - leave_total_days = 0 # 累计请假天数 + # 初始化基础变量 + is_late = is_early = is_no = is_work = is_abnormal = 0 + probation_days = regular_days = expatriation_days = 0 + actual_attendance = leave_total_days = 0 is_retired = rec.employee_id.user_state in ('leave', 'withdraw') - # 初始化外派相关变量 - is_outside = False # 是否有外派单 - exp_has_return = False # 是否有返回单 - exp_start_date = None # 外派开始日期 - exp_end_return = None # 返回单结束日期 - exp_end_date = None # 外派实际结束日期 + # 核心:外出天数统计(按workflow_id去重) + go_out_days = 0.0 # 外出总天数 + processed_workflow_ids = set() # 已处理的workflow_id(去重) + + # 外派相关变量 + is_outside = exp_has_return = False + exp_start_date = exp_end_return = exp_end_date = None if rec.employee_id and rec.vacation_date and not is_retired: - year = rec.vacation_date.year - month = rec.vacation_date.month + # 考勤月份时间范围 + year, month = rec.vacation_date.year, rec.vacation_date.month month_start = date(year, month, 1) month_end = date(year, month, calendar.monthrange(year, month)[1]) entry_date = rec.employee_id.entry_date days_in_month = calendar.monthrange(year, month)[1] - # ===================== 1. 查询外派/返回单 ===================== + # 1. 查询外派/返回单 employee = rec.employee_id - - # 1.1 查询该员工当月有效的外派驻点审批单 exp_approval = self.env['expatriation.approval'].sudo().search([ - ('expatriates_ids', 'in', [employee.id]), # 外派人员包含当前员工 - ('start_date', '<=', month_end), # 外派开始日期 <= 当月末 - ('work_end', '=', True) + ('expatriates_ids', 'in', [employee.id]), + ('start_date', '<=', month_end), ('work_end', '=', True) ], limit=1) - if exp_approval: is_outside = True - exp_start_date = exp_approval.start_date # 取外派单的开始日期 - - # 1.2 查询该员工当月有效的外派返回确认单 + exp_start_date = exp_approval.start_date + # 查询返回单 back_approval = self.env['back.approval'].sudo().search([ - ('expatriates_id', '=', employee.id), # 匹配当前员工 - ('end_date', '>=', month_start), # 返回结束日期 >= 当月初 - ('end_date', '<=', month_end), # 返回结束日期 <= 当月末 - ('work_end', '=', True) + ('expatriates_id', '=', employee.id), + ('end_date', '>=', month_start), ('end_date', '<=', month_end), ('work_end', '=', True) ], limit=1) - if back_approval: exp_has_return = True - exp_end_return = back_approval.end_date # 取返回单的结束日期 - exp_end_date = min(date.today(), exp_end_return) # 有返回单:外派结束=返回结束日 + exp_end_return = back_approval.end_date + exp_end_date = min(date.today(), exp_end_return) else: - # 无返回单:外派结束=当前日期(且不超过当月末) exp_end_date = min(date.today(), month_end) - # ===================== 2. 计算外派天数 ===================== + # 2. 计算外派天数 if is_outside and exp_start_date and exp_end_date: - # 外派天数 = 外派开始日 到 外派结束日 的工作日数 - # 确保外派开始日不早于当月初,避免跨月统计 actual_exp_start = max(exp_start_date, month_start) expatriation_days = rec._create_leave_record(employee, actual_exp_start, exp_end_date) - # 确定考勤统计的起始日 - if not entry_date or month_start > entry_date: - start_day = 1 - else: - start_day = min(entry_date.day, days_in_month) - - # 根据外派返回单调整考勤统计起始日 + # 3. 确定考勤统计起始日(适配外派/入职) + start_day = 1 if (not entry_date or month_start > entry_date) else min(entry_date.day, days_in_month) if is_outside and exp_has_return and exp_end_return: - # 有返回单:从返回结束次日开始统计 - if exp_end_return.month == month: # 确保返回日期在当月 - actual_start_day = exp_end_return.day + 1 - else: - actual_start_day = start_day - actual_start_day = max(actual_start_day, start_day) # 不早于入职/月初 + actual_start_day = exp_end_return.day + 1 if exp_end_return.month == month else start_day + actual_start_day = max(actual_start_day, start_day) elif is_outside and not exp_has_return: - # 无返回单:不统计考勤(实到应到均为0) - actual_start_day = days_in_month + 1 # 让range为空 + actual_start_day = days_in_month + 1 # 无返回单不统计 else: - # 非外派:使用原有起始日 actual_start_day = start_day - actual_days_range = range(actual_start_day, days_in_month + 1) - - for day in actual_days_range: + # 4. 遍历当月考勤日统计 + for day in range(actual_start_day, days_in_month + 1): current_date = date(year, month, day) + # 跳过外派日期 + if is_outside and exp_start_date and exp_end_date and ( + exp_start_date <= current_date <= exp_end_date): + continue - # 跳过外派日期(外派开始到返回结束/当前日期) - if is_outside and exp_start_date and exp_end_date: - if exp_start_date <= current_date <= exp_end_date: - continue - + # 查询当日考勤记录 attendances = self.env["hr.attendance"].sudo().search([ - ('employee_id', '=', rec.employee_id.id), - ("current_data", '=', current_date) + ('employee_id', '=', rec.employee_id.id), ("current_data", '=', current_date) ]) - if attendances: - # 1. 过滤有效考勤:仅排除休息/异常,保留请假记录 - valid_attendances = [ - att for att in attendances - if (att.check_in or att.check_out) - and att.shift_name != '休息' - and not att.is_abnormal - ] - leave_hours = 0 + # 过滤有效考勤记录 + valid_attendances = [att for att in attendances if ( + att.check_in or att.check_out) and att.shift_name != '休息' and not att.is_abnormal] leave_days = 0 - processed_leave_codes = set() # 记录已处理的请假单code,避免重复扣减 + processed_leave_codes = set() + # 遍历考勤记录统计 for att in attendances: + # ========== 核心:外出天数统计(按workflow_id去重) ========== + if (att.workflow_id and att.workflow_id.workflow_id + and att.workflow_id.workflow_id.name == '外出' + and att.workflow_id.id not in processed_workflow_ids): + # 通过model+res_id获取外出记录 + leave_model = self.env[att.workflow_id.res_model].sudo() + leave_record = leave_model.browse(att.workflow_id.res_id) + if leave_record: + processed_workflow_ids.add(att.workflow_id.id) + # 区分按天/按小时计算 + if leave_record.is_day: + go_out_days += float(leave_record.days or 0) + elif leave_record.is_hour: + go_out_days += float(leave_record.hours or 0) / 8 + + # 原有请假天数统计 if att.workflow_id and att.workflow_id.workflow_id.name == '请假': leave_record = self.env[att.workflow_id.res_model].sudo().browse(att.workflow_id.res_id) if leave_record.code and leave_record.code in processed_leave_codes: continue - if leave_record.days: leave_days += leave_record.days - if leave_record.code: - processed_leave_codes.add(leave_record.code) + processed_leave_codes.add(leave_record.code) + # 计算当日实到天数 total_leave_days = leave_days leave_total_days += leave_days if valid_attendances: - daily_actual = max(1 - total_leave_days, 0) - actual_attendance += daily_actual - - rec.actual_attendance = max(actual_attendance, 0) + actual_attendance += max(1 - total_leave_days, 0) + # 统计考勤异常次数 for att in attendances: - if att.is_late_card: - is_late += 1 - if att.is_early_card: - is_early += 1 - if att.is_no_card: - is_no += 1 - if att.absenteeism_card: - is_work += 1 - if att.is_abnormal: - is_abnormal += 1 + if att.is_late_card: is_late += 1 + if att.is_early_card: is_early += 1 + if att.is_no_card: is_no += 1 + if att.absenteeism_card: is_work += 1 + if att.is_abnormal: is_abnormal += 1 - # 重新计算应到天数:考虑外派返回单 + # 5. 计算应到天数(适配外派) contract = employee.contract_id if is_outside: if exp_has_return and exp_end_return: - # 有返回单:应到天数 = 返回结束次日 到 月末的工作日 - duty_start = exp_end_return + timedelta(days=1) - duty_end = month_end - work_days = rec._create_leave_record(rec.employee_id, duty_start, duty_end) + work_days = rec._create_leave_record(employee, exp_end_return + timedelta(days=1), month_end) else: - # 无返回单:应到天数为0 work_days = 0 else: - # 非外派:使用原有逻辑计算应到天数 - if contract and contract.date_start: - start_date = month_start if (is_outside and not contract) else max(contract.date_start, - month_start) - else: - start_date = month_start - end_date = month_end if (is_outside and not contract) else ( - min(contract.date_end, month_end) if contract and contract.date_end else month_end) - work_days = rec._create_leave_record(rec.employee_id, start_date, end_date) + start_date = max(contract.date_start, month_start) if ( + contract and contract.date_start) else month_start + end_date = min(contract.date_end, month_end) if (contract and contract.date_end) else month_end + work_days = rec._create_leave_record(employee, start_date, end_date) - # 试用期天数计算(原有逻辑) + # 6. 计算试用期/转正天数 probation_contract = self.env['hr.contract'].search([ - ('employee_id', '=', rec.employee_id.id), - ('is_probation', '=', True), - ('end_date', '>=', month_start), + ('employee_id', '=', rec.employee_id.id), ('is_probation', '=', True), + ('end_date', '>=', month_start) ], limit=1) if probation_contract: prob_end = min(probation_contract.end_date, month_end) prob_start = max(probation_contract.start_date, month_start) - if prob_start <= prob_end: # 避免开始日期大于结束日期 - probation_days = rec._create_leave_record(rec.employee_id, prob_start, prob_end) - - # 转正阶段的时间范围:当月剩余时段(实习结束后 → 月末) - reg_start = prob_end + timedelta(days=1) - reg_end = month_end - if reg_start <= reg_end: - regular_days = rec._create_leave_record(rec.employee_id, reg_start, reg_end) + if prob_start <= prob_end: + probation_days = rec._create_leave_record(employee, prob_start, prob_end) + # 转正天数 + reg_start = prob_end + timedelta(days=1) + reg_end = month_end + if reg_start <= reg_end: + regular_days = rec._create_leave_record(employee, reg_start, reg_end) + # 赋值考勤字段 + rec.go_out = go_out_days # 外出天数最终赋值 rec.should_attendance = work_days rec.test_days = probation_days rec.right_days = regular_days @@ -1015,22 +979,14 @@ class YuthonVacationReports(models.Model): rec.is_no_card = is_no rec.is_work = is_work rec.is_abnormal = is_abnormal - rec.actual_attendance = actual_attendance + rec.actual_attendance = max(actual_attendance, 0) rec.att_mount = actual_attendance rec.expatriation = expatriation_days else: - # 重置字段值(员工为空/离职/无vacation_date时) - rec.test_days = 0 - rec.right_days = 0 - rec.actual_attendance = 0 - rec.should_attendance = 0 - rec.is_late_card = 0 - rec.is_early_card = 0 - rec.is_no_card = 0 - rec.is_work = 0 - rec.is_abnormal = 0 - rec.att_mount = 0 - rec.expatriation = 0 + # 重置字段(员工为空/离职/无考勤月份) + rec.test_days = rec.right_days = rec.actual_attendance = rec.should_attendance = 0 + rec.is_late_card = rec.is_early_card = rec.is_no_card = rec.is_work = rec.is_abnormal = 0 + rec.att_mount = rec.expatriation = rec.go_out = 0.0 def update_is_confirm1(self): records = self.env["yuthon.vacation.reports"].browse( diff --git a/yuthon_hr_attendance/views/yuthon_vacation_reports_views.xml b/yuthon_hr_attendance/views/yuthon_vacation_reports_views.xml index 854713d4b..2460d5e86 100644 --- a/yuthon_hr_attendance/views/yuthon_vacation_reports_views.xml +++ b/yuthon_hr_attendance/views/yuthon_vacation_reports_views.xml @@ -63,6 +63,7 @@ + @@ -131,6 +132,7 @@ + diff --git a/yuthon_notice/models/yuthon_notice.py b/yuthon_notice/models/yuthon_notice.py index 64b8cdc62..7ccbd4290 100644 --- a/yuthon_notice/models/yuthon_notice.py +++ b/yuthon_notice/models/yuthon_notice.py @@ -195,11 +195,13 @@ class YuthonNotice(models.Model): self.sequence = 0 def notice_publish(self): + self.ensure_one() if self.is_change_notice: self.notice_state = 'yes' - conf_old = self.env['yuthon.confirm.users'].search( - [('notice_id', '=', self.id), ('employee_id', 'in', self.users_ids.mapped('employee_ids.id'))] - ) + conf_old = self.env['yuthon.confirm.users'].search([ + ('notice_id', '=', self.id), + ('employee_id', 'in', self.users_ids.mapped('employee_ids.id')) + ]) if conf_old: conf_old.write({'state': 'no'}) user_wecom_mapping = [] @@ -210,20 +212,30 @@ class YuthonNotice(models.Model): url = f'https://phone.thtzjt.com/phone/Notice/{self.id}?employee_id={employee.id}' result_data = self.sudo().env["wecom.apps"].sync_send_message_textcard( category='notice', - user_id=user_id.employee_id.wecom_userid, + touser=user_id.employee_id.wecom_userid, title="通知公告", - description=self.name + '发布人:' + self.users_id.name, + description=f"{self.name} 发布人: {self.users_id.name}", url=url, btntxt="详细信息" ) + msg_id = None if result_data and isinstance(result_data, tuple) and len(result_data) >= 2: res_dict = result_data[1] if res_dict.get('errcode') == 0 and res_dict.get('errmsg') == 'ok' and 'msgid' in res_dict: msg_id = res_dict['msgid'] + user_wecom_mapping.append({ + 'emp_id': employee.id, + 'msg_id': msg_id + }) + for mapping in user_wecom_mapping: emp_id = mapping['emp_id'] - is_exist = self.env['yuthon.confirm.users'].search([('notice_id', '=', self.id), ('employee_id', '=', emp_id)],limit=1) + msg_id = mapping['msg_id'] + is_exist = self.env['yuthon.confirm.users'].search([ + ('notice_id', '=', self.id), + ('employee_id', '=', emp_id) + ], limit=1) if not is_exist: self.env['yuthon.confirm.users'].create({ 'notice_id': self.id, @@ -234,8 +246,6 @@ class YuthonNotice(models.Model): 'state': 'no', 'msg_id': msg_id, }) - else: - raise UserError('该公告不允许通知,请勾选通知') def look_up_notice_users(self): return {