CREATE OR REPLACE FUNCTION BUSCA_SITUACAO_ITEMPEDIDO(P_CDFILIAL  IN ITEMPEDIDO.CDFILIAL%TYPE,
                                                     P_NRPEDIDO  IN ITEMPEDIDO.NRPEDIDO%TYPE)RETURN VARCHAR2
AS
   V_CDMODPGTO    MODPGTO.CDMODPGTO%TYPE;
   V_IDTIPOPEDIDO PEDIDO.IDTIPOPEDIDO%TYPE;
   V_IDLANCPEDMOD MODPGTO.IDLANCPEDMOD%TYPE;
   V_IDSITITEMPED PEDIDO.IDSITPEDIDO%TYPE;
   V_IDLANCPED    PARAMPEDIDO.IDLANCPED%TYPE;
BEGIN
   BEGIN
      SELECT CDMODPGTO, IDTIPOPEDIDO
        INTO V_CDMODPGTO, V_IDTIPOPEDIDO
        FROM PEDIDO
       WHERE CDFILIAL = P_CDFILIAL
         AND NRPEDIDO = P_NRPEDIDO;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN BEGIN
         V_CDMODPGTO := NULL; V_IDTIPOPEDIDO := '1';
      END;
   END;
   IF (V_IDTIPOPEDIDO = '2') THEN
      V_IDSITITEMPED := '1';
   ELSE
      BEGIN
         SELECT IDLANCPEDMOD
           INTO V_IDLANCPEDMOD
           FROM MODPGTO
          WHERE CDMODPGTO = V_CDMODPGTO;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_IDLANCPEDMOD := NULL;
         END;
      END;

      V_IDSITITEMPED := NULL;
      IF (V_IDLANCPEDMOD IS NOT NULL) THEN
          V_IDSITITEMPED := V_IDLANCPEDMOD;
      ELSE
         BEGIN
            SELECT NVL(IDLANCPED, '1')
              INTO V_IDLANCPED
              FROM PARAMPEDIDO
             WHERE CDFILIAL = P_CDFILIAL;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_IDLANCPED := '1';
            END;
         END;
         V_IDSITITEMPED := V_IDLANCPED;
      END IF;
   END IF;
   RETURN(V_IDSITITEMPED);
END;
/