/* $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('0001-01-01','YYYY-MM-DD');
  l_END     CONSTANT  DATE          := xxas_util_pkg.eot;
  l_FF_NAME CONSTANT  VARCHAR2(80)  := 'XXAS_EMP_PI_ADDR_VIEW';
  l_FF_DESC CONSTANT  VARCHAR2(240) := 'Applaud EmployeeCenter Addresses 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_PI_ADDR_VIEW
Formula Type: Applaud Assignment
Description:  Applaud EmployeeCenter Addresses View

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

  Default for Transaction_Type        is ''NONE''
  Default for Workflow_Status_Code    is ''NONE''
  Default for Workflow_Status_Meaning is ''NONE''
  Default for Primary_Flag            is ''N''
  Default for Primary_Flag_p          is ''N''
  Default for Address_Type_Meaning    is ''Unknown''
  Default for Address_Type_Meaning_p  is ''Unknown''
  Default for Address_Line1_Meaning   is ''Unknown''
  Default for Address_Line1_Meaning_p is ''Unknown''
  Default for Address_Line2_Meaning   is ''Unknown''
  Default for Address_Line2_Meaning_p is ''Unknown''
  Default for Address_Line3_Meaning   is ''Unknown''
  Default for Address_Line3_Meaning_p is ''Unknown''
  Default for Town_or_City_Meaning    is ''Unknown''
  Default for Town_or_City_Meaning_p  is ''Unknown''
  Default for Region_1_Meaning        is ''Unknown''
  Default for Region_1_Meaning_p      is ''Unknown''
  Default for Region_2_Meaning        is ''Unknown''
  Default for Region_2_Meaning_p      is ''Unknown''
  Default for Region_3_Meaning        is ''Unknown''
  Default for Region_3_Meaning_p      is ''Unknown''
  Default for Postal_Code_Meaning     is ''Unknown''
  Default for Postal_Code_Meaning_p   is ''Unknown''
  Default for Country_Meaning         is ''Unknown''
  Default for Country_Meaning_p       is ''Unknown''
  Default for Latitude                is 0
  Default for Longtitude              is 0

  Inputs are Transaction_Type, Workflow_Status_Code, Workflow_Status_Meaning,
             Primary_Flag, Primary_Flag_p, Address_Type_Meaning, Address_Type_Meaning_p,
             Address_Line1_Meaning, Address_Line1_Meaning_p,
             Address_Line2_Meaning, Address_Line2_Meaning_p,
             Address_Line3_Meaning, Address_Line3_Meaning_p,
             Town_or_City_Meaning, Town_or_City_Meaning_p,
             Region_1_Meaning, Region_1_Meaning_p,
             Region_2_Meaning, Region_2_Meaning_p,
             Region_3_Meaning, Region_3_Meaning_p,
             Postal_Code_Meaning, Postal_Code_Meaning_p,
             Country_Meaning, Country_Meaning_p,
             Latitude, Longtitude

  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
  (
     Primary = Primary_Flag_p
     Address_Type = Address_Type_Meaning_p
     Line1 = Address_Line1_Meaning_p
     Line2 = Address_Line2_Meaning_p
     Line3 = Address_Line3_Meaning_p
     Town_or_City = Town_or_City_Meaning_p
     Region1 = Region_1_Meaning_p
     Region2 = Region_2_Meaning_p
     Region3 = Region_3_Meaning_p
     Postal_Code = Postal_Code_Meaning_p
     Country = Country_Meaning_p
  )
  Else
  (
     Primary = Primary_Flag
     Address_Type = Address_Type_Meaning
     Line1 = Address_Line1_Meaning
     Line2 = Address_Line2_Meaning
     Line3 = Address_Line3_Meaning
     Town_or_City = Town_or_City_Meaning
     Region1 = Region_1_Meaning
     Region2 = Region_2_Meaning
     Region3 = Region_3_Meaning
     Postal_Code = Postal_Code_Meaning
     Country = Country_Meaning
  )

  /* Set the address type */
  Summary_Text = Address_Type + '' - ''
  If Primary = ''Y'' Then
    (Summary_Text = Summary_Text + xxas_get_message(''XXAS'',''XXAS_EMP_PRIMARY_ADDRESS''))
  Else
    (Summary_Text = Summary_Text + xxas_get_message(''XXAS'',''XXAS_EMP_SECONDARY_ADDRESS''))

  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)

    Status_Text = ''<font color="'' + Status_Color + ''">'' + Status_Text + ''</font>''
      If Transaction_Type = ''DELETE'' Then
        (Summary_Text = ''<s>'' + Summary_Text + ''</s>'')
    Summary_Text = ''<html>'' + Summary_Text + ''<br/>'' + Status_Text + ''</html>''
    )

  /* Set the address */
  Address = Line1
  If Line2 <> ''Unknown'' Then
    (Address = Address + ''<br/>'' + Line2)
  If Line3 <> ''Unknown'' Then
    (Address = Address + ''<br/>'' + Line3)
  If Town_or_City <> ''Unknown'' Then
    (Address = Address + ''<br/>'' + Town_or_City)
  If Postal_Code <> ''Unknown'' Then
    (Address = Address + ''<br/>'' + Postal_Code)
  If Country <> ''Unknown'' Then
    (Address = Address + ''<br/>'' + Country)

  Summary_Message = Address

  /* Set the address google map */
  If Latitude <> 0 And Longtitude <> 0 Then
      (Geocode = to_char(Latitude) + '','' + to_char(Longtitude))
  Else
      (Geocode = replace(Address, ''<br/>'', '',''))

  l_key = xxas_get_profile_option(''XXAS_COM_GOOGLE_MAPS_KEY'')
			
  If l_key <> '' '' Then
   Html = xxas_emp_set_long_output
    (''SUMMARY_CONTEXT'', ''XXAS'', ''XXAS_EMP_VIEW_GOOGLE_MAP_KEY''
    ,''MARKER_COLOR'', ''blue''
    ,''LOCATION'', Geocode
    ,''KEY'',l_key)   
  Else
   Html = xxas_emp_set_long_output
    (''SUMMARY_CONTEXT'', ''XXAS'', ''XXAS_EMP_VIEW_GOOGLE_MAP''
    ,''MARKER_COLOR'', ''blue''
    ,''LOCATION'', Geocode)

  /* Set the Address_Type field */
  Func_Result = xxas_emp_set_text_field
    (Current_Field_Num, xxas_get_message(''XXAS'',''XXAS_RB_NOTE_TYPE''), Address_Type_Meaning
    ,Pending, Address_Type_Meaning_p, Status_Color, xxas_get_message(''XXAS'',''XXAS_EMP_UNKNOWN''))

  /* Set the Address field */
  Func_Result = xxas_emp_set_text_field
    (Current_Field_Num, xxas_get_message(''XXAS'',''XXAS_EMP_ADDRESS''), Address)

  /* Set the update/delete permissions to employee-only
     (prevent manager updates out the box)  */
  Edit_Allowed = xxas_ess_mss_policy(''Y'', ''N'')
  Update_Allowed = Edit_Allowed
  Delete_Allowed = Edit_Allowed
  /* Don''t allow primary addresses to be deleted */
  If Primary = ''Y'' Then
    (Delete_Allowed = ''N'')

  Return Update_Allowed, Delete_Allowed,
         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;
