@@ -16,7 +16,6 @@ class YuthonDutyPlan(models.Model):
|
||||
plan_line_ids = fields.One2many('duty.plan.line', 'duty_plan_id', string='关联计划明细')
|
||||
white_line_ids = fields.One2many('white.plan.line', 'duty_plan_id', string='关联计划明细')
|
||||
notice = fields.Text(string='计划说明')
|
||||
select_date = fields.Date(string='选择日期', default=fields.Date.today())
|
||||
select_time = fields.Selection([('1', '一月'), ('2', '二月'), ('3', '三月'), ('4', '四月'),
|
||||
('5', '五月'), ('6', '六月'), ('7', '七月'), ('8', '八月'),
|
||||
('9', '九月'), ('10', '十月'), ('11', '十一月'),
|
||||
@@ -33,12 +32,12 @@ class YuthonDutyPlan(models.Model):
|
||||
}
|
||||
|
||||
def auto_duty_plan(self, next_date=False):
|
||||
selected_month = int(self.select_time)
|
||||
year = self.select_date.year
|
||||
month = self.start_index_time.month
|
||||
year = self.start_index_time.year
|
||||
if not next_date:
|
||||
first_day_of_month = datetime(year, selected_month, 1)
|
||||
first_day_of_month = datetime(year, month, 1)
|
||||
# 计算结束日期(下个月的第一天)
|
||||
next_month = (selected_month % 12) + 1
|
||||
next_month = (month % 12) + 1
|
||||
next_year = year + (1 if next_month == 1 else 0) # 如果是12月,年份加1
|
||||
last_day_of_month = datetime(next_year, next_month, 1)
|
||||
existing_slots = self.env['planning.slot'].search([
|
||||
@@ -49,9 +48,9 @@ class YuthonDutyPlan(models.Model):
|
||||
if len(existing_slots) > 0:
|
||||
raise UserError(f"该月已经有排程,请选择其他时间。")
|
||||
else:
|
||||
next_month = (selected_month % 12) + 1
|
||||
next_month = (month % 12) + 1
|
||||
next_year = year + (1 if next_month == 1 else 0)
|
||||
first_day_of_month = datetime(next_year, next_month, 1)
|
||||
first_day_of_month = datetime(next_year, month, 1)
|
||||
next_next_month = (next_month % 12) + 1
|
||||
next_next_year = next_year + (1 if next_next_month == 1 else 0)
|
||||
last_day_of_month = datetime(next_next_year, next_next_month, 1)
|
||||
@@ -62,7 +61,19 @@ class YuthonDutyPlan(models.Model):
|
||||
])
|
||||
if len(existing_slots) > 0:
|
||||
raise UserError(f"该月已经有排程,请选择其他时间。")
|
||||
|
||||
latest_slot = self.env['planning.slot'].search([('role_id.name', '=', '联安粮库')], order='start_datetime desc',limit=1)
|
||||
if latest_slot:
|
||||
latest_month = latest_slot.start_datetime.month
|
||||
latest_year = latest_slot.start_datetime.year
|
||||
if latest_month == 12:
|
||||
latest_year += 1
|
||||
first_day_of_month = datetime(next_year, 1, 1)
|
||||
last_day_of_month = datetime(next_year,2, 1)
|
||||
else:
|
||||
first_day_of_month = datetime(next_year, latest_month+1, 1)
|
||||
next_next_month = (latest_month % 12) + 1
|
||||
next_year = year + (1 if next_month == 1 else 0)
|
||||
last_day_of_month = datetime(next_year, next_next_month+1, 1)
|
||||
role_a = self.env['planning.role'].search([('name', '=', '联安粮库')], limit=1)
|
||||
role_template_a = self.env['planning.slot.template'].search([('planning_name', '=', '白班'), ('role_id', '=', role_a.id)])
|
||||
role_template_b = self.env['planning.slot.template'].search([('planning_name', '=', '白值'), ('role_id', '=', role_a.id)])
|
||||
@@ -84,70 +95,35 @@ class YuthonDutyPlan(models.Model):
|
||||
'allocated_hours': 8,
|
||||
})
|
||||
current_date += timedelta(days=1)
|
||||
check_existing_slots = self.env['planning.slot'].search([
|
||||
('role_id.name', '=', '联安粮库'),
|
||||
('start_datetime', '>=', first_day_of_month- relativedelta(months=1)),
|
||||
('start_datetime', '<=', last_day_of_month- relativedelta(months=1)),
|
||||
])
|
||||
if not check_existing_slots:
|
||||
i = 0
|
||||
zu = 0
|
||||
current_date2 = first_day_of_month
|
||||
while current_date2 <= last_day_of_month:
|
||||
self.zu = zu
|
||||
if zu == len(self.plan_line_ids):
|
||||
zu = 0
|
||||
sorted_plan_line_ids = self.plan_line_ids.sorted(key=lambda r: r.sequence)
|
||||
for employee in sorted_plan_line_ids[zu].employee_ids:
|
||||
employee_id = self.env['planning.slot'].search([('resource_id', '=', employee.resource_id.id), ('start_datetime', '>=', first_day_of_month), ('start_datetime', '<=', last_day_of_month)]).sorted('start_datetime')
|
||||
if i < len(employee_id):
|
||||
start_hour2 = int(role_template_b.start_time)
|
||||
end_hour2 = int(role_template_b.end_time)
|
||||
current_datetime = datetime.combine(current_date2, time(0, 0, 0))
|
||||
start_time2 = current_datetime.replace(hour=start_hour2, minute=30) - timedelta(hours=8)
|
||||
end_time2 = current_datetime.replace(hour=end_hour2, minute=30) - timedelta(hours=8)
|
||||
employee_id[i].write({
|
||||
'resource_id': employee.resource_id.id,
|
||||
'template_id': role_template_b.id,
|
||||
'role_id': role_a.id,
|
||||
'start_datetime': start_time2,
|
||||
'end_datetime': end_time2,
|
||||
'allocated_hours': 8,
|
||||
})
|
||||
i += 1
|
||||
zu += 1
|
||||
current_date2 += timedelta(days=1)
|
||||
|
||||
else:
|
||||
i = 0
|
||||
zu = self.zu
|
||||
current_date2 = first_day_of_month
|
||||
while current_date2 <= last_day_of_month:
|
||||
self.zu = zu
|
||||
if zu == len(self.plan_line_ids):
|
||||
zu = 0
|
||||
sorted_plan_line_ids = self.plan_line_ids.sorted(key=lambda r: r.sequence)
|
||||
for employee in sorted_plan_line_ids[zu].employee_ids:
|
||||
employee_id = self.env['planning.slot'].search(
|
||||
[('resource_id', '=', employee.resource_id.id), ('start_datetime', '>=', first_day_of_month),
|
||||
('start_datetime', '<=', last_day_of_month)]).sorted('start_datetime')
|
||||
if i < len(employee_id):
|
||||
start_hour2 = int(role_template_b.start_time)
|
||||
end_hour2 = int(role_template_b.end_time)
|
||||
current_datetime = datetime.combine(current_date2, time(0, 0, 0))
|
||||
start_time2 = current_datetime.replace(hour=start_hour2, minute=30) - timedelta(hours=8)
|
||||
end_time2 = current_datetime.replace(hour=end_hour2, minute=30) - timedelta(hours=8)
|
||||
employee_id[i].write({
|
||||
'resource_id': employee.resource_id.id,
|
||||
'template_id': role_template_b.id,
|
||||
'role_id': role_a.id,
|
||||
'start_datetime': start_time2,
|
||||
'end_datetime': end_time2,
|
||||
'allocated_hours': 8,
|
||||
})
|
||||
i += 1
|
||||
zu += 1
|
||||
current_date2 += timedelta(days=1)
|
||||
i = 0
|
||||
zu = self.zu
|
||||
current_date2 = first_day_of_month
|
||||
while current_date2 <= last_day_of_month:
|
||||
self.zu = zu
|
||||
if zu == len(self.plan_line_ids):
|
||||
zu = 0
|
||||
sorted_plan_line_ids = self.plan_line_ids.sorted(key=lambda r: r.sequence)
|
||||
for employee in sorted_plan_line_ids[zu].employee_ids:
|
||||
employee_id = self.env['planning.slot'].search(
|
||||
[('resource_id', '=', employee.resource_id.id), ('start_datetime', '>=', first_day_of_month),
|
||||
('start_datetime', '<=', last_day_of_month)]).sorted('start_datetime')
|
||||
if i < len(employee_id):
|
||||
start_hour2 = int(role_template_b.start_time)
|
||||
end_hour2 = int(role_template_b.end_time)
|
||||
current_datetime = datetime.combine(current_date2, time(0, 0, 0))
|
||||
start_time2 = current_datetime.replace(hour=start_hour2, minute=30) - timedelta(hours=8)
|
||||
end_time2 = current_datetime.replace(hour=end_hour2, minute=30) - timedelta(hours=8)
|
||||
employee_id[i].write({
|
||||
'resource_id': employee.resource_id.id,
|
||||
'template_id': role_template_b.id,
|
||||
'role_id': role_a.id,
|
||||
'start_datetime': start_time2,
|
||||
'end_datetime': end_time2,
|
||||
'allocated_hours': 8,
|
||||
})
|
||||
i += 1
|
||||
zu += 1
|
||||
current_date2 += timedelta(days=1)
|
||||
|
||||
for i in self.env['planning.slot'].search([('role_id.name', '=', '联安粮库'), ('template_id.planning_name', '=', '白班')]).sorted('start_datetime'):
|
||||
week = i.start_datetime.weekday()
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
<field name="arch" type="xml">
|
||||
<form string="排版规则" create="0">
|
||||
<header>
|
||||
<button name="auto_duty_plan" type="object" string="安排值班" class="oe_highlight"/>
|
||||
<button name="duty_next_plan" type="object" string="安排下个月值班" class="oe_highlight"/>
|
||||
<!-- <button name="auto_duty_plan" type="object" string="安排值班" class="oe_highlight"/>-->
|
||||
<button name="duty_next_plan" type="object" string="顺延安排" class="oe_highlight"/>
|
||||
<button name="bottom_delete_schedule" type="object" string="删除计划排班" class="oe_highlight"/>
|
||||
</header>
|
||||
<sheet>
|
||||
@@ -32,9 +32,7 @@
|
||||
<group>
|
||||
<group>
|
||||
<field name="notice"/>
|
||||
<field name="select_date" invisible="1"/>
|
||||
<field name="zu" invisible="1"/>
|
||||
<field name="select_time" required="1"/>
|
||||
</group>
|
||||
<group>
|
||||
<field name="start_index_time"/>
|
||||
|
||||
Reference in New Issue
Block a user