diff --git a/yuthon_hr_attendance/wizard/hr_attendance_month_wizard.py b/yuthon_hr_attendance/wizard/hr_attendance_month_wizard.py index 1d3441713..8576a1006 100644 --- a/yuthon_hr_attendance/wizard/hr_attendance_month_wizard.py +++ b/yuthon_hr_attendance/wizard/hr_attendance_month_wizard.py @@ -5,6 +5,7 @@ from odoo import models, api, fields, _ from odoo.tools.float_utils import float_round from odoo.exceptions import ValidationError, UserError from datetime import date +from datetime import timedelta class HrAttendanceMonthWizard(models.TransientModel): @@ -101,16 +102,16 @@ class HrAttendanceMonthWizard(models.TransientModel): # 1 生成出勤生成记录 # 2 生成出勤月度明细 # 获取出勤月度汇总记录结果表 - for record in self: - year = record.current_year - month = record.current_month - existing_records = self.env["hr.attendance.month"].search([ - ('current_year', '=', year), - ('current_month', '=', month), - ('company_id', '=', record.company_id.id) - ]) - if existing_records: - raise ValidationError(f"{year}年{month}月分的记录已经存在,不能重复操作.") + # for record in self: + # year = record.current_year + # month = record.current_month + # existing_records = self.env["hr.attendance.month"].search([ + # ('current_year', '=', year), + # ('current_month', '=', month), + # ('company_id', '=', record.company_id.id) + # ]) + # if existing_records: + # raise ValidationError(f"{year}年{month}月分的记录已经存在,不能重复操作.") self.ensure_one() attendance_month_obj = self._get_attendance_month_obj() # 查询所有的当前员工? 是否需要判断当前员工是否关联企业微信??预留 @@ -127,7 +128,7 @@ class HrAttendanceMonthWizard(models.TransientModel): for employee in hr_employee_obj: if ( employee.id not in resource_user_ids and - employee.contract.state not in ['draft', 'cancel'] + employee.contract_id.state not in ['draft', 'cancel'] ): if employee.id not in all_employee_ids: # 判断当前是否已经创建了考勤明细 @@ -140,8 +141,8 @@ class HrAttendanceMonthWizard(models.TransientModel): attendance_month_detail_obj.analysis_attendance_month_detail() self.get_leave_and_overtime_info(employee.id) # 返回action动作 - action = self._hr_attendance_month_detail_action() - return action + action = self._hr_attendance_month_detail_action() + return action # 考勤定时任务 def button_attendance_month(self): @@ -218,10 +219,8 @@ class HrAttendanceMonthWizard(models.TransientModel): start_date = start_in_month end_date = end_in_month - leave = self._create_leave_record(holiday_status_id, start_date, end_date) - leave_days[leave_type] = leave.number_of_days - # leave_days[leave_type] = self._process_shift_days(leave, start_date, end_date) - leave.unlink() + days = self._create_leave_record(holiday_status_id, start_date, end_date) + leave_days[leave_type] = days else: for leave in leave_records: total_days += leave.number_of_days_display @@ -241,13 +240,44 @@ class HrAttendanceMonthWizard(models.TransientModel): # 创建用户记录传入日期来获取天数 def _create_leave_record(self, holiday_status_id, start_date, end_date): - admin_id = self.env['hr.employee'].search([("name", "=", 'Administrator')]) - if not admin_id: - return None - return self.env['hr.leave'].sudo().create({ - 'employee_id': admin_id.id, - 'holiday_status_id': holiday_status_id.id, - 'request_date_from': start_date, - 'request_date_to': end_date, - 'state': 'confirm', - }) + if holiday_status_id.type_of_work == 0: + calendar_leaves = self.env['resource.calendar.leaves'].search([("calendar_id", "=", "行政考勤工作日")]) + holidays_list = [] + shift_days_list = [] + # 提取节假日和调休日 + for leave in calendar_leaves: + if leave.holidays: + holidays_list.extend(leave.holidays.split(',')) + if leave.alternate_rest_day: + holidays_list.extend(leave.alternate_rest_day.split(',')) + if leave.shift_replacement_day: + shift_days_list.extend(leave.shift_replacement_day.split(',')) + + holidays_list = [self.remove_leading_zero(date) for date in holidays_list] + shift_days_list = [self.remove_leading_zero(date) for date in shift_days_list] + + days = 0 # 初始化工作日计数 + current_date = start_date + + while current_date <= end_date: + current_date_str = current_date.strftime('%m月%d日') + current_date_str = self.remove_leading_zero(current_date_str) + is_weekend = current_date.weekday() in [5, 6] + + if current_date_str in holidays_list: + # 如果是法定节假日,不计为工作日 + pass + elif current_date_str in shift_days_list: + # 如果是调休日,无论是否周末都算作工作日 + days += 1 + elif not is_weekend: + # 如果不是周末且不是节假日,算作工作日 + days += 1 + + current_date += timedelta(days=1) + else: + delta = end_date - start_date + days = delta.days + 1 + return days + + diff --git a/yuthon_salary/models/inherit_hr_payslip.py b/yuthon_salary/models/inherit_hr_payslip.py index fc9c45ead..dc675ba0f 100644 --- a/yuthon_salary/models/inherit_hr_payslip.py +++ b/yuthon_salary/models/inherit_hr_payslip.py @@ -18,7 +18,7 @@ class InheritHrPayslip(models.Model): date_of_entry = fields.Date(related='employee_id.entry_date', string="入职时间", store=True) base_salary = fields.Float(string='基本工资', compute='_compute_salary', store=True) bank_account_id = fields.Many2one(string='银行账号', related='employee_id.bank_account_id', store=True) - tax_payable = fields.Float(string='应补(退)税额', store=True) + tax_payable = fields.Float(string='应补(退)税额') actual_salary = fields.Float(string='实发工资', store=True, compute='_compute_actual_salary') three_one = fields.Float(string='三险一金', store=True, compute='_compute_salary') identification_id = fields.Char(string='证件号码', related='employee_id.identification_id', store=True)