SET @admin_role_id := (SELECT `id` FROM ohrm_user_role WHERE `name` = 'Admin');
SET @supervisor_role_id := (SELECT `id` FROM ohrm_user_role WHERE `name` = 'Supervisor');
SET @ess_role_id := (SELECT `id` FROM ohrm_user_role WHERE `name` = 'ESS');

-- INSERT INTO `ohrm_module` (`name`, `status`) VALUES ('csvExport', 1);

SET @module_id := (SELECT `id` FROM `ohrm_module` WHERE `name` = 'time');

SET @hod_id := (SELECT `id` FROM `ohrm_user_role` WHERE `name` = 'HeadOfDepartment');
SET @supervisor_user_role_id := 3;

INSERT INTO ohrm_screen (`name`, `module_id`, `action_url`, `route_name`) VALUES
('Timesheet Csv Extract', @module_id, 'viewTimesheetCsvExtract', 'time_viewTimesheetCsvExtract');

SET @timesheet_csv_screen_id := (SELECT LAST_INSERT_ID());

INSERT INTO ohrm_user_role_screen (user_role_id, screen_id, can_read, can_create, can_update, can_delete) VALUES
(@admin_role_id, @timesheet_csv_screen_id, 1, 1, 1, 0),
(@supervisor_role_id, @timesheet_csv_screen_id, 1, 1, 1, 0),
(@hod_id, @timesheet_csv_screen_id, 1, 1, 1, 0);

INSERT INTO `ohrm_config` (`property` ,`value`) VALUES ('timesheetcsv.can_filter_by_timesheet_status', 'Yes'), ('timesheetcsv.grouping_level', '2');

INSERT INTO `ohrm_data_group` (`name`, `description`, `can_read`, `can_create`, `can_update`, `can_delete`) VALUES
('time_export_to_csv', 'Time Tracking - Export Timesheet Data', 1, 1, 1, 0);

SET @time_export_to_csv_data_group_id := (SELECT LAST_INSERT_ID());

INSERT INTO `ohrm_user_role_data_group` (`user_role_id`, `data_group_id`, `can_read`, `can_create`, `can_update`, `can_delete`, `self`) VALUES
(@admin_role_id, @time_export_to_csv_data_group_id, 1, 1, 1, 0, 0),
(@supervisor_role_id, @time_export_to_csv_data_group_id, 1, 1, 0, 0, 0),
(@ess_role_id, @time_export_to_csv_data_group_id, 1, 0, 0, 0, 1);

SET @view_time_module_screen_id := (SELECT `id` FROM ohrm_screen WHERE `action_url` = 'viewTimeModule');

INSERT INTO `ohrm_data_group_screen`(`data_group_id`, `screen_id`, `permission`) VALUES
(@time_export_to_csv_data_group_id, @timesheet_csv_screen_id, 1),
(@time_export_to_csv_data_group_id, @view_time_module_screen_id, 1);

INSERT INTO ohrm_user_role_type_data_group(user_role_type_id, data_group_id) VALUES
(1, @time_export_to_csv_data_group_id);

INSERT INTO ohrm_user_role_type_data_group (user_role_type_id, data_group_id, can_read, can_create, can_update, can_delete) VALUES
    (2, @time_export_to_csv_data_group_id, 1, 0, 0, 0),
    (3, @time_export_to_csv_data_group_id, 1, 1, 0, 0);

-- HOD user role permission

insert into ohrm_user_role_data_group(user_role_id,data_group_id,can_read,can_create,can_update,can_delete,self)
select @hod_id,data_group_id,can_read,can_create,can_update,can_delete,self from ohrm_user_role_data_group where user_role_id=@supervisor_user_role_id and data_group_id in (SELECT id FROM ohrm_data_group WHERE name in
('time_export_to_csv'));
