I want to show the duration of the vacation at a floating cost

I created a half_day_allowed field in hr_leave_rules.py

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)

and also, I have inherited the get_number_of_days fields , which calculates that the held pending period is so many days and holiday_status_id , which indicates the type of vacation. what I'm trying to do is if for a special holiday_status_id , if half_day_allowed is yes, then in get_number_of_days it should take floating point values, otherwise it will take an integer value. For this, I tried the code below, but it does not work. Plz help me.

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
+4
1

...

-, Yes/No, Boolean, Char. . if/else, .

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

-, , . .

:

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 , "" ""

. ? ? , ?

, , , , number_of_days_temp ( ) Integer, . Float .

0

Source: https://habr.com/ru/post/1681170/


All Articles