/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2016 Applaud Solutions UK Ltd                |
 |                             All rights reserved                            |
 *===========================================================================*/

SET VERIFY OFF
SET FEEDBACK OFF
WHENEVER OSERROR  EXIT FAILURE ROLLBACK
WHENEVER SQLERROR EXIT FAILURE ROLLBACK

DECLARE

  --
  -- Local constants and variables
  --
  e_no_bg_id          EXCEPTION;
  e_bg_not_exists     EXCEPTION;
  e_ff_type_not_found EXCEPTION;
  l_BG_ID   CONSTANT  NUMBER        := to_number('&1');
  l_USER_ID CONSTANT  NUMBER        := to_number('&2');
  l_START   CONSTANT  DATE          := to_date('0001-01-01','YYYY-MM-DD');
  l_END     CONSTANT  DATE          := xxas_util_pkg.eot;
  l_FF_NAME CONSTANT  VARCHAR2(80)  := 'XXAS_EMP_VIEW_TERM_EDIT';
  l_FF_DESC CONSTANT  VARCHAR2(240) := 'Applaud EmployeeCenter Leaver Request Edit';
  l_FF_TYPE CONSTANT  varchar2(30)  := 'Applaud Person';
  l_formula_text      LONG;
  l_formula_type_id   NUMBER;
  l_ff_id             NUMBER;
  l_bg_exists         VARCHAR2(1);

  --
  -- Gets the FF Id
  --
  CURSOR csr_ff_id IS
  SELECT ff.formula_id
  FROM   ff_formulas_f ff
  WHERE  ff.business_group_id = l_BG_ID
  AND    ff.formula_name = l_FF_NAME;

  --
  -- Tests if the Business Group already exists
  --
  CURSOR csr_bg_exists IS
  SELECT 'Y'
  FROM   per_business_groups_perf bg
  WHERE  bg.business_group_id = l_BG_ID;

  --
  -- Gets the Formula Type Id
  --
  CURSOR csr_ff_type IS
  SELECT fft.formula_type_id
  FROM   ff_formula_types fft
  WHERE  fft.formula_type_name = l_FF_TYPE;

BEGIN

  --
  -- Check that the Business Group Id was passed in
  --
  IF l_BG_ID IS NULL THEN
    RAISE e_no_bg_id;
  END IF;

  --
  -- Check the Business Group exists
  --
  OPEN  csr_bg_exists;
  FETCH csr_bg_exists INTO l_bg_exists;
  CLOSE csr_bg_exists;

  IF l_bg_exists IS NULL THEN
    RAISE e_bg_not_exists;
  END IF;

  --
  -- Fetch the Fast Formula Id
  --
  OPEN  csr_ff_id;
  FETCH csr_ff_id INTO l_ff_id;
  CLOSE csr_ff_id;

  --
  -- Get the Formula Type; error if it cannot be found
  --
  OPEN  csr_ff_type;
  FETCH csr_ff_type INTO l_formula_type_id;
  CLOSE csr_ff_type;

  IF l_formula_type_id IS NULL THEN
    RAISE e_ff_type_not_found;
  END IF;

  --
  -- Set the Formula Text
  --
  l_formula_text := q'[
/*==============================================================================

Formula Name: XXAS_EMP_VIEW_TERM_EDIT
Formula Type: Applaud Person
Description:  Edit current and historical terminations

==============================================================================*/
  Default for Refresh_Field_Name                  is 'Unknown'
  Default for Refresh_Event                       is 'Unknown'
  Default for Direct_Reports_Count_Value_in       is '0'
  Default for Last_Day_Of_Work_Value_In           is '0001/01/01 00:00:00' (date)
  Default for Final_Approver_Date_Value_In        is '0001/01/01 00:00:00' (date)
  Default for Person_Type_Value_in                is 0
  Default for Asg_Status_Value_in                 is 0
  Default for Date_Start_Value_in                 is 'Unknown'
  Default for Effective_Date_Option_Value_in      is 'Unknown'

  Inputs are Refresh_Field_Name
            ,Refresh_Event
            ,Direct_Reports_Count_Value_in
            ,Last_Day_Of_Work_Value_In
            ,Final_Approver_Date_Value_In
            ,Person_Type_Value_in
            ,Asg_Status_Value_in
            ,Date_Start_Value_in
            ,Effective_Date_Option_Value_in

  DATE_FORMAT_MASK    = 'FMDD-Mon-YYYY'

  If Person_Type_Value_in was defaulted Then
  (
    Person_Type_Value = XXAS_GET_DEFAULT_EX_EMP_PER_STATUS('EX_EMP')
  )
  Else if Person_Type_Value_in was not defaulted Then
  (
    Person_Type_Value = Person_Type_Value_in
  )

  If Asg_Status_Value_in was defaulted Then
  (
    Asg_Status_Value = XXAS_GET_DEFAULT_EX_EMP_ASG_STATUS('TERM_ASSIGN')
  )
  Else if Asg_Status_Value_in was not defaulted Then
  (
    Asg_Status_Value = Asg_Status_Value_in
  )

  If Direct_Reports_Count_Value_in was not defaulted And to_number(Direct_Reports_Count_Value_in) = 1 Then
  (
    Is_Manager_Value = 'Y'
    New_Manager_Dr_Label = 'New Manager for ' + Direct_Reports_Count_Value_in + ' ' + 'DR'
  )
  Else if Direct_Reports_Count_Value_in was not defaulted And to_number(Direct_Reports_Count_Value_in) > 1 Then
  (
    Is_Manager_Value = 'Y'
    New_Manager_Dr_Label = 'New Manager for ' + Direct_Reports_Count_Value_in + ' ' + 'DRs'
  )
  Else
  (
    Is_Manager_Value = 'N'
    New_Manager_Dr_Label = 'New Manager for DRs'
  )

  Last_Day_Of_Work_Description = 'Last day of Work cannot be before ' + to_char(to_date(Date_Start_Value_in, 'YYYY/MM/DD HH24:MI:SS'), DATE_FORMAT_MASK) + '.'
  Final_Approver_Date_Description = Last_Day_Of_Work_Description

  If Effective_Date_Option_Value_in was defaulted then
  (
    /*Last_Day_Of_Work_Visible = 'false'
    Final_Approver_Date_Visible = 'false'*/
    Last_Day_Of_Work_Value   = trunc(sysdate)
    Final_Approver_Date_Value = trunc(sysdate)
  )
  Else If Effective_Date_Option_Value_in = 'E' Then
  (
    /*Last_Day_Of_Work_Visible = 'true'
    Final_Approver_Date_Visible = 'false'*/

    If Last_Day_Of_Work_Value_in was not defaulted Then
    (
      Last_Day_Of_Work_Value = Last_Day_Of_Work_Value_in
    )
  )
  Else If Effective_Date_Option_Value_in = 'A' Then
  (
    /*Final_Approver_Date_Visible = 'true'
    Last_Day_Of_Work_Visible = 'false'*/
    Final_Approver_Date_Enabled = 'false'
    Final_Approver_Date_Value   = trunc(sysdate)
  )

  /* Hide all flexfield segments by default */
  Attribute_Category_Visible = 'false'
  Attribute1_Visible = 'false'
  Attribute2_Visible = 'false'
  Attribute3_Visible = 'false'
  Attribute4_Visible = 'false'
  Attribute5_Visible = 'false'
  Attribute6_Visible = 'false'
  Attribute7_Visible = 'false'
  Attribute8_Visible = 'false'
  Attribute9_Visible = 'false'
  Attribute10_Visible = 'false'
  Attribute11_Visible = 'false'
  Attribute12_Visible = 'false'
  Attribute13_Visible = 'false'
  Attribute14_Visible = 'false'
  Attribute15_Visible = 'false'
  Attribute16_Visible = 'false'
  Attribute17_Visible = 'false'
  Attribute18_Visible = 'false'
  Attribute19_Visible = 'false'
  Attribute20_Visible = 'false'

  PDS_Information_Category_Visible = 'false'
  PDS_Information1_Visible = 'false'
  PDS_Information2_Visible = 'false'
  PDS_Information3_Visible = 'false'
  PDS_Information4_Visible = 'false'
  PDS_Information5_Visible = 'false'
  PDS_Information6_Visible = 'false'
  PDS_Information7_Visible = 'false'
  PDS_Information8_Visible = 'false'
  PDS_Information9_Visible = 'false'
  PDS_Information10_Visible = 'false'
  PDS_Information11_Visible = 'false'
  PDS_Information12_Visible = 'false'
  PDS_Information13_Visible = 'false'
  PDS_Information14_Visible = 'false'
  PDS_Information15_Visible = 'false'
  PDS_Information16_Visible = 'false'
  PDS_Information17_Visible = 'false'
  PDS_Information18_Visible = 'false'
  PDS_Information19_Visible = 'false'
  PDS_Information20_Visible = 'false'
  PDS_Information21_Visible = 'false'
  PDS_Information22_Visible = 'false'
  PDS_Information23_Visible = 'false'
  PDS_Information24_Visible = 'false'
  PDS_Information25_Visible = 'false'
  PDS_Information26_Visible = 'false'
  PDS_Information27_Visible = 'false'
  PDS_Information28_Visible = 'false'
  PDS_Information29_Visible = 'false'
  PDS_Information30_Visible = 'false'

  Return Person_Type_Value
        ,Asg_Status_Value
        /*,Last_Day_Of_Work_Visible
        ,Final_Approver_Date_Visible*/
        ,Last_Day_Of_Work_Value
        ,Final_Approver_Date_Value
        ,Final_Approver_Date_Enabled
        ,Is_Manager_Value
        ,New_Manager_Dr_Label
        ,Last_Day_Of_Work_Description
        ,Final_Approver_Date_Description
        ,Attribute_Category_Visible
        ,Attribute1_Visible
        ,Attribute2_Visible
        ,Attribute3_Visible
        ,Attribute4_Visible
        ,Attribute5_Visible
        ,Attribute6_Visible
        ,Attribute7_Visible
        ,Attribute8_Visible
        ,Attribute9_Visible
        ,Attribute10_Visible
        ,Attribute11_Visible
        ,Attribute12_Visible
        ,Attribute13_Visible
        ,Attribute14_Visible
        ,Attribute15_Visible
        ,Attribute16_Visible
        ,Attribute17_Visible
        ,Attribute18_Visible
        ,Attribute19_Visible
        ,Attribute20_Visible
        ,PDS_Information_Category_Visible
        ,PDS_Information1_Visible
        ,PDS_Information2_Visible
        ,PDS_Information3_Visible
        ,PDS_Information4_Visible
        ,PDS_Information5_Visible
        ,PDS_Information6_Visible
        ,PDS_Information7_Visible
        ,PDS_Information8_Visible
        ,PDS_Information9_Visible
        ,PDS_Information10_Visible
        ,PDS_Information11_Visible
        ,PDS_Information12_Visible
        ,PDS_Information13_Visible
        ,PDS_Information14_Visible
        ,PDS_Information15_Visible
        ,PDS_Information16_Visible
        ,PDS_Information17_Visible
        ,PDS_Information18_Visible
        ,PDS_Information19_Visible
        ,PDS_Information20_Visible
        ,PDS_Information21_Visible
        ,PDS_Information22_Visible
        ,PDS_Information23_Visible
        ,PDS_Information24_Visible
        ,PDS_Information25_Visible
        ,PDS_Information26_Visible
        ,PDS_Information27_Visible
        ,PDS_Information28_Visible
        ,PDS_Information29_Visible
        ,PDS_Information30_Visible

]';


  BEGIN

    IF l_ff_id IS NULL THEN
      --
      -- Create the Fast Formula
      --
      INSERT INTO ff_formulas_f
        (formula_id
        ,effective_start_date
        ,effective_end_date
        ,business_group_id
        ,formula_type_id
        ,formula_name
        ,description
        ,formula_text)
      VALUES
        (ff_formulas_s.nextval
        ,l_START
        ,l_END
        ,l_BG_ID
        ,l_formula_type_id
        ,l_FF_NAME
        ,l_FF_DESC
        ,l_formula_text);

    ELSE
      --
      -- Update the existing Fast Formula text
      -- (if it has multiple date-track versions the text of
      -- all versions is updated).
      --
      UPDATE ff_formulas_f
      SET    formula_text = l_formula_text
            ,description  = l_FF_DESC
      WHERE  formula_id = l_ff_id;

    END IF;

    COMMIT;

    EXCEPTION
      WHEN others THEN
        ROLLBACK;
        RAISE;
    END;

    --
    -- Compile the Fast Formula
    --
    xxas_util_pkg.compile_ff
      (p_user_id => l_USER_ID
      ,p_ff_type => l_FF_TYPE
      ,p_ff_name => l_FF_NAME);

EXCEPTION

  WHEN e_no_bg_id THEN
    RAISE no_data_found;

  WHEN e_bg_not_exists THEN
    RAISE no_data_found;

  WHEN e_ff_type_not_found THEN
    RAISE no_data_found;

END;
/
COMMIT;
EXIT;
