-- ******************************************************************
-- *
-- * 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 = 'CIBLPRTV' AND type = 'P')
   DROP PROCEDURE RPTUSER.CIBLPRTV 
GO

CREATE PROCEDURE RPTUSER.CIBLPRTV
@P_USER_ID CHAR(8),
@P_FROM_BILL_ID CHAR(12),
@P_LANGUAGE_CD CHAR(3)
AS

/******************************************************************************
   NAME:       CIBLPRTV
   PURPOSE:    Supply report extract for Crystal Report - Get Bill Message(s)

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

   DECLARE   @V_LANGUAGE_CD   CHAR(3)
   DECLARE   @V_USER_LANG_CD  CHAR(3)
   DECLARE   @V_FROM_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   CI_LANGUAGE
   WHERE  LANGUAGE_CD = @P_LANGUAGE_CD

   IF @@ROWCOUNT = 0
      SET @V_LANGUAGE_CD = 'ENG'

   IF @V_RETURN_CD = 0
      BEGIN
         SELECT @V_USER_LANG_CD = LANGUAGE_CD
         FROM   SC_USER
         WHERE  SC_USER.USER_ID  = @P_USER_ID

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

   IF @V_RETURN_CD = 0
      BEGIN
         SELECT @V_FROM_BILL_ID = BILL_ID
         FROM   CI_BILL
         WHERE  BILL_ID = @P_FROM_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
                ,NULL MESSAGE_TEXT
                ,BMSG.MSG_PRIORITY_FLG
                ,BMSG.INSERT_CD
                ,BMSGL.MSG_ON_BILL
         FROM   CI_BILL_MSGS BMSGS
                ,CI_BILL_MSG BMSG
                ,CI_BILL_MSG_L BMSGL 
         WHERE  BMSGS.BILL_ID = @V_FROM_BILL_ID  
                AND BMSGS.BILL_MSG_CD = BMSG.BILL_MSG_CD 
                AND BMSGL.BILL_MSG_CD = BMSG.BILL_MSG_CD 
                AND BMSGL.LANGUAGE_CD = @V_LANGUAGE_CD
         ORDER BY BMSG.MSG_PRIORITY_FLG 		  
      END
  
   ELSE  
      BEGIN
         SELECT MESSAGE_NBR RETURN_CODE
                ,MESSAGE_TEXT
                ,NULL MSG_PRIORITY_FLG
                ,NULL INSERT_CD
                ,NULL MSG_ON_BILL
         FROM   CI_MSG_L
         WHERE  MESSAGE_CAT_NBR = 30
	        AND MESSAGE_NBR = @V_RETURN_CD
	        AND LANGUAGE_CD = @V_USER_LANG_CD;
      END

RETURN
         
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

