CREATE OR REPLACE FUNCTION QUERY_TIPOCUSTO_DFS48000(P_FILOPELOG IN FILIAL.CDFILIAL%TYPE) RETURN VARCHAR
IS
   V_CDFCUSNFROMA    PARAMFILIAL.CDFCUSNFROMA%TYPE;
   V_IDTCUSNFROMA    PARAMFILIAL.IDTCUSNFROMA%TYPE;
   V_TIPOCUSTO       PARAMFILIAL.CDTIPCUSTEST%TYPE;
BEGIN
   SELECT MAX(CDFCUSNFROMA), MAX(NVL(IDTCUSNFROMA, '¿')), MAX(TO_NUMBER(NVL(CDTIPCUSTEST,'03')))
          INTO V_CDFCUSNFROMA, V_IDTCUSNFROMA, V_TIPOCUSTO
     FROM PARAMFILIAL
    WHERE CDFILIAL = P_FILOPELOG;
   IF V_CDFCUSNFROMA IS NOT NULL THEN
      SELECT MAX(CDFCUSNFROMA), MAX(NVL(IDTCUSNFROMA, '¿')), MAX(TO_NUMBER(NVL(CDTIPCUSTEST,'03')))
             INTO V_CDFCUSNFROMA, V_IDTCUSNFROMA, V_TIPOCUSTO
        FROM PARAMFILIAL
       WHERE CDFILIAL = V_CDFCUSNFROMA;
   ELSE
      SELECT MAX(CDFCUSNFROMA), MAX(NVL(IDTCUSNFROMA, '¿')), MAX(TO_NUMBER(NVL(CDTIPCUSTEST,'03')))
             INTO V_CDFCUSNFROMA, V_IDTCUSNFROMA, V_TIPOCUSTO
        FROM PARAMFILIAL
       WHERE CDFILIAL = P_FILOPELOG;
   END IF;
   IF  V_IDTCUSNFROMA <> '¿' THEN
      V_TIPOCUSTO := TO_NUMBER(V_IDTCUSNFROMA);
   END IF;
   RETURN(NVL(V_TIPOCUSTO, '03'));
END;
/