125 lines
5.8 KiB
Python
125 lines
5.8 KiB
Python
from odoo import models, fields, api
|
|
import re
|
|
|
|
|
|
def time_H_selection():
|
|
return [('00', '00'), ('01', '01'), ('02', '02'), ('03', '03'), ('04', '04'), ('05', '05'),
|
|
('06', '06'), ('07', '07'), ('08', '08'), ('09', '09'), ('10', '10'), ('11', '11'),
|
|
('12', '12'), ('13', '13'), ('14', '14'), ('15', '15'), ('16', '16'), ('17', '17'),
|
|
('18', '18'), ('19', '19'), ('20', '20'), ('21', '21'), ('22', '22'), ('23', '23')]
|
|
|
|
|
|
def time_M_or_S_selection():
|
|
return [('00', '00'), ('05', '05'), ('10', '10'), ('15', '15'), ('20', '20'), ('25', '25'),
|
|
('30', '30'), ('35', '35'), ('40', '40'), ('45', '45'), ('50', '50'), ('55', '55')]
|
|
|
|
|
|
class WorkLogSetting(models.Model):
|
|
_name = 'sf.work.log.setting'
|
|
_description = '工作日历设置'
|
|
|
|
def _get_code(self):
|
|
"""
|
|
自动生成编码
|
|
:return:
|
|
"""
|
|
fixture_material = self.env['sf.work.log.setting'].sudo().search(
|
|
[('code', '!=', '')],
|
|
limit=1,
|
|
order="id desc")
|
|
if not fixture_material:
|
|
num = "%03d" % 1
|
|
else:
|
|
m = int(fixture_material.code) + 1
|
|
num = "%03d" % m
|
|
return num
|
|
|
|
code = fields.Char(string='序号', default=_get_code, readonly=True)
|
|
name = fields.Char(string='工作日历名称', required=True)
|
|
|
|
start_time = fields.Char(string='日开始时间', readonly=True, compute='_compute_start_time')
|
|
start_time_H = fields.Selection(time_H_selection(), '时', required=True)
|
|
start_time_M = fields.Selection(time_M_or_S_selection(), '分', required=True)
|
|
end_time = fields.Char(string='日结束时间', readonly=True, compute='_compute_end_time')
|
|
end_time_H = fields.Selection(time_H_selection(), '时', required=True)
|
|
end_time_M = fields.Selection(time_M_or_S_selection(), '分', required=True)
|
|
|
|
duration = fields.Char(string='时长', readonly=True, compute='_compute_duration')
|
|
day_off = fields.Char(string='休息日', required=True)
|
|
|
|
user_defined_working_shift_status = fields.Boolean(string='自定义班次', default=False)
|
|
working_shift = fields.Char(string='班次')
|
|
working_shift_char = fields.Char(string='班次', readonly=True, compute='_compute_working_shift_time')
|
|
working_shift_select = fields.Selection([('早班00:00-08:00', '早班00:00-08:00'),
|
|
('白班08:00-16:00', '白班08:00-16:00'),
|
|
('晚班16:00-24:00', '晚班16:00-24:00'),
|
|
('长白班08:00-20:00', '长白班08:00-20:00'),
|
|
('长晚班20:00-08:00', '长晚班20:00-08:00')], string='班次')
|
|
working_shift_start_time_H = fields.Selection(time_H_selection(), '班次开始时间:时',
|
|
attr={'required': [('user_defined_working_shift_status', '=', 'True')]})
|
|
working_shift_start_time_M = fields.Selection(time_M_or_S_selection(), '分',
|
|
attr={'required': [('user_defined_working_shift_status', '=', 'True')]})
|
|
working_shift_end_time_H = fields.Selection(time_H_selection(), '班次结束时间:时',
|
|
attr={'required': [('user_defined_working_shift_status', '=', 'True')]})
|
|
working_shift_end_time_M = fields.Selection(time_M_or_S_selection(), '分',
|
|
attr={'required': [('user_defined_working_shift_status', '=', 'True')]})
|
|
|
|
status = fields.Boolean(string='状态', default=True)
|
|
update_person = fields.Char(string='更新人', default=lambda self: self.env.user.name)
|
|
update_time = fields.Datetime(string='更新时间', default=lambda self: fields.Datetime.now())
|
|
|
|
@api.depends('start_time_H', 'start_time_M')
|
|
def _compute_start_time(self):
|
|
"""
|
|
设置输入日开始时间
|
|
:return:
|
|
"""
|
|
for record in self:
|
|
record.start_time = f"{record.start_time_H}:{record.start_time_M}:00"
|
|
|
|
@api.depends('end_time_H', 'end_time_M')
|
|
def _compute_end_time(self):
|
|
"""
|
|
设置输入日结束时间
|
|
:return:
|
|
"""
|
|
for record in self:
|
|
record.end_time = f"{record.end_time_H}:{record.end_time_M}:00"
|
|
|
|
@api.onchange('working_shift_char', 'working_shift_select')
|
|
def _onchange_working_shift(self):
|
|
"""
|
|
对班次是否手动输入是进行不同的展示
|
|
:return:
|
|
"""
|
|
for record in self:
|
|
if record.working_shift_select:
|
|
record.working_shift = record.working_shift_select
|
|
else:
|
|
record.working_shift = record.working_shift_char
|
|
|
|
@api.depends(
|
|
'working_shift_start_time_H', 'working_shift_start_time_M',
|
|
'working_shift_end_time_H', 'working_shift_end_time_M')
|
|
def _compute_working_shift_time(self):
|
|
start_time = f"{self.working_shift_start_time_H}:{self.working_shift_start_time_M}:00"
|
|
end_time = f"{self.working_shift_end_time_H}:{self.working_shift_end_time_M}:00"
|
|
self.working_shift_char = f"自定义班次{start_time}-{end_time}"
|
|
|
|
@api.depends('start_time_H', 'start_time_M', 'end_time_H', 'end_time_M')
|
|
def _compute_duration(self):
|
|
"""
|
|
根据日开始时间和日结束时间计算每日工作时长
|
|
:return:
|
|
"""
|
|
for record in self:
|
|
st_h = float(record.start_time_H)
|
|
st_m = float(record.start_time_M)
|
|
end_h = float(record.end_time_H)
|
|
end_m = float(record.end_time_M)
|
|
# 日开始时间小于日结束时间
|
|
if st_h < end_h:
|
|
record.duration = str(round(end_h-st_h+(end_m-st_m)/60, 2))
|
|
else:
|
|
record.duration = str(round(end_h-st_h+(end_m-st_m)/60+24, 2))
|