CREATE OR REPLACE
FUNCTION QTDE_SERVICO_COC
  (P_CDFILIAL IN VARCHAR2,
   P_CDCIA IN VARCHAR2,
   P_NRVOO IN VARCHAR2,
   P_CDTRECHO IN VARCHAR2,
   P_CDSERVICO IN VARCHAR2,   
   P_QTTOTPAX IN FLOAT,
   P_IDPROPSERV IN VARCHAR2,
   P_REFSERV IN FLOAT
   )
RETURN FLOAT
IS
   V_QTSERVICO FLOAT;
BEGIN

   /*1 por PAX*/
   IF P_IDPROPSERV = '1' THEN
      RETURN P_QTTOTPAX;
   
   /* Proporcional fixa */
   ELSIF P_IDPROPSERV = '2' THEN
      IF NVL(P_REFSERV, 0) = 0 THEN
         RETURN 0;
      ELSE
         RETURN CEIL(P_QTTOTPAX/P_REFSERV);
      END IF;
   
   /* Proporcional variavel */
   ELSIF P_IDPROPSERV = '3' THEN
      BEGIN
         SELECT MAX(QTSERVICO)
           INTO V_QTSERVICO
           FROM PROPSERVICO
          WHERE CDFILIAL = P_CDFILIAL
            AND CDCIA = P_CDCIA
            AND NRVOO = P_NRVOO
            AND CDTRECHO = P_CDTRECHO
            AND CDSERVICO = P_CDSERVICO
            AND P_QTTOTPAX BETWEEN QTPAXDE AND QTPAXATE;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            RETURN 0;
      END;
      RETURN V_QTSERVICO;
   /* Loading */   
   ELSIF P_IDPROPSERV = '4' THEN
      RETURN CEIL(P_QTTOTPAX * NVL(P_REFSERV, 0)/100);
   
   /* Quantidade fixa*/
   ELSIF P_IDPROPSERV = '5' THEN
      RETURN P_REFSERV;
      
   ELSE
      RETURN 0;
      
   END IF;
END;
/