CREATE OR REPLACE FUNCTION RETORNA_DATA_FATOGERADOR(
   P_CDFILIAL      IN NOTAFISCAL.CDFILIAL%TYPE,
   P_NRLANCTONF    IN NOTAFISCAL.NRLANCTONF%TYPE,
   P_IDENTRSAIDOP  IN NOTAFISCAL.IDENTRSAIDOP%TYPE,
   P_DTINICIAL     IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_DTFINAL       IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_CDIMPOSTO     IN IMPOSTONF.CDIMPOSTO%TYPE) RETURN DATE
AS
   V_IDFORMAPUR    VARCHAR2(01);
   V_IDCONSVRIMPAG VARCHAR2(01);
   V_DTFATOGERADOR DATE;
BEGIN
   BEGIN
      SELECT IDFORMAPUR, IDCONSVRIMPAG
        INTO V_IDFORMAPUR, V_IDCONSVRIMPAG
        FROM IMPOSTO
       WHERE CDIMPOSTO = P_CDIMPOSTO;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_IDFORMAPUR    := 'C';    
         V_IDCONSVRIMPAG := 'T';
   END;
   
   V_IDFORMAPUR    := NVL(V_IDFORMAPUR,'C');
   V_IDCONSVRIMPAG := NVL(V_IDCONSVRIMPAG,'T');
   
   IF V_IDFORMAPUR = 'C' AND P_IDENTRSAIDOP = 'E' THEN
      V_IDFORMAPUR := 'D';
   ELSIF V_IDFORMAPUR = 'C' AND P_IDENTRSAIDOP = 'S' THEN
      V_IDFORMAPUR := 'E';
   END IF;
   
   IF V_IDFORMAPUR = 'E' THEN      
      SELECT DTEMISSAO
        INTO V_DTFATOGERADOR
        FROM NOTAFISCAL
       WHERE CDFILIAL   = P_CDFILIAL
         AND NRLANCTONF = P_NRLANCTONF;
   ELSIF V_IDFORMAPUR = 'D' THEN
      SELECT DTENTRSAID
        INTO V_DTFATOGERADOR
        FROM NOTAFISCAL
       WHERE CDFILIAL   = P_CDFILIAL
         AND NRLANCTONF = P_NRLANCTONF;
   ELSIF V_IDFORMAPUR = 'P' THEN
      IF V_IDCONSVRIMPAG = 'T' THEN
         IF P_IDENTRSAIDOP = 'E' THEN         
            SELECT MIN(DTBAIXAPAG)
              INTO V_DTFATOGERADOR
              FROM (
            SELECT BX.DTBAIXAPAG AS DTBAIXAPAG              
              FROM CONTAPAGAR C, BOLETPAGAR B, BAIXAPAGA BX
             WHERE C.CDFILIBPAG   = B.CDFILIAL
                                                             AND C.NRLANBOLPAG  = B.NRLANBOLPAG
                                                             AND B.CDFILIBAIPAG = BX.CDFILIAL
                                                             AND B.NRBAIXAPAG   = BX.NRBAIXAPAG
                                                             AND C.CDFILIAL     = P_CDFILIAL
                                                             AND C.NRLANCTONF   = P_NRLANCTONF
                                                           UNION ALL
                                                          SELECT BX.DTBAIXAREC AS DTBAIXAPAG
                                                            FROM BOLETRECEBER B, BAIXARECEBIDA BX, DEVOLUCAOREC D
                                                           WHERE D.CDFILIBOLREC = B.CDFILIAL
                                                             AND D.NRLANBOLREC  = B.NRLANBOLREC
                                                             AND B.NRBAIXAREC   = BX.NRBAIXAREC
                                                             AND B.CDFILIBAIREC = BX.CDFILIAL
                                                             AND D.CDFILIAL     = P_CDFILIAL
                                                             AND D.NRLANCTONF   = P_NRLANCTONF);
         ELSE 
            SELECT MIN(DTBAIXAREC)
              INTO V_DTFATOGERADOR
              FROM (SELECT BX.DTBAIXAREC AS DTBAIXAREC
                                                            FROM CONTARECEBER C, BOLETRECEBER B, BAIXARECEBIDA BX
                                                           WHERE C.CDFILIBOLREC = B.CDFILIAL
                                                             AND C.NRLANBOLREC  = B.NRLANBOLREC
                                                             AND B.CDFILIBAIREC = BX.CDFILIAL
                                                             AND B.NRBAIXAREC   = BX.NRBAIXAREC
                                                             AND C.CDFILIAL     = P_CDFILIAL
                                                             AND C.NRLANCTONF   = P_NRLANCTONF
                                                           UNION ALL
                                                          SELECT BX.DTBAIXAPAG AS DTBAIXAREC
                                                            FROM BOLETPAGAR B, BAIXAPAGA BX, DEVOLUCAOPAG D
                                                           WHERE D.CDFILIBOLPAG = B.CDFILIAL
                                                             AND D.NRLANBOLPAG  = B.NRLANBOLPAG
                                                             AND B.NRBAIXAPAG   = BX.NRBAIXAPAG
                                                             AND B.CDFILIBAIPAG = BX.CDFILIAL
                                                             AND D.CDFILIAL     = P_CDFILIAL
                                                             AND D.NRLANCTONF   = P_NRLANCTONF);
         END IF;
      ELSE
         SELECT MIN(DTBAIXAREC) AS DTBAIXAREC
           INTO V_DTFATOGERADOR
           FROM IMPBAIXAREC I 
          WHERE I.CDFILIAL = P_CDFILIAL
            AND I.NRLANCTONF   = P_NRLANCTONF
            AND I.CDIMPOSTO   = P_CDIMPOSTO
            AND I.DTBAIXAREC BETWEEN P_DTINICIAL AND P_DTFINAL;
      END IF;
   END IF;
   RETURN V_DTFATOGERADOR;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('RETORNA_DATA_FATOGERADOR','6.49.030'); END;
/

