/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2013 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_QUA_EDU_EDIT';
  l_FF_DESC CONSTANT  VARCHAR2(240) := 'Applaud EmployeeCenter Education and Qualifications Edit';
  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 := '
/*==============================================================================
Formula Name: XXAS_EMP_QUA_EDU_EDIT
Formula Type: Applaud Assignment
Description:  Applaud EmployeeCenter Education and Qualifications Edit
==============================================================================*/

  /* Set the qualification mode */
  Mode_Value = ''EDUCATION''

  /* Hide professional qualification-only fields */
  License_Number_Visible = ''false''
  Expiry_Date_Visible = ''false''
  License_Restrictions_Visible = ''false''
  
  /* Hide all flexfield segments by default */
  Pea_Attribute_Category_Visible = ''false''
  Pea_Attribute1_Visible = ''false''
  Pea_Attribute2_Visible = ''false''
  Pea_Attribute3_Visible = ''false''
  Pea_Attribute4_Visible = ''false''
  Pea_Attribute5_Visible = ''false''
  Pea_Attribute6_Visible = ''false''
  Pea_Attribute7_Visible = ''false''
  Pea_Attribute8_Visible = ''false''
  Pea_Attribute9_Visible = ''false''
  Pea_Attribute10_Visible = ''false''
  Pea_Attribute11_Visible = ''false''
  Pea_Attribute12_Visible = ''false''
  Pea_Attribute13_Visible = ''false''
  Pea_Attribute14_Visible = ''false''
  Pea_Attribute15_Visible = ''false''
  Pea_Attribute16_Visible = ''false''
  Pea_Attribute17_Visible = ''false''
  Pea_Attribute18_Visible = ''false''
  Pea_Attribute19_Visible = ''false''
  Pea_Attribute20_Visible = ''false''
  
  Pq_Attribute_Category_Visible = ''false''
  Pq_Attribute1_Visible = ''false''
  Pq_Attribute2_Visible = ''false''
  Pq_Attribute3_Visible = ''false''
  Pq_Attribute4_Visible = ''false''
  Pq_Attribute5_Visible = ''false''
  Pq_Attribute6_Visible = ''false''
  Pq_Attribute7_Visible = ''false''
  Pq_Attribute8_Visible = ''false''
  Pq_Attribute9_Visible = ''false''
  Pq_Attribute10_Visible = ''false''
  Pq_Attribute11_Visible = ''false''
  Pq_Attribute12_Visible = ''false''
  Pq_Attribute13_Visible = ''false''
  Pq_Attribute14_Visible = ''false''
  Pq_Attribute15_Visible = ''false''
  Pq_Attribute16_Visible = ''false''
  Pq_Attribute17_Visible = ''false''
  Pq_Attribute18_Visible = ''false''
  Pq_Attribute19_Visible = ''false''
  Pq_Attribute20_Visible = ''false''
  
  Pq_Information_Category_Visible = ''false''
  Pq_Information1_Visible = ''false''
  Pq_Information2_Visible = ''false''
  Pq_Information3_Visible = ''false''
  Pq_Information4_Visible = ''false''
  Pq_Information5_Visible = ''false''
  Pq_Information6_Visible = ''false''
  Pq_Information7_Visible = ''false''
  Pq_Information8_Visible = ''false''
  Pq_Information9_Visible = ''false''
  Pq_Information10_Visible = ''false''
  Pq_Information11_Visible = ''false''
  Pq_Information12_Visible = ''false''
  Pq_Information13_Visible = ''false''
  Pq_Information14_Visible = ''false''
  Pq_Information15_Visible = ''false''
  Pq_Information16_Visible = ''false''
  Pq_Information17_Visible = ''false''
  Pq_Information18_Visible = ''false''
  Pq_Information19_Visible = ''false''
  Pq_Information20_Visible = ''false''

  
  Return Mode_Value,
         License_Number_Visible,
         Expiry_Date_Visible,
         License_Restrictions_Visible,
         Pea_Attribute_Category_Visible,
         Pea_Attribute1_Visible,
         Pea_Attribute2_Visible,
         Pea_Attribute3_Visible,
         Pea_Attribute4_Visible,
         Pea_Attribute5_Visible,
         Pea_Attribute6_Visible,
         Pea_Attribute7_Visible,
         Pea_Attribute8_Visible,
         Pea_Attribute9_Visible,
         Pea_Attribute10_Visible,
         Pea_Attribute11_Visible,
         Pea_Attribute12_Visible,
         Pea_Attribute13_Visible,
         Pea_Attribute14_Visible,
         Pea_Attribute15_Visible,
         Pea_Attribute16_Visible,
         Pea_Attribute17_Visible,
         Pea_Attribute18_Visible,
         Pea_Attribute19_Visible,
         Pea_Attribute20_Visible,
         Pq_Attribute_Category_Visible,
         Pq_Attribute1_Visible,
         Pq_Attribute2_Visible,
         Pq_Attribute3_Visible,
         Pq_Attribute4_Visible,
         Pq_Attribute5_Visible,
         Pq_Attribute6_Visible,
         Pq_Attribute7_Visible,
         Pq_Attribute8_Visible,
         Pq_Attribute9_Visible,
         Pq_Attribute10_Visible,
         Pq_Attribute11_Visible,
         Pq_Attribute12_Visible,
         Pq_Attribute13_Visible,
         Pq_Attribute14_Visible,
         Pq_Attribute15_Visible,
         Pq_Attribute16_Visible,
         Pq_Attribute17_Visible,
         Pq_Attribute18_Visible,
         Pq_Attribute19_Visible,
         Pq_Attribute20_Visible,
         Pq_Information_Category_Visible,
         Pq_Information1_Visible,
         Pq_Information2_Visible,
         Pq_Information3_Visible,
         Pq_Information4_Visible,
         Pq_Information5_Visible,
         Pq_Information6_Visible,
         Pq_Information7_Visible,
         Pq_Information8_Visible,
         Pq_Information9_Visible,
         Pq_Information10_Visible,
         Pq_Information11_Visible,
         Pq_Information12_Visible,
         Pq_Information13_Visible,
         Pq_Information14_Visible,
         Pq_Information15_Visible,
         Pq_Information16_Visible,
         Pq_Information17_Visible,
         Pq_Information18_Visible,
         Pq_Information19_Visible,
         Pq_Information20_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;
