CREATE OR REPLACE PROCEDURE GERAR_EXP_REPLICACAO
(P_CDFILIAL        IN PEDIDO.CDFILIAL%TYPE,
 P_NRLANCTONF      IN NOTAFISCAL.NRLANCTONF%TYPE,
 P_CDTIPOOPERORIG  IN TIPOOPERAC.CDTIPOOPER%TYPE,
 P_CDFILIDEST      IN PEDIDO.CDFILIALPED%TYPE,
 P_CDTIPOOPERDEST  IN TIPOOPERAC.CDTIPOOPER%TYPE,
 P_SERIEDEST       IN SERIEENTR.CDSERIEENTR%TYPE,
 P_CDEMPRESADEST   IN EMPRESA.CDEMPRESA%TYPE DEFAULT NULL)
AS
  V_IDUTILALMOX     PARAMFILIAL.IDUTILALMOX%TYPE;
  V_IDUTILLCESTQ    PARAMFILIAL.IDUTILLCESTQ%TYPE;
  V_CDALMOXARTRD    TRABREPLICDOC.CDALMOXARTRD%TYPE;
  V_CDLOCESTQTRD    TRABREPLICDOC.CDLOCESTQTRD%TYPE;
  V_CDFILIALREPLIC  REPLICDOC.CDFILIAL%TYPE;
  V_NRREPLICDOC     REPLICDOC.NRREPLICDOC%TYPE;
  V_SEQNF           NOVOCODIGO.NRSEQUENCIAL%TYPE;
  V_NRLANCTONFDEST  NOTAFISCAL.NRLANCTONF%TYPE;
  V_SEQEST          NOVOCODIGO.NRSEQUENCIAL%TYPE;
  V_PRXLANCESTQDEST LANCTOESTOQ.NRLANCESTQ%TYPE;
  V_DTENTRSAID      DATE;
  V_DTPROCESSA      PARAMFILIAL.DTPROCESSA%TYPE;
  V_DTINICIAL       DATE;
  V_DTFINAL         DATE;
  V_CDTIPOOPER      TIPOOPERAC.CDTIPOOPER%TYPE;
  V_CDSERIE         NOTAFISCAL.CDSERIE%TYPE;
  V_CDTIPOOPERDEST  TIPOOPERAC.CDTIPOOPER%TYPE;
  V_SERIEDEST       NOTAFISCAL.CDSERIE%TYPE;
  V_IDEMPRESANFEXP  PAREXPEDICAO.IDEMPRESANFEXP%TYPE;
  V_CDEMPRESADEST   EMPRESA.CDEMPRESA%TYPE;
BEGIN
   BEGIN
      SELECT NVL(PA.IDUTILALMOX,'N'),  NVL(PA.IDUTILLCESTQ, 'N')
        INTO V_IDUTILALMOX, V_IDUTILLCESTQ
        FROM PARAMFILIAL PA
       WHERE (PA.CDFILIAL = P_CDFILIAL);
   EXCEPTION
      WHEN NO_DATA_FOUND THEN BEGIN
         V_IDUTILALMOX  := 'N';
         V_IDUTILLCESTQ := 'N';
      END;
   END;
   IF V_IDUTILALMOX = 'S' THEN
      V_CDALMOXARTRD := 'P';
   ELSE
      V_CDALMOXARTRD := ' ';
   END IF;
   IF V_IDUTILLCESTQ = 'S' THEN
      V_CDLOCESTQTRD := 'P';
   ELSE
      V_CDLOCESTQTRD := ' ';
   END IF;
   BEGIN
      SELECT DTENTRSAID,   CDTIPOOPER, CDSERIE
        INTO V_DTENTRSAID, V_CDTIPOOPER, V_CDSERIE
        FROM NOTAFISCAL
       WHERE CDFILIAL = P_CDFILIAL
         AND NRLANCTONF = P_NRLANCTONF;
   EXCEPTION
        WHEN NO_DATA_FOUND THEN BEGIN
           V_DTENTRSAID  := NULL;
           V_CDTIPOOPER  := NULL;
           V_CDSERIE     := NULL;
        END;
   END;
   V_CDTIPOOPERDEST := P_CDTIPOOPERDEST;
   IF (P_CDTIPOOPERDEST IS NULL) THEN
      V_CDTIPOOPERDEST := V_CDTIPOOPER;
   END IF;
   V_SERIEDEST := P_SERIEDEST;
   IF (P_SERIEDEST IS NULL) THEN
      V_SERIEDEST := V_CDSERIE;
   END IF;
   V_CDFILIALREPLIC := '';
   V_NRREPLICDOC    := '';
   BEGIN
      SELECT R.NRREPLICDOC, I.CDFILIAL as CDFILIALREPLIC
        INTO V_NRREPLICDOC, V_CDFILIALREPLIC
        FROM REPLICDOC R JOIN ITREPLICDOC I
                           ON (I.CDFILIAL = R.CDFILIAL)
                          AND (I.NRREPLICDOC = R.NRREPLICDOC)
       WHERE (R.CDFILIAL     = P_CDFILIAL)
         AND (R.CDTIPOOPER   = P_CDTIPOOPERORIG)
         AND (R.IDENTRSAIDOP = 'S')
         AND (I.CDFILIORIGRD = P_CDFILIAL)
         AND (I.CDFILIDESTRD = P_CDFILIDEST)
         AND (I.IDENTRSAIDOP = 'E')
         AND (I.CDTIPOOPER = V_CDTIPOOPERDEST);
   EXCEPTION
      WHEN NO_DATA_FOUND THEN BEGIN
         V_CDFILIALREPLIC := '';
         V_NRREPLICDOC    := '';
      END;
   END;
   IF (P_CDEMPRESADEST IS NULL) THEN
      BEGIN
         SELECT NVL(IDEMPRESANFEXP,'EMPRESAFILIAL')
           INTO V_IDEMPRESANFEXP
           FROM PAREXPEDICAO
          WHERE CDFILIAL = P_CDFILIAL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_IDEMPRESANFEXP  := 'EMPRESAFILIAL';
      END;
      IF (V_IDEMPRESANFEXP = 'INFORMALANCEXP') THEN
         V_CDEMPRESADEST := P_CDEMPRESADEST;
      ELSIF (V_IDEMPRESANFEXP = 'EMPRESAFILIAL') THEN
         BEGIN
            SELECT CDEMPRESA
              INTO V_CDEMPRESADEST
              FROM FILIAL
             WHERE CDFILIAL = P_CDFILIDEST;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_CDEMPRESADEST := NULL;
         END;
      END IF;
   ELSE
      V_CDEMPRESADEST := P_CDEMPRESADEST;
   END IF;
   INSERT INTO TRABREPLICDOC
   (CDFILIALTRD,     NRLANCTONFTRD,  CDFILIORIGTRD, CDFILIDESTTRD,
    CDALMOXARTRD,    CDLOCESTQTRD,   IDENTRSAIDTRD, CDTIPOOPERTRD,
    CDEMPRESATRD,    CDINSCESTATRD,  CDSERIETRD,    CDFILIALREPLIC,
    NRREPLICDOC)
   VALUES
   (P_CDFILIAL,      P_NRLANCTONF,   P_CDFILIAL,    P_CDFILIDEST,
    V_CDALMOXARTRD,  V_CDLOCESTQTRD, 'E',           V_CDTIPOOPERDEST,
    V_CDEMPRESADEST, '',             V_SERIEDEST,   V_CDFILIALREPLIC,
    V_NRREPLICDOC);

   NOVO_CODIGO('LANCNF/FILIAL' || P_CDFILIAL,V_SEQNF);
   V_NRLANCTONFDEST := SUBSTR(V_SEQNF,11,10);

   NOVO_CODIGO('LANCTOEST'|| P_CDFILIAL,V_SEQEST);
   V_PRXLANCESTQDEST := SUBSTR(V_SEQEST,11,10);

   REPLICACAO_DOCUMENTOS(P_CDFILIAL, P_NRLANCTONF, P_CDFILIAL, V_NRLANCTONFDEST, V_PRXLANCESTQDEST, 'EXP', NULL);

   BEGIN
      SELECT DECODE(PG.IDUTILDTPROC,'S', TRUNC(NVL(PF.DTPROCESSA,SYSDATE)), TRUNC(SYSDATE))
        INTO V_DTPROCESSA
        FROM PARAMFILIAL PF,
             (SELECT IDUTILDTPROC FROM PARAMGERAL WHERE CDPARGERAL = '0001') PG
       WHERE PF.CDFILIAL = P_CDFILIAL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_DTPROCESSA := TRUNC(SYSDATE);
   END;
   IF V_DTENTRSAID < V_DTPROCESSA THEN
      V_DTINICIAL := V_DTENTRSAID;
   ELSE
      V_DTINICIAL := V_DTPROCESSA;
   END IF;

   IF V_DTENTRSAID > V_DTPROCESSA THEN
      V_DTFINAL := V_DTENTRSAID;
   ELSE
      V_DTFINAL := V_DTPROCESSA;
   END IF;

   ATUALIZA_ESTOQUE(P_CDFILIDEST, V_DTINICIAL, V_DTFINAL, P_CDFILIDEST, V_NRLANCTONFDEST, 'N');

   DELETE FROM TRABREPLICDOC
         WHERE CDFILIALTRD   = P_CDFILIAL
           AND NRLANCTONFTRD = P_NRLANCTONF;

END;
/
