优化工作日历设置开始时间和结束时间为根据所选班次自动生成

This commit is contained in:
yuxianghui
2023-07-17 16:39:30 +08:00
parent cf22c6f6fc
commit 5b0df0befd
3 changed files with 32 additions and 82 deletions

View File

@@ -49,7 +49,7 @@ class ProcedureEquipmentResourceSetting(models.Model):
@api.depends('working_calendar_id')
def _onchange_working_calendar_id(self):
for record in self:
record.working_shift_id = record.working_calendar_id.working_shift_id
record.working_shift_ids = record.working_calendar_id.working_shift_ids

View File

@@ -1,21 +1,5 @@
from datetime import datetime, timedelta, date
from datetime import datetime, timedelta
from odoo import models, fields, api
import re
from odoo.exceptions import ValidationError
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):
@@ -38,20 +22,15 @@ class WorkLogSetting(models.Model):
num = "%03d" % m
return num
code = fields.Char(string='序号', default=_get_code, readonly=True)
code = fields.Char(string='序号', default=_get_code)
name = fields.Char(string='工作日历名称', required=True, size=15, length=30)
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)
working_shift_ids = fields.Many2many('sf.working.shift', string='班次')
start_time = fields.Datetime(string='日开始时间', readonly=True, compute='_compute_working_shift_ids')
end_time = fields.Datetime(string='日结束时间', readonly=True, compute='_compute_working_shift_ids')
duration = fields.Char(string='时长', readonly=True, compute='_compute_working_shift_ids')
duration = fields.Char(string='时长', readonly=True, compute='_compute_duration')
day_off_id = fields.Many2many('sf.day.off', string='休息日', required=True)
working_shift_id = fields.Many2many('sf.working.shift', string='班次')
day_off_ids = fields.Many2many('sf.day.off', string='休息日', required=True)
status = fields.Boolean(string='状态', default=True)
update_person = fields.Char(string='更新人', default=lambda self: self.env.user.name)
@@ -59,48 +38,28 @@ class WorkLogSetting(models.Model):
setting_to_calendar_ids = fields.One2many('sf.work.schedule.calendar', 'name_id', '工作日历')
# @api.model
# def create(self, vals):
# vals['setting_to_calendar_ids'] = [(4, child.id)]
# return super(WorkLogSetting, self).create(vals)
@api.depends('start_time_H', 'start_time_M')
def _compute_start_time(self):
@api.depends('working_shift_ids')
def _compute_working_shift_ids(self):
"""
设置输入日开始时间
根据所选班次自动生成开始时间和结束时间,同时计算出工作时长
:return:
"""
for record in self:
record.start_time = f"{record.start_time_H}:{record.start_time_M}:00"
if record:
for working_shift_id in record.working_shift_ids:
if not record.start_time:
record.start_time = working_shift_id.start_time
record.end_time = working_shift_id.end_time
else:
if (working_shift_id.start_time - record.start_time).total_seconds() < 0:
record.start_time = working_shift_id.start_time
if (working_shift_id.end_time - record.end_time).total_seconds() > 0:
record.end_time = working_shift_id.end_time
@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"
record.duration = record.end_time - record.start_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))
# @api.onchange('day_off_id')
# def _onchange_day_off_id(self):
# @api.onchange('day_off_ids')
# def _onchange_day_off_ids(self):
# # 先删除之前创建的工作日历事件记录
# self.env['sf.work.schedule.calendar'].search([('calendar_code', '=', self.code)]).unlink()
#
@@ -176,7 +135,7 @@ class WorkLogSetting(models.Model):
start_date = datetime.now().replace(month=1, day=1).date()
end_date = datetime.now().replace(month=12, day=31).date()
# 休息日列表
rest_days = self.day_off_id.mapped('name')
rest_days = self.day_off_ids.mapped('name')
for single_date in self.daterange(start_date, end_date):
is_workday = single_date.strftime("%A")
if is_workday in rest_days:

View File

@@ -11,8 +11,8 @@
<field name="start_time"/>
<field name="end_time"/>
<field name="duration"/>
<field name="day_off_id" widget="many2many_tags"/>
<field name="working_shift_id" widget="many2many_tags"/>
<field name="day_off_ids" widget="many2many_tags"/>
<field name="working_shift_ids" widget="many2many_tags"/>
<field name="status"/>
<field name="update_person"/>
<field name="update_time"/>
@@ -40,30 +40,20 @@
</group>
</group>
<group string="选择班次">
<field name="working_shift_id"/>
<field name="working_shift_ids"/>
</group>
<group string="工作时间">
<group>
<group>
<field name="start_time"/>
<field name="end_time"/>
</group>
<group>
<field name="start_time_H"/>
<field name="end_time_H"/>
</group>
<field name="start_time"/>
</group>
<group>
<group>
<field name="start_time_M"/>
<field name="end_time_M"/>
</group>
<field name="end_time"/>
</group>
</group>
<group>
<group>
<field name="duration"/>
<field name="day_off_id"
<field name="day_off_ids"
widget="many2many_tags"
options="{'no_create': True, 'no_quick_create': True}"/>
</group>
@@ -198,6 +188,7 @@
<field name="arch" type="xml">
<calendar string="工作日历" mode="year" date_start="date_time">
<field name="name"/>
<!-- <field name="monthly_rest_days" widget="char" position="right_bottom"/>-->
</calendar>
</field>
</record>