IF EXISTS (SELECT * FROM SYSOBJECTS O, SYSUSERS U
           WHERE O.NAME = 'CONTCTBL_IMPOSTO' AND
                 TYPE = 'FN' 		       AND
	              U.UID = O.UID            AND
	              U.NAME = USER)
DROP FUNCTION CONTCTBL_IMPOSTO
GO

CREATE FUNCTION CONTCTBL_IMPOSTO (
   @P_CDIMPOSTO       VARCHAR(2),
   @P_IDTIPOCONT      VARCHAR(1),
   @P_IDENTRSAIDOP    VARCHAR(1),
   @P_CDTIPOOPER      VARCHAR(2),
   @P_IDTPIJURNF      VARCHAR(1)) RETURNS VARCHAR(20)
AS
BEGIN
   DECLARE @V_CDCCTBLDESP     VARCHAR(20);
   DECLARE @V_CDCCTBLPASS     VARCHAR(20);
   DECLARE @V_CDCCTBLATIV     VARCHAR(20);
                                               
   DECLARE @V_CDCCTBDESPIF    VARCHAR(20);
   DECLARE @V_CDCCTBPASSIF    VARCHAR(20);
   DECLARE @V_CDCCTBATIVIF    VARCHAR(20);
                                               
   DECLARE @V_CDCCTBDESPTO    VARCHAR(20);
   DECLARE @V_CDCCTBPASSTO    VARCHAR(20);
   DECLARE @V_CDCCTBATIVTO    VARCHAR(20);

   DECLARE @V_IDIMPORECUP     VARCHAR(2);
   DECLARE @V_CDCONTCTBLIMP   VARCHAR(20);
   DECLARE @V_DSRETURNAUX     VARCHAR(20);
                                               
                                               
   SELECT
     @V_CDCCTBLDESP = CDCCTBLDESP,
     @V_CDCCTBLPASS = CDCCTBLPASS,
     @V_CDCCTBLATIV = CDCCTBLATIV,
     @V_IDIMPORECUP = IDIMPORECUP
   FROM
     IMPOSTO
   WHERE
     CDIMPOSTO = @P_CDIMPOSTO;
                                               
   SELECT                                               
     @V_CDCCTBDESPIF = CDCCTBDESPIF,
     @V_CDCCTBPASSIF = CDCCTBPASSIF,
     @V_CDCCTBATIVIF = CDCCTBATIVIF
   FROM 
     CCTBIMPFORN
   WHERE 
     CDIMPOSTO = @P_CDIMPOSTO 
   AND
     IDTPIJURIMPO = @P_IDTPIJURNF;
                                               
   SELECT 
     @V_CDCCTBDESPTO = CDCCTBDESPTO,
     @V_CDCCTBPASSTO = CDCCTBPASSTO,
     @V_CDCCTBATIVTO = CDCCTBATIVTO
   FROM 
     CTBIMPTPOPER
   WHERE
     CDIMPOSTO    = @P_CDIMPOSTO
   AND 
     IDENTRSAIDOP = @P_IDENTRSAIDOP
   AND 
     CDTIPOOPER   = @P_CDTIPOOPER;
                                               
                                               
   IF @V_CDCCTBATIVTO IS NOT NULL BEGIN
      SET @V_CDCCTBLATIV = @V_CDCCTBATIVTO;
   END
   ELSE IF @V_CDCCTBATIVIF IS NOT NULL BEGIN
           SET @V_CDCCTBLATIV = @V_CDCCTBATIVIF;
        END;
                                               
   IF @V_CDCCTBPASSTO IS NOT NULL BEGIN
      SET @V_CDCCTBLPASS = @V_CDCCTBPASSTO;
   END
   ELSE IF @V_CDCCTBPASSIF IS NOT NULL BEGIN
           SET @V_CDCCTBLPASS = @V_CDCCTBPASSIF;
        END;
                                               
   IF @V_CDCCTBDESPTO IS NOT NULL BEGIN
      SET @V_CDCCTBLDESP = @V_CDCCTBDESPTO;
   END
   ELSE IF @V_CDCCTBDESPIF IS NOT NULL BEGIN
           SET @V_CDCCTBLDESP = @V_CDCCTBDESPIF;
        END;
                                               
   IF @V_IDIMPORECUP = 'S' BEGIN
      SET @V_CDCONTCTBLIMP = @V_CDCCTBLATIV;
   END
   ELSE BEGIN
      SET @V_CDCONTCTBLIMP = @V_CDCCTBLPASS;
   END;
                                               
   IF @P_IDTIPOCONT = 'A' BEGIN
      SET @V_DSRETURNAUX = @V_CDCCTBLATIV;
   END
   ELSE IF @P_IDTIPOCONT = 'P' BEGIN
           SET @V_DSRETURNAUX = @V_CDCCTBLPASS;
        END
        ELSE IF @P_IDTIPOCONT = 'D' BEGIN
                SET @V_DSRETURNAUX = @V_CDCCTBLDESP;
              END
              ELSE IF @P_IDTIPOCONT = 'E' BEGIN
                   SET @V_DSRETURNAUX = @V_CDCONTCTBLIMP;
              END;

   RETURN (@V_DSRETURNAUX);
END
GO