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

SET VERIFY OFF
SET FEEDBACK OFF
SET ESCAPE '\'
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('1900-01-01','YYYY-MM-DD');
  l_END     CONSTANT  DATE          := xxas_util_pkg.eot;
  l_FF_NAME CONSTANT  VARCHAR2(80)  := 'XXAS_CAND_APPL_FORM_EDIT';
  l_FF_DESC CONSTANT  VARCHAR2(240) := 'Applaud Candidate Application Form 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);

  --
  -- 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 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;

  --
  -- 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_CAND_APPL_FORM_EDIT
Formula Type: Applaud Person
Description:  Applaud Candidate Application Form Edit
==============================================================================*/

  Default for Person_Id             is -1
  Default for Vacancy_Id            is -1
  Default for Current_Employee_Flag is 'N'
  Default for Current_Npw_Flag      is 'N'
  Default for Test_Id_Value_in      is 0
  Default for National_Identifier   is 'Unknown'

  Inputs are Person_Id
            ,Vacancy_Id
            ,Current_Employee_Flag
            ,Current_Npw_Flag
            ,Test_Id_Value_in
            ,National_Identifier
   
  /* Form setup */
  Phone_Numbers_Formula_Name = 'XXAS_CAND_APPL_FORM_PHONE_EDIT'
   
  /* Hide sections for internal employees */
  Is_Current_Worker = 'N'   
  If Current_Employee_Flag = 'Y' Or
     Current_Npw_Flag = 'Y'
  Then
    (Is_Current_Worker = 'Y')
    
  If Is_Current_Worker = 'Y' Then
  (
    Basic_Visible = 'false'
    Address_Visible = 'false'
    Style_Value = 'XXASNULLVAL'
    Country_Value = 'XXASNULLVAL'

    Address_Line1_Required = 'false'
    Postal_Code_Required = 'false'
    Source_Type_Required = 'false'
  )

  /* Edit National Insurance Number */
	If National_Identifier was defaulted Then
		(
		  National_Identifier_Enabled = 'true'
		)
  
  /* Set the upload location */
  CV_Document_Id_post_new_to_href = 'people/' + to_char(Person_Id) + '/resumes-letters?fileName={fileName}\&fileMimeType={fileMimeType}\&type=RESUME'
  Cov_Let_Document_Id_post_new_to_href = 'people/' + to_char(Person_Id) + '/resumes-letters?fileName={fileName}\&fileMimeType={fileMimeType}\&type=COVERLETTER'
  
  /* Hide the address country and phone fields */
  Style_Visible = 'true'
  Style_Longlist = 'true' /* Force the values not to load */
  Country_Visible = 'true'

  Telephone_Number_1_Visible = 'false'
  Telephone_Number_2_Visible = 'false'

  If Test_Id_Value_in was not defaulted Then
  (
    OLM_GRP_Visible = 'true'
    OLM_GRP_Label   = XXAS_OLM_ASSESSMENT_NAME(Test_Id_Value_in)
  )
  Else
  (
    OLM_GRP_Visible = 'false'
  )
  
  Address_Line1_autocomplete_hint = 'address-line1'
  Address_Line2_autocomplete_hint = 'address-line2'
  Address_Line3_autocomplete_hint = 'address-line3'

RETURN Phone_Numbers_Formula_Name
      ,Basic_Visible
      ,Address_Visible
      ,National_Identifier_Enabled
      ,CV_Document_Id_post_new_to_href
      ,Cov_Let_Document_Id_post_new_to_href
      ,Style_Visible
      ,Style_Longlist
      ,Style_Value
      ,Country_Visible
      ,Country_Value
      ,Address_Line1_Required
      ,Postal_Code_Required
      ,Source_Type_Required
      ,Telephone_Number_1_Visible
      ,Telephone_Number_2_Visible
      ,OLM_GRP_Visible
      ,OLM_GRP_Label
	  ,Address_Line1_Autocomplete_hint
	  ,Address_Line2_Autocomplete_hint
	  ,Address_Line3_Autocomplete_hint

]';



  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;
