CREATE OR REPLACE FUNCTION ORIGEM_NOTA
   (P_CDFILIAL   IN NOTAFISCAL.CDFILIAL%TYPE,
    P_NRLANCTONF IN NOTAFISCAL.NRLANCTONF%TYPE,
    P_ENTRADA    IN NOTAFISCAL.IDSTATUS%TYPE) RETURN VARCHAR
AS
   V_IDORIG          VARCHAR2(01);
   V_CDESTAEXPO      VARCHAR2(02);
   V_SGESTACFOP      VARCHAR2(02);
   V_IDMUNIALC       VARCHAR2(01);
   V_CDMUNICFOP      VARCHAR2(04);
   V_CDMUNIEXPO      VARCHAR2(05);
   V_CDMUNIIBGE      VARCHAR2(07);
   V_IDENTRSAIDOP    VARCHAR2(10);
BEGIN
   BEGIN
      SELECT N.IDORIGDESTNF, IDENTRSAIDOP
      INTO V_IDORIG, V_IDENTRSAIDOP
      FROM NOTAFISCAL N
      WHERE N.CDFILIAL     = P_CDFILIAL     AND
            N.NRLANCTONF   = P_NRLANCTONF;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_IDORIG := NULL;
   END;

   IF V_IDORIG IS NULL THEN
      IF V_IDENTRSAIDOP = 'E' THEN
         V_IDORIG := 'F';
      ELSE
         V_IDORIG := 'C';
      END IF;
   END IF;

   /* cliente */
   IF (V_IDORIG = 'C') THEN
      BEGIN
         SELECT ET.CDESTAEXPO, E.SGESTADO AS SGESTACFOP, NVL(M.IDMUNIALC,' ') AS IDMUNIALC,
                E.CDMUNICIPIO AS CDMUNICFOP, M.CDMUNIEXPO, M.CDMUNICIBGE
         INTO V_CDESTAEXPO, V_SGESTACFOP, V_IDMUNIALC, V_CDMUNICFOP, V_CDMUNIEXPO, V_CDMUNIIBGE
         FROM NOTAFISCAL N, CLIENTE C, ENDECLIE E, ESTADO ET, MUNICIPIO M
         WHERE N.CDFILIAL     = P_CDFILIAL      AND
               N.NRLANCTONF   = P_NRLANCTONF    AND
               N.CDCLIENTE    = C.CDCLIENTE     AND
               E.CDCLIENTE    = C.CDCLIENTE     AND
               E.CDPAIS       = ET.CDPAIS       AND
               E.SGESTADO     = ET.SGESTADO     AND
               E.CDPAIS       = M.CDPAIS        AND
               E.SGESTADO     = M.SGESTADO      AND
               E.CDMUNICIPIO  = M.CDMUNICIPIO   AND
               E.IDTPENDECLIE = 'P';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDESTAEXPO := NULL;
            V_SGESTACFOP := NULL;
            V_IDMUNIALC  := NULL;
            V_CDMUNICFOP := NULL;
            V_CDMUNIEXPO := NULL;
            V_CDMUNIIBGE := NULL;
      END;
   END IF;

   /* fornecedor */
   IF (V_IDORIG = 'F') THEN
      BEGIN
         SELECT ET.CDESTAEXPO, E.SGESTADO AS SGESTACFOP, NVL(M.IDMUNIALC,' ') AS IDMUNIALC,
                E.CDMUNICIPIO AS CDMUNICFOP, M.CDMUNIEXPO, M.CDMUNICIBGE
         INTO V_CDESTAEXPO, V_SGESTACFOP, V_IDMUNIALC, V_CDMUNICFOP, V_CDMUNIEXPO, V_CDMUNIIBGE
         FROM NOTAFISCAL N, FORNECEDOR F, ENDEFORN E, ESTADO ET, MUNICIPIO M
         WHERE N.CDFILIAL     = P_CDFILIAL      AND
               N.NRLANCTONF   = P_NRLANCTONF    AND
               N.CDFORNECED   = F.CDFORNECED    AND
               E.CDFORNECED   = F.CDFORNECED    AND
               E.CDPAIS       = ET.CDPAIS       AND
               E.SGESTADO     = ET.SGESTADO     AND
               E.CDPAIS       = M.CDPAIS        AND
               E.SGESTADO     = M.SGESTADO      AND
               E.CDMUNICIPIO  = M.CDMUNICIPIO   AND
               E.IDTPENDEFORN = 'P';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDESTAEXPO := NULL;
            V_SGESTACFOP := NULL;
            V_IDMUNIALC  := NULL;
            V_CDMUNICFOP := NULL;
            V_CDMUNIEXPO := NULL;
            V_CDMUNIIBGE := NULL;
      END;
   END IF;

   /* filial */
   IF (V_IDORIG = 'L') THEN
      BEGIN
         SELECT ET.CDESTAEXPO, E.SGESTADO AS SGESTACFOP, NVL(M.IDMUNIALC,' ') AS IDMUNIALC,
                E.CDMUNICIPIO AS CDMUNICFOP, M.CDMUNIEXPO, M.CDMUNICIBGE
         INTO V_CDESTAEXPO, V_SGESTACFOP, V_IDMUNIALC, V_CDMUNICFOP, V_CDMUNIEXPO, V_CDMUNIIBGE
         FROM NOTAFISCAL N, FILIAL FI, ENDEFILI E, ESTADO ET, MUNICIPIO M
         WHERE N.CDFILIAL     = P_CDFILIAL      AND
               N.NRLANCTONF   = P_NRLANCTONF    AND
               N.CDFILIAL     = FI.CDFILIAL     AND
               E.CDFILIAL     = DECODE(N.IDENTRSAIDOP, 'E', N.CDFILIEMIT, N.CDFILIRECE) AND
               E.CDPAIS       = ET.CDPAIS       AND
               E.SGESTADO     = ET.SGESTADO     AND
               E.CDPAIS       = M.CDPAIS        AND
               E.SGESTADO     = M.SGESTADO      AND
               E.CDMUNICIPIO  = M.CDMUNICIPIO   AND
               E.IDTPENDEFILI = 'P';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDESTAEXPO := NULL;
            V_SGESTACFOP := NULL;
            V_IDMUNIALC  := NULL;
            V_CDMUNICFOP := NULL;
            V_CDMUNIEXPO := NULL;
            V_CDMUNIIBGE := NULL;
      END;
   END IF;
   IF P_ENTRADA = 'E' THEN
      RETURN(V_CDESTAEXPO);
   END IF;
   IF P_ENTRADA = 'C' THEN
      RETURN(V_SGESTACFOP);
   END IF;
   IF P_ENTRADA = 'M' THEN
      RETURN(V_IDMUNIALC);
   END IF;
   IF P_ENTRADA = 'U' THEN
      RETURN(V_CDMUNICFOP);
   END IF;
   IF P_ENTRADA = 'O' THEN
      RETURN(V_CDMUNIEXPO);
   END IF;
   IF P_ENTRADA = 'I' THEN
      RETURN(V_CDMUNIIBGE);
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('ORIGEM_NOTA','5.11.057'); END;
/

