/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2012 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('1900-01-01','YYYY-MM-DD');
  l_END     CONSTANT  DATE          := xxas_util_pkg.eot;
  l_FF_NAME CONSTANT  VARCHAR2(80)  := 'XXAS_EMP_VIEW_CON_VIEW';
  l_FF_DESC CONSTANT  VARCHAR2(240) := 'Seeded contact details 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 := '
/*==============================================================================

Formula Name: XXAS_EMP_VIEW_CON_VIEW
Formula Type: Applaud Assignment
Description:  Assignment Contact Details EmployeeCenter View

==============================================================================*/


  Default for PER_FULL_NAME  is ''Unknown''
  Default for PER_FIRST_NAME  is ''Unknown''
  Default for PER_LAST_NAME  is ''Unknown''
  Default for PER_EMP_NUMBER is ''Unknown''

    /* Constants */
  UNKNOWN = ''Unknown''

  /* Default information to Unknown */
  Contact_Type_Code  = UNKNOWN
  Contact_Type_Name  = UNKNOWN
  Contact_Data       = UNKNOWN
  Device_Type        = xxas_device_type()
  Status             = UNKNOWN
  FTE                = UNKNOWN
  Job_Title          = UNKNOWN
  Worker_Type        = UNKNOWN
  Job_Name           = UNKNOWN
  Position_Name      = UNKNOWN
  Department         = UNKNOWN
  Service            = UNKNOWN
  Location           = UNKNOWN
  Location_Address   = UNKNOWN
  Latitude           = 0
  Longtitude         = 0

  /* Set the current form item */
  Current_Form_Item = 1

      Contact_Type_Name = xxas_emp_set_long_output
        (''ITEM'' + to_char(Current_Form_Item) + ''_PROMPT''
        ,''XXAS'', ''XXAS_EMP_NAME'')

      Contact_Data = xxas_emp_set_long_output
        (''ITEM'' + to_char(Current_Form_Item) + ''_HTML''
        ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', PER_FULL_NAME)

      Current_Form_Item = Current_Form_Item + 1

      Contact_Type_Name = xxas_emp_set_long_output
        (''ITEM'' + to_char(Current_Form_Item) + ''_PROMPT'' 
        ,''XXAS'',''XXAS_EMP_EMPLOYEE_NUMBER'')

      Contact_Data = xxas_emp_set_long_output
        (''ITEM'' + to_char(Current_Form_Item) + ''_HTML''
        ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', PER_EMP_NUMBER)

       Current_Form_Item = Current_Form_Item + 1

  /* Retrieve up to 6 contact details */
  /* Get the first contact detail */
  result = xxas_get_contact_details
             (1, Contact_Type_Code, Contact_Type_Name, Contact_Data)

  If result = 0 Then
  (

       If Contact_Type_Code = ''EMAIL'' THEN
         (Contact_Data = ''<a href="mailto:'' + Contact_Data + ''">'' + Contact_Data + ''</a>'')
       Else If Device_Type = ''PHONE'' And Contact_Type_Code <> ''FAX'' THEN
         (Contact_Data = ''<a href="tel:'' + Contact_Data + ''">'' + Contact_Data + ''</a>'')


      Contact_Type_Name = xxas_emp_set_long_output
        (''ITEM'' + to_char(Current_Form_Item) + ''_PROMPT''
        ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Contact_Type_Name)

      Contact_Data = xxas_emp_set_long_output
        (''ITEM'' + to_char(Current_Form_Item) + ''_HTML''
        ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Contact_Data)

      Current_Form_Item = Current_Form_Item + 1
  )

  /* Get the second contact detail */
  result = xxas_get_contact_details
             (2, Contact_Type_Code, Contact_Type_Name, Contact_Data)

  If result = 0 Then
  (


      If Contact_Type_Code = ''EMAIL'' THEN
         (Contact_Data = ''<a href="mailto:'' + Contact_Data + ''">'' + Contact_Data + ''</a>'')
      Else If Device_Type = ''PHONE'' And Contact_Type_Code <> ''FAX'' THEN
         (Contact_Data = ''<a href="tel:'' + Contact_Data + ''">'' + Contact_Data + ''</a>'')

      Contact_Type_Name = xxas_emp_set_long_output
        (''ITEM'' + to_char(Current_Form_Item) + ''_PROMPT''
        ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Contact_Type_Name)

      Contact_Data = xxas_emp_set_long_output
        (''ITEM'' + to_char(Current_Form_Item) + ''_HTML''
        ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Contact_Data)

      Current_Form_Item = Current_Form_Item + 1
  )

  /* Get the third contact detail */
  result = xxas_get_contact_details
             (3, Contact_Type_Code, Contact_Type_Name, Contact_Data)

  If result = 0 Then
  (

      If Contact_Type_Code = ''EMAIL'' THEN
         (Contact_Data = ''<a href="mailto:'' + Contact_Data + ''">'' + Contact_Data + ''</a>'')
      Else If Device_Type = ''PHONE'' And Contact_Type_Code <> ''FAX'' THEN
         (Contact_Data = ''<a href="tel:'' + Contact_Data + ''">'' + Contact_Data + ''</a>'')


      Contact_Type_Name = xxas_emp_set_long_output
        (''ITEM'' + to_char(Current_Form_Item) + ''_PROMPT''
        ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Contact_Type_Name)

      Contact_Data = xxas_emp_set_long_output
        (''ITEM'' + to_char(Current_Form_Item) + ''_HTML''
        ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Contact_Data)

      Current_Form_Item = Current_Form_Item + 1
  )

  /* Get the fourth contact detail */
  result = xxas_get_contact_details
             (4, Contact_Type_Code, Contact_Type_Name, Contact_Data)

  If result = 0 Then
  (

      If Contact_Type_Code = ''EMAIL'' THEN
         (Contact_Data = ''<a href="mailto:'' + Contact_Data + ''">'' + Contact_Data + ''</a>'')
      Else If Device_Type = ''PHONE'' And Contact_Type_Code <> ''FAX'' THEN
         (Contact_Data = ''<a href="tel:'' + Contact_Data + ''">'' + Contact_Data + ''</a>'')


      Contact_Type_Name = xxas_emp_set_long_output
        (''ITEM'' + to_char(Current_Form_Item) + ''_PROMPT''
        ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Contact_Type_Name)

      Contact_Data = xxas_emp_set_long_output
        (''ITEM'' + to_char(Current_Form_Item) + ''_HTML''
        ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Contact_Data)

      Current_Form_Item = Current_Form_Item + 1
  )

  /* Get the fifth contact detail */
  result = xxas_get_contact_details
             (5, Contact_Type_Code, Contact_Type_Name, Contact_Data)

  If result = 0 Then
  (

      If Contact_Type_Code = ''EMAIL'' THEN
         (Contact_Data = ''<a href="mailto:'' + Contact_Data + ''">'' + Contact_Data + ''</a>'')
      Else If Device_Type = ''PHONE'' And Contact_Type_Code <> ''FAX'' THEN
         (Contact_Data = ''<a href="tel:'' + Contact_Data + ''">'' + Contact_Data + ''</a>'')


      Contact_Type_Name = xxas_emp_set_long_output
        (''ITEM'' + to_char(Current_Form_Item) + ''_PROMPT''
        ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Contact_Type_Name)

      Contact_Data = xxas_emp_set_long_output
        (''ITEM'' + to_char(Current_Form_Item) + ''_HTML''
        ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Contact_Data)

      Current_Form_Item = Current_Form_Item + 1
  )

  /* Get the sixth contact detail */
  result = xxas_get_contact_details
             (6, Contact_Type_Code, Contact_Type_Name, Contact_Data)

  If result = 0 Then
  (

      If Contact_Type_Code = ''EMAIL'' THEN
         (Contact_Data = ''<a href="mailto:'' + Contact_Data + ''">'' + Contact_Data + ''</a>'')
      Else If Device_Type = ''PHONE'' And Contact_Type_Code <> ''FAX'' THEN
         (Contact_Data = ''<a href="tel:'' + Contact_Data + ''">'' + Contact_Data + ''</a>'')

      Contact_Type_Name = xxas_emp_set_long_output
        (''ITEM'' + to_char(Current_Form_Item) + ''_PROMPT''
        ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Contact_Type_Name)

      Contact_Data = xxas_emp_set_long_output
        (''ITEM'' + to_char(Current_Form_Item) + ''_HTML''
        ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Contact_Data)

      Current_Form_Item = Current_Form_Item + 1
  )




  /* Return just a single Dummy output; all the real Outputs are returned above with the
     xxas_emp_set_long_output calls above. */
  Dummy_Output = ''Dummy''

  RETURN Dummy_Output
';


  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;
