/* $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_CO_PROP';
  l_FF_DESC CONSTANT  VARCHAR2(240) := 'Assignment Company Property EmployeeCenter 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_CO_PROP
Formula Type: Applaud Assignment
Description:  Assignment Company Property EmployeeCenter View

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

  Default for Transaction_Type        is ''NONE''
  Default for Workflow_Status_Code    is ''NONE''
  Default for Workflow_Status_Meaning is ''None''
  Default for Segment1_Meaning        is ''Unknown''
  Default for Segment3                is ''Unknown''
  Default for Segment4                is ''Unknown''
  Default for Segment5_Meaning        is ''No''
  Default for Segment6                is ''0001/01/01 00:00:00'' (date)
  Default for Segment7                is ''0001/01/01 00:00:00'' (date)
  Default for Segment1_Meaning_p      is ''Unknown''
  Default for Segment3_p              is ''Unknown''
  Default for Segment4_p              is ''Unknown''
  Default for Segment5_Meaning_p      is ''No''
  Default for Segment6_p              is ''0001/01/01 00:00:00'' (date)
  Default for Segment7_p              is ''0001/01/01 00:00:00'' (date)
  
  Inputs are Transaction_Type, Workflow_Status_Code, Workflow_Status_Meaning,
             Segment1_Meaning, Segment3, Segment4,
             Segment5_Meaning, Segment6, Segment7,
             Segment1_Meaning_p, Segment3_p, Segment4_p,
             Segment5_Meaning_p, Segment6_p, Segment7_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
    (
     Company_Property = Segment1_Meaning_p
     Serial_Number = Segment3_p
     Account_Number = Segment4_p
     Under_Warranty = Segment5_Meaning_p
     Issue_Date = Segment6_p
     Date_Returned = Segment7_p
    )
  Else
    (
     Company_Property = Segment1_Meaning
     Serial_Number = Segment3
     Account_Number = Segment4
     Under_Warranty = Segment5_Meaning
     Issue_Date = Segment6
     Date_Returned = Segment7
    )

  /* 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>''
 
  Summary_Text = Company_Property
  If Transaction_Type = ''DELETE'' Then
    (Summary_Text = ''<s>'' + Summary_Text + ''</s>'')
   
  /* Set details of when it was issued and whether it had a warranty or not */
  If Issue_Date = START_OF_TIME then
    (Summary_Message = xxas_get_message(''XXAS'',''XXAS_EMP_AWAITING_ISSUE''))
  Else
    (Summary_Message =  xxas_get_message(''XXAS'',''XXAS_EMP_ISSUED_ON'',''ISSUE_DATE'', to_char(Issue_Date, ''fmDd Month YYYY'')))
    
  If Under_Warranty = ''No'' then
    (Summary_Message = Summary_Message +'' ''+ ''(''+ xxas_get_message(''XXAS'',''XXAS_EMP_NO_WARRANTY'') +'')'') 
  Else
    (Summary_Message = Summary_Message +'' ''+''(''+ xxas_get_message(''XXAS'',''XXAS_EMP_WITH_WARRANTY'') +'')'')
  
  /* Set the detail header to the summary header */
  Detail_Header_Context = Summary_Context
  Detail_Header_Text    = Summary_Text
  Detail_Header_Message = Summary_Message
  
  /* Set the detail items */
  Item1_Prompt = xxas_get_message(''XXAS'',''XXAS_EMP_SERIAL_NUMBER'')
  If Serial_Number = ''Unknown'' then
    (Item1_HTML = xxas_get_message(''XXAS'',''XXAS_EMP_NOT_SPECIFIED''))
  Else
    (Item1_HTML = Serial_Number)
    
  Item2_Prompt = xxas_get_message(''XXAS'',''XXAS_EMP_ACCOUNT_NUMBER'')
  If Account_Number = ''Unknown'' then
    (Item2_HTML = xxas_get_message(''XXAS'',''XXAS_EMP_NOT_SPECIFIED''))
  Else
    (Item2_HTML = Account_Number)
        
  Item3_Prompt = xxas_get_message(''XXAS'',''XXAS_EMP_UNDER_WARRANTY'')
  Item3_HTML = Under_Warranty
  
  Item4_Prompt = xxas_get_message(''XXAS'',''XXAS_EMP_DATE_RETURNED'')
  If Date_Returned = START_OF_TIME then
    (Item4_HTML = ''<font color="#FF4000">'' + xxas_get_message(''XXAS'',''XXAS_EMP_NOT_YET_RETURNED'')+ ''</font>'')
  Else
    (Item4_HTML = to_char(Date_Returned, ''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, Summary_Text, Summary_Message,
         Detail_Header_Context, Detail_Header_Text, Detail_Header_Message,
         Item1_Prompt, Item1_HTML,
         Item2_Prompt, Item2_HTML,
         Item3_Prompt, Item3_HTML,
         Item4_Prompt, Item4_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;
