我想以浮动值显示休假持续时间

时间:2017-07-11 07:06:12

标签: python openerp odoo-10

我在 hr_leave_rules.py

中创建了字段 half_day_allowed
from odoo import models, fields, api, _

class HRLeaveRules(models.Model):
    _name = 'hr_leave_rules.leave_rules'

    half_day_allowed = fields.Selection([
        ('yes',"Yes"),
        ('no',"No")],
        string="Half Day Allowed", required=True)

此外,我还继承了字段 get_number_of_days ,用于计算应用的休假天数和 holiday_status_id ,表示休假类型。我想要做的是,如果 holiday_status_id ,如果 half_day_allowed 是'是'然后在 get_number_of_days 中它应该采用 float 值,否则它将采用整数值。为此,我尝试了下面的代码,但它没有工作.plz帮帮我。

leave_type.py

from odoo import fields, models, api, _
from math import ceil
from datetime import timedelta
from openerp.exceptions import UserError

HOURS_PER_DAY = 8

class leave(models.Model):
    _inherit = "hr.holidays"


    @api.onchange('number_of_days_temp')
    def _holiday_status_id(self):

        current = self.env['hr_leave_rules.leave_rules'].search([(
                'holiday_status_id','=',self.holiday_status_id.id)])

    @api.onchange('date_from')
    def _onchange_date_from(self):

        date_from = self.date_from
        date_to = self.date_to

        if date_from and not date_to:
            date_to_with_delta = fields.Datetime.from_string(date_from) + timedelta(hours=HOURS_PER_DAY)
            self.date_to = str(date_to_with_delta)
            current = self.env['hr_leave_rules.leave_rules'].search([(
                    'holiday_status_id','=',self.holiday_status_id.id)])

            if current.half_day_allowed == 'yes':
                if (date_to and date_from) and (date_from <= date_to):
                    self.number_of_days_temp = self._get_number_of_days(
                            date_from, date_to, self.employee_id.id)
                else:
                    self.number_of_days_temp = 0
            else:
                if (date_to and date_from) and (date_from <= date_to):
                        self.number_of_days_temp = ceil(self._get_number_of_days(
                            date_from, date_to, self.employee_id.id))
                else:
                    self.number_of_days_temp = 0

    @api.onchange('date_to')
    def _onchange_date_to(self):

        date_from = self.date_from
        date_to = self.date_to

        current = self.env['hr_leave_rules.leave_rules'].search([(
                'holiday_status_id','=',self.holiday_status_id.id)])

        if current.half_day_allowed == 'yes':
            if (date_to and date_from) and (date_from <= date_to):
                self.number_of_days_temp = self._get_number_of_days(
                    date_from, date_to, self.employee_id.id)
            else:
                self.number_of_days_temp = 0
        else:
            if (date_to and date_from) and (date_from <= date_to):
                self.number_of_days_temp = ceil(self._get_number_of_days(
                    date_from, date_to, self.employee_id.id))
            else:
                self.number_of_days_temp = 0

1 个答案:

答案 0 :(得分:0)

从哪里开始...

首先,如果您的字段为是/否,则应将其定义为Boolean字段,而不是Char字段。 Fields Documentation。它将允许您执行更简单的if / else逻辑,更不用说它只是一种更好的做法。

if half_day_allowed:
    # Do this way
else:
    # Do that way

其次,如果你有重复的代码,你应该将它分解为自己的方法以便于阅读。 Don't repeat yourself

你重复了整整一节两次:

if half_day_allowed == 'yes':
    if (date_to and date_from) and (date_from <= date_to):
        self.number_of_days_temp = self._get_number_of_days(
                date_from, date_to, self.employee_id.id)
    else:
        self.number_of_days_temp = 0
else:
    if (date_to and date_from) and (date_from <= date_to):
            self.number_of_days_temp = ceil(self._get_number_of_days(
                date_from, date_to, self.employee_id.id))
    else:
        self.number_of_days_temp = 0

相反,只需创建一个新方法并在需要时调用它。

@api.multi
def get_number_of_days_temp(self, half_day_allowed=False):
    self.ensure_one()
    if half_day_allowed == 'yes':
        # Do this
    else:
        # Do that

@api.onchange('date_from')
def _onchange_date_from(self):
    ...
    self.number_of_days_temp = self.get_number_of_days_temp(current.half_day_allowed)

@api.onchange('date_to')
def _onchange_date_to(self):
    ...
    self.number_of_days_temp = self.get_number_of_days_temp(current.half_day_allowed)

最后,要确定未达到预期结果的问题,我们需要您提供更多信息。你到目前为止所说的只是:

  

我尝试了以下代码,但它无效。

     

在两种情况下都没有显示正确的get_number_of_days,即在&#39;是&#39;和&#39;没有&#39;

这对确定问题没有帮助。有错误信息吗?它是否返回无效数据?如果是这样,那么输入和输出是什么以及期望它应该是什么?

我不知道任何进一步的事情,我唯一猜到的是你的number_of_days_temp字段(你的问题中没有包含字段定义)被定义为Integer字段,在这种情况下,它将始终忽略小数。必须将其定义为Float才能使其正常工作。