CREATE OR REPLACE FUNCTION VALID_PROD_SEMMOVI(P_CDFILIAL     IN VARCHAR, 
                            P_CDALMOXARIFE IN VARCHAR, 
                            P_CDLOCALESTOQ IN VARCHAR, 
                            P_NRLOTEESTQ   IN VARCHAR, 
                            P_NRSUBLOTE    IN VARCHAR, 
                            P_CDPRODMOVI   IN VARCHAR, 
                            P_DTLIMITE     IN DATE, 
                            P_NRORG        IN NUMBER) RETURN NUMBER 
IS    
V_COUNT NUMBER := 0;
BEGIN
   BEGIN
   SELECT 1
     INTO V_COUNT
     FROM ITLANCTOEST J, (SELECT MAX(C.DTLANCMOVI) DTLANCMOVI
                            FROM ITLANCTOEST C
                           WHERE C.CDFILIMOVI          = P_CDFILIAL
                             AND C.CDALMOXARIFE        = P_CDALMOXARIFE
                             AND C.CDLOCALESTOQ        = P_CDLOCALESTOQ
                             AND C.NRLOTEESTQ          = P_NRLOTEESTQ
                             AND C.NRSUBLOTE           = P_NRSUBLOTE
                             AND (((C.IDTIPOMOVI <> '0') AND (C.IDTIPOMOVI <> '1')) OR ('T' = 'T'))
                             AND (C.CDPRODMOVI           = P_CDPRODMOVI)
                             AND (NVL(C.IDEVOLESTOQ,'S') = 'S')
                             AND C.NRORG                 = P_NRORG) B
    WHERE J.CDFILIMOVI         = P_CDFILIAL
      AND J.CDALMOXARIFE         = P_CDALMOXARIFE
      AND J.CDLOCALESTOQ         = P_CDLOCALESTOQ
      AND J.NRLOTEESTQ           = P_NRLOTEESTQ
      AND J.NRSUBLOTE            = P_NRSUBLOTE
      AND (((J.IDTIPOMOVI <> '0') AND (J.IDTIPOMOVI <> '1'))OR ('T' = 'T'))
      AND J.CDPRODMOVI           = P_CDPRODMOVI
      AND (NVL(J.IDEVOLESTOQ,'S') = 'S')
      AND J.DTLANCMOVI            = B.DTLANCMOVI
      AND J.DTLANCMOVI           <= P_DTLIMITE
      AND J.NRORG                 = P_NRORG;
    EXCEPTION 
      WHEN OTHERS THEN
         V_COUNT := 0;
    END;
   
   RETURN V_COUNT; 
   
END VALID_PROD_SEMMOVI;
/

-- VERSAO --
BEGIN ATUVEROBJ('VALID_PROD_SEMMOVI','6.10.xxx'); END;
/