-- ******************************************************************
-- *
-- * Copyright (c) 2000, 2007, Oracle. All rights reserved.
-- *
-- ******************************************************************

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO


IF EXISTS (SELECT name FROM sysobjects 
         WHERE name = 'CIBLPRTH' AND type = 'P')
   DROP PROCEDURE RPTUSER.CIBLPRTH 
GO

CREATE  PROCEDURE RPTUSER.CIBLPRTH
@P_USER_ID CHAR(8),
@P_BILL_ID CHAR(12)
AS

/******************************************************************************
   NAME:       CIBLPRTH
   PURPOSE:    Supply report extract for Crystal Report - Get Bill Payments

******************************************************************************/

   DECLARE   @V_LANGUAGE_CD   CHAR(3)
   DECLARE   @V_BILL_ID       CHAR(12)
   DECLARE   @V_RETURN_CD     INT

   SET @V_RETURN_CD = 0

BEGIN


/******************************************************************************
   Checking for nulls in parameters and setting default values for select 
   statements
******************************************************************************/

   SELECT @V_LANGUAGE_CD = LANGUAGE_CD
   FROM   SC_USER
   WHERE  SC_USER.USER_ID = @P_USER_ID

   IF @@ROWCOUNT = 0
      BEGIN
         SET @V_LANGUAGE_CD = 'ENG'
         SET @V_RETURN_CD = 1003  -- ERROR: INVALID USER ID
      END

   IF @V_RETURN_CD = 0
      BEGIN
         SELECT @V_BILL_ID = BILL_ID
         FROM CI_BILL
         WHERE BILL_ID = @P_BILL_ID
      
         IF @@ROWCOUNT = 0
            SET @V_RETURN_CD = 1027  -- ERROR: INVALID BILL ID
      END
   
   IF @V_RETURN_CD = 0
      BEGIN
         SELECT @V_RETURN_CD RETURN_CODE
                ,CAST(NULL AS VARCHAR) MESSAGE_TEXT
                ,PE.PAY_DT
                ,FT.FT_TYPE_FLG
                ,PY.CAN_RSN_CD
                ,FT.CURRENCY_CD
                ,SUM(FT.CUR_AMT) CUR_AMT
                ,SUM(FT.TOT_AMT) TOT_AMT
         FROM   CI_FT FT 
                ,CI_PAY_SEG PS 
                ,CI_PAY PY 
                ,CI_PAY_EVENT PE 
         WHERE  FT.BILL_ID = @V_BILL_ID
                AND FT.FT_TYPE_FLG IN ('PS','PX') 
                AND FT.SHOW_ON_BILL_SW='Y' 
                AND FT.SIBLING_ID=PS.PAY_SEG_ID 
                AND PS.PAY_ID = PY.PAY_ID 
                AND PE.PAY_EVENT_ID = PY.PAY_EVENT_ID 
         GROUP BY FT.FT_TYPE_FLG ,PE.PAY_DT ,PY.CAN_RSN_CD ,FT.CURRENCY_CD 
      END
  
   ELSE  
      BEGIN
         SELECT MESSAGE_NBR RETURN_CODE
                ,MESSAGE_TEXT
                ,CAST(NULL AS DATETIME) PAY_DT
                ,CAST(NULL AS CHAR) FT_TYPE_FLG
                ,CAST(NULL AS CHAR) CAN_RSN_CD
                ,CAST(NULL AS CHAR) CURRENCY_CD
                ,CAST(NULL AS INT) CUR_AMT
                ,CAST(NULL AS INT) TOT_AMT
         FROM   CI_MSG_L
         WHERE  MESSAGE_CAT_NBR = 30
                AND MESSAGE_NBR = @V_RETURN_CD
                AND LANGUAGE_CD = @V_LANGUAGE_CD
      END

RETURN
         
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

