/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2011 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_LIST';
  l_FF_DESC CONSTANT  VARCHAR2(240) := 'Applaud Payslip Data List of Payslips';
  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 := q'[
/*==============================================================================

Formula Name: XXAS_PAYSLIP_DATA_LIST
Formula Type: Applaud Assignment
Description:  Applaud Payslip Data List of Payslips

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

  Default for Action_Information14 is 'NONE'
  Default for Check_Count          is 0
  Default for Action_Context_Id    is 0
  Default for Effective_Date       is '0001/01/01 00:00:00' (date)


  Inputs are Action_Information14, Check_Count, Action_Context_Id,
             Effective_Date

  Assignment_Number = Action_Information14
  Legislation_Code = xxas_get_bg_value('Org_Information9')
  Payslip_Id = Action_Context_Id
  BI_Publisher_Template_Code = 'Unknown'
  Using_Flash_Payslip = 'N' /* Set this to 'Y' if Applaud's payslip is to be generated */
  
/* Default the summary text */
  Summary_Text = to_char(Effective_Date, 'FMDd Mon YYYY') + ' - '
  If Action_Information14 was not defaulted then
    (Summary_Text = Summary_Text + Assignment_Number + ' - ') 
  Summary_Text = Summary_Text + 'Payslip ' + to_char(Check_Count)


   

  /* Check if Oracle Online payslip is enabled */
  
 IF Using_Flash_Payslip = 'N'   /* Check if Applaud's Flash payslip is enabled */ 
   AND
    (( Legislation_Code = 'US' OR  Legislation_Code = 'CA' OR
      Legislation_Code = 'MX' OR  Legislation_Code = 'DK' OR Legislation_Code = 'NO' OR
      Legislation_Code = 'FI' OR  Legislation_Code = 'SE' OR Legislation_Code = 'IP' OR
      Legislation_Code = 'AE' )
     AND
    ( xxas_oracle_pdf_payslip_enabled(Effective_Date, BI_Publisher_Template_Code) = 'Y')
     )     THEN
    (
     REST_API_Path = 'assignments/' + To_Char(XXAS_GET_ASSIGNMENT_ID()) + '/oracle-payslips/' + 
                     Legislation_Code + '/' + To_Char(Payslip_Id) + 
                     '%3F_accept=application/pdf%26inline=true%26biPublisherTemplate=' + BI_Publisher_Template_Code
                     
     Return Payslip_Id, REST_API_Path, Summary_Text
     )
 ELSE (
     /* If Oracle Online payslip is NOT enabled 
         or If Applaud's Flash Payslip is enabled*/     
      
      /* Set the template according to the employee legislation */
      If Legislation_Code = 'AU' Or Legislation_Code = 'NZ' Then
      (
         Template = 'XXAS_PAYSLIP_AU_TEMPLATE'
      )
      Else If Legislation_Code = 'US' Then
      (
         Template = 'XXAS_PAYSLIP_US_TEMPLATE'
      )
      Else If Legislation_Code = 'SA' Then
      (
         Template = 'XXAS_PAYSLIP_SA_TEMPLATE'
      )
        Else If Legislation_Code = 'AE' Then
      (
         Template = 'XXAS_PAYSLIP_AE_TEMPLATE'
      )
      Else
      (
         Template = 'XXAS_PAYSLIP_US_TEMPLATE'
      )

      Return Payslip_Id, Template, Summary_Text   
    )
   ]';


  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;
