CREATE OR REPLACE FUNCTION CTB_CCTBL_IMPFIL(
P_CDFILIAL IN FILIAL.CDFILIAL%TYPE,
P_IDENTRSAIDOP IN TIPOOPERAC.IDENTRSAIDOP%TYPE,
P_CDTIPOOPER IN TIPOOPERAC.CDTIPOOPER%TYPE,
P_CDIMPOSTO IN IMPOSTO.CDIMPOSTO%TYPE,
P_CDCONTCTBL IN CONTCTBL.CDCONTCTBL%TYPE,
P_IDTIPOCONT IN TIPOOPERAC.IDENTRSAIDOP%TYPE) RETURN VARCHAR2
AS
   V_CONTA VARCHAR2(20);
BEGIN
   BEGIN
      SELECT DECODE(P_IDTIPOCONT, 'A', CDCONTATIVO, DECODE(P_IDTIPOCONT, 'P', CDCONTPASSIVO,
             DECODE(P_IDTIPOCONT, 'D', CDCONTDESPESA, DECODE(P_IDTIPOCONT, 'T', CDCONTDESPTRIB, ''))))
        INTO V_CONTA
        FROM PARAMIMPOSTOFIL
       WHERE CDFILIAL     = P_CDFILIAL
         AND IDENTRSAIDOP = P_IDENTRSAIDOP
         AND CDTIPOOPER   = P_CDTIPOOPER
         AND CDIMPOSTO    = P_CDIMPOSTO;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CONTA := NULL;
   END;

   IF V_CONTA IS NULL THEN
      RETURN P_CDCONTCTBL;
   ELSE
      RETURN V_CONTA;
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('CTB_CCTBL_IMPFIL','5.50.000'); END;
/
