Files
gzth/yuthon_catering/wizard/catering_conf_change_wizard.py
T
2026-06-02 16:06:11 +08:00

73 lines
2.9 KiB
Python

# -*- coding: utf-8 -*-
from odoo import api, fields, models
class CateringConfChangeWizard(models.TransientModel):
_name = 'catering.conf.change.wizard'
_description = '餐饮人员配置变更确认'
conf_id = fields.Many2one(string='配置记录', comodel_name='yuthon.catering.conf', required=True)
employee_ids = fields.Many2many(
string='员工',
comodel_name='hr.employee',
)
@api.model
def default_get(self, fields_list):
res = super().default_get(fields_list)
conf_id = self._context.get('active_id')
if conf_id:
conf = self.env['yuthon.catering.conf'].browse(conf_id)
res['conf_id'] = conf_id
res['employee_ids'] = [(6, 0, conf.employee_ids.ids)]
return res
def _compute_change_note(self):
"""计算增减员备注"""
old_ids = set(self.conf_id.employee_ids.ids)
new_ids = set(self.employee_ids.ids)
added_ids = new_ids - old_ids
removed_ids = old_ids - new_ids
parts = []
if added_ids:
added_names = self.env['hr.employee'].browse(list(added_ids)).mapped('name')
parts.append('增员:' + '、'.join(added_names))
if removed_ids:
removed_names = self.env['hr.employee'].browse(list(removed_ids)).mapped('name')
parts.append('减员:' + '、'.join(removed_names))
return '\n'.join(parts) if parts else ''
def action_confirm(self):
"""确认:旧记录加失效日期,生成新记录"""
# 检查员工列表是否有变化,无变化则不允许生成新记录
if set(self.conf_id.employee_ids.ids) == set(self.employee_ids.ids):
from odoo.exceptions import ValidationError
raise ValidationError('员工列表未发生变化,无需生成新记录。\n'
'如需放弃操作,请点击下方"取消"按钮。')
today = fields.Date.today()
note = self._compute_change_note()
from odoo.models import Model
Model.write(self.conf_id, {'expiry_date': today})
# 生成新记录
self.env['yuthon.catering.conf'].create({
'company_id': self.conf_id.company_id.id,
'employee_ids': [(6, 0, self.employee_ids.ids)],
'effective_date': today,
'note': note,
})
return {'type': 'ir.actions.act_window_close'}
def action_cancel(self):
"""取消:不做任何变更"""
return {'type': 'ir.actions.act_window_close'}
def action_modify(self):
"""仅修改当前记录员工,不生成新记录,不更新日期"""
note = self._compute_change_note()
from odoo.models import Model
Model.write(self.conf_id, {
'employee_ids': [(6, 0, self.employee_ids.ids)],
'note': note,
})
return {'type': 'ir.actions.act_window_close'}