CREATE OR REPLACE FUNCTION CTB_BONIFICACAO_DFS(
   P_CDFILIAL     IN NOTAFISCAL.CDFILIAL%TYPE,
   P_NRLANCTONF   IN NOTAFISCAL.NRLANCTONF%TYPE) RETURN VARCHAR2 IS

   V_CONTABILIZA  VARCHAR2(01);
BEGIN
   V_CONTABILIZA := 'N';

   BEGIN
      SELECT 'S' AS CONTAB
        INTO V_CONTABILIZA
        FROM IMPOSTONF IMP, NOTAFISCAL NF, IMPOSTO I, PARAMFILIAL PF, TIPOOPERAC T, 
             (SELECT DISTINCT CDIMPOSTO,
                              CONTCTBL_IMPOSTO(I.CDIMPOSTO, 'A', 'S', NF.CDTIPOOPER, NF.IDTPIJURNF) AS CDCONTATIV,
                              CONTCTBL_IMPOSTO(I.CDIMPOSTO, 'P', 'S', NF.CDTIPOOPER, NF.IDTPIJURNF) AS CDCONTPASS,
                              CONTCTBL_IMPOSTO(I.CDIMPOSTO, 'D', 'S', NF.CDTIPOOPER, NF.IDTPIJURNF) AS CDCONTDESP
                FROM IMPOSTONF I, NOTAFISCAL NF
               WHERE I.CDFILIAL   = NF.CDFILIAL
                 AND I.NRLANCTONF = NF.NRLANCTONF
                 AND I.CDFILIAL   = P_CDFILIAL
                 AND I.NRLANCTONF = P_NRLANCTONF) CT
       WHERE IMP.VRIMPOSTONF > 0          AND
             IMP.CDIMPOSTO = CT.CDIMPOSTO AND
             IMP.CDIMPOSTO = I.CDIMPOSTO  AND
             ((IMP.IDIMPOSTONF IN ('1','4')) OR (IMP.IDIMPOSTONF IS NULL)) AND
             I.IDTIPCONTAB  = 'D'         AND
             ((((I.IDIMPORECUP  = 'S'  AND IMP.CDFISCALNF = '1' AND PF.IDCARACFIS  = 'C') OR (PF.IDCARACFIS = 'A')) AND (CT.CDCONTPASS IS NOT NULL))
              OR
              ((I.IDIMPORECUP <> 'S') AND (CT.CDCONTPASS IS NOT NULL)))     AND
             NF.CDFILIAL       = P_CDFILIAL      AND
             NF.NRLANCTONF     = P_NRLANCTONF    AND
             NF.CDFILIEMIT     = PF.CDFILIAL     AND
             NF.CDFILIAL       = IMP.CDFILIAL    AND
             NF.NRLANCTONF     = IMP.NRLANCTONF  AND
             NF.CDTIPOOPER     = T.CDTIPOOPER    AND
             T.IDCONTSOMENIMPO = 'S'
       GROUP BY 1;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CONTABILIZA := 'N';
   END;

   RETURN(V_CONTABILIZA);
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('CTB_BONIFICACAO_DFS','5.70.XXX'); END;
/

