!2195 添加功能

Merge pull request !2195 from 李鹏宇/dev_zhou
This commit is contained in:
李鹏宇
2025-03-30 11:36:56 +00:00
committed by Gitee
2 changed files with 93 additions and 49 deletions
@@ -4,6 +4,7 @@ import calendar
from odoo import api, fields, models, tools, _
from odoo.tools.float_utils import float_round
from dateutil.relativedelta import relativedelta
from datetime import date
class HrAttendanceYear(models.Model):
@@ -414,6 +415,9 @@ class HrAttendanceMonthDetail(models.Model):
def button_confirm(self):
current_employee = self.env['hr.employee'].search([('user_id', '=', self.env.user.id)], limit=1)
start_date,end_date = self._get_days(current_employee)
day = self._create_leave_record(start_date, end_date)
work_day = self._process_shift_days(day,start_date,end_date) # 自然日中的工作日
for record in self:
current_year = record.current_year
current_month = int(record.current_month)
@@ -467,9 +471,8 @@ class HrAttendanceMonthDetail(models.Model):
performance_deduction += (performance_bonuses / 21.75) * personal_leave
# 产假
maternity_leave = record.maternity_leave
commuting_deduction += (commuting_allowance / month_days) * maternity_leave
performance_deduction += (performance_bonuses / month_days) * maternity_leave
commuting_deduction += (commuting_allowance / month_days) * work_day
performance_deduction += (performance_bonuses / month_days) * work_day
# 陪产假
paternity_leave = record.paternity_leave
@@ -507,6 +510,78 @@ class HrAttendanceMonthDetail(models.Model):
'last_month_commuting_deduction': commuting_deduction
})
# 行政考勤 调休日期
def _process_shift_days(self, days , start_date, end_date):
calendar_leaves = self.env['resource.calendar.leaves'].search([])
all_days = []
for calendar in calendar_leaves:
shift_days = calendar.shift_replacement_day.split(',') if calendar.shift_replacement_day else []
for day_str in shift_days:
day = datetime.datetime.strptime(day_str, '%m月%d日')
day = day.replace(year=start_date.year).date()
all_days.append(day)
unique_days = list(set(all_days))
total_days = days
for day in unique_days:
if start_date <= day <= end_date:
total_days += 1
return total_days
# 创建用户记录传入日期来获取天数(自然日中工作日的逻辑)
def _create_leave_record(self, start_date, end_date):
holiday_status_id = self.env['hr.leave.type'].search([('name', '=', '年假')])
admin_id = self.env['hr.employee'].search([("name", "=", 'Administrator')])
leave = 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',
})
days = leave.number_of_days
return days
# 获得假种的当月开始日期结束日期 (目前先产假)
def _get_days(self, employee_id):
year = int(self.current_year)
month = int(self.current_month)
if month == 12:
date_to = date(year, 12, 31)
else:
next_month = month + 1
date_to = date(year, next_month, 1) - datetime.timedelta(days=1)
date_from = date(year, month, 1)
holiday_status_id = self.env['hr.leave.type'].search([('name', '=', '产假')])
leave_records = self.env['hr.leave'].search([
('employee_id', '=', employee_id.id),
('holiday_status_id', '=', holiday_status_id.id),
('date_from', '<', date_to),
('date_to', '>', date_from),
('state', '=', 'validate')
])
for leave in leave_records:
leave_start = leave.date_from.date()
leave_end = leave.date_to.date()
start_in_month = max(leave_start, date_from)
end_in_month = min(leave_end, date_to)
# 判断是否跨月
# is_cross_month = leave_start.year != leave_end.year or leave_start.month != leave_end.month
# if is_cross_month:
if leave_start.year == year and leave_start.month == month:
start_date = leave_start
if leave_end.year == year and leave_end.month == month:
end_date = leave_end
else:
end_date = date(year, month + 1, 1) if month < 12 else date(year + 1, 1, 1)
end_date = end_date - datetime.timedelta(days=1) # 取到当月最后一天
elif leave_end.year == year and leave_end.month == month:
start_date = date(year, month, 1)
end_date = leave_end
else:
start_date = start_in_month
end_date = end_in_month
return start_date,end_date
def _get_attendance_month_obj(self):
"""
获取出勤月度汇总记录结果表
@@ -206,38 +206,24 @@ class HrAttendanceMonthWizard(models.TransientModel):
# 判断是否跨月
is_cross_month = leave_start.year != leave_end.year or leave_start.month != leave_end.month
if is_cross_month:
# if holiday_status_id.type_of_work == '0':
# if leave_start.year == year and leave_start.month == month:
# # 请假开始在当前月,如 2 月 16 号开始,2 月取 2 月底减去 16
# days_in_month = (date(year, month + 1, 1) - leave_start).days if month < 12 else (date(year + 1, 1, 1) - leave_start).days
# leave_days[leave_type] = days_in_month
# elif leave_end.year == year and leave_end.month == month:
# # 请假结束在当前月,如 5 月 6 号结束,5 月取 6 天
# days_in_month = (leave_end - date(year, month, 1)).days + 1
# leave_days[leave_type] = days_in_month
# else:
# # 正常情况,计算当前月内的天数
# days_in_month = (end_in_month - start_in_month).days
# leave_days[leave_type] = days_in_month
# else:
if leave_start.year == year and leave_start.month == month:
start_date = leave_start
if leave_end.year == year and leave_end.month == month:
end_date = leave_end
else:
end_date = date(year, month + 1, 1) if month < 12 else date(year + 1, 1, 1)
end_date = end_date - datetime.timedelta(days=1) # 取到当月最后一天
elif leave_end.year == year and leave_end.month == month:
start_date = date(year, month, 1)
if leave_start.year == year and leave_start.month == month:
start_date = leave_start
if leave_end.year == year and leave_end.month == month:
end_date = leave_end
else:
start_date = start_in_month
end_date = end_in_month
end_date = date(year, month + 1, 1) if month < 12 else date(year + 1, 1, 1)
end_date = end_date - datetime.timedelta(days=1) # 取到当月最后一天
elif leave_end.year == year and leave_end.month == month:
start_date = date(year, month, 1)
end_date = leave_end
else:
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()
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()
else:
for leave in leave_records:
total_days += leave.number_of_days_display
@@ -267,20 +253,3 @@ class HrAttendanceMonthWizard(models.TransientModel):
'request_date_to': end_date,
'state': 'confirm',
})
# 行政考勤 调休日期
# def _process_shift_days(self, leave , start_date, end_date):
# calendar_leaves = self.env['resource.calendar.leaves'].search([])
# all_days = []
# for calendar in calendar_leaves:
# shift_days = calendar.shift_replacement_day.split(',') if calendar.shift_replacement_day else []
# for day_str in shift_days:
# day = datetime.datetime.strptime(day_str, '%m月%d日')
# day = day.replace(year=start_date.year).date()
# all_days.append(day)
# unique_days = list(set(all_days))
# total_days = leave.number_of_days
# for day in unique_days:
# if start_date <= day <= end_date:
# total_days += 1
# return total_days