CREATE OR REPLACE 
FUNCTION FUN_CONSULTA_CDINSCESTA
   (P_CDFILIAL    IN FILIAL.CDFILIAL%TYPE,
    P_CDINSCESTA  IN INSCRESTAD.CDINSCESTA%TYPE,
    P_CDEMPRESA   IN FILIAL.CDEMPRESA%TYPE,
    P_CDEMPRESAAP IN FILIAL.CDEMPRESA%TYPE) RETURN CHAR
IS 
   V_CDINSCESTA   INSCRESTAD.CDINSCESTA%TYPE; 
   V_CDEMPRESA    EMPRESA.CDEMPRESA%TYPE;
   V_NRINSJURCLIE CLIENTE.NRINSJURCLIE%TYPE;
   V_IDTIPEMITGER PARCMPSUP.IDTIPEMITGER%TYPE;
BEGIN 
   SELECT NVL(MAX(IDTIPEMITGER), '0')
     INTO V_IDTIPEMITGER
     FROM PARCMPSUP
    WHERE CDFILIAL = P_CDFILIAL;

   IF V_IDTIPEMITGER <> '3' THEN
      RETURN(V_IDTIPEMITGER||P_CDEMPRESA||'-'||P_CDINSCESTA);
   ELSE
      SELECT NVL(MAX(CLIENTE.NRINSESTCLIE), 'WWW')
        INTO V_NRINSJURCLIE
        FROM CLIENFILIAL, CLIENTE
       WHERE CLIENFILIAL.CDCLIENTE = CLIENTE.CDCLIENTE
        AND CLIENFILIAL.CDFILIAL = P_CDFILIAL
        AND NVL(CLIENFILIAL.IDCLIENPRIN, 'N') = 'S';
      RETURN(V_IDTIPEMITGER||P_CDEMPRESA||'-'||P_CDINSCESTA);
      /*IF V_NRINSJURCLIE = 'WWW' THEN
         RETURN(V_IDTIPEMITGER||P_CDEMPRESA||'-'||P_CDINSCESTA);
      ELSE
         RETURN(V_IDTIPEMITGER||P_CDEMPRESAAP||'-'||V_NRINSJURCLIE);
      END IF;*/
   END IF;
END;
/
