#leave related changes
SET @admin_user_role_id := (SELECT `id` FROM ohrm_user_role WHERE `name` = 'Admin');
SET @leave_types_screen_id := (SELECT `id` FROM ohrm_screen WHERE `name` = 'Leave Type List');
SET @define_workweek_screen_id := (SELECT `id` FROM ohrm_screen WHERE `name` = 'Define WorkWeek');
SET @define_leave_period_screen_id := (SELECT `id` FROM ohrm_screen WHERE `name` = 'Define Leave Period');
SET @undelete_leave_type_screen_id := (SELECT `id` FROM ohrm_screen WHERE `name` = 'Undelete Leave Type');
SET @delete_leave_type_screen_id := (SELECT `id` FROM ohrm_screen WHERE `name` = 'Delete Leave Type');
SET @leave_module_id := (SELECT `id` FROM ohrm_module WHERE `name` = 'leave');
UPDATE ohrm_user_role_screen SET  can_read = 1, can_create = 0, can_update = 0, can_delete = 0 WHERE user_role_id=@admin_user_role_id 
    AND (screen_id=@leave_types_screen_id 
        OR screen_id=@define_workweek_screen_id 
        OR screen_id=@define_leave_period_screen_id);
DELETE FROM ohrm_user_role_screen WHERE user_role_id=@admin_user_role_id AND screen_id=@undelete_leave_type_screen_id OR screen_id=@delete_leave_type_screen_id;

UPDATE ohrm_data_group SET  can_read = 1, can_create = 0, can_update = 0, can_delete = 0 WHERE name='leave_period' OR name='leave_types' OR name='work_week';

SET @leave_period_data_group_id := (SELECT id FROM `ohrm_data_group` WHERE `name` = 'leave_period');
SET @leave_types_data_group_id := (SELECT id FROM `ohrm_data_group` WHERE `name` = 'leave_types');
SET @work_week_data_group_id := (SELECT id FROM `ohrm_data_group` WHERE `name` = 'work_week');
UPDATE ohrm_user_role_data_group SET  can_read = 1, can_create = 0, can_update = 0, can_delete = NULL, self = 0 WHERE user_role_id IN (SELECT `id` FROM ohrm_user_role WHERE `type_id` = 1) AND data_group_id=@leave_period_data_group_id;
UPDATE ohrm_user_role_data_group SET  can_read = 1, can_create = 0, can_update = 0, can_delete = 0, self = 0 WHERE user_role_id IN (SELECT `id` FROM ohrm_user_role WHERE `type_id` = 1) 
    AND (data_group_id=@work_week_data_group_id OR data_group_id=@leave_types_data_group_id);

DELETE FROM ohrm_module_default_page WHERE `module_id`= @leave_module_id AND `user_role_id`=@admin_user_role_id AND `action`='leave/defineLeavePeriod';
INSERT INTO ohrm_module_default_page (`module_id`, `user_role_id`, `action`, `enable_class`, `priority`) VALUES 
    (@leave_module_id, @admin_user_role_id, 'leave/showLeavePeriodNotDefinedWarning', 'LeavePeriodDefinedHomePageEnabler', 100);

SET @leaveTypeListAdvanced_screenId := (SELECT `id` FROM ohrm_screen WHERE `action_url` = 'leaveTypeListAdvanced');
SET @defineLeaveTypeAdvanced_screenId := (SELECT `id` FROM ohrm_screen WHERE `action_url` = 'defineLeaveTypeAdvanced');
SET @defineLeaveTypeRule_screenId := (SELECT `id` FROM ohrm_screen WHERE `action_url` = 'defineLeaveTypeRule');
SET @defineLeaveTypeRuleWhoCanApply_screenId := (SELECT `id` FROM ohrm_screen WHERE `action_url` = 'defineLeaveTypeRuleWhoCanApply');
SET @defineLeaveTypeRuleApplyRestriction_screenId := (SELECT `id` FROM ohrm_screen WHERE `action_url` = 'defineLeaveTypeRuleApplyRestriction');
SET @defineLeaveTypeRuleAccrualRule_screenId := (SELECT `id` FROM ohrm_screen WHERE `action_url` = 'defineLeaveTypeRuleAccrualRule');
SET @defineLeaveTypeCarryForwardRule_screenId := (SELECT `id` FROM ohrm_screen WHERE `action_url` = 'defineLeaveTypeCarryForwardRule');
SET @defineWorkWeekAdvanced_screenId := (SELECT `id` FROM ohrm_screen WHERE `action_url` = 'defineWorkWeekAdvanced');

UPDATE ohrm_user_role_screen SET  can_read=1, can_create=0, can_update=0, can_delete=0 WHERE user_role_id=@admin_user_role_id AND 
    (screen_id = @leaveTypeListAdvanced_screenId
    OR screen_id = @defineLeaveTypeAdvanced_screenId
    OR screen_id = @defineLeaveTypeRule_screenId
    OR screen_id = @defineLeaveTypeRuleWhoCanApply_screenId
    OR screen_id = @defineLeaveTypeRuleApplyRestriction_screenId
    OR screen_id = @defineLeaveTypeRuleAccrualRule_screenId
    OR screen_id = @defineLeaveTypeCarryForwardRule_screenId
    OR screen_id = @defineWorkWeekAdvanced_screenId);

#authentication related changes
UPDATE ohrm_data_group SET  can_read = 1, can_create = 0, can_update = 0, can_delete = 0 WHERE name='security_authentication_configuration';
SET @security_configuration_data_group_id := (SELECT `id` FROM ohrm_data_group WHERE `name`='security_authentication_configuration');
UPDATE ohrm_user_role_data_group SET  can_read = 1, can_create = 0, can_update = 0, can_delete = 0, self = 0 WHERE user_role_id IN (SELECT `id` FROM ohrm_user_role WHERE `type_id` = 1) 
    AND data_group_id=@security_configuration_data_group_id;

SET @ldap_configuration_screen_id := (SELECT `id` FROM ohrm_screen WHERE `name` = 'LDAP Configuration');
UPDATE ohrm_user_role_screen SET  can_read=1, can_create=0, can_update=0, can_delete=0 WHERE user_role_id=@admin_user_role_id AND screen_id = @ldap_configuration_screen_id;
UPDATE ohrm_data_group SET  can_read = 1, can_create = 0, can_update = 0, can_delete = 0 WHERE name='ldap_configuration';
SET @ldap_configuration_data_group_id := (SELECT `id` FROM ohrm_data_group WHERE `name`='ldap_configuration');
UPDATE ohrm_user_role_data_group SET  can_read = 1, can_create = 0, can_update = 0, can_delete = 0, self = 0 WHERE user_role_id IN (SELECT `id` FROM ohrm_user_role WHERE `type_id` = 1)
    AND data_group_id=@ldap_configuration_data_group_id;


