/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2013 Applaud Solutions UK Ltd                |
 |                             All rights reserved                            |
 *===========================================================================*/

SET VERIFY OFF
SET FEEDBACK OFF
SET ESCAPE '\'
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_PAYSLIP_DATA_SA_EMP_DET';
  l_FF_DESC CONSTANT  VARCHAR2(240) := 'Applaud Payslip Data SA Employee 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);

  --
  -- 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 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;

  --
  -- 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_PAYSLIP_DATA_SA_EMP_DET
Formula Type: Applaud Assignment
Description:  Applaud Payslip Data SA Employee Details

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

  Default for Full_Name                is ''Unknown''
  Default for Employee_Number          is ''Unknown''
  Default for National_Identifier      is ''Unknown''
  Default for Location_Name            is ''Unknown''
  Default for Grade                    is ''Unknown''
  Default for Pai_Action_Information5  is ''Unknown''
  Default for Pai_Action_Information6  is ''Unknown''
  Default for Pai_Action_Information9  is ''Unknown''
  Default for Pai_Action_Information10 is ''Unknown''
  Default for Pai_Action_Information11 is ''Unknown''
  Default for Pai_Action_Information8  is ''Unknown''
  Default for Pai_Action_Information12 is ''Unknown''
  Default for Aai_Action_Information4  is ''Unknown''
  Default for Aai_Action_Information5  is ''Unknown''
  Default for Aai_Action_Information14 is ''Unknown''
  Default for Organization_Name        is ''Unknown''
  Default for Aai_Action_Information13 is ''Unknown''
  Default for Position                 is ''Unknown''

  Inputs are Full_Name, Employee_Number, National_Identifier, Location_Name,
             Grade, Pai_Action_Information5, Pai_Action_Information6,
             Pai_Action_Information9, Pai_Action_Information10,
             Pai_Action_Information11, Pai_Action_Information8,
             Pai_Action_Information12, Aai_Action_Information4,
             Aai_Action_Information5, Aai_Action_Information14,
             Organization_Name, Aai_Action_Information13, Position

  DATE_FORMAT_MASK     = ''FMDd Mon YYYY''

  Current_Field_Num = 1

  /* Set the Employee field */
  Func_Result = xxas_emp_set_long_output
    (''FIELD'' + to_char(Current_Field_Num) + ''_LABEL''
    ,''XXAS'',''XXAS_EMP_EMPLOYEE_NAME'')
  Func_Result = xxas_emp_set_long_output
    (''FIELD'' + to_char(Current_Field_Num) + ''_VALUE''
    ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Full_Name)
  Current_Field_Num = Current_Field_Num + 1

  /* Set the Employee Number field */
  If Employee_Number was not defaulted then (
    Func_Result = xxas_emp_set_long_output
      (''FIELD'' + to_char(Current_Field_Num) + ''_LABEL''
      ,''XXAS'',''XXAS_EMP_EMPLOYEE_NUMBER'')
    Func_Result = xxas_emp_set_long_output
      (''FIELD'' + to_char(Current_Field_Num) + ''_VALUE''
      ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Employee_Number)
    Current_Field_Num = Current_Field_Num + 1
  )

  /* Set the National Identifier field */
  If National_Identifier was not defaulted then (
    Func_Result = xxas_emp_set_long_output
      (''FIELD'' + to_char(Current_Field_Num) + ''_LABEL''
      ,''XXAS'',''XXAS_EMP_CIA'')
    Func_Result = xxas_emp_set_long_output
      (''FIELD'' + to_char(Current_Field_Num) + ''_VALUE''
      ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', National_Identifier)
    Current_Field_Num = Current_Field_Num + 1
  )

  /* Set the Location field */
  If Location_Name was not defaulted then (
    Func_Result = xxas_emp_set_long_output
      (''FIELD'' + to_char(Current_Field_Num) + ''_LABEL''
      ,''XXAS'',''XXAS_RB_EMP_LOC'')
    Func_Result = xxas_emp_set_long_output
      (''FIELD'' + to_char(Current_Field_Num) + ''_VALUE''
      ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Location_Name)
    Current_Field_Num = Current_Field_Num + 1
  )

  /* Set the Grade field */
  If Grade was not defaulted then (
    Func_Result = xxas_emp_set_long_output
      (''FIELD'' + to_char(Current_Field_Num) + ''_LABEL''
      ,''XXAS'',''XXAS_EMP_GRADE'')
    Func_Result = xxas_emp_set_long_output
      (''FIELD'' + to_char(Current_Field_Num) + ''_VALUE''
      ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Grade)
    Current_Field_Num = Current_Field_Num + 1
  )

  /* Set the Employee Address field */
  Address = Pai_Action_Information5 + '',''
  If Pai_Action_Information6 was not defaulted then
    (Address = Address + Pai_Action_Information6 + '','')
  If Pai_Action_Information9 was not defaulted then
    (Address = Address + Pai_Action_Information9 + '','')
  If Pai_Action_Information10 was not defaulted then
    (Address = Address + Pai_Action_Information10 + '','')
  If Pai_Action_Information11 was not defaulted then
    (Address = Address + Pai_Action_Information11 + '','')
  If Pai_Action_Information8 was not defaulted then
    (Address = Address + Pai_Action_Information8 + '','')
  If Pai_Action_Information12 was not defaulted then
    (Address = Address + Pai_Action_Information12)
  Address = Address

  Func_Result = xxas_emp_set_long_output
    (''FIELD'' + to_char(Current_Field_Num) + ''_LABEL''
    ,''XXAS'',''XXAS_EMP_EMPLOYEE_ADDRESS'')
  Func_Result = xxas_emp_set_long_output
    (''FIELD'' + to_char(Current_Field_Num) + ''_VALUE''
    ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Address)
  Current_Field_Num = Current_Field_Num + 1

  /* Set the Nationality field */
  If Aai_Action_Information4 was not defaulted then (
    Func_Result = xxas_emp_set_long_output
      (''FIELD'' + to_char(Current_Field_Num) + ''_LABEL''
      ,''XXAS'',''XXAS_EMP_NATIONALITY'')
    Func_Result = xxas_emp_set_long_output
      (''FIELD'' + to_char(Current_Field_Num) + ''_VALUE''
      ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Aai_Action_Information4)
    Current_Field_Num = Current_Field_Num + 1
  )

  /* Set the Cost Center field */
  If Aai_Action_Information5 was not defaulted then (
    Func_Result = xxas_emp_set_long_output
      (''FIELD'' + to_char(Current_Field_Num) + ''_LABEL''
      ,''XXAS'',''XXAS_EMP_COST_CENTER'')
    Func_Result = xxas_emp_set_long_output
      (''FIELD'' + to_char(Current_Field_Num) + ''_VALUE''
      ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Aai_Action_Information5)
    Current_Field_Num = Current_Field_Num + 1
  )

  /* Set the Company Name field */
  If Aai_Action_Information14 was not defaulted then (
    Func_Result = xxas_emp_set_long_output
      (''FIELD'' + to_char(Current_Field_Num) + ''_LABEL''
      ,''XXAS'',''XXAS_RB_EMP_EXP_CN'')
    Func_Result = xxas_emp_set_long_output
      (''FIELD'' + to_char(Current_Field_Num) + ''_VALUE''
      ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Aai_Action_Information14)
    Current_Field_Num = Current_Field_Num + 1
  )

  /* Set the Organization field */
  If Organization_Name was not defaulted then (
    Func_Result = xxas_emp_set_long_output
      (''FIELD'' + to_char(Current_Field_Num) + ''_LABEL''
      ,''XXAS'',''XXAS_EMP_ORGANIZATION'')
    Func_Result = xxas_emp_set_long_output
      (''FIELD'' + to_char(Current_Field_Num) + ''_VALUE''
      ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Organization_Name)
    Current_Field_Num = Current_Field_Num + 1
  )

  /* Set the GOSI Reference Salary field */
  If Aai_Action_Information13 was not defaulted then (
    Func_Result = xxas_emp_set_long_output
      (''FIELD'' + to_char(Current_Field_Num) + ''_LABEL''
      ,''XXAS'',''XXAS_EMP_GOSI'')
    Func_Result = xxas_emp_set_long_output
      (''FIELD'' + to_char(Current_Field_Num) + ''_VALUE''
      ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Aai_Action_Information13)
    Current_Field_Num = Current_Field_Num + 1
  )

  /* Set the Position field */
  If Position was not defaulted then (
    Func_Result = xxas_emp_set_long_output
      (''FIELD'' + to_char(Current_Field_Num) + ''_LABEL''
      ,''XXAS'',''XXAS_RB_EMP_POSITION'')
    Func_Result = xxas_emp_set_long_output
      (''FIELD'' + to_char(Current_Field_Num) + ''_VALUE''
      ,''XXAS'', ''XXAS_COM_TEXT'', ''TEXT'', Position)
    Current_Field_Num = Current_Field_Num + 1
  )

  /* The return value here isn''t actually used because the return items are set 

above instead */
  Field1_Label = ''Dummy''
  Return Field1_Label

';


  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;
