
/*------ ATD 356778 ------*/
CREATE OR REPLACE FUNCTION fValidaRegimeEspecial( p_NUMORD IN NUMBER)
RETURN NUMBER
AS
    l_RETORNO NUMBER;
    l_UTILIZAREGIME NUMBER;
    l_SEMPROTOCOLO NUMBER;
BEGIN
    l_RETORNO := 1;
    l_UTILIZAREGIME := 0;
    l_SEMPROTOCOLO := 0;
    SELECT COUNT(*) INTO l_UTILIZAREGIME FROM COMPLEMENTONFSAIDA CNFS
        JOIN NFSAIDACAD NFS ON NFS.NUMORD = CNFS.NUMORD AND SUBSTR(NFS.TPO, 1,1) = '2'  
        JOIN FILIALCAD FIL ON FIL.FILIAL = NFS.FILIAL
        JOIN ADITIVO ADT ON ADT.RITEM = FIL.OID AND ADT.RDEFINICAO = 37600 AND UPPER(RTRIM(SVALOR)) = 'S'
     WHERE CNFS.NUMORD = p_NUMORD;
     IF l_UTILIZAREGIME > 0 THEN
       SELECT COUNT(*) INTO l_SEMPROTOCOLO FROM COMPLEMENTONFSAIDA CNFS
			JOIN NFSAIDACAD NFS ON NFS.NUMORD = CNFS.NUMORD AND NFS.MODELONF = '55'   
        WHERE 
         CNFS.NUMORD IN (
             SELECT ITEMCLICAD.NUMORDRET FROM ITEMCLICAD WHERE NUMORDFAT = p_NUMORD AND NUMORDRET > 0
             UNION
             SELECT ITEMCLICAD.NUMORDTRANSF FROM ITEMCLICAD WHERE NUMORDFAT = p_NUMORD AND NUMORDTRANSF > 0)
            AND NULLIF(CNFS.NUMEROPROTOCOLO,0) = 0
         AND LENGTH(NULLIF( CNFS.RECIBODPEC, '')) = 0;
      END IF;
  IF l_SEMPROTOCOLO > 0 THEN
       l_RETORNO := 0;
    END IF;
  RETURN l_RETORNO;
END fValidaRegimeEspecial
;
ALTER FUNCTION fValidaRegimeEspecial COMPILE;