ALTER TABLE `ohrm_organization_gen_info` ADD COLUMN `cost_center_enabled` TINYINT(1) NOT NULL DEFAULT 0;

CREATE TABLE  IF NOT EXISTS ohrm_cost_centre (
  `id` BIGINT AUTO_INCREMENT,
  `cost_code` VARCHAR(20) NOT NULL,
  `name` VARCHAR(40) NOT NULL,
  `description` VARCHAR(255),
  `is_deleted` tinyint(1) default 0,
  PRIMARY KEY(id)
) ENGINE = INNODB;

ALTER TABLE `ohrm_customer`  ADD `cost_centre_id` BIGINT NULL DEFAULT NULL AFTER `description`;
ALTER TABLE `ohrm_project`  ADD `cost_centre_id` BIGINT NULL DEFAULT NULL AFTER `description`;
ALTER TABLE `ohrm_subunit`  ADD `cost_centre_id` BIGINT NULL DEFAULT NULL AFTER `description`;
ALTER TABLE `ohrm_organization_gen_info`  ADD `cost_centre_id` BIGINT NULL DEFAULT NULL AFTER `registration_number`;

ALTER TABLE `ohrm_job_record`  ADD `cost_centre_id` BIGINT NULL DEFAULT NULL AFTER `contracted_hours`;

ALTER TABLE `ohrm_job_record`
ADD CONSTRAINT `ohrm_job_record_ohrm_cost_centre` FOREIGN KEY (`cost_centre_id`)
REFERENCES `ohrm_cost_centre`(`id`) ON DELETE SET NULL;

ALTER TABLE `ohrm_job_record_history`  ADD `cost_centre_id` BIGINT NULL DEFAULT NULL AFTER `deleted`;
ALTER TABLE `ohrm_job_history_record`  ADD `cost_centre_id` BIGINT NULL DEFAULT NULL AFTER `contracted_hours`;

ALTER TABLE `ohrm_customer` add constraint `fk_ohrm_customer_ohrm_cost_centre` FOREIGN KEY (`cost_centre_id`) REFERENCES `ohrm_cost_centre` (`id`) ON DELETE SET NULL;
ALTER TABLE `ohrm_project` add constraint `fk_ohrm_project_ohrm_cost_centre` FOREIGN KEY (`cost_centre_id`) REFERENCES `ohrm_cost_centre` (`id`) ON DELETE SET NULL;
ALTER TABLE `ohrm_subunit` add constraint `fk_ohrm_subunit_ohrm_cost_centre` FOREIGN KEY (`cost_centre_id`) REFERENCES `ohrm_cost_centre` (`id`) ON DELETE SET NULL;
ALTER TABLE `ohrm_organization_gen_info` add constraint `fk_ohrm_organization_gen_info_ohrm_cost_centre` FOREIGN KEY (`cost_centre_id`) REFERENCES `ohrm_cost_centre` (`id`) ON DELETE SET NULL;

SET @module_id := (SELECT `id` FROM `ohrm_module` WHERE `name` = 'admin');
INSERT INTO `ohrm_screen` (`name`, `module_id`, `action_url`) VALUES
('Cost Centers', @module_id, 'viewCostCentreList');

SET @cost_centre_screen_id := (SELECT LAST_INSERT_ID());
SET @organization_menu_item_id := (SELECT `id` FROM `ohrm_menu_item` WHERE `menu_title`='Organization');
INSERT INTO `ohrm_menu_item` (`menu_title`, `screen_id`, `parent_id`, `level`, `order_hint`, `url_extras`, `uisref`, `status`) VALUES
('Cost Centers', @cost_centre_screen_id, @organization_menu_item_id, 3, 400, NULL, 'admin.costCentres', 0);

SET @admin_user_role_id := (SELECT `id` FROM `ohrm_user_role` WHERE `name`='Admin');
INSERT INTO `ohrm_user_role_screen` (`user_role_id`, `screen_id`, `can_read`, `can_create`, `can_update`, `can_delete`) VALUES
(@admin_user_role_id, @cost_centre_screen_id, 1, 1, 1, 1);

INSERT INTO `ohrm_data_group` ( `name`, `description`, `can_read`, `can_create`, `can_update`, `can_delete`) VALUES
( 'admin_cost_centres', 'Admin - Cost Centers', 1, 1, 1, 1);
SET @cost_centre_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_user_role_id, @cost_centre_data_group_id, 1, 1, 1, 1, 0);

INSERT INTO `ohrm_token`(`name`, `display_name`, `class_name`, `entity_name`, `value_getters`, `data_group_id`, `is_date`, `localize`,`parameters`) VALUES
('directory.employeeCostCentre','Employee Cost Center',null,'Employee','getJobRecord,getCostCentre,getCostCodeWithName',null,0,0,null);
SET @lastTokenId := (SELECT LAST_INSERT_ID());
SET @module_id := (SELECT `id` FROM `ohrm_module` WHERE `name` = 'directory');

INSERT INTO `ohrm_restricted_module_token`(`token_id`,`restricted_module`)VALUES
(@lastTokenId,@module_id);