RENAME TABLE `ohrm_audittrail_pim_job_title_trail` TO `ohrm_audittrail_pim_job_record_trail`;
RENAME TABLE `ohrm_audittrail_pim_salary_trail` TO `ohrm_audittrail_pim_salary_record_trail`;
RENAME TABLE `ohrm_audittrail_pim_emp_directdebit_trail` TO `ohrm_audittrail_pim_direct_deposit_record_trail`;

UPDATE `ohrm_audittrail_section`  SET `name` = 'JobRecord'
  WHERE `name` = 'JobTitle';

UPDATE `ohrm_audittrail_section`  SET `name` = 'SalaryRecord'
  WHERE `name` = 'Salary';

INSERT INTO `ohrm_audittrail_section` (`name`, `description`, `affected_entity`, `module_id`) VALUES ('DirectDepositRecord', 'Direct Deposit', 'Employee', '1');

UPDATE `ohrm_audittrail_section_actions` SET `action` = 'UPDATE JOB RECORD', `description` = 'Update Job Record'
  WHERE `action` = 'UPDATE JOB DETAILS' LIMIT 1;

INSERT INTO `ohrm_audittrail_section_actions` VALUES (2, 'DELETE JOB HISTORY', 'Delete Job History');

DELETE FROM `ohrm_audittrail_section_actions`
  WHERE `action` = 'ADD SALARY DETAILS';

INSERT INTO `ohrm_audittrail_section_actions` (`section_id`, `action`, `description`) VALUES
(3, 'ADD SALARY RECORD', 'Add Salary Record');

UPDATE `ohrm_audittrail_section_actions` SET `action` = 'UPDATE SALARY RECORD', `description` = 'Update Salary Record'
  WHERE `action` = 'UPDATE SALARY DETAILS' LIMIT 1;

UPDATE `ohrm_audittrail_section_actions` SET `action` = 'DELETE SALARY HISTORY', `description` = 'Delete Salary History'
  WHERE `action` = 'DELETE SALARY DETAILS' LIMIT 1;

SET @direct_deposit_section := (SELECT id FROM ohrm_audittrail_section WHERE name = 'DirectDepositRecord');

UPDATE `ohrm_audittrail_section_actions` SET `section_id` = @direct_deposit_section
  WHERE `action` IN ('ADD DIRECT DEPOSIT DETAILS', 'UPDATE DIRECT DEPOSIT DETAILS', 'DELETE DIRECT DEPOSIT DETAILS');

DROP TRIGGER IF EXISTS `watch_insert_hs_hr_emp_directdebit`;
DROP PROCEDURE IF EXISTS `audit_insert_hs_hr_emp_directdebit`;

DROP TRIGGER IF EXISTS `watch_update_hs_hr_emp_directdebit`;
DROP PROCEDURE IF EXISTS `audit_update_hs_hr_emp_directdebit`;

DROP TRIGGER IF EXISTS `watch_delete_hs_hr_emp_directdebit`;
DROP PROCEDURE IF EXISTS `audit_delete_hs_hr_emp_directdebit`;

DROP VIEW IF EXISTS `ohrm_audittrail_all_tables`;

CREATE VIEW `ohrm_audittrail_all_tables` AS 
(SELECT 'Contact Information' AS SECTION, pci.* FROM `ohrm_audittrail_pim_contact_info_trail` pci) 
UNION (SELECT 'Dependent' AS SECTION, pdt.* FROM `ohrm_audittrail_pim_dependents_trail` pdt) 
UNION (SELECT 'Direct Deposit' AS SECTION, pddt.* FROM `ohrm_audittrail_pim_direct_deposit_record_trail` pddt) 
UNION (SELECT 'Emergency Contacts' AS SECTION, ect.* FROM `ohrm_audittrail_pim_emergency_contacts_trail` ect) 
UNION (SELECT 'Membership' AS SECTION, emd.* FROM `ohrm_audittrail_pim_emp_member_detail_trail` emd) 
UNION (SELECT 'Job Details' AS SECTION, jtt.* FROM `ohrm_audittrail_pim_job_record_trail` jtt) 
UNION (SELECT 'Immigration' AS SECTION, ppt.* FROM `ohrm_audittrail_pim_passport_trail` ppt) 
UNION (SELECT 'Personal Details' AS SECTION, ppd.* FROM `ohrm_audittrail_pim_personal_details_trail` ppd) 
UNION (SELECT 'Qualification' AS SECTION, pqt.* FROM `ohrm_audittrail_pim_qualification_trail` pqt) 
UNION (SELECT 'Supervisor/Subordinate' AS SECTION, prt.* FROM `ohrm_audittrail_pim_reportto_trail` prt) 
UNION (SELECT 'Salary' AS SECTION, pst.* FROM `ohrm_audittrail_pim_salary_record_trail` pst) 
UNION (SELECT 'Tax Exemptions' AS SECTION, put.* FROM `ohrm_audittrail_pim_us_tax_trail` put)
UNION (SELECT 'Employee' AS SECTION, pet.* FROM `ohrm_audittrail_pim_employee_trail` pet);
