I created the field half_day_allowed 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 fields get_number_of_days which calculates leave applied is for how many days and holiday_status_id which indicates leave type . what I am trying to do is if for particular holiday_status_id if half_day_allowed is 'yes' then in get_number_of_days it should take float values otherwise it will take integer value. For this I tried the below code but it is not working.plz help me out.
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