CREATE OR REPLACE FUNCTION QUERY_DTENTRSAID_DFS48000(P_FILOPELOG IN FILIAL.CDFILIAL%TYPE,
                                                     P_DTENTRSAID DATE) RETURN VARCHAR
IS
   V_ERRO VARCHAR2(30) := '';/*Data_Mai   Data informada não deve ser maior que a data de processamento.
                               Data_Men   Data informada não deve ser menor que a data de processamento.
                               Data_M     Data informada fora do período permitido.
                               Sem_Serie  Filial OL sem serie padrão cadastrada
                               Null       Data correta.*/
   V_CONTAERRO NUMERIC;

BEGIN
   FOR C_PARAM IN(SELECT PE.IDLANCDTMAI, PE.IDLANCDTMEN, PF.IDLANCDTMENFIL, PF.NRDIASSAIDFIL,
                         PE.NRDIASSAID, (CASE PG.IDUTILDTPROC
                                            WHEN 'N'
                                            THEN SYSDATE
                                            ELSE DTPROCESSA
                                         END) DTPROCESSA, PF.IDLANCDTMAIFIL, PF.NRDIASSAIDPDFIL, PE.NRDIASSAIDPD
                    FROM (SELECT P_FILOPELOG AS CDFILIAL, NVL(IDUTILDTPROC, 'N') IDUTILDTPROC
                            FROM PARAMGERAL PG) PG,
                         (SELECT PF.CDFILIAL, NVL(PF.DTPROCESSA, SYSDATE) DTPROCESSA, PF.IDLANCDTMENFIL,
                                 PF.NRDIASSAIDFIL, PF.IDLANCDTMAIFIL, PF.NRDIASSAIDPDFIL
                            FROM PARAMFILIAL PF
                           WHERE PF.CDFILIAL = P_FILOPELOG) PF,
                         (SELECT P_FILOPELOG AS CDFILIAL, PE.IDLANCDTMAI, PE.IDLANCDTMEN, PE.NRDIASSAID, PE.NRDIASSAIDPD
                            FROM PARAMESTOQUE PE) PE
                   WHERE PG.CDFILIAL = PF.CDFILIAL(+)
                     AND PG.CDFILIAL = PE.CDFILIAL(+))
   LOOP
      SELECT COUNT(1)
        INTO V_CONTAERRO
        FROM FILIAL F
       WHERE F.CDFILIAL   = P_FILOPELOG
         AND NVL(F.CDSERIE, 'TEKNISA') <> 'TEKNISA' ;
      IF  V_CONTAERRO > 0 THEN
         IF (C_PARAM.IDLANCDTMAI = 'N') AND (C_PARAM.IDLANCDTMAIFIL = 'N') AND
            (P_DTENTRSAID > C_PARAM.DTPROCESSA) THEN
            V_ERRO := 'Data_Mai';
         END IF;

         IF (C_PARAM.IDLANCDTMEN = 'N') AND (C_PARAM.IDLANCDTMENFIL = 'N') AND
            (P_DTENTRSAID < C_PARAM.DTPROCESSA) THEN
            V_ERRO := 'Data_Men';
         END IF;

         IF (C_PARAM.IDLANCDTMENFIL = 'S') AND (P_DTENTRSAID < C_PARAM.DTPROCESSA) THEN
            IF (P_DTENTRSAID < (C_PARAM.DTPROCESSA - C_PARAM.NRDIASSAIDFIL)) THEN
               V_ERRO := 'Data_M';
            END IF;
         ELSE
            IF (C_PARAM.IDLANCDTMEN = 'S') AND (P_DTENTRSAID < C_PARAM.DTPROCESSA) THEN
               IF (P_DTENTRSAID < (C_PARAM.DTPROCESSA - C_PARAM.NRDIASSAID)) THEN
                  V_ERRO := 'Data_M';
               END IF;
            END IF;
         END IF;
         IF (C_PARAM.IDLANCDTMAIFIL = 'S') AND (P_DTENTRSAID > C_PARAM.DTPROCESSA) THEN
            IF (P_DTENTRSAID > C_PARAM.DTPROCESSA + C_PARAM.NRDIASSAIDPDFIL) THEN
               V_ERRO := 'Data_M';
            END IF;
         ELSE
            IF (C_PARAM.IDLANCDTMAI = 'S') AND (P_DTENTRSAID > C_PARAM.DTPROCESSA) THEN
               IF (P_DTENTRSAID > (C_PARAM.DTPROCESSA + C_PARAM.NRDIASSAIDPD)) THEN
                  V_ERRO := 'Data_M';
               END IF;
            END IF;
         END IF;
      ELSE
         V_ERRO := 'Sem_Serie';
      END IF;
   END LOOP;
   RETURN(V_ERRO);
END;
/