'Testing leave request flow'

@mute_logger('odoo.addons.base.ir.ir_model', 'odoo.models')
def test_00_leave_request_flow(self):

    Holidays = self.env['hr.holidays']
    HolidaysStatus = self.env['hr.holidays.status']
    def _check_holidays_status(holiday_status, ml, lt, rl, vrl):
        self.assertEqual(holiday_status.max_leaves, ml, 'hr_holidays:    wrong    type    days    computation')
        self.assertEqual(holiday_status.leaves_taken, lt, 'hr_holidays:    wrong    type    days    computation')
        self.assertEqual(holiday_status.remaining_leaves, rl, 'hr_holidays:    wrong    type    days    computation')
        self.assertEqual(holiday_status.virtual_remaining_leaves, vrl, 'hr_holidays:    wrong    type    days    computation')
    with self.assertRaises(AccessError):
        HolidaysStatus.sudo(self.user_hruser_id).create({'name': 'UserCheats', 'limit': True})
    HolidayStatusManagerGroup = HolidaysStatus.sudo(self.user_hrmanager_id)
    HolidayStatusManagerGroup.create({'name': 'WithMeetingType', 'limit': True, 'categ_id': self.env['calendar.event.type'].sudo(self.user_hrmanager_id).create({'name': 'NotLimitedMeetingType'}).id})
    self.holidays_status_1 = HolidayStatusManagerGroup.create({'name': 'NotLimited', 'limit': True})
    self.holidays_status_2 = HolidayStatusManagerGroup.create({'name': 'Limited', 'limit': False, 'double_validation': True})
    HolidaysEmployeeGroup = Holidays.sudo(self.user_employee_id)
    with self.assertRaises(ValidationError):
        HolidaysEmployeeGroup.create({'name': 'Hol10', 'employee_id': self.employee_hruser_id, 'holiday_status_id': self.holidays_status_1.id, 'date_from': (datetime.today() - relativedelta(days=1)), 'date_to': datetime.today(), 'number_of_days_temp': 1})
    Holidays.search([('name', '=', 'Hol10')]).unlink()
    hol1_employee_group = HolidaysEmployeeGroup.create({'name': 'Hol11', 'employee_id': self.employee_emp_id, 'holiday_status_id': self.holidays_status_1.id, 'date_from': (datetime.today() - relativedelta(days=1)), 'date_to': datetime.today(), 'number_of_days_temp': 1})
    hol1_user_group = hol1_employee_group.sudo(self.user_hruser_id)
    self.assertEqual(hol1_user_group.state, 'confirm', 'hr_holidays:    newly    created    leave    request    should    be    in    confirm    state')
    with self.assertRaises(UserError):
        hol1_employee_group.action_approve()
    self.assertEqual(hol1_user_group.state, 'confirm', 'hr_holidays:    employee    should    not    be    able    to    validate    its    own    leave    request')
    hol1_user_group.action_approve()
    self.assertEqual(hol1_user_group.state, 'validate', 'hr_holidays:    validates    leave    request    should    be    in    validate    state')
    with self.assertRaises(ValidationError):
        HolidaysEmployeeGroup.create({'name': 'Hol21', 'employee_id': self.employee_emp_id, 'holiday_status_id': self.holidays_status_1.id, 'date_from': (datetime.today() - relativedelta(days=1)).strftime('%Y-%m-%d    %H:%M'), 'date_to': datetime.today(), 'number_of_days_temp': 1})
    with self.assertRaises(ValidationError):
        HolidaysEmployeeGroup.create({'name': 'Hol22', 'employee_id': self.employee_emp_id, 'holiday_status_id': self.holidays_status_2.id, 'date_from': (datetime.today() + relativedelta(days=0)).strftime('%Y-%m-%d    %H:%M'), 'date_to': (datetime.today() + relativedelta(days=1)), 'number_of_days_temp': 1})
    Holidays.search([('name', 'in', ['Hol21', 'Hol22'])]).unlink()
    aloc1_user_group = Holidays.sudo(self.user_hruser_id).create({'name': 'Days    for    limited    category', 'employee_id': self.employee_emp_id, 'holiday_status_id': self.holidays_status_2.id, 'type': 'add', 'number_of_days_temp': 2})
    aloc1_user_group.action_approve()
    aloc1_user_group.sudo(self.user_hrmanager_id).action_validate()
    hol_status_2_employee_group = self.holidays_status_2.sudo(self.user_employee_id)
    _check_holidays_status(hol_status_2_employee_group, 2.0, 0.0, 2.0, 2.0)
    hol2 = HolidaysEmployeeGroup.create({'name': 'Hol22', 'employee_id': self.employee_emp_id, 'holiday_status_id': self.holidays_status_2.id, 'date_from': (datetime.today() + relativedelta(days=2)).strftime('%Y-%m-%d    %H:%M'), 'date_to': (datetime.today() + relativedelta(days=3)), 'number_of_days_temp': 1})
    hol2_user_group = hol2.sudo(self.user_hruser_id)
    _check_holidays_status(hol_status_2_employee_group, 2.0, 0.0, 2.0, 1.0)
    hol2_user_group.action_approve()
    self.assertEqual(hol2.state, 'validate1', 'hr_holidays:    first    validation    should    lead    to    validate1    state')
    hol2_user_group.sudo(self.user_hrmanager_id).action_validate()
    self.assertEqual(hol2.state, 'validate', 'hr_holidays:    second    validation    should    lead    to    validate    state')
    _check_holidays_status(hol_status_2_employee_group, 2.0, 1.0, 1.0, 1.0)
    hol2.sudo(self.user_hrmanager_id).action_refuse()
    self.assertEqual(hol2.state, 'refuse', 'hr_holidays:    refuse    should    lead    to    refuse    state')
    _check_holidays_status(hol_status_2_employee_group, 2.0, 0.0, 2.0, 2.0)
    with self.assertRaises(UserError):
        hol2_user_group.action_draft()
    self.assertEqual(hol2.state, 'refuse', 'hr_holidays:    hr_user    should    not    be    able    to    reset    a    refused    leave    request')
    hol2_manager_group = hol2.sudo(self.user_hrmanager_id)
    hol2_manager_group.action_draft()
    self.assertEqual(hol2.state, 'draft', 'hr_holidays:    resetting    should    lead    to    draft    state')
    hol2_manager_group.write({'date_from': (datetime.today() + relativedelta(days=4)).strftime('%Y-%m-%d    %H:%M'), 'date_to': (datetime.today() + relativedelta(days=7)), 'number_of_days_temp': 4})
    with self.assertRaises(ValidationError):
        hol2_manager_group.action_confirm()
    employee_id = self.ref('hr.employee_root')
    hol3_status = self.env.ref('hr_holidays.holiday_status_cl').with_context(employee_id=employee_id)
    hol3 = Holidays.create({'name': 'Sick    Leave', 'holiday_status_id': hol3_status.id, 'date_from': datetime.today().strftime('%Y-%m-10    10:00:00'), 'date_to': datetime.today().strftime('%Y-%m-11    19:00:00'), 'employee_id': employee_id, 'type': 'remove', 'number_of_days_temp': 1})
    hol3.action_refuse()
    self.assertEqual(hol3.state, 'refuse', 'hr_holidays:    refuse    should    lead    to    refuse    state')
    hol3.action_draft()
    self.assertEqual(hol3.state, 'draft', 'hr_holidays:    resetting    should    lead    to    draft    state')
    hol3.action_confirm()
    self.assertEqual(hol3.state, 'confirm', 'hr_holidays:    confirming    should    lead    to    confirm    state')
    hol3.action_approve()
    self.assertEqual(hol3.state, 'validate', 'hr_holidays:    validation    should    lead    to    validate    state')
    _check_holidays_status(hol3_status, 20.0, 1.0, 19.0, 19.0)
