我想将字段days_compensation
的值传输到另一个字段days_compensation_remaining
,但它们不在同一模型中,并且字段2具有计算和逆函数,我被封锁了,谢谢前进(对每个人都是挑战哈哈)
我的代码:
在此模型中(hr.employee): 类别和字段:
class HrEmployee(models.Model):
_inherit = 'hr.employee'
days_compensation_remaining = fields.Float(
'Jours de récupération restant',
compute='_compute_days_compensation_remaining',
readonly=True,
inverse='_inverse_days_compensation_remaining',
help='Nombre total des jours de récupération ',
)
反方法:
@api.multi
def _inverse_days_compensation_remaining(self):
self.ensure_one()
compensation_legal_leave = self.company_id.days_compensation_holidays_status_id
if not compensation_legal_leave:
raise UserError(_("le type de congé pour les jours de récupération n'est pas défini"))
diff = self.days_compensation_remaining - compensation_legal_leave.get_days(
self.id)[compensation_legal_leave.id].get('days_compensation_remaining', 0.0)
if diff > 0:
leave = self.env['hr.holidays'].create(
{
'name': 'Allocation for %s' % self.name,
'employee_id': self.id,
'holiday_status_id': compensation_legal_leave.id,
'type': 'add',
'holiday_type': 'employee',
'number_of_days_temp': diff
}
)
leave.action_approve()
if leave.double_validation:
leave.action_validate()
elif diff < 0:
raise UserError(_('vous ne pouvez pas réduire le nombre de jours'))
计算方法:
@api.multi
def _compute_days_compensation_remaining(self):
for r in self:
compensation_legal_leave = r.company_id.days_compensation_holidays_status_id
if not compensation_legal_leave:
raise UserError(_("le type de congé pour les jours de récupération n'est pas défini"))
r.days_compensation_remaining = compensation_legal_leave.get_days(
r.id)[compensation_legal_leave.id].get('days_compensation_remaining', 0.0)
在此模型中(hr.employee.mission):
class HrMission(models.Model):
_inherit = "hr.employee.mission"
days_compensation =fields.Float(compute='get_compensation', string='Jours de récupération', help="Jours de récupération si la mission contient les jours de repos",
required=True, readonly=True, store=True)
@api.multi
@api.depends("mission_start_date", "mission_end_date")
def get_compensation(self):
for rec in self:
if rec.mission_start_date and rec.mission_end_date:
date_start = datetime.strptime(rec.mission_start_date,"%Y-%m-%d").date()
date_end = datetime.strptime(rec.mission_end_date,"%Y-%m-%d").date()
delta_day = timedelta(days=1)
days = {'mon':0,'tue':1,'wed':2,'thu':3,'fri':4,'sat':5,'sun':6}
dt = date_start
fri_count = 0
while dt <= date_end:
if dt.weekday() == days['fri']:
fri_count+=1
dt += delta_day
rec.days_compensation = fri_count