/* $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_PASSPORT';
  l_FF_DESC CONSTANT  VARCHAR2(240) := 'Sample single row EIT (Passport Details)';
  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_PASSPORT
Formula Type: Applaud Assignment
Description:  Assignment Passport EmployeeCenter View

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

  Default for Transaction_Type           is ''NONE''
  Default for Workflow_Status_Code       is ''NONE''
  Default for Workflow_Status_Meaning    is ''None''
  Default for Pei_Information5           is ''Unknown''
  Default for Pei_Information5_p         is ''Unknown''
  Default for Pei_Information5_Meaning   is ''Unknown''
  Default for Pei_Information5_Meaning_p is ''Unknown''
  Default for Pei_Information6           is ''Unknown''
  Default for Pei_Information6_p         is ''Unknown''
  Default for Pei_Information7           is ''0001/01/01 00:00:00'' (date)
  Default for Pei_Information7_p         is ''0001/01/01 00:00:00'' (date)
  Default for Pei_Information8           is ''4712/12/31 00:00:00'' (date)
  Default for Pei_Information8_p         is ''4712/12/31 00:00:00'' (date)

  Inputs are Transaction_Type, Workflow_Status_Code, Workflow_Status_Meaning,
             Pei_Information5, Pei_Information5_Meaning, Pei_Information6,
             Pei_Information7, Pei_Information8, Pei_Information5_p,
             Pei_Information5_Meaning_p, Pei_Information6_p,
             Pei_Information7_p, Pei_Information8_p

  COLOR_GREEN    = ''#499324''
  COLOR_AMBER    = ''#FF9900''
  COLOR_RED      = ''#CC0000''
  COLOR_DEEP_RED = ''#FF0000''
  COLOR_GREY     = ''#333333''
  COLOR_BLUE     = ''#3366CC''
  START_OF_TIME  = to_date(''0001/01/01 00:00:00'', ''YYYY/MM/DD HH24:MI:SS'')

  If Transaction_Type = ''INSERT'' Then
    (
     Country_Code = Pei_Information5_p
     Country = Pei_Information5_Meaning_p
     Passport_Number = Pei_Information6_p
     Issue_Date = Pei_Information7_p
     Expiry_Date = Pei_Information8_p
    )
  Else
    (
     Country_Code = Pei_Information5
     Country = Pei_Information5_Meaning
     Passport_Number = Pei_Information6
     Issue_Date = Pei_Information7
     Expiry_Date = Pei_Information8
    )

  /* Set the status color based on its status */
  Status_Text  = xxas_get_message(''XXAS'',''XXAS_EMP_APPROVED'')
  Status_Color = COLOR_GREEN

  If Transaction_Type was defaulted then
    (
     Status_Text  = xxas_get_message(''XXAS'',''XXAS_EMP_APPROVED'')
     Status_Color = COLOR_GREEN
    )
  Else
    (
     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_Context = ''<font color="'' + Status_Color + ''">'' + Status_Text + 

''</font>''

  /* Set the detail items */
  Item1_Prompt = xxas_get_message(''XXAS'',''XXAS_RB_TCM_STATUS'')
  Item1_HTML = Summary_Context

  Item2_Prompt = xxas_get_message(''XXAS'',''XXAS_EMP_COUNTRY_OF_ISSUE'')
  Item2_HTML = Country
  /* Add the flag if the country code is available */
  If Country_Code <> ''Unknown'' Then
  (
    Item2_HTML = xxas_emp_set_long_output
      (''ITEM2_HTML'', ''XXAS'', ''XXAS_EMP_VIEW_PASSPORT_FLAG''
      ,''COUNTRY'', Country
      ,''FLAG_NAME'', upper(Country_Code) + ''.png'')
  )

  Item3_Prompt = xxas_get_message(''XXAS'',''XXAS_EMP_PASSPORT_NUMBER'')
  Item3_HTML = Passport_Number

  Item4_Prompt = xxas_get_message(''XXAS'',''XXAS_EMP_ISSUE_DATE'')
  Item4_HTML = to_char(Issue_Date, ''fmDd Month YYYY'')

  Item5_Prompt = xxas_get_message(''XXAS'',''XXAS_EMP_EXPIRY_DATE'')
  Item5_HTML = to_char(Expiry_Date, ''fmDd Month YYYY'')

  /* Set the update/delete permissions */
  Edit_Allowed = xxas_ess_mss_policy(''Y'', ''Y'')
  Update_Allowed = Edit_Allowed
  Delete_Allowed = Edit_Allowed

  Return Update_Allowed, Delete_Allowed,
         Status_Text, Status_Color,
         Summary_Context,
         Item1_Prompt, Item1_HTML,
         Item2_Prompt, Item2_HTML,
         Item3_Prompt, Item3_HTML,
         Item4_Prompt, Item4_HTML,
         Item5_Prompt, Item5_HTML


';


  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;

