CREATE OR REPLACE FUNCTION RETURN_EQUIP_VOO
   (P_CDFILIAL   IN NOTAFISCAL.CDFILIAL%TYPE,
    P_NRLANCTONF IN NOTAFISCAL.NRLANCTONF%TYPE) RETURN STRING
AS
   V_SGEQUIP  EQUIPAMENTO.SGEQUIP%TYPE;
   V_NRVOO    PLANOVOO.NRVOO%TYPE;
   V_IDTIPVOO VOO.IDTIPVOO%TYPE;
BEGIN
   SELECT SUBSTR(REPLACE(MAX(E.SGEQUIP),'-',''),1,4), MAX(PV.NRVOO), MAX(VO.IDTIPVOO)
   INTO V_SGEQUIP, V_NRVOO, V_IDTIPVOO
   FROM SERVPLANO SP, PLANOVOO PV, VOO VO, EQUIPAMENTO E
   WHERE SP.CDFILIALNF   = P_CDFILIAL      AND
         SP.NRLANCTONF   = P_NRLANCTONF    AND
         PV.CDFILIAL     = SP.CDFILIAL     AND
         PV.CDCIA        = SP.CDCIA        AND
         PV.NRVOO        = SP.NRVOO        AND
         PV.CDTRECHO     = SP.CDTRECHO     AND
         PV.DTHRPLAVOOPV = SP.DTHRPLAVOOPV AND
         VO.CDFILIAL     = SP.CDFILIAL     AND
         VO.CDCIA        = SP.CDCIA        AND
         VO.NRVOO        = SP.NRVOO        AND
         PV.CDEQUIP      = E.CDEQUIP;

   IF V_NRVOO LIKE 'VIP%' THEN
      RETURN(RPAD(NVL(V_SGEQUIP,' '), 7)||'01');
   ELSIF V_NRVOO LIKE '9%' THEN
      RETURN(RPAD(NVL(V_SGEQUIP,' '), 7)||'04');
   ELSIF V_IDTIPVOO = '1' THEN
      RETURN(RPAD(NVL(V_SGEQUIP,' '), 7)||'03');
   ELSE
      RETURN(RPAD(NVL(V_SGEQUIP,' '), 7)||'05');
   END IF;
END;
/


