create or replace FUNCTION POSSUI_PEDIDO_SC(
   P_CDFILIAL IN FILIAL.CDFILIAL%TYPE,
   P_CDFILLANCA IN ITEMSOLI.CDFILLAN%TYPE,
   P_CDFILISOLI IN ITEMSOLI.CDFILSOLI%TYPE,
   P_NRSOLICMP IN ITEMSOLI.NRSOLICMP%TYPE,
   P_DTPROGENTR IN ITEMSOLI.DTPROGENTR%TYPE) RETURN VARCHAR2 IS

   V_COUNT INTEGER;
   V_IDIMPSCDTENTR PARAMIMPPEDIDO.IDIMPSCDTENTR%TYPE;

BEGIN
   V_COUNT := 0;
   SELECT MAX(IDIMPSCDTENTR)
     INTO V_IDIMPSCDTENTR
     FROM PARAMIMPPEDIDO
     WHERE CDFILIAL = P_CDFILIAL;

   IF V_IDIMPSCDTENTR = 'S' THEN
      SELECT COUNT(1)
        INTO V_COUNT
        FROM PEDIDO
       WHERE CDFILLANCA = P_CDFILLANCA
         AND CDFILSOLI = P_CDFILISOLI
         AND NRSOLICMP = P_NRSOLICMP
         AND DTENTPED = P_DTPROGENTR;
    ELSE
      SELECT COUNT(1)
        INTO V_COUNT
        FROM PEDIDO
       WHERE CDFILLANCA = P_CDFILLANCA
         AND CDFILSOLI = P_CDFILISOLI
         AND NRSOLICMP = P_NRSOLICMP;
   END IF;
   IF V_COUNT = 0 THEN
      RETURN('N');
   ELSE
      RETURN('S');
   END IF;
END;
/