CREATE OR REPLACE FUNCTION GERAR_COMPLEMENTAR_PREV_PROD(
  P_CDFILIAL          IN PREVPAXGERARCOMPL.CDFILIAL%TYPE,
  P_NRPREVISAOPROD    IN PREVPAXGERARCOMPL.NRPREVISAOPROD%TYPE,
  P_NRORG             IN PREVPAXGERARCOMPL.NRORG%TYPE
) RETURN VARCHAR2 
AS 
  V_IDGERACOMPL VARCHAR2(1);
  V_COUNTCOMPL  NUMBER;
  V_DTPREVISAOPROD DATE;
BEGIN
    
    V_IDGERACOMPL := 'S';
    V_COUNTCOMPL := 0;
    
    /*Data de Producao da Previsao*/  
    SELECT MAX(DTPREVISAOPROD) 
      INTO V_DTPREVISAOPROD
      FROM PREVPAXPRODM
    WHERE CDFILIAL          = P_CDFILIAL
    AND   NRPREVISAOPROD    = P_NRPREVISAOPROD;
    
    SELECT COUNT(NRPREVPAXGERARCOMPL)
         INTO V_COUNTCOMPL 
        FROM PREVPAXGERARCOMPL
         WHERE CDFILIAL          = P_CDFILIAL
         AND   DTGERARCOMPL    = V_DTPREVISAOPROD
         AND   NRORG             = P_NRORG;
  
  IF V_COUNTCOMPL = 0 THEN
    V_IDGERACOMPL := 'N';
  END IF;
  
  RETURN V_IDGERACOMPL;
END;
/