Hi Forum!
I don't understand why i get a value of 0 in the method 1 below.
Method 1
yearly_holidays_ids
= fields.One2many('hr.employee.yearly.holidays',
'employee_id')
current_holiday_pool
= fields.Integer(compute="_get_current_holiday_pool",
store=True)
field in hr.employee.yearly.holidays:
holidays_ids = fields.Many2many('hr.holidays',compute='_compute_holidays_ids')
field in hr.holidays:
holiday_status_id = fields.Many2one("hr.holidays.status", string="Leave Type", required=True, readonly=True, states={'draft': [('readonly', False)], 'confirm': [('readonly', False)]})
@api.multi
@api.depends('yearly_holidays_ids')
def
_get_current_holiday_pool(self):
for
record
in
self:
current_year = datetime.date.today().year
total_holidays = record.yearly_holidays_ids.filtered(lambda
lm:
lm.year == current_year)
total_record =
total_holidays.mapped("holidays_ids").filtered(lambda
lm:
lm.holiday_status_id.name == 'Annual')
if
len(total_record)
> 1:
total_record = total_record[0]
record.current_holiday_pool =
total_record.remaining_count
elif
len(total_record)
== 1:
record.current_holiday_pool =
total_record.remaining_count
elif
len(total_record)
== 0:
record.current_holiday_pool = 0
Method 2
with this method:
@api.multi
@api.depends('yearly_holidays_ids')
def
_get_current_holiday_pool(self):
for
record
in
self:
current_year = datetime.date.today().year
total_record = record.yearly_holidays_ids.filtered(lambda
lm:
lm.year == current_year)
if
len(total_record)
> 1:
total_record = total_record[0]
record.current_holiday_pool =
total_record.remaining_count
elif
len(total_record)
== 1:
record.current_holiday_pool =
total_record.remaining_count
elif
len(total_record)
== 0:
record.current_holiday_pool = 0
I get the correct value, which is 20, that would be the base I started from.
In the first method (Method 1), I tried to achieve this:
The process is to collect the remaining annual leave for your employees, which is 20 days. This method should be supplemented so that if, for example, the employee takes sick leave, the remaining limit is 20 days, stay 20 days, but if he takes annual leave, he subtracts the remaining limit from the 20 days according to the date of the taken leave. So if an employee has 20 days of remaining leave and takes 1 day of remaining leave, he has 20 days of remaining leave, but if he takes 1 day of annual leave, he has 19 days of remaining leave.
This should extend the _get_current_holiday_pool method (Method 1)
In essence, the method should only be deducted from the number of leave (20) if the employee takes annual leave.
I hope i was clear,
Thank you for your help
Regards,
Steven