diff --git a/yuthon_holiday/models/inherit_hr_leave.py b/yuthon_holiday/models/inherit_hr_leave.py index 12b483b63..ba2d29a30 100644 --- a/yuthon_holiday/models/inherit_hr_leave.py +++ b/yuthon_holiday/models/inherit_hr_leave.py @@ -167,25 +167,45 @@ class HrLeave(models.Model): holiday.number_of_hours = hours holiday.number_of_days = days if holiday.holiday_status_id.type_of_work == "0": - calendar = self.env['resource.calendar'].sudo().search([("name", "=", "行政考勤工作日")], limit=1) - calendar_leaves = self.env['resource.calendar.leaves'].search([]) - found = False + calendar_leaves = self.env['resource.calendar.leaves'].search([("calendar_id", "=", "行政考勤工作日")]) target_day = holiday.request_date_from for leaves in calendar_leaves: shift_days = leaves.shift_replacement_day.split(',') if leaves.shift_replacement_day else [] for day_str in shift_days: day = datetime.strptime(day_str, '%m月%d日') if day.month == target_day.month and day.day == target_day.day: - hours = float(holiday.request_hour_to) - float(holiday.request_hour_from) -2 - holiday.number_of_hours = hours - found = True - break - if not found: - day, hours = holiday._get_duration(resource_calendar=calendar) - total_hours = (self.request_date_to - self.request_date_from).days - days = calendar.hours_per_day / 8 * total_hours + 1 - holiday.number_of_hours = hours - holiday.number_of_days = days + hour = float(holiday.request_hour_to) - float(holiday.request_hour_from) -2 + holiday.number_of_hours = hour + holidays_list = [] + shift_days_list = [] + for leaves in calendar_leaves: + if leaves.holidays: + holidays_list.extend(leaves.holidays.split(',')) + if leaves.shift_replacement_day: + shift_days_list.extend(leaves.shift_replacement_day.split(',')) + days = (self.request_date_to - self.request_date_from).days + 1 + current_date = self.request_date_from + while current_date <= self.request_date_to: + 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: + days -= 1 + elif current_date_str in shift_days_list: + if not is_weekend: + days += 1 + elif is_weekend: + days -= 1 + current_date += timedelta(days=1) + + self.number_of_days = days + + def remove_leading_zero(self, date_str): + month, day = date_str.split('月') + month = month.lstrip('0') + day = day.rstrip('日') + day = day.lstrip('0') + return f'{month}月{day}日' def _compute_request_unit_hours(self): if not self.env.context.get('no_change_request_unit_hours'): diff --git a/yuthon_holiday/views/hr_leave_batch.xml b/yuthon_holiday/views/hr_leave_batch.xml index 476e5c22d..c1e605be0 100644 --- a/yuthon_holiday/views/hr_leave_batch.xml +++ b/yuthon_holiday/views/hr_leave_batch.xml @@ -130,7 +130,7 @@ - +