/* $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_CHANGE_ASG_VIEW';
  l_FF_DESC CONSTANT  VARCHAR2(240) := 'Applaud EmployeeCenter Change Assignment View';
  l_FF_TYPE CONSTANT  varchar2(30)  := 'Applaud Assignment';
  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_CHANGE_ASG_VIEW
Formula Type: Applaud Assignment
Description:  Edit current and historical Change Assignment Actions

==============================================================================*/
  Default for Contingent_Worker_Value_in     is 'N'
  Default for Transaction_Id_Value_in        is 'Unknown'
  Default for Transaction_Step_Id_Value_in   is 'Unknown'
  Default for Effective_Date_Option_Value_in is 'Unknown'

  Inputs are Contingent_Worker_Value_in
            ,Transaction_Id_Value_in
            ,Transaction_Step_Id_Value_in
            ,Effective_Date_Option_Value_in

  If Contingent_Worker_Value_in = 'Y' Then
  (
    Grade_Id_Visible                     = 'false'
    Grade_Ladder_Pgm_Id_Visible          = 'false'
    Special_Ceiling_Step_Id_Visible      = 'false'
    Payroll_Id_Visible                   = 'false'
    Pay_Basis_Id_Visible                 = 'false'
    Sal_Review_Period_Visible            = 'false'
    Sal_Review_Period_Frequency_Visible  = 'false'
    Perf_Review_Period_Visible           = 'false'
    Perf_Review_Period_Frequency_Visible = 'false'
    Asg_Status_Visible                   = 'false'
    Asg_Category_Visible                 = 'false'
    Employee_Category_Visible            = 'false'
    ST_Grp_Visible                       = 'false'
    Probation_Period_Visible             = 'false'
    Probation_Unit_Visible               = 'false'
    Date_Probation_End_Visible           = 'false'
    Notice_Period_Visible                = 'false'
    Notice_Period_Uom_Visible            = 'false'
    Project_Title_Visible                = 'false'  /* cwk worker */
    Change_Reason_Visible                = 'false'
    Cwk_Change_Reason_Visible            = 'false'  /* cwk worker */
    Projected_Assignment_End_Visible     = 'false'  /* cwk worker */
    Collective_Agreement_Id_Visible      = 'false'
    Bargaining_Unit_Code_Visible         = 'false'
    Labour_Union_Member_Flag_Visible     = 'false'
    Cagr_Grp_Visible                     = 'false'
  )
  Else
  (
    Cwk_Asg_Status_Visible           = 'false'
    Cwk_Asg_Category_Visible         = 'false'
    Cwk_Change_Reason_Visible        = 'false'
    Projected_Assignment_End_Visible = 'false'
    Vendor_Id_Visible                = 'false'
    Vendor_Site_Id_Visible           = 'false'
    Vendor_Employee_Number_Visible   = 'false'
    Vendor_Assignment_Number_Visible = 'false'
    Po_Header_Id_Visible             = 'false'
    Po_Line_Id_Visible               = 'false'
  )

  If Transaction_Id_Value_in was not defaulted and 
     Transaction_Step_Id_Value_in was not defaulted Then
  (
    Normal_Hours_Visible       = 'true'
    Frequency_Visible          = 'true'
    Time_Normal_Start_Visible  = 'true'
    Time_Normal_Finish_Visible = 'true'
    Work_At_Home_Visible       = 'true'
    Location_Id_Visible        = 'true'
    Job_Id_Visible             = 'true'
    Position_Id_Visible        = 'true'

    If Contingent_Worker_Value_in = 'Y' Then
    (
      Grade_Id_Visible = 'false'
    )
    Else
    (
      Grade_Id_Visible = 'true'
    )

    If Effective_Date_Option_Value_in = 'E' Then
    (
      Effective_From_Visible = 'true'
    )
    Else If Effective_Date_Option_Value_in = 'A' Then
    (
      Effective_From_Visible = 'false'
    )

    Job_Id_Refresh_on_Change      = 'false'
    Position_Id_Refresh_on_Change = 'false'
    Grade_Id_Refresh_on_Change    = 'false'
  )

  Approvers_Visible = 'true'
  Show_Changes = 'true'

  Return Grade_Id_Visible
        ,Grade_Ladder_Pgm_Id_Visible
        ,Special_Ceiling_Step_Id_Visible
        ,Payroll_Id_Visible
        ,Pay_Basis_Id_Visible
        ,Sal_Review_Period_Visible
        ,Sal_Review_Period_Frequency_Visible
        ,Perf_Review_Period_Visible
        ,Perf_Review_Period_Frequency_Visible
        ,Asg_Status_Visible
        ,Asg_Category_Visible
        ,Employee_Category_Visible
        ,ST_Grp_Visible
        ,Probation_Period_Visible
        ,Probation_Unit_Visible
        ,Date_Probation_End_Visible
        ,Notice_Period_Visible
        ,Notice_Period_Uom_Visible
        ,Project_Title_Visible
        ,Change_Reason_Visible
        ,Cwk_Change_Reason_Visible
        ,Projected_Assignment_End_Visible
        ,Collective_Agreement_Id_Visible
        ,Bargaining_Unit_Code_Visible
        ,Labour_Union_Member_Flag_Visible
        ,Cagr_Grp_Visible
        ,Cwk_Asg_Status_Visible
        ,Cwk_Asg_Category_Visible
        ,Vendor_Id_Visible
        ,Vendor_Site_Id_Visible
        ,Vendor_Employee_Number_Visible
        ,Vendor_Assignment_Number_Visible
        ,Po_Header_Id_Visible
        ,Po_Line_Id_Visible
        ,Normal_Hours_Visible
        ,Frequency_Visible
        ,Time_Normal_Start_Visible
        ,Time_Normal_Finish_Visible
        ,Work_At_Home_Visible
        ,Location_Id_Visible
        ,Job_Id_Visible
        ,Position_Id_Visible
        ,Approvers_Visible
        ,Effective_From_Visible
        ,Job_Id_Refresh_on_Change
        ,Position_Id_Refresh_on_Change
        ,Grade_Id_Refresh_on_Change
        ,Show_Changes

]';


  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;
