Odoo Mindscape. Understand Odoo faster. Explore modules, fields, dependencies and version differences in one place instead of digging through the source code.

Module: Time Off

Entity: hr.department

Compute Method: _compute_leave_count

Inputs Method Outputs
  • id
Requests = self.env['hr.leave']
Allocations = self.env['hr.leave.allocation']
today_date = datetime.now(timezone.utc).date()
today_start = fields.Datetime.to_string(today_date)
today_end = fields.Datetime.to_string(today_date + relativedelta(hours=23, minutes=59, seconds=59))
leave_data = Requests._read_group(
            [('department_id', 'in', self.ids),
             ('state', '=', 'confirm')],
            ['department_id'], ['__count'])
allocation_data = Allocations._read_group(
            [('department_id', 'in', self.ids),
             ('state', '=', 'confirm')],
            ['department_id'], ['__count'])
absence_data = Requests._read_group(
            [('department_id', 'in', self.ids), ('state', '=', 'validate'),
             ('date_from', '<=', today_end), ('date_to', '>=', today_start)],
            ['department_id'], ['__count'])
res_leave = {department.id: count for department, count in leave_data}
res_allocation = {department.id: count for department, count in allocation_data}
res_absence = {department.id: count for department, count in absence_data}
for department in self:
            department.leave_to_approve_count = res_leave.get(department.id, 0)
            department.allocation_to_approve_count = res_allocation.get(department.id, 0)
            department.absence_of_today = res_absence.get(department.id, 0)

Back to entity