CREATE OR REPLACE FUNCTION PRATO_QTDEPADRAO(
   P_CDFILIAL IN VARCHAR2,
   P_CDSERVFILI IN VARCHAR2,
   P_CDPRATO IN VARCHAR2,
   P_CDFILAUXPRAT IN VARCHAR2) RETURN NUMBER IS
   V_QTDEPADRO NUMBER;
   V_RECEITA VARCHAR2(1);
BEGIN
   V_RECEITA := RECEITA_UTILIZADA(P_CDPRATO, P_CDFILAUXPRAT, P_CDFILIAL, P_CDSERVFILI);
   IF V_RECEITA = 'S' THEN 
      SELECT MAX(QTPADRAOSERV) INTO V_QTDEPADRO
      FROM PRATSERV 
      WHERE CDFILIAL = P_CDFILIAL 
        AND CDSERVFILI = P_CDSERVFILI 
        AND CDPRATO = P_CDPRATO 
        AND CDFILAUXPRAT = P_CDFILAUXPRAT;
   END IF;

   IF V_RECEITA = 'F' THEN 
      SELECT MAX(QTPADRAOFILI) INTO V_QTDEPADRO
      FROM PRATFILI 
      WHERE CDFILIAL = P_CDFILIAL 
        AND CDFILAUXPRAT = P_CDFILAUXPRAT
        AND CDPRATO = P_CDPRATO;
   END IF;

   IF V_RECEITA = 'P' THEN 
      SELECT MAX(QTPADRAOPRAT) INTO V_QTDEPADRO
      FROM PRATPADR
      WHERE CDFILAUXPRAT = P_CDFILAUXPRAT
        AND CDPRATO = P_CDPRATO;   
   END IF;
   RETURN(V_QTDEPADRO);
END;
/