Files
gzth/yuthon_hr_attendance/models/overtime_approval.py
T
2025-04-03 10:29:59 +08:00

203 lines
9.9 KiB
Python

# -*- coding: utf-8 -*-
from odoo import api, fields, models, tools, _
from datetime import datetime, time, timedelta
from odoo.exceptions import UserError
from odoo.exceptions import UserError, ValidationError
from dateutil.relativedelta import relativedelta
from datetime import datetime
class OvertimeApprovals(models.Model):
_name = 'overtime.approval'
_description = "加班审批"
_inherit = ['mail.thread.main.attachment', 'mail.activity.mixin', 'soong.workflow.mixin']
_rec_name = 'name'
name = fields.Char(string="标题", compute='_compute_name')
code = fields.Char(string="编号", default=lambda self: self.env['ir.sequence'].next_by_code('overtime_approval'))
users_id = fields.Many2one('res.users', string="申请人", default=lambda self: self.env.user.id)
company_id = fields.Many2one('res.company', string='公司', index=True, default=lambda self: self.env.company)
# overtime_approval_ids = fields.One2many('overtime.user.line', 'overtime_approval_id', string="加班审批行")
reason_application = fields.Text(string="事由")
department_id = fields.Many2one(related='users_id.employee_id.department_id', string="部门")
is_issues = fields.Boolean(string='是否上会')
issues_result = fields.Html(string="会议结果")
details = fields.Text(string="加班说明")
meal = fields.Selection([('yes', '是'), ('no', '否')], string="误餐费", default=None)
employee_ids = fields.Many2many('hr.employee', string="加班人", default=lambda self: self.env['hr.employee'].search([('user_id', '=', self.env.uid)]))
related_issues_id = fields.Many2one('issue.library', string="关联议题")
documents_ids = fields.Many2many('ir.attachment', string="附件")
duration = fields.Float(string="时长", compute='_compute_duration', store=True)
overtime_hours = fields.Float(string="本月加班时长")
start_time = fields.Datetime(string="开始时间")
end_time = fields.Datetime(string="结束时间")
type_overtime = fields.Selection([('work', '工作日加班'), ('weekend', '周末加班'), ('holiday', '节假日加班')],
string="加班类型", compute="_compute_overtime_type")
urgency_level = fields.Selection([('normal', '普件'), ('urgent', '急件'), ('emergency', '特急件')], string='紧急度',
default='normal')
active = fields.Boolean('Active', default=True, tracking=True)
work_end = fields.Boolean(string="结束")
def _get_default_name_title(self):
return self._description if self._description else self._name
name_title = fields.Char(default=_get_default_name_title)
@api.depends('start_time', 'end_time')
def _compute_duration(self):
for rec in self:
if rec.start_time and rec.end_time:
rec.duration = round((rec.end_time - rec.start_time).total_seconds() / 3600, 1)
else:
rec.duration = 0.0
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
company_id = vals.get('company_id')
if company_id:
company = self.env['res.company'].browse(company_id)
if company.name == '天河物管' :
if vals.get('duration'):
if (vals['duration']) % 1 != 0.5:
raise ValidationError("时长不对!请重新编辑")
return super(OvertimeApprovals, self).create(vals_list)
@api.onchange('is_issues')
def _onchange_is_issues(self):
if self.is_issues:
issue_id = self.env['issue.library'].create({'name': self.name})
self.related_issues_id = issue_id.id
workflow_record_id = self.env["soong.workflow.record"].search(
[("res_model", "=", self._name), ("res_id", "=", self.id)])
self.env['issue.library.approval.line'].create({
'issue_library_id': issue_id.id,
'workflow_record_id': workflow_record_id.id,
'approval_type': workflow_record_id.workflow_id.id,
'code': self.code,
'approval_date': fields.Datetime.now(),
})
@api.onchange('overtime_department_id')
def onchange_overtime_users(self):
users_data_list = []
default_datetime = datetime.combine(datetime.now().date(), time(hour=17, minute=30)) - timedelta(hours=8),
users_list = []
for user in self.overtime_user_ids:
users_list.append(user.user_id.id)
for over in self:
if not over.overtime_department_id:
if over.users_id.id not in users_list:
users_data_list.append((0, 0, {
'overtime_approval_id': over.id,
'user_id': over.users_id.id,
'start_time': default_datetime[0],
'type_overtime': 'work',
'compensation_mode': 'salary',
}))
else:
employee_ids = self.env['hr.employee'].search(
[('department_id', '=', self.overtime_department_id.id)])
for user in employee_ids:
if user.user_id.id not in users_list:
users_data_list.append((0, 0, {
'overtime_approval_id': over.id,
'user_id': user.user_id.id,
'start_time': default_datetime[0],
'type_overtime': 'work',
'compensation_mode': 'salary',
}))
over.overtime_user_ids = users_data_list
@api.onchange('company_id')
def onchange_company(self):
for record in self:
if record.company_id:
# 根据 company_id 搜索 overtime.page 模型中的记录
overtime_id = record.env['overtime.page'].search([('company_id', '=', record.company_id.id)], limit=1)
if overtime_id:
record.details = (f"1、每天加班不得超过{overtime_id.day_time}\n"
f"2、每月加班不得超过{overtime_id.month_time}\n")
else:
record.details = ""
def put_hr_employee(self):
for record in self:
employee_id = self.env['hr.employee'].search([('id', '=', record.users_id.employee_id.id)])
meal_number = employee_id.meal_number
if record.type_overtime == 'work':
employee_id.write({'normal_overtime_hours': employee_id.normal_overtime_hours + record.duration})
elif record.type_overtime == 'weekend':
employee_id.write({'weekend_overtime_hours': employee_id.weekend_overtime_hours + record.duration})
else:
employee_id.write({'holiday_overtime_hours': employee_id.holiday_overtime_hours + record.duration})
record.overtime_hours = employee_id.holiday_overtime_hours + employee_id.weekend_overtime_hours + employee_id.normal_overtime_hours
if self.meal == 'yes':
meal_number +=1
employee_id.write({'meal_number':meal_number})
holiday_status_id = self.env['hr.leave.type'].search([('name', '=', '补休')])
allocation = self.env['hr.leave.allocation'].create({
'employee_id': employee_id.id,
'name': '补休',
'number_of_days': self.duration/7,
'holiday_status_id': holiday_status_id.id,
'allocation_type': 'regular',
"number_of_days_display": self.duration/7,
})
if allocation:
allocation.sudo().action_validate()
@api.depends('users_id', 'duration')
def _compute_name(self):
for rec in self:
if rec.duration == 0:
rec.name = f"{rec.users_id.name}加班"
else:
rec.name = f"{rec.users_id.name}加班{rec.duration}小时"
@api.depends('start_time', 'end_time')
def _compute_overtime_type(self):
for record in self:
if record.start_time and record.end_time:
if record.start_time > record.end_time:
raise UserError("开始时间不能晚于结束时间")
start_date = fields.Datetime.from_string(record.start_time)
end_date = fields.Datetime.from_string(record.end_time)
public_holidays = self.env['resource.calendar.leaves'].search([])
for holiday in public_holidays:
holiday_start = fields.Datetime.from_string(holiday.date_from)
holiday_end = fields.Datetime.from_string(holiday.date_to)
if start_date <= holiday_end and end_date >= holiday_start:
record.type_overtime = 'holiday'
break
else:
if start_date.weekday() < 5 and end_date.weekday() < 5: # Not Saturday or Sunday
record.type_overtime = 'work'
elif start_date.weekday() >= 5 or end_date.weekday() >= 5: # Saturday or Sunday
record.type_overtime = 'weekend'
else:
record.type_overtime = False
class OvertimeUserLine(models.Model):
_name = 'overtime.user.line'
# def is_holiday(self, date):
# holiday_obj = self.env['resource.calendar.leaves']
# holidays = holiday_obj.search([
# ('date_from', '<=', date),
# ('date_to', '>=', date)
# ])
# return bool(holidays)
# def attendance_button(self):
# return {
# 'type': 'ir.actions.client',
# 'tag': 'display_notification',
# 'target': 'new',
# 'params': {
# 'title': '考勤信息',
# 'message': _(self.user_id.name + "的签到时间:2024-7-12 8:00:00 签出时间:2024-7-12 20:00:00"),
# 'sticky': True
# }
# }