SET @sys_admin_role_id := (SELECT `id` FROM ohrm_user_role WHERE `name` = 'ConfigAdmin');

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

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

INSERT INTO ohrm_screen (`name`, `module_id`, `action_url`, `route_name`) VALUES
('TestEmulator', @module_id, 'viewCronTasks', 'managementtool_notship_viewCronTasks');

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

INSERT INTO ohrm_screen (`name`, `module_id`, `action_url`) VALUES
('TestEmulatorExecute', @module_id, 'execute');

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

INSERT INTO ohrm_screen (`name`, `module_id`, `action_url`) VALUES
('TestEmulatorHistory', @module_id, 'cronHistory');

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

INSERT INTO ohrm_screen (`name`, `module_id`, `action_url`) VALUES
('TestEmulatorOutput', @module_id, 'cronOutput');

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

INSERT INTO ohrm_screen (`name`, `module_id`, `action_url`) VALUES
  ('Scheduled Tasks', @module_id, 'viewScheduledCronTasks');

SET @viewScheduledCronTasks_screen_id  := (SELECT `id` FROM `ohrm_screen`WHERE `action_url` = 'viewScheduledCronTasks');


SET @admin_menu_id := (SELECT `id` FROM ohrm_menu_item WHERE `menu_title` = 'Admin' AND `level` = 1);


SET @system_administration_menu_id = (SELECT `id` FROM ohrm_menu_item WHERE `menu_title` = 'System Administration' AND parent_id IS NULL AND level = 1);
SET @management_tools_menu_id = (SELECT `id` FROM ohrm_menu_item WHERE `menu_title` = 'Management Tools' AND `parent_id` = @system_administration_menu_id);

INSERT INTO ohrm_menu_item (`menu_title`, `screen_id`, `parent_id`, `level`, `order_hint`, `url_extras`, `status`) VALUES
('Executable Tasks', @TestEmulator_screen_id, @management_tools_menu_id, 3, 1000, '', 1);

INSERT INTO ohrm_menu_item (`menu_title`, `screen_id`, `parent_id`, `level`, `order_hint`, `url_extras`, `status`) VALUES
('Scheduled Tasks', @viewScheduledCronTasks_screen_id, @management_tools_menu_id, 3, 1100, '', 1);


INSERT INTO ohrm_user_role_screen (user_role_id, screen_id, can_read, can_create, can_update, can_delete) VALUES
(@sys_admin_role_id, @TestEmulator_screen_id, 1, 0, 0, 0),
(@sys_admin_role_id, @TestEmulatorExecute_screen_id, 1, 0, 0, 0),
(@sys_admin_role_id, @TestEmulatorHistory_screen_id, 1, 0, 0, 0),
(@sys_admin_role_id, @TestEmulatorOutput_screen_id, 1, 0, 0, 0),
(@sys_admin_role_id, @viewScheduledCronTasks_screen_id, 1, 0, 1, 0);


INSERT INTO `ohrm_data_group` (`name`, `description`, `can_read`, `can_create`, `can_update`, `can_delete`) VALUES
('test_emulator', 'HR Administration - Test Emulator', 1, 0, 1, 0);
SET @test_emulator_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
(@sys_admin_role_id , @test_emulator_data_group_id, 1, 0, 1, 0, 0);

INSERT INTO `ohrm_data_group_screen`(`data_group_id`, `screen_id`, `permission`) VALUES
(@test_emulator_data_group_id,@TestEmulator_screen_id,1),
(@test_emulator_data_group_id,@TestEmulator_screen_id,2),
(@test_emulator_data_group_id,@TestEmulator_screen_id,3),
(@test_emulator_data_group_id,@TestEmulator_screen_id,4),
(@test_emulator_data_group_id,@TestEmulatorExecute_screen_id,1),
(@test_emulator_data_group_id,@TestEmulatorExecute_screen_id,2),
(@test_emulator_data_group_id,@TestEmulatorExecute_screen_id,3),
(@test_emulator_data_group_id,@TestEmulatorExecute_screen_id,4),
(@test_emulator_data_group_id,@TestEmulatorHistory_screen_id,1),
(@test_emulator_data_group_id,@TestEmulatorHistory_screen_id,2),
(@test_emulator_data_group_id,@TestEmulatorHistory_screen_id,3),
(@test_emulator_data_group_id,@TestEmulatorHistory_screen_id,4),
(@test_emulator_data_group_id,@TestEmulatorOutput_screen_id,1),
(@test_emulator_data_group_id,@TestEmulatorOutput_screen_id,2),
(@test_emulator_data_group_id,@TestEmulatorOutput_screen_id,3),
(@test_emulator_data_group_id,@TestEmulatorOutput_screen_id,4),
(@test_emulator_data_group_id, @viewScheduledCronTasks_screen_id, 1),
(@test_emulator_data_group_id, @viewScheduledCronTasks_screen_id, 2),
(@test_emulator_data_group_id, @viewScheduledCronTasks_screen_id, 3),
(@test_emulator_data_group_id, @viewScheduledCronTasks_screen_id, 4);



INSERT INTO ohrm_module_default_page (`module_id`, `user_role_id`, `action`, `enable_class`, `priority`) VALUES
(@module_id, @sys_admin_role_id, 'managementtool_notship/viewCronTasks', NULL, 100);

UPDATE `ohrm_menu_item` SET `parent_id`= @management_tools_menu_id,`level`= 3 WHERE `menu_title` = 'Bulk Data Upload';
UPDATE `ohrm_menu_item` SET `parent_id`= @management_tools_menu_id,`level`= 3 WHERE `menu_title` = 'License Update';


SET @admin_module_id := (SELECT `id` FROM `ohrm_audittrail_module` WHERE `name`='HR Administration');

CREATE TABLE `ohrm_audittrail_management_tools` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `action_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `action_owner_id` varchar(6),
  `action_owner_name` VARCHAR(300) NOT NULL,
  `version_id` INT NOT NULL DEFAULT 0,
  `action` varchar(255) NOT NULL,
  `affected_entity_id` VARCHAR(20),
  `affected_entity_name` TEXT,
  `action_description` TEXT CHARSET UTF8,
  `is_description_formatting_enabled` BOOLEAN NOT NULL DEFAULT FALSE ,
  `app_id` INT DEFAULT NULL,
  `app_name` VARCHAR(1000) DEFAULT NULL,
  `screen_name` VARCHAR(1000) DEFAULT NULL,
  PRIMARY KEY (`id`),
  INDEX audit_management_tools_idx_app_id (app_id),
  INDEX audit_management_tools_idx_screen_name (screen_name)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `ohrm_audittrail_module` (`name`) VALUES ('Management Tools');
SET @management_tools_audittrail_module_id := (SELECT `id` FROM `ohrm_audittrail_module` WHERE `name` = 'Management Tools');

INSERT INTO `ohrm_audittrail_section` (`name`, `description`, `affected_entity`, `module_id`) VALUES
('ManagementTools', 'Management Tools', '', @management_tools_audittrail_module_id);

SET @management_tools_section_id := (SELECT `id` FROM `ohrm_audittrail_section` WHERE `name`='ManagementTools');
INSERT INTO `ohrm_audittrail_section_actions` (`section_id` ,`action` ,`description`) VALUES
(@management_tools_section_id, 'UPDATE COUNTRY LEAVE ACCRUAL SCHEDULE', 'Update Country Leave Accrual Schedule');

INSERT INTO `ohrm_event_listener` (`event`, `listener_class`, `enabled`, `position`) VALUES
('admin.leave_accrual_time_schedule_change','LeaveAccrualTimeScheduleChangeListener', 1, 61);
