/* $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_PI_CON_PHN_VIEW';
  l_FF_DESC CONSTANT  VARCHAR2(240) := 'Personal Information Phones EmployeeCenter View';
  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 := '
/*==============================================================================

Formula Name: XXAS_EMP_PI_CON_PHN_VIEW
Formula Type: Applaud Person
Description:  Personal Information Phones EmployeeCenter View

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

  Default for Per_Person_Id           is 0
  Default for Transaction_Type        is ''NONE''
  Default for Workflow_Status_Code    is ''NONE''
  Default for Workflow_Status_Meaning is ''NONE''
  Default for Phone_Number            is ''Unknown''
  Default for Phone_Number_p          is ''Unknown''
  Default for Phone_Type_Meaning      is ''Unknown''
  Default for Phone_Type_Meaning_p    is ''Unknown''

  Inputs are Per_Person_Id,
             Transaction_Type, Workflow_Status_Code, Workflow_Status_Meaning,
             Phone_Number, Phone_Number_p, Phone_Type_Meaning,
             Phone_Type_Meaning_p

  COLOR_BLACK    = ''#000000''
  COLOR_GREEN    = ''#499324''
  COLOR_AMBER    = ''#FF9900''
  COLOR_RED      = ''#CC0000''
  COLOR_DEEP_RED = ''#FF0000''
  COLOR_GREY     = ''#333333''
  COLOR_BLUE     = ''#3366CC''
  MSG_APP        = ''XXAS''

  Status_Color      = COLOR_BLACK
  Current_Field_Num = 1
  Pending           = ''N''

  /* Set the phone type */
  If Transaction_Type = ''INSERT'' Then
  (
     Phone = Phone_Number_p
     Phone_Type = Phone_Type_Meaning_p
  )
  Else
  (
     Phone = Phone_Number
     Phone_Type = Phone_Type_Meaning
  )

  If Transaction_Type was not defaulted then
    (
     Pending = ''Y''
     If Transaction_Type = ''INSERT'' then
       (Status_Text  = xxas_get_message(''XXAS'',''XXAS_EMP_NEW''))
     Else If Transaction_Type = ''UPDATE'' then
       (Status_Text  = xxas_get_message(''XXAS'',''XXAS_EMP_UPDATED''))
     Else
       (Status_Text  = xxas_get_message(''XXAS'',''XXAS_EMP_DELETED''))

     Status_Text = Status_Text + Workflow_Status_Meaning

     If Workflow_Status_Code = ''RFC'' Then
       (Status_Color = COLOR_RED)
     Else If Workflow_Status_Code = ''ERROR'' Then
       (Status_Color = COLOR_DEEP_RED)
     Else If Workflow_Status_Code = ''SFL'' Then
       (Status_Color = COLOR_GREY)
     Else If Workflow_Status_Code = ''APPROVED'' Then
       (Status_Color = COLOR_GREEN)
     Else
       (Status_Color = COLOR_AMBER)

    Summary_Message = ''<font color="'' + Status_Color + ''">'' + Status_Text + ''</font>''
    )

  /* Set the phone number */
  Summary_Text = Phone
  If Transaction_Type = ''DELETE'' Then
    (Summary_Text = ''<s>'' + Summary_Text + ''</s>'')

  /* Set the phone type */
  Summary_Context = Phone_Type

  /* Set the Phone Number field */
  Func_Result = xxas_emp_set_text_field
    (Current_Field_Num, xxas_get_message(''XXAS'',''XXAS_EMP_PHONE_NUMBER''), Phone_Number
    ,Pending, Phone_Number_p, Status_Color, ''Unknown'')

  /* Set the Phone Type field */
  Func_Result = xxas_emp_set_text_field
    (Current_Field_Num, xxas_get_message(''XXAS'',''XXAS_RB_NOTE_TYPE''), Phone_Type_Meaning
    ,Pending, Phone_Type_Meaning_p, Status_Color, ''Unknown'')

  /* Set the update/delete permissions to employee-only
     (prevent manager updates out the box) */
  Edit_Allowed = xxas_ess_mss_per_policy(''Y'', ''N'')

  /* Check if this person has past, current or future periods of
     service/placement on record */
  If Edit_Allowed = ''Y'' And Per_Person_Id > 0 Then
    (Has_Work_History = xxas_per_has_work_history(Per_Person_Id))
  Else
    (Has_Work_History = ''N'')

  If Has_Work_History = ''Y'' Then
    (Edit_Allowed = ''N'')

  Update_Allowed = Edit_Allowed
  Delete_Allowed = Edit_Allowed

  Return Update_Allowed, Delete_Allowed,
         Summary_Context, Summary_Text, Summary_Message, Status_Color
';


  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;
