CREATE OR REPLACE PROCEDURE MONTA_HISTORICO(
   P_CODIGO IN VARCHAR2,
   P_STLANCAMENTO IN VARCHAR2,
   P_CDHISTPADR IN HISTPADR.CDHISTPADR%TYPE,
   P_NRNOTAFISC IN VARCHAR2,
   P_DTEMISSAO IN DATE,
   P_TIPOHISTORICO IN VARCHAR2,
   P_CDAJUCONTA IN VARCHAR2,
   P_DSHISTPADR OUT VARCHAR2)
IS
   V_DSHISTPADR VARCHAR2(1000);
   V_STDSHISTPADR VARCHAR2(1000);
   V_STNOTAFISC VARCHAR2(20);
   V_NRINSJURCLIE VARCHAR2(20);
   V_IDTPIJURCLIE VARCHAR2(1);
   V_NMFANTFORN VARCHAR2(50);
   V_NMFANTCLIE VARCHAR2(50);
   V_NMRAZSOCCLIE VARCHAR2(50);
   V_NRINSJURFORN VARCHAR2(50);
   V_NMRAZSOCFORN VARCHAR2(50);
   V_IDENTRSAIDOP VARCHAR2(1);
   V_CDFILIEMIT VARCHAR2(4);
   V_CDFILIRECE VARCHAR2(4);
   V_DTEMISSAO DATE;
   V_DTENTRSAID DATE;
   V_CDFILIAL VARCHAR2(4);
   V_NRLANCTONF VARCHAR2(10);
   V_DSPRODEMIS VARCHAR2(400);
   V_NRNOTAFISC VARCHAR2(10);
   V_IDTPIJURFORN VARCHAR2(1);
   V_HISTCREDPRES VARCHAR2(1000);
BEGIN
   BEGIN
    SELECT DSHISTPADR
      INTO V_DSHISTPADR
      FROM HISTPADR
     WHERE CDHISTPADR = P_CDHISTPADR;
   EXCEPTION
    WHEN NO_DATA_FOUND THEN
     V_DSHISTPADR :=  NULL;
   END ;
   CASE P_TIPOHISTORICO
    WHEN '00' THEN
     BEGIN
       BEGIN
         SELECT NF.CDFILIAL, NF.NRLANCTONF, NF.NRNOTAFISC,
                C.NRINSJURCLIE, C.IDTPIJURCLIE,
                C.NMFANTCLIE,   C.NMRAZSOCCLIE,
                F.NRINSJURFORN, F.IDTPIJURFORN,
                F.NMFANTFORN,   F.NMRAZSOCFORN,
                NF.IDENTRSAIDOP, NF.CDFILIEMIT, NF.CDFILIRECE,
                NF.DTEMISSAO, NF.DTENTRSAID
           INTO V_CDFILIAL, V_NRLANCTONF, V_NRNOTAFISC,
                V_NRINSJURCLIE, V_IDTPIJURCLIE,
                V_NMFANTCLIE, V_NMRAZSOCCLIE,
                V_NRINSJURFORN, V_IDTPIJURFORN,
                V_NMFANTFORN, V_NMRAZSOCFORN,
                V_IDENTRSAIDOP, V_CDFILIEMIT, V_CDFILIRECE,
                V_DTEMISSAO, V_DTENTRSAID
           FROM NOTAFISCAL NF, CLIENTE C, FORNECEDOR F
          WHERE CDFILIAL      = P_CODIGO
            AND NRLANCTONF    = P_STLANCAMENTO
            AND NF.CDCLIENTE  = C.CDCLIENTE(+)
            AND NF.CDFORNECED = F.CDFORNECED(+);
       EXCEPTION
         WHEN NO_DATA_FOUND THEN
              V_CDFILIAL      := NULL;
              V_NRLANCTONF    := NULL;
              V_NRNOTAFISC    := NULL;
              V_NRINSJURCLIE  := NULL;
              V_IDTPIJURCLIE  := NULL;
              V_NRINSJURFORN  := NULL;
              V_IDTPIJURFORN  := NULL;
              V_IDTPIJURFORN  := NULL;
              V_NMFANTFORN    := NULL;
              V_NMRAZSOCFORN  := NULL;
              V_IDENTRSAIDOP  := NULL;
              V_CDFILIEMIT    := NULL;
              V_CDFILIRECE    := NULL;
              V_DTEMISSAO     := NULL;
              V_DTENTRSAID    := NULL;
       END;
      BEGIN
         SELECT MAX(DSPRODEMIS)
           INTO V_DSPRODEMIS
           FROM ITEMPRODUTO
          WHERE CDFILIAL   = P_CODIGO
            AND NRLANCTONF = P_STLANCAMENTO;
      EXCEPTION
           WHEN NO_DATA_FOUND THEN
             V_DSPRODEMIS :=NULL;
      END;
       IF P_NRNOTAFISC IS NULL THEN
          V_STNOTAFISC := V_NRNOTAFISC;
       ELSE
          V_STNOTAFISC := P_NRNOTAFISC;
       END IF;
       V_STDSHISTPADR := V_DSHISTPADR;
       V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DOC]', V_STNOTAFISC);
       IF V_IDENTRSAIDOP = 'E' THEN
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[FIL]', V_CDFILIRECE);
       ELSE
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[FIL]', V_CDFILIEMIT);
       END IF;
       V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[SEQ]', V_NRLANCTONF);
       V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[CLI]', V_NRINSJURCLIE);
       V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[NFC]', V_NMFANTCLIE);
       V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[RSC]', V_NMRAZSOCCLIE);
       V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[RSC]', V_NMRAZSOCCLIE);
       V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[FOR]', V_NRINSJURFORN);
       V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[NFF]', V_NMFANTFORN);
       V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[RSF]', V_NMRAZSOCFORN);
       V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[NMPR]', V_DSPRODEMIS);
       V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DTEM-MA]', SUBSTR(TO_CHAR(P_DTEMISSAO),4,10));
       V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DTES-MA]', SUBSTR(TO_CHAR(V_DTENTRSAID),4,10));
       V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DTEM]', TO_CHAR(V_DTEMISSAO));
       V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DTES]', TO_CHAR(V_DTENTRSAID));
       IF INSTR(V_STDSHISTPADR,'[DOCO]') > 0 THEN
          BEGIN
            SELECT N.NRNOTAFISC
              INTO V_STNOTAFISC
              FROM ITEMNOTAFISC I, NOTAFISCAL N
             WHERE I.CDFILIAL = P_CODIGO
               AND I.NRLANCTONF = P_STLANCAMENTO
               AND I.CDFILIALIT = N.CDFILIAL
               AND I.NRLANCTONFIT = N.NRLANCTONF;
          EXCEPTION
             WHEN NO_DATA_FOUND THEN
              V_STNOTAFISC := NULL;
          END;
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DOCO]', V_STNOTAFISC);
       END IF;
     END;
    WHEN '06' THEN
     BEGIN
       V_STDSHISTPADR := V_DSHISTPADR;
       FOR C_CONTAPAGAR IN(
         SELECT CP.CDFILIAL    , CP.NRLANCTONF  , CP.NRSEQVENPAG , CP.VRATUAPAG  ,
                CP.DTATUAVENPAG, CP.DTORIGVENPAG, CP.DSCONTAPAG  , CP.CDTPCTPAGAR,
                CP.QTDIASVENPAG, CP.NRPARCELAPAG, CP.IDSTATPAG   , CP.CDFILIBPAG ,
                CP.NRLANBOLPAG , NF.CDSERIEENTR , CP.CDMOTIPARC  , CP.VRORIGPAG  ,
                CP.CDCLASSFINA , CP.CDEMPRESA   , CP.CDFIMOVFLPAG, CP.DTMOVFLUXO ,
                CP.IDMOVFLUXO, NF.CDINSCESTA, NF.CDINSCMUNI, NF.VRNOTAFISC, CP.CDMOTIPROR,
                NF.CDFILIRECE AS CDFILIORIG, NF.CDTIPOOPER, NF.IDTPIJURNF AS IDTPINSJUR, NF.NRINSJURNF AS NRINSJURFORN,
                NF.CDFORNECED, NF.NRNOTAFISC, NF.DTEMISSAO, NF.DTENTRSAID,  NF.IDSTATUS,
                NF.CDAUTDESPESA AS CDAUTODESP, NF.NRLANCTBDOC,
                F.NMFANTFORN, F.NMRAZSOCFORN
           FROM CONTAPAGAR CP, NOTAFISCAL NF, FORNECEDOR F
          WHERE (NF.CDFILIAL   = P_CODIGO)
            AND (NF.NRLANCTONF = P_STLANCAMENTO)
            AND (CP.CDFILIAL   = NF.CDFILIAL  )
            AND (CP.NRLANCTONF = NF.NRLANCTONF)
            AND (NF.CDFORNECED = F.CDFORNECED)
          ORDER BY CP.NRLANCTONF, CP.NRSEQVENPAG, CP.DTATUAVENPAG, CP.NRPARCELAPAG) LOOP
            FOR C_CDAJUCTPAG IN(
             SELECT T.DSAJUCTPAG
               FROM TIPOAJPAGAR T
              WHERE (T.CDAJUCTPAG = P_CDAJUCONTA)) LOOP
              V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[TAP]', C_CDAJUCTPAG.DSAJUCTPAG);
              V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DAP]', P_DTEMISSAO);
            END LOOP;
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[FIL]', C_CONTAPAGAR.CDFILIORIG);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[FOR]', C_CONTAPAGAR.NRINSJURFORN);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[NFF]', C_CONTAPAGAR.NMFANTFORN);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[RSF]', C_CONTAPAGAR.NMRAZSOCFORN);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DOC]', C_CONTAPAGAR.NRNOTAFISC);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[SEQ]', C_CONTAPAGAR.NRLANCTBDOC);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[TCP]', C_CONTAPAGAR.CDTPCTPAGAR);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DCP]', C_CONTAPAGAR.DSCONTAPAG);
       END LOOP;
     END;
    WHEN '07' THEN
     BEGIN
       V_STDSHISTPADR := V_DSHISTPADR;
       FOR C_CONTARECEBER IN (
         SELECT CP.CDFILIAL    , CP.NRLANCTONF  , CP.NRSEQVENREC , CP.VRATUAREC  ,
                CP.DTATUAVENREC, CP.DTORIGVENREC, CP.DSCONTAREC  , CP.CDTPCTRECEB,
                CP.QTDIASVENREC, CP.NRPARCELAREC, CP.IDSTATREC   , CP.CDFILIBOLREC ,
                CP.NRLANBOLREC , NF.CDSERIE     , CP.CDMOTIPARC  , CP.VRORIGREC  ,
                CP.CDCLASSFINA , CP.CDEMPRESA   , CP.CDFIMOVFLREC, CP.DTMOVFLUXO ,
                CP.IDMOVFLUXO, NF.CDINSCESTA, NF.CDINSCMUNI, NF.VRNOTAFISC,
                NF.CDFILIEMIT, NF.CDTIPOOPER, NF.IDTPIJURNF AS IDTPINSJUR, NF.NRINSJURNF AS NRINSJURCLIE,
                NF.CDCLIENTE, NF.NRNOTAFISC, NF.DTEMISSAO, NF.DTENTRSAID,  NF.IDSTATUS,
                NF.NRLANCTBDOC, CP.DTPREVRECEBI, CP.QTDIASPREREC,
                CL.NMFANTCLIE, CL.NMRAZSOCCLIE
           FROM CONTARECEBER CP, NOTAFISCAL NF, CLIENTE CL
          WHERE (NF.CDFILIAL      = P_CODIGO)
            AND (NF.NRLANCTONF    = P_STLANCAMENTO)
            AND (CP.CDFILIAL      = NF.CDFILIAL)
            AND (CP.NRLANCTONF    = NF.NRLANCTONF)
            AND (CL.CDCLIENTE(+)  = NF.CDCLIENTE)
          ORDER BY CP.NRLANCTONF, CP.NRSEQVENREC, CP.DTATUAVENREC, CP.NRPARCELAREC) LOOP
          FOR C_CDAJUCTPAG IN(
            SELECT T.DSAJUCTREC
              FROM TIPOAJRECEBER T
             WHERE (T.CDAJUCTREC = P_CDAJUCONTA)) LOOP
            V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[TAP]', C_CDAJUCTPAG.DSAJUCTREC);
            V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DAP]', P_DTEMISSAO);
          END LOOP;
         V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[FIL]', C_CONTARECEBER.CDFILIEMIT);
         V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[CLI]', C_CONTARECEBER.NRINSJURCLIE);
         V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[NFC]', C_CONTARECEBER.NMFANTCLIE);
         V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[RSC]', C_CONTARECEBER.NMRAZSOCCLIE);
         V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DOC]', C_CONTARECEBER.NRNOTAFISC);
         V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[SEQ]', C_CONTARECEBER.NRLANCTBDOC);
         V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[TCR]', C_CONTARECEBER.CDTPCTRECEB);
         V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DCR]', C_CONTARECEBER.DSCONTAREC);
       END LOOP;
     END;
    WHEN '08' THEN
     BEGIN
       V_STDSHISTPADR := V_DSHISTPADR;
        FOR C_BCO_LANCCORR IN(
          SELECT M.DTMOVICRED, C.DSTPCREDBANC AS TIPO, C.CDHISTPADR, C.CDCONTCTBL, M.CDTPCREDBANC AS CDTIPO,
                 M.NRLCTBMCRED AS LANCCONT, M.DSMOVICRED, M.VRMOVICRED, M.NRMOVICRED AS NUMERO, M.NRMOVBANC ,
                 M.CDEMPRESA, M.CDFILIAL, M.CDFILILANCRE AS CDFILILANCA, C.IDCTBLCRBANC AS IDCONTABILIZA,
                 L.NMRAZSOCCLIE, L.NMFANTCLIE, O.NMRAZSOCFORN, O.NMFANTFORN,
                 'C' AS DEBCRED, B.NRDOCMBANC
            FROM TPCREDBANC C, MOVBANCARIO B, MOVCREDBANC M, FORNECEDOR O, CLIENTE L
           WHERE (C.CDTPCREDBANC = M.CDTPCREDBANC)
             AND (M.NRMOVBANC    = B.NRMOVBANC)
             AND (B.CDREDUCONT   = M.CDREDUCONT)
             AND (M.CDREDUCONT   = P_CODIGO)
             AND (M.NRMOVBANC   = P_STLANCAMENTO)
             AND (M.CDCLIENTE    = L.CDCLIENTE(+))
             AND (M.CDFORNECED   = O.CDFORNECED(+))
           UNION ALL
          SELECT M.DTMOVIDEBI, C.DSTPDEBIBANC AS TIPO, C.CDHISTPADR, C.CDCONTCTBL, M.CDTPDEBIBANC AS CDTIPO,
                 M.NRLCTBMDEB AS LANCCONT, M.DSMOVIDEBI, M.VRMOVIDEBI, M.NRMOVIDEBI AS NUMERO,M.NRMOVBANC,
                 M.CDEMPRESA, M.CDFILIAL, M.CDFILILANDEB AS CDFILILANCA, C.IDCTBLDBBANC AS IDCONTABILIZA,
                 L.NMRAZSOCCLIE, L.NMFANTCLIE, O.NMRAZSOCFORN, O.NMFANTFORN,
                 'D' AS DEBCRED, B.NRDOCMBANC
           FROM TPDEBIBANC C, MOVBANCARIO B, MOVDEBBANC  M, FORNECEDOR  O, CLIENTE L
          WHERE (C.CDTPDEBIBANC = M.CDTPDEBIBANC)
            AND (M.NRMOVBANC    = B.NRMOVBANC)
            AND (B.CDREDUCONT   = M.CDREDUCONT(+))
            AND (M.CDREDUCONT   = P_CODIGO)
            AND (M.NRMOVBANC   = P_STLANCAMENTO)
            AND (M.CDCLIENTE    = L.CDCLIENTE(+))
            AND (M.CDFORNECED   = O.CDFORNECED(+))) LOOP
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DESC]', C_BCO_LANCCORR.DSMOVICRED);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[TIPO]', C_BCO_LANCCORR.TIPO);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[HIMB]', C_BCO_LANCCORR.DSMOVICRED||'('||C_BCO_LANCCORR.TIPO||')');
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DOC]', C_BCO_LANCCORR.NRDOCMBANC);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[RSC]', C_BCO_LANCCORR.NMRAZSOCCLIE);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[NFC]', C_BCO_LANCCORR.NMFANTCLIE);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[RSF]', C_BCO_LANCCORR.NMRAZSOCFORN);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[NFF]', C_BCO_LANCCORR.NMFANTFORN);
        END LOOP;
     END;
    WHEN '09' THEN
     BEGIN
        V_STDSHISTPADR := V_DSHISTPADR;
        FOR C_BCO_TRANSCCOR IN(
           SELECT I.CDEMPRESA, I.NRTRANBANC, I.CDREDUCONT, I.NRMOVBANC, I.IDMOVITBANC, I.VRMOVITBANC,
                  I.NRCHEQUE, C.CDBANCO, C.CDAGENCIA, C.CDCONTCORR, C.DSCONTCORR, B.CDPICTCONT, C.CDCONTCTBL,
                  T.NRLCTBTRAN, T.DSHISTTBANC, T.DTTRANBANC
             FROM ITTRANSFBANC I, CONTCORR C, BANCO B, TRANSFBANC T, LISTA LI
            WHERE (T.CDEMPRESA  = I.CDEMPRESA)
              AND (T.NRTRANBANC = I.NRTRANBANC)
              AND (I.CDREDUCONT = C.CDREDUCONT)
              AND (C.CDBANCO    = B.CDBANCO)
              AND (I.CDREDUCONT = P_CODIGO)
              AND (I.NRMOVBANC = P_STLANCAMENTO))LOOP
              V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[HITR]', C_BCO_TRANSCCOR.DSHISTTBANC);
              V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[#CHQ]', C_BCO_TRANSCCOR.NRCHEQUE);
        END LOOP;
     END;
    WHEN '10' THEN
     BEGIN
       V_STDSHISTPADR := V_DSHISTPADR;
       FOR C_DSBAIXA IN (
       SELECT B.NRCHEQUE, B.DSNOMCHEQUE, B.DSBAIXAPAG
         FROM BAIXAPAGA B
        WHERE B.CDFILIAL = P_CODIGO
          AND B.NRBAIXAPAG = P_STLANCAMENTO) LOOP
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[#CHQ]', C_DSBAIXA.NRCHEQUE);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DCHQ]', C_DSBAIXA.DSNOMCHEQUE);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DBXPG]', C_DSBAIXA.DSBAIXAPAG);
       END LOOP;
     END;
    WHEN '11' THEN
     BEGIN
       V_STDSHISTPADR := V_DSHISTPADR;
       FOR C_DSBAIXA IN (
       SELECT B.DSBAIXAREC
         FROM BAIXARECEBIDA B
        WHERE B.CDFILIAL = P_CODIGO
          AND B.NRBAIXAREC = P_STLANCAMENTO) LOOP
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DBXPG]', C_DSBAIXA.DSBAIXAREC);
       END LOOP;
     END;
    WHEN '12' THEN
     BEGIN
       V_STDSHISTPADR := V_DSHISTPADR;
       FOR C_NOTAFISCAL IN (SELECT NF.CDFILIAL, NF.NRLANCTONF, NF.CDINSCESTA, NF.CDINSCMUNI, NF.VRNOTAFISC,
                                   NF.CDFILIEMIT, NF.CDTIPOOPER, NF.IDTPIJURNF AS IDTPINSJUR,
                                   NF.NRINSJURNF AS NRINSJURCLIE, NF.CDCLIENTE, NF.NRNOTAFISC, NF.DTEMISSAO,
                                   NF.DTENTRSAID,  NF.IDSTATUS, NF.NRLANCTBDOC, CL.NMFANTCLIE, CL.NMRAZSOCCLIE
                              FROM NOTAFISCAL NF, CLIENTE CL
                             WHERE NF.CDFILIAL     = P_CODIGO
                               AND NF.NRLANCTONF   = P_STLANCAMENTO
                               AND CL.CDCLIENTE(+) = NF.CDCLIENTE
                             ORDER BY NF.NRLANCTONF) LOOP
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR,  '[FIL]', C_NOTAFISCAL.CDFILIEMIT);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR,  '[CLI]', C_NOTAFISCAL.NRINSJURCLIE);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR,  '[NFC]', C_NOTAFISCAL.NMFANTCLIE);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR,  '[RSC]', C_NOTAFISCAL.NMRAZSOCCLIE);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR,  '[DOC]', C_NOTAFISCAL.NRNOTAFISC);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DTEM]', TO_CHAR(C_NOTAFISCAL.DTEMISSAO));
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR,  '[SEQ]', C_NOTAFISCAL.NRLANCTBDOC);
       END LOOP;
       FOR C_CDAJUCTPAG IN(
          SELECT T.DSAJUCTREC
            FROM TIPOAJRECEBER T
           WHERE (T.CDAJUCTREC = P_CDAJUCONTA)) LOOP
           V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[TAP]', C_CDAJUCTPAG.DSAJUCTREC);
           V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DAP]', P_DTEMISSAO);
       END LOOP;
       FOR C_CONTARECEBER IN (SELECT CP.DSCONTAREC, CP.CDTPCTRECEB
                                FROM CONTARECEBER CP
                               WHERE CP.CDFILIAL   = P_CODIGO
                                 AND CP.NRLANCTONF = P_STLANCAMENTO) LOOP
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR,  '[TCR]', C_CONTARECEBER.CDTPCTRECEB);
          V_STDSHISTPADR := REPLACE(V_STDSHISTPADR,  '[DCR]', C_CONTARECEBER.DSCONTAREC);
       END LOOP;
       FOR C_ECF IN(
           /*Quando existir NFC-e e Reducao Z para o mesmo Dia, Caixa e Filial, a NFC-e tem prioridade*/
          SELECT CDFILIAL, CDCAIXA, NRECFREDZ, NRREDREDZ
            FROM (SELECT DISTINCT '1' AS ID, MV.CDFILIAL, MV.CDCAIXA, R.NRECFREDZ, R.NRREDREDZ
                    FROM MOVCAIXA MV, REDUCAOZ R
                   WHERE MV.CDFILIAL            = R.CDFILIAL
                     AND MV.CDCAIXA             = R.CDCAIXA
                     AND TRUNC(MV.DTMOVIMCAIXA) = R.DTREDUCAOZ
                     AND MV.CDFILIALNF          = P_CODIGO
                     AND MV.NRLANCTONF          = P_STLANCAMENTO
                   UNION ALL
                  SELECT DISTINCT '2' AS ID, VE.CDFILIAL, VE.CDCAIXA, ' ' AS NRECFREDZ, TO_CHAR(VE.DTEMISSAONFCE,'DDMMYYYY') || VE.CDCAIXA AS NRREDREDZ
                    FROM MOVCAIXA MV, VENDA VE
                   WHERE MV.CDFILIALNF    = P_CODIGO
                     AND MV.NRLANCTONF    = P_STLANCAMENTO
                     AND MV.CDFILIAL      = VE.CDFILIAL
                     AND MV.CDCAIXA       = VE.CDCAIXA
                     AND MV.NRSEQVENDA    = VE.NRSEQVENDA
                     AND VE.IDTPEMISVEND  IN ('N','S')
                     AND VE.IDTPAMBNFCE   = '1'
                     AND VE.IDSTATUSNFCE  = 'A'
                   UNION ALL
                  SELECT DISTINCT '1' AS ID, MV.CDFILIAL, MV.CDCAIXA, R.NRECFREDZ, R.NRREDREDZ
                    FROM MOVCAIXA MV, REDUCAOZ R
                   WHERE MV.CDFILIAL            = R.CDFILIAL
                     AND MV.CDCAIXA             = R.CDCAIXA
                     AND TRUNC(MV.DTMOVIMCAIXA) = R.DTREDUCAOZ
                     AND MV.CDFILIALNFFIS       = P_CODIGO
                     AND MV.NRLANCTONFFIS       = P_STLANCAMENTO
                   UNION ALL
                  SELECT DISTINCT '2' AS ID, VE.CDFILIAL, VE.CDCAIXA, ' ' AS NRECFREDZ, TO_CHAR(VE.DTEMISSAONFCE,'DDMMYYYY') || VE.CDCAIXA AS NRREDREDZ
                    FROM MOVCAIXA MV, VENDA VE
                   WHERE MV.CDFILIALNFFIS = P_CODIGO
                     AND MV.NRLANCTONFFIS = P_STLANCAMENTO
                     AND MV.CDFILIAL      = VE.CDFILIAL
                     AND MV.CDCAIXA       = VE.CDCAIXA
                     AND MV.NRSEQVENDA    = VE.NRSEQVENDA
                     AND VE.IDTPEMISVEND  IN ('N','S')
                     AND VE.IDTPAMBNFCE   = '1'
                     AND VE.IDSTATUSNFCE  = 'A'
                   ORDER BY ID DESC)
           WHERE ROWNUM = 1) LOOP
           V_STDSHISTPADR := REPLACE(V_STDSHISTPADR,  '[ECF]', C_ECF.NRECFREDZ);
           V_STDSHISTPADR  := REPLACE(V_STDSHISTPADR, '[CXA]', C_ECF.CDCAIXA);
           V_STDSHISTPADR  := REPLACE(V_STDSHISTPADR, '[RED]', C_ECF.NRREDREDZ);
       END LOOP;
     END;
    WHEN '13' THEN
     BEGIN
      V_STDSHISTPADR := V_DSHISTPADR;
      BEGIN
         SELECT HISTLANC
           INTO V_DSHISTPADR
           FROM
         (SELECT
             NVL(DSESTORNOCRED,' ')||' '||'IE '||IE.CDINSCESTA||' '||TO_CHAR(P_DTEMISSAO,'MONTH')||'-'||TO_CHAR(P_DTEMISSAO,'YYYY')||'-'||P_CODIGO AS HISTLANC
           FROM ESTORNO E, INSCRESTAD IE
          WHERE E.CDINSCESTA = P_STLANCAMENTO
		      AND IE.CDINSCESTA  = E.CDINSCESTA
			   AND IE.SGESTADO  = 'PE'
          UNION ALL
         SELECT
             NVL(DSESTORNOCRED,' ')||' '||'IE '||IE.CDINSCESTA||' '||TO_CHAR(P_DTEMISSAO,'MONTH')||'-'||TO_CHAR(P_DTEMISSAO,'YYYY')||'-'||P_CODIGO AS HISTLANC
           FROM ESTORNO E, INSCRESTAD IE
          WHERE E.CDINSCESTA = P_STLANCAMENTO
		      AND IE.CDINSCESTA  = E.CDINSCESTA
		    	AND IE.SGESTADO  = 'SP'
          UNION ALL
         SELECT
             DECODE(P_CDAJUCONTA,'D',NVL(DSESTORNOCRED,' '),NVL(DSESTORNODEBI,''))||' '||'IE '||IE.CDINSCESTA||' '||TO_CHAR(P_DTEMISSAO,'MONTH')||'-'||TO_CHAR(P_DTEMISSAO,'YYYY')||'-'||P_CODIGO AS HISTLANC
           FROM ESTORNO E, INSCRESTAD IE
          WHERE E.CDINSCESTA = P_STLANCAMENTO
		      AND IE.CDINSCESTA  = E.CDINSCESTA
			   AND IE.SGESTADO  = 'BA'
          UNION ALL
         SELECT
             DECODE(P_CDAJUCONTA,'D',DSESTORNODEBI, DSESTORNOCRED)||' '||'IE '||IE.CDINSCESTA||' '||TO_CHAR(P_DTEMISSAO,'MONTH')||'-'||TO_CHAR(P_DTEMISSAO,'YYYY')||'-'||P_CODIGO AS HISTLANC
           FROM ESTORNO E, INSCRESTAD IE
          WHERE E.CDINSCESTA = P_STLANCAMENTO
		      AND IE.CDINSCESTA  = E.CDINSCESTA
		    	AND IE.SGESTADO  = 'RJ');
      EXCEPTION WHEN NO_DATA_FOUND THEN
           V_DSHISTPADR := NULL;
      END;
       V_STDSHISTPADR :=  V_DSHISTPADR;
     END;
    WHEN '14' THEN
      V_STDSHISTPADR := V_DSHISTPADR;
      V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[EMP]',  P_CODIGO);
      V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[FIL]',  P_STLANCAMENTO);
      V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DOC]',  P_NRNOTAFISC);
      V_STDSHISTPADR := REPLACE(V_STDSHISTPADR, '[DTEM]', P_DTEMISSAO);
    END CASE;
    P_DSHISTPADR := V_STDSHISTPADR;
END;
/