/*
 * OrangeHRM Enterprise is a closed sourced comprehensive Human Resource Management (HRM)
 * System that captures all the essential functionalities required for any enterprise.
 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
 *
 * OrangeHRM Inc is the owner of the patent, copyright, trade secrets, trademarks and any
 * other intellectual property rights which subsist in the Licensed Materials. OrangeHRM Inc
 * is the owner of the media / downloaded OrangeHRM Enterprise software files on which the
 * Licensed Materials are received. Title to the Licensed Materials and media shall remain
 * vested in OrangeHRM Inc. For the avoidance of doubt title and all intellectual property
 * rights to any design, new software, new protocol, new interface, enhancement, update,
 * derivative works, revised screen text or any other items that OrangeHRM Inc creates for
 * Customer shall remain vested in OrangeHRM Inc. Any rights not expressly granted herein are
 * reserved to OrangeHRM Inc.
 *
 * Please refer http://www.orangehrm.com/Files/OrangeHRM_Commercial_License.pdf for the license which includes terms and conditions on using this software.
 *
 */

SET @recruitment_module_id := (SELECT `id`
                               FROM `ohrm_module`
                               WHERE `name` = 'recruitment');

SET @copy_preboarding_task_options :=
        (SELECT JSON_OBJECT(
                        'executorHelper', 'CopyFormRecordToWizardSessionDataExecutionHelper'
                    ));

INSERT INTO `ohrm_advanced_task_template`
(`name`, `description`, `module_id`, `mediator`, `workflow`, `form_version_id`, `options`)
VALUES ('Copy Onboarding Forms to Employee', 'Copy Preboarding Forms to Employee', @recruitment_module_id,
        '', NULL, NULL, @copy_preboarding_task_options);

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

SET @custom_field_screen_id := (SELECT id FROM ohrm_custom_field_screen WHERE name = 'job-standard');
SET @job_other_details_section_id := (SELECT id FROM ohrm_custom_field_section WHERE custom_field_screen_id = @custom_field_screen_id);
SET @job_hours_custom_field_id := (SELECT `id` FROM `ohrm_custom_field` WHERE `name` = 'Job Hours' AND deleted = 0 AND custom_field_section_id = @job_other_details_section_id);
SET @contract_type_custom_field_id := (SELECT `id`  FROM `ohrm_custom_field` WHERE `name` = 'Contract Type' AND deleted = 0 AND custom_field_section_id = @job_other_details_section_id);
SET @holiday_entitlement_custom_field_id := (SELECT `id` FROM `ohrm_custom_field` WHERE `name` = 'Holiday Entitlement' AND deleted = 0 AND custom_field_section_id = @job_other_details_section_id);
SET @probation_and_notice_period_custom_field_id := (SELECT `id` FROM `ohrm_custom_field` WHERE `name` = 'Probation & Notice Period' AND deleted = 0 AND custom_field_section_id = @job_other_details_section_id);

SET @ft_annual_salary_id := (SELECT id FROM ohrm_salary_component WHERE name = 'Annual Basic Payment');
SET @actual_annual_salary_id := (SELECT id FROM ohrm_salary_component WHERE name = 'Actual Annual Salary');
SET @car_allowance_id := (SELECT id FROM ohrm_salary_component WHERE name = 'Car Allowance');
SET @bonus_id := (SELECT id FROM ohrm_salary_component WHERE name = 'Bonus');
SET @standard_hourly_rate_id := (SELECT id FROM ohrm_salary_component WHERE name = 'Standard Hourly Rate');
SET @night_enhancement_id := (SELECT id FROM ohrm_salary_component WHERE name = 'Night Enhancement');
SET @weekend_enhancement_id := (SELECT id FROM ohrm_salary_component WHERE name = 'Weekend Enhancement');

SET @metadata := CONCAT('{
  "formName": "Job Offer Information",
  "wizardDataTemplate": {
    "data": {
      "employee": {
        "locationId": "${location}",
        "joinedDate": "${intended_join_date}"
      },
      "employeeJobRecord": {
        "job_title_id": "${job_title}",
        "joined_date": "${intended_join_date}",
        "employment_status_id": "${employment_status}"
      },
      "customFieldValue": {
        "', @job_other_details_section_id, '": {
          "', @contract_type_custom_field_id, '": "${contract_type}",
          "', @holiday_entitlement_custom_field_id,'": "${holiday_entitlement}",
          "', @probation_and_notice_period_custom_field_id,'": "${probation_notice_period}",
          "', @job_hours_custom_field_id, '": "${job_hours}"
        }
      },
      "salary": {
        "currency_id": "GBP",
        "effective_date": "joined_date",
        "annual_basic_payment": "${ft_annual_salary}",
        "components": [
          {
            "id": "', @ft_annual_salary_id, '",
            "value": "${ft_annual_salary}"
          },
          {
            "id": "', @actual_annual_salary_id, '",
            "value": "${actual_annual_salary}%"
          },
          {
            "id": "', @car_allowance_id, '",
            "value": "${car_allowance}"
          },
          {
            "id": "', @bonus_id, '",
            "value": "${bonus}%"
          },
          {
            "id": "', @standard_hourly_rate_id, '",
            "value": "${standard_hourly_rate}"
          },
          {
            "id": "', @night_enhancement_id, '",
            "value": "${night_enhancement}"
          },
          {
            "id": "', @weekend_enhancement_id, '",
            "value": "${weekend_enhancement}"
          }
        ]
      }
    }
  }
}');

INSERT INTO `ohrm_advanced_task` (task_template_id, name, description, metadata)
VALUES (@copy_preboarding_forms_task_template_id, 'pre_add_candidate_as_employee',
        'task run at when candidate added as employee to copy preboarding form information to employee', @metadata);
