
DROP PROCEDURE IF EXISTS `audit_insert_to_ohrm_audittrail_all`;$$

CREATE PROCEDURE audit_insert_to_ohrm_audittrail_all
  (
    IN action_time DATETIME,
    IN action_owner_id VARCHAR(6) CHARSET UTF8,
    IN action_owner_name TEXT CHARSET UTF8,
    IN action TEXT CHARSET UTF8,
    IN action_description TEXT CHARSET UTF8,
    IN version_id BIGINT,
    IN affected_entity_id VARCHAR(20) CHARSET UTF8,
    IN affected_entity_name TEXT CHARSET UTF8,
    IN section TEXT CHARSET UTF8
  )
  BEGIN
    INSERT INTO `ohrm_audittrail_all` (action_time, action_owner_id, action_owner_name,action,action_description,version_id,affected_entity_id,affected_entity_name,section) VALUES (
      action_time,
      action_owner_id,
      action_owner_name,
      action,
      action_description,
      version_id,
      affected_entity_id,
      affected_entity_name,
      section
    );

  END;$$

DELIMITER ;


DELIMITER $$

DROP TRIGGER IF EXISTS `watch_insert_hs_hr_emp_attachment`;$$
CREATE TRIGGER watch_insert_hs_hr_emp_attachment AFTER  INSERT
ON  hs_hr_emp_attachment
FOR EACH ROW BEGIN
    DECLARE orangehrm_user_name VARCHAR(300) CHARSET UTF8;
    DECLARE affected_entity_name VARCHAR(300 ) CHARSET UTF8;
    DECLARE action_description VARCHAR(500 ) CHARSET UTF8;
    DECLARE new_version INT;
    SET new_version = (SELECT MAX(`version_id`) FROM `ohrm_audittrail_pim_job_record_trail` WHERE `affected_entity_id` = NEW.emp_number) + 1;
    SET new_version = IFNULL(new_version, 1);
    SET orangehrm_user_name=(SELECT IFNULL(CONCAT(hs_hr_employee.emp_firstname, ' ' , hs_hr_employee.emp_middle_name ,' ', hs_hr_employee.emp_lastname),ohrm_user.user_name) FROM ohrm_user LEFT JOIN  hs_hr_employee ON hs_hr_employee.emp_number = ohrm_user.emp_number WHERE ohrm_user.id = @orangehrm_user );
    SET orangehrm_user_name = IFNULL(orangehrm_user_name, 'admin');
    SET affected_entity_name=(SELECT CONCAT(hs_hr_employee.emp_firstname, ' ' , hs_hr_employee.emp_middle_name ,' ', hs_hr_employee.emp_lastname) FROM hs_hr_employee WHERE hs_hr_employee.emp_number = NEW.emp_number LIMIT 1);
    SET action_description = CONCAT_WS('','Contract attachment "' , NEW.eattach_filename , '" added.');

    IF NEW.screen ='contract'  THEN

    INSERT INTO `ohrm_audittrail_pim_job_record_trail` (`action_time`,`action_owner_id`,`action_owner_name`,`version_id`,`action`,`affected_entity_id`,`affected_entity_name`,`action_description`) VALUES (
      CURRENT_TIMESTAMP,
      @orangehrm_user,
      orangehrm_user_name,
      new_version,
      'UPDATE JOB RECORD',
      NEW.emp_number,
      affected_entity_name,
      action_description
    );

  END IF;
END;$$

DROP TRIGGER IF EXISTS `watch_update_hs_hr_emp_attachment`;$$
CREATE TRIGGER watch_update_hs_hr_emp_attachment AFTER  UPDATE
ON  hs_hr_emp_attachment
FOR EACH ROW BEGIN
    DECLARE orangehrm_user_name VARCHAR(300) CHARSET UTF8;
    DECLARE affected_entity_name VARCHAR(300 ) CHARSET UTF8;
    DECLARE action_description VARCHAR(500 ) CHARSET UTF8;
    DECLARE new_version INT;
    SET new_version = (SELECT MAX(`version_id`) FROM `ohrm_audittrail_pim_job_record_trail` WHERE `affected_entity_id` = NEW.emp_number) + 1;
    SET new_version = IFNULL(new_version, 1);
    SET orangehrm_user_name=(SELECT IFNULL(CONCAT(hs_hr_employee.emp_firstname, ' ' , hs_hr_employee.emp_middle_name ,' ', hs_hr_employee.emp_lastname),ohrm_user.user_name) FROM ohrm_user LEFT JOIN  hs_hr_employee ON hs_hr_employee.emp_number = ohrm_user.emp_number WHERE ohrm_user.id = @orangehrm_user );
    SET orangehrm_user_name = IFNULL(orangehrm_user_name, 'admin');
    SET affected_entity_name=(SELECT CONCAT(hs_hr_employee.emp_firstname, ' ' , hs_hr_employee.emp_middle_name ,' ', hs_hr_employee.emp_lastname) FROM hs_hr_employee WHERE hs_hr_employee.emp_number = NEW.emp_number LIMIT 1);
    SET action_description = CONCAT_WS('','Contract attachment "' , OLD.eattach_filename , '" updated to "' , NEW.eattach_filename , '".');

    IF NEW.screen ='contract'  THEN

    INSERT INTO `ohrm_audittrail_pim_job_record_trail` (`action_time`,`action_owner_id`,`action_owner_name`,`version_id`,`action`,`affected_entity_id`,`affected_entity_name`,`action_description`) VALUES (
      CURRENT_TIMESTAMP,
      @orangehrm_user,
      orangehrm_user_name,
      new_version,
      'UPDATE JOB RECORD',
      NEW.emp_number,
      affected_entity_name,
      action_description
    );

    END IF;
END;$$

DROP TRIGGER IF EXISTS `watch_delete_hs_hr_emp_attachment`;$$
CREATE TRIGGER watch_delete_hs_hr_emp_attachment AFTER  DELETE
ON  hs_hr_emp_attachment
FOR EACH ROW BEGIN
    DECLARE orangehrm_user_name VARCHAR(300) CHARSET UTF8;
    DECLARE affected_entity_name VARCHAR(300 ) CHARSET UTF8;
    DECLARE action_description VARCHAR(500 ) CHARSET UTF8;
    DECLARE new_version INT;
    SET new_version = (SELECT MAX(`version_id`) FROM `ohrm_audittrail_pim_job_record_trail` WHERE `affected_entity_id` = OLD.emp_number) + 1;
    SET new_version = IFNULL(new_version, 1);
    SET orangehrm_user_name=(SELECT IFNULL(CONCAT(hs_hr_employee.emp_firstname, ' ' , hs_hr_employee.emp_middle_name ,' ', hs_hr_employee.emp_lastname),ohrm_user.user_name) FROM ohrm_user LEFT JOIN  hs_hr_employee ON hs_hr_employee.emp_number = ohrm_user.emp_number WHERE ohrm_user.id = @orangehrm_user );
    SET orangehrm_user_name = IFNULL(orangehrm_user_name, 'admin');
    SET affected_entity_name=(SELECT CONCAT(hs_hr_employee.emp_firstname, ' ' , hs_hr_employee.emp_middle_name ,' ', hs_hr_employee.emp_lastname) FROM hs_hr_employee WHERE hs_hr_employee.emp_number = OLD.emp_number LIMIT 1);
    SET action_description = CONCAT_WS('','Contract attachment "' , OLD.eattach_filename , '" deleted.');

    IF OLD.screen ='contract'  THEN

    INSERT INTO `ohrm_audittrail_pim_job_record_trail` (`action_time`,`action_owner_id`,`action_owner_name`,`version_id`,`action`,`affected_entity_id`,`affected_entity_name`,`action_description`) VALUES (
      CURRENT_TIMESTAMP,
      @orangehrm_user,
      orangehrm_user_name,
      new_version,
      'UPDATE JOB RECORD',
      OLD.emp_number,
      affected_entity_name,
      action_description
    );

    END IF;
END;$$


DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_contact_info_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_contact_info_trail
AFTER INSERT ON ohrm_audittrail_pim_contact_info_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Contact Information');
END;$$


DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_dependents_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_dependents_trail
AFTER INSERT ON ohrm_audittrail_pim_dependents_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Dependent');
END;$$


DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_emergency_contacts_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_emergency_contacts_trail
AFTER INSERT ON ohrm_audittrail_pim_emergency_contacts_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Emergency Contacts');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_emp_member_detail_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_emp_member_detail_trail
AFTER INSERT ON ohrm_audittrail_pim_emp_member_detail_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Membership');
END;$$


DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_job_record_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_job_record_trail
AFTER INSERT ON ohrm_audittrail_pim_job_record_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Job Details');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_passport_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_passport_trail
AFTER INSERT ON ohrm_audittrail_pim_passport_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Immigration');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_personal_details_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_personal_details_trail
AFTER INSERT ON ohrm_audittrail_pim_personal_details_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Personal Details');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_qualification_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_qualification_trail
AFTER INSERT ON ohrm_audittrail_pim_qualification_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Qualification');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_reportto_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_reportto_trail
AFTER INSERT ON ohrm_audittrail_pim_reportto_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Supervisor/Subordinate');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_us_tax_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_us_tax_trail
AFTER INSERT ON ohrm_audittrail_pim_us_tax_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Tax Exemptions');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_employee_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_employee_trail
AFTER INSERT ON ohrm_audittrail_pim_employee_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Employee');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_tab_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_tab_trail
AFTER INSERT ON ohrm_audittrail_pim_tab_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'PIM Tab');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_custom_field_section_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_custom_field_section_trail
AFTER INSERT ON ohrm_audittrail_pim_custom_field_section_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Custom Field Section');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_custom_field_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_custom_field_trail
AFTER INSERT ON ohrm_audittrail_pim_custom_field_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Custom Field');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_personal_custom_field_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_personal_custom_field_trail
AFTER INSERT ON ohrm_audittrail_pim_personal_custom_field_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Personal Details Custom Fields');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_emergency_custom_field_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_emergency_custom_field_trail
AFTER INSERT ON ohrm_audittrail_pim_emergency_custom_field_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Emergency Custom Fields');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_contact_custom_field_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_contact_custom_field_trail
AFTER INSERT ON ohrm_audittrail_pim_contact_custom_field_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Contact Custom Fields');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_dependents_custom_field_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_dependents_custom_field_trail
AFTER INSERT ON ohrm_audittrail_pim_dependents_custom_field_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Dependent Custom Fields');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_immigration_custom_field_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_immigration_custom_field_trail
AFTER INSERT ON ohrm_audittrail_pim_immigration_custom_field_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Immigration Custom Fields');
END;$$


DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_job_custom_field_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_job_custom_field_trail
AFTER INSERT ON ohrm_audittrail_pim_job_custom_field_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Job Custom Fields');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_salary_custom_field_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_salary_custom_field_trail
AFTER INSERT ON ohrm_audittrail_pim_salary_custom_field_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Salary Custom Fields');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_reportto_custom_field_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_reportto_custom_field_trail
AFTER INSERT ON ohrm_audittrail_pim_reportto_custom_field_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Supervisor/Subordinate Custom Fields');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_qualifications_custom_field_trail`;$$

CREATE TRIGGER watch_insert_ohrm_qualifications_custom_field_trail
AFTER INSERT ON ohrm_audittrail_pim_qualifications_custom_field_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Qualifications Custom Field');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_membership_custom_field_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_membership_custom_field_trail
AFTER INSERT ON ohrm_audittrail_pim_membership_custom_field_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Membership Custom Fields');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_tax_custom_field_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_tax_custom_field_trail
AFTER INSERT ON ohrm_audittrail_pim_tax_custom_field_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Tax Custom Fields');
END;$$

DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_salary_record_trail`;$$

CREATE TRIGGER watch_insert_ohrm_audittrail_pim_salary_record_trail
AFTER INSERT ON ohrm_audittrail_pim_salary_record_trail
FOR EACH ROW BEGIN
  CALL audit_insert_to_ohrm_audittrail_all(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,'Salary');
END;$$

DELIMITER ;

DELIMITER $$
DROP TRIGGER IF EXISTS `watch_insert_ohrm_audittrail_pim_tab_custom_field_trail`;$$
DROP PROCEDURE IF EXISTS `audit_insert_ohrm_audittrail_pim_tab_custom_field_trail`;$$


CREATE TRIGGER watch_insert_ohrm_audittrail_pim_tab_custom_field_trail
AFTER INSERT ON ohrm_audittrail_pim_tab_custom_field_trail
FOR EACH ROW BEGIN
  CALL audit_insert_ohrm_audittrail_pim_tab_custom_field_trail(NEW.action_time, NEW.action_owner_id, NEW.action_owner_name, NEW.action,NEW.action_description,NEW.version_id,NEW.affected_entity_id,NEW.affected_entity_name,NEW.pim_tab);
END;$$

CREATE PROCEDURE audit_insert_ohrm_audittrail_pim_tab_custom_field_trail
  (
    IN action_time DATETIME,
    IN action_owner_id VARCHAR(6) CHARSET UTF8,
    IN action_owner_name TEXT CHARSET UTF8,
    IN action TEXT CHARSET UTF8,
    IN action_description TEXT CHARSET UTF8,
    IN version_id BIGINT,
    IN affected_entity_id VARCHAR(20)CHARSET  UTF8,
    IN affected_entity_name TEXT CHARSET UTF8,
    IN pim_tab VARCHAR(255) CHARSET UTF8
  )
  BEGIN
    DECLARE tab_count TINYINT(1);
    DECLARE section TEXT CHARSET UTF8;

    SELECT count(* ) INTO tab_count FROM ohrm_audittrail_section  WHERE is_deleted = 0 AND name = pim_tab;
    SELECT description INTO section FROM ohrm_audittrail_section  WHERE is_deleted = 0 AND name = pim_tab;

    IF (tab_count !=0) THEN
    INSERT INTO `ohrm_audittrail_all` (action_time, action_owner_id, action_owner_name,action,action_description,version_id,affected_entity_id,affected_entity_name,section) VALUES (
    action_time,
    action_owner_id,
    action_owner_name,
    action,
    action_description,
    version_id,
    affected_entity_id,
    affected_entity_name,
    section
    );

  END IF;
END;$$

DELIMITER ;
