CREATE OR REPLACE FUNCTION ARQUIVO_DIC_53(
   P_CDIMPOSTO    IN IMPOSTO.CDIMPOSTO%TYPE,
   P_CDEMPRESA    IN EMPRESA.CDEMPRESA%TYPE,
   P_CDINSCESTA   IN EMPRESA.CDINSCESTA%TYPE,
   P_DTINICIAL    IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_DTFINAL      IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_CDPAIS       IN ESTADO.CDPAIS%TYPE,
   P_SGESTADO     IN ESTADO.SGESTADO%TYPE,
   P_SEQUENCIAL   IN ARQMAGNETICO.NRSEQARQMAG%TYPE,
   P_CONTADOR     IN NUMBER,
   P_CONTTOTAL    IN VARCHAR2,
   P_CONTGERAL    IN NUMBER) RETURN VARCHAR2
AS
   V_STTOTAL        VARCHAR2(4000);
   V_CONTGERAL      NUMBER;
   V_CONTREG        NUMBER;
   V_CONTADOR       NUMBER;
BEGIN

   V_CONTGERAL := NVL(P_CONTGERAL,0);
   V_CONTADOR := P_CONTADOR;
   V_CONTREG := 0;

   FOR C_REGISTRO53 IN (SELECT '53'||
                               LPAD(NVL(DECODE(CL.IDTPIJURCLIE,'I','','O', '',CL.NRINSJURCLIE),'00000000000000'),14,'0')||
                               RPAD(NVL(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(CL.NRINSESTCLIE, ' ', ''),'.',''),',',''),'-',''),'/',''),'ISENTO'),14,' ')||
                               TO_CHAR(DECODE(NF.IDENTRSAIDOP, 'E', NF.DTENTRSAID, NF.DTEMISSAO),'yyyymmdd')||
                               RPAD(NVL(EC.SGESTADO,' '),2,' ')||
                               RPAD(NVL(NF.CDMODDOCFISC,'01'), 2, ' ')||
                               RPAD(DECODE(TRIM(NVL(NF.CDSERIE,NF.CDSERIEENTR)),'-',' ',REPLACE(NVL(NF.CDSERIE,NF.CDSERIEENTR), '-', '')),3,' ')||
                               LPAD(SUBSTR(NF.NRNOTAFISC,LENGTH(NF.NRNOTAFISC)-5,6),6,'0')||
                               RPAD(NVL(NF.CDFISCOPERNF,'0'),4,' ')||
                               'P'||
                               LTRIM(REPLACE(TO_CHAR(ABS(NF.VRBASECALCNF),'00000000000.00'),'.',''))||
                               LTRIM(REPLACE(TO_CHAR(ABS(NF.VRIMPOSTONF) ,'00000000000.00'),'.',''))||
                               LTRIM(REPLACE(TO_CHAR(ABS(NF.VRDESPESAS)  ,'00000000000.00'),'.',''))||
                               'N'||
                               RPAD(' ', 88, ' ') AS DSARQMAG
                        FROM CLIENTE CL, ENDECLIE EC, CLIENTE CA,
                            (SELECT I.CDFILIAL, I.NRLANCTONF,N.NRNOTAFISC, N.CDSERIE, N.CDSERIEENTR, N.CDCLIENTE,
                                    N.DTEMISSAO, N.DTENTRSAID, I.VRALIQNF, I.CDFISCOPERNF,N.CDMODDOCFISC, N.IDORIGDESTNF, N.IDENTRSAIDOP,
                                    SUM(I.VRCONTABILNF) AS VRCONTABILNF,
                                    SUM(DECODE(I.CDFISCALNF,'1',I.VRBASECALCNF,0)) AS VRBASECALCNF,
                                    SUM(DECODE(I.CDFISCALNF,'1',I.VRIMPOSTONF,0))  AS VRIMPOSTONF,
                                    SUM(DECODE(I.CDFISCALNF,'3',I.VRCONTABILNF,0))   AS VROUTRCRED,
                                    SUM(DECODE(I.CDFISCALNF,'2',I.VRCONTABILNF,'1',DECODE(I.VRPERREDBCNF,0, 0,I.VRCONTABILNF-I.VRBASECALCNF), 0)) AS VRISENTRIB,
                                    0 AS VRDESPESAS
                             FROM MAPAAPURAC M, NOTAFISCAL N, IMPOSTONF I, MODDOCFISC MD, IMPOSTO P,
                                  (SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF
                                   FROM MAPAAPURAC M, IMPOSTONF IP, IMPOSTO I
                                   WHERE M.CDEMPRESA    = P_CDEMPRESA     AND
                                         M.CDINSCESTA   = P_CDINSCESTA    AND
                                         M.DTINIPERIODO = P_DTINICIAL     AND
                                         M.DTFIMPERIODO = P_DTFINAL       AND
                                         M.CDIMPOSTO    = P_CDIMPOSTO     AND
                                         M.NRSEQUMAPAPU = IP.NRSEQUMAPAPU AND
                                         IP.CDIMPOSTO   = I.CDIMPOSTO     AND
                                         I.CDIMPOSTONFE = P_CDIMPOSTO) IC
                             WHERE M.CDEMPRESA       = P_CDEMPRESA                   AND
                                   M.CDINSCESTA      = P_CDINSCESTA                  AND
                                   M.DTINIPERIODO    = P_DTINICIAL                   AND
                                   M.DTFIMPERIODO    = P_DTFINAL                     AND
                                   M.CDIMPOSTO       = '80'                          AND
                                   M.NRSEQUMAPAPU    = I.NRSEQUMAPAPU                AND
                                   N.CDFILIAL        = I.CDFILIAL                    AND
                                   N.NRLANCTONF      = I.NRLANCTONF                  AND
                                   I.CDFILIAL        = IC.CDFILIAL                   AND
                                   I.NRLANCTONF      = IC.NRLANCTONF                 AND
                                   P.CDIMPOSTONFE    = '80'                          AND
                                   I.CDIMPOSTO       = P.CDIMPOSTO                   AND
                                   N.IDSTATUS IN ('A','E')                           AND
                                   NVL(I.IDIMPOSTONF, '1') <> '3'                    AND
                                   NVL(SUBSTR(I.CDFISCOPERNF,1,1),' ') <> '3'        AND
                                   NVL(SUBSTR(I.CDFISCOPERNF,1,1),' ') <> '7'        AND
                                   NVL(N.CDMODDOCFISC, '01') = MD.CDMODDOCFISC       AND
                                   MD.CDTPREGFISC  = '50'
                             GROUP BY I.CDFILIAL, I.NRLANCTONF, N.NRNOTAFISC, N.CDSERIE, N.CDSERIEENTR,
                                      N.CDCLIENTE,N.DTEMISSAO, N.DTENTRSAID, I.VRALIQNF, I.CDFISCOPERNF,
                                      N.CDMODDOCFISC, N.IDORIGDESTNF, N.IDENTRSAIDOP) NF
                        WHERE CL.CDCLIENTE(+)    = NF.CDCLIENTE                 AND
                              CA.CDCLIENTE       = NF.CDCLIENTE                 AND
                              EC.CDCLIENTE(+)    = CA.CDCLIENTE                 AND
                              EC.IDTPENDECLIE(+) = NVL(CA.IDENDEMISCLI,'P')     AND
                              EC.CDPAIS          = NVL(P_CDPAIS,EC.CDPAIS)     AND
                              (INSTR(NVL(P_SGESTADO, EC.SGESTADO), EC.SGESTADO) > 0)
                        UNION ALL
                        SELECT '53'||
                               LPAD(NVL(DECODE(FO.IDTPIJURFORN,'I','','O', '',FO.NRINSJURFORN),'00000000000000'),14,'0')||
                               RPAD(NVL(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(FO.NRINSESTFORN, ' ', ''),'.',''),',',''),'-',''),'/',''),'ISENTO'),14,' ')||
                               TO_CHAR(DECODE(NF.IDENTRSAIDOP, 'S', NF.DTEMISSAO, NF.DTENTRSAID),'yyyymmdd')||
                               RPAD(NVL(EF.SGESTADO,' '),2,' ')||
                               RPAD(NVL(NF.CDMODDOCFISC,'01'), 2, ' ')||
                               RPAD(DECODE(TRIM(NVL(NF.CDSERIEENTR,NF.CDSERIE)),'-',' ',REPLACE(NVL(NF.CDSERIEENTR,NF.CDSERIE), '-', '')),3,' ')||
                               LPAD(SUBSTR(NF.NRNOTAFISC,LENGTH(NF.NRNOTAFISC)-5,6),6,'0')||
                               RPAD(NVL(NF.CDFISCOPERNF,'0'),4,' ')||
                               'P'||
                               LTRIM(REPLACE(TO_CHAR(ABS(NF.VRBASECALCNF),'00000000000.00'),'.',''))||
                               LTRIM(REPLACE(TO_CHAR(ABS(NF.VRIMPOSTONF) ,'00000000000.00'),'.',''))||
                               LTRIM(REPLACE(TO_CHAR(ABS(NF.VRDESPESAS)  ,'00000000000.00'),'.',''))||
                               'N'||
                               RPAD(' ', 88, ' ') AS DSARQMAG
                        FROM FORNECEDOR FO, ENDEFORN EF, FORNECEDOR FA,
                            (SELECT I.CDFILIAL, I.NRLANCTONF,N.NRNOTAFISC, N.CDSERIE, N.CDSERIEENTR, N.CDFORNECED,
                                    N.DTENTRSAID, N.DTEMISSAO, I.VRALIQNF, I.CDFISCOPERNF,N.CDMODDOCFISC, N.IDORIGDESTNF, N.IDENTRSAIDOP,
                                    SUM(I.VRCONTABILNF) AS VRCONTABILNF,
                                    SUM(DECODE(I.CDFISCALNF,'1',I.VRBASECALCNF,0)) AS VRBASECALCNF,
                                    SUM(DECODE(I.CDFISCALNF,'1',I.VRIMPOSTONF,0))  AS VRIMPOSTONF,
                                    SUM(DECODE(I.CDFISCALNF,'3',I.VRCONTABILNF,0))   AS VROUTRCRED,
                                    SUM(DECODE(I.CDFISCALNF,'2',I.VRCONTABILNF, '1', DECODE(I.VRPERREDBCNF,0, 0,I.VRCONTABILNF-I.VRBASECALCNF), 0)) AS VRISENTRIB,
                                    0 AS VRDESPESAS
                             FROM MAPAAPURAC M, NOTAFISCAL N, IMPOSTONF I, MODDOCFISC MD, IMPOSTO P,
                                  (SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF
                                   FROM MAPAAPURAC M, IMPOSTONF IP, IMPOSTO I
                                   WHERE M.CDEMPRESA     = P_CDEMPRESA     AND
                                         M.CDINSCESTA    = P_CDINSCESTA    AND
                                         M.DTINIPERIODO  = P_DTINICIAL     AND
                                         M.DTFIMPERIODO  = P_DTFINAL       AND
                                         M.CDIMPOSTO     = P_CDIMPOSTO     AND
                                         M.NRSEQUMAPAPU  = IP.NRSEQUMAPAPU AND
                                         IP.CDIMPOSTO    = I.CDIMPOSTO     AND
                                         I.CDIMPOSTONFE = P_CDIMPOSTO) IC
                             WHERE M.CDEMPRESA       = P_CDEMPRESA                   AND
                                   M.CDINSCESTA      = P_CDINSCESTA                  AND
                                   M.DTINIPERIODO    = P_DTINICIAL                   AND
                                   M.DTFIMPERIODO    = P_DTFINAL                     AND
                                   M.CDIMPOSTO       = '80'                          AND
                                   M.NRSEQUMAPAPU    = I.NRSEQUMAPAPU                AND
                                   N.CDFILIAL        = I.CDFILIAL                    AND
                                   N.NRLANCTONF      = I.NRLANCTONF                  AND
                                   I.CDFILIAL        = IC.CDFILIAL                   AND
                                   I.NRLANCTONF      = IC.NRLANCTONF                 AND
                                   P.CDIMPOSTONFE    = '80'                          AND
                                   I.CDIMPOSTO       = P.CDIMPOSTO                   AND
                                   N.IDSTATUS IN ('A','E')                           AND
                                   NVL(I.IDIMPOSTONF, '1') <> '3'                    AND
                                   NVL(SUBSTR(I.CDFISCOPERNF,1,1),' ') <> '3'        AND
                                   NVL(SUBSTR(I.CDFISCOPERNF,1,1),' ') <> '7'        AND
                                   NVL(N.CDMODDOCFISC, '01')  = MD.CDMODDOCFISC      AND
                                   MD.CDTPREGFISC  = '50'
                             GROUP BY I.CDFILIAL, I.NRLANCTONF, N.NRNOTAFISC, N.CDSERIE, N.CDSERIEENTR,
                                      N.CDFORNECED,N.DTENTRSAID, N.DTEMISSAO, I.VRALIQNF, I.CDFISCOPERNF,
                                      N.CDMODDOCFISC, N.IDORIGDESTNF, N.IDENTRSAIDOP) NF
                        WHERE FO.CDFORNECED(+)   = NF.CDFORNECED            AND
                              FA.CDFORNECED      = NF.CDFORNECED            AND
                              EF.CDFORNECED(+)   = FA.CDFORNECED            AND
                              EF.IDTPENDEFORN(+) = NVL(FA.IDENDEMISFOR,'P') AND
                              EF.CDPAIS          = NVL(P_CDPAIS,EF.CDPAIS)  AND
                              (INSTR(NVL(P_SGESTADO, EF.SGESTADO), EF.SGESTADO) > 0)
                        UNION ALL
                        SELECT '53'||
                               RPAD(NVL(REPLACE(REPLACE(REPLACE(FI.NRINSJURFILI, '-', ''), '.', ''), '/', ''),'00000000000000'),14,' ')||
                               RPAD(NVL(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(DECODE(SUBSTR(UPPER(FI.CDINSCESTA),1,5),'ISENT','ISENTO',FI.CDINSCESTA), ' ', ''),'.',''),',',''),'-',''),'/',''),'ISENTO'),14,' ')||
                               TO_CHAR(NF.DTENTRSAID,'yyyymmdd')||
                               RPAD(NVL(DECODE(NVL(F.IDENDEMISFIL,'P'),'I',IE.CDINSCESTA,EF.SGESTADO),' '),2,' ')||
                               RPAD(NVL(NF.CDMODDOCFISC,'01'), 2, ' ')||
                               RPAD(DECODE(TRIM(NVL(NF.CDSERIE,NF.CDSERIEENTR)),'-',' ',REPLACE(NVL(NF.CDSERIE,NF.CDSERIEENTR), '-', '')),3,' ')||
                               LPAD(SUBSTR(NF.NRNOTAFISC,LENGTH(NF.NRNOTAFISC)-5,6),6,'0')||
                               RPAD(NVL(NF.CDFISCOPERNF,'0'),4,' ')||
                               'P'||
                               LTRIM(REPLACE(TO_CHAR(ABS(NF.VRBASECALCNF),'00000000000.00'),'.',''))||
                               LTRIM(REPLACE(TO_CHAR(ABS(NF.VRIMPOSTONF) ,'00000000000.00'),'.',''))||
                               LTRIM(REPLACE(TO_CHAR(ABS(NF.VRDESPESAS)  ,'00000000000.00'),'.',''))||
                               'N'||
                               RPAD(' ', 88, ' ') AS DSARQMAG
                        FROM FILIAL FI, ENDEFILI EF, FILIAL F, INSCRESTAD IE,
                            (SELECT I.CDFILIAL, I.NRLANCTONF,N.NRNOTAFISC, N.CDSERIE, N.CDSERIEENTR, N.CDFILIEMIT,
                                    N.DTENTRSAID, I.VRALIQNF, I.CDFISCOPERNF,N.CDMODDOCFISC, N.IDORIGDESTNF,
                                    SUM(I.VRCONTABILNF) AS VRCONTABILNF, N.CDINSCESTA,
                                    SUM(DECODE(I.CDFISCALNF,'1',I.VRBASECALCNF,0)) AS VRBASECALCNF,
                                    SUM(DECODE(I.CDFISCALNF,'1',I.VRIMPOSTONF,0))  AS VRIMPOSTONF,
                                    SUM(DECODE(I.CDFISCALNF,'3',I.VRCONTABILNF,0))   AS VROUTRCRED,
                                    SUM(DECODE(I.CDFISCALNF,'2',I.VRCONTABILNF, '1', DECODE(I.VRPERREDBCNF,0, 0,I.VRCONTABILNF-I.VRBASECALCNF), 0)) AS VRISENTRIB,
                                    0 AS VRDESPESAS
                             FROM MAPAAPURAC M, NOTAFISCAL N, IMPOSTONF I, MODDOCFISC MD, IMPOSTO P,
                                  (SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF
                                   FROM MAPAAPURAC M, IMPOSTONF IP, IMPOSTO I
                                   WHERE M.CDEMPRESA    = P_CDEMPRESA     AND
                                         M.CDINSCESTA   = P_CDINSCESTA    AND
                                         M.DTINIPERIODO = P_DTINICIAL     AND
                                         M.DTFIMPERIODO = P_DTFINAL       AND
                                         M.CDIMPOSTO    = P_CDIMPOSTO     AND
                                         M.NRSEQUMAPAPU = IP.NRSEQUMAPAPU AND
                                         IP.CDIMPOSTO   = I.CDIMPOSTO     AND
                                         I.CDIMPOSTONFE = P_CDIMPOSTO) IC
                             WHERE M.CDEMPRESA       = P_CDEMPRESA                                  AND
                                   M.CDINSCESTA      = P_CDINSCESTA                                 AND
                                   M.DTINIPERIODO    = P_DTINICIAL                                  AND
                                   M.DTFIMPERIODO    = P_DTFINAL                                    AND
                                   M.CDIMPOSTO       = '80'                                         AND
                                   M.NRSEQUMAPAPU    = I.NRSEQUMAPAPU                               AND
                                   N.IDENTRSAIDOP    = 'E'                                          AND
                                   NVL(N.IDORIGDESTNF, DECODE(N.IDENTRSAIDOP, 'E', 'F', 'C')) = 'L' AND
                                   N.CDFILIAL        = I.CDFILIAL                                   AND
                                   N.NRLANCTONF      = I.NRLANCTONF                                 AND
                                   I.CDFILIAL        = IC.CDFILIAL                                  AND
                                   I.NRLANCTONF      = IC.NRLANCTONF                                AND
                                   P.CDIMPOSTONFE    = '80'                                         AND
                                   I.CDIMPOSTO       = P.CDIMPOSTO                                  AND
                                   NVL(I.IDIMPOSTONF, '1') <> '3'                                   AND
                                   N.IDSTATUS IN ('A','E')                                          AND
                                   NVL(SUBSTR(I.CDFISCOPERNF,1,1),' ') <> '3'                       AND
                                   NVL(N.CDMODDOCFISC, '01') = MD.CDMODDOCFISC                      AND
                                   MD.CDTPREGFISC  = '50'
                             GROUP BY I.CDFILIAL, I.NRLANCTONF, N.NRNOTAFISC, N.CDSERIE, N.CDSERIEENTR, N.CDFILIEMIT,
                                      N.DTENTRSAID, I.VRALIQNF, I.CDFISCOPERNF,N.CDMODDOCFISC, N.IDORIGDESTNF, N.CDINSCESTA) NF
                        WHERE FI.CDFILIAL(+)     = NF.CDFILIEMIT                AND
                              F.CDFILIAL         = NF.CDFILIEMIT                AND
                              EF.CDFILIAL(+)     = F.CDFILIAL                   AND
                              EF.IDTPENDEFILI(+) = NVL(F.IDENDEMISFIL,'P')      AND
                              NF.CDINSCESTA      = IE.CDINSCESTA                AND
                              IE.CDEMPRESA       = P_CDEMPRESA                  AND
                              NVL(EF.CDPAIS,IE.CDPAIS) = NVL(P_CDPAIS,NVL(EF.CDPAIS,IE.CDPAIS)) AND
                              (INSTR(NVL(P_SGESTADO, NVL(EF.SGESTADO,IE.SGESTADO)), NVL(EF.SGESTADO,IE.SGESTADO)) > 0)
                        UNION ALL
                        SELECT '53'||
                               RPAD(NVL(REPLACE(REPLACE(REPLACE(FI.NRINSJURFILI, '-', ''), '.', ''), '/', ''),'00000000000000'),14,' ')||
                               RPAD(NVL(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(DECODE(SUBSTR(UPPER(FI.CDINSCESTA),1,5),'ISENT','ISENTO',FI.CDINSCESTA), ' ', ''),'.',''),',',''),'-',''),'/',''),'ISENTO'),14,' ')||
                               TO_CHAR(NF.DTEMISSAO,'yyyymmdd')||
                               RPAD(NVL(DECODE(NVL(F.IDENDEMISFIL,'P'),'I',IE.SGESTADO,EF.SGESTADO),' '),2,' ')||
                               RPAD(NVL(NF.CDMODDOCFISC,'01'), 2, ' ')||
                               RPAD(DECODE(TRIM(NVL(NF.CDSERIE,NF.CDSERIEENTR)),'-',' ',REPLACE(NVL(NF.CDSERIE,NF.CDSERIEENTR), '-', '')),3,' ')||
                               LPAD(SUBSTR(NF.NRNOTAFISC,LENGTH(NF.NRNOTAFISC)-5,6),6,'0')||
                               RPAD(NVL(NF.CDFISCOPERNF,'0'),4,' ')||
                               'P'||
                               LTRIM(REPLACE(TO_CHAR(ABS(NF.VRBASECALCNF),'00000000000.00'),'.',''))||
                               LTRIM(REPLACE(TO_CHAR(ABS(NF.VRIMPOSTONF) ,'00000000000.00'),'.',''))||
                               LTRIM(REPLACE(TO_CHAR(ABS(NF.VRDESPESAS)  ,'00000000000.00'),'.',''))||
                               'N'||
                               RPAD(' ', 88, ' ') AS DSARQMAG
                        FROM FILIAL FI, ENDEFILI EF, FILIAL F, INSCRESTAD IE,
                            (SELECT I.CDFILIAL, I.NRLANCTONF,N.NRNOTAFISC, N.CDSERIE, N.CDSERIEENTR, N.CDFILIRECE,
                                    N.DTEMISSAO, I.VRALIQNF, I.CDFISCOPERNF,N.CDMODDOCFISC, N.IDORIGDESTNF,
                                    SUM(I.VRCONTABILNF) AS VRCONTABILNF, N.CDINSCESTA,
                                    SUM(DECODE(I.CDFISCALNF,'1',I.VRBASECALCNF,0)) AS VRBASECALCNF,
                                    SUM(DECODE(I.CDFISCALNF,'1',I.VRIMPOSTONF,0))  AS VRIMPOSTONF,
                                    SUM(DECODE(I.CDFISCALNF,'3',I.VRCONTABILNF,0))   AS VROUTRCRED,
                                    SUM(DECODE(I.CDFISCALNF,'2',I.VRCONTABILNF,'1', DECODE(I.VRPERREDBCNF,0, 0,I.VRCONTABILNF-I.VRBASECALCNF), 0)) AS VRISENTRIB,
                                    0 AS VRDESPESAS
                             FROM MAPAAPURAC M, NOTAFISCAL N, IMPOSTONF I, MODDOCFISC MD, IMPOSTO P,
                                  (SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF
                                   FROM MAPAAPURAC M, IMPOSTONF IP, IMPOSTO I
                                   WHERE M.CDEMPRESA    = P_CDEMPRESA     AND
                                         M.CDINSCESTA   = P_CDINSCESTA    AND
                                         M.DTINIPERIODO = P_DTINICIAL     AND
                                         M.DTFIMPERIODO = P_DTFINAL       AND
                                         M.CDIMPOSTO    = P_CDIMPOSTO     AND
                                         M.NRSEQUMAPAPU = IP.NRSEQUMAPAPU AND
                                         IP.CDIMPOSTO   = I.CDIMPOSTO     AND
                                         I.CDIMPOSTONFE = P_CDIMPOSTO) IC
                             WHERE M.CDEMPRESA       = P_CDEMPRESA                                  AND
                                   M.CDINSCESTA      = P_CDINSCESTA                                 AND
                                   M.DTINIPERIODO    = P_DTINICIAL                                  AND
                                   M.DTFIMPERIODO    = P_DTFINAL                                    AND
                                   M.CDIMPOSTO       = '80'                                         AND
                                   M.NRSEQUMAPAPU    = I.NRSEQUMAPAPU                               AND
                                   N.IDENTRSAIDOP    = 'S'                                          AND
                                   NVL(N.IDORIGDESTNF, DECODE(N.IDENTRSAIDOP, 'E', 'F', 'C')) = 'L' AND
                                   N.CDFILIAL        = I.CDFILIAL                                   AND
                                   N.NRLANCTONF      = I.NRLANCTONF                                 AND
                                   I.CDFILIAL        = IC.CDFILIAL                                  AND
                                   I.NRLANCTONF      = IC.NRLANCTONF                                AND
                                   P.CDIMPOSTONFE    = '80'                                         AND
                                   I.CDIMPOSTO       = P.CDIMPOSTO                                  AND
                                   N.IDSTATUS IN ('A','E')                                          AND
                                   NVL(I.IDIMPOSTONF, '1') <> '3'                                   AND
                                   NVL(SUBSTR(I.CDFISCOPERNF,1,1),' ') <> '7'                       AND
                                   NVL(N.CDMODDOCFISC, '01') = MD.CDMODDOCFISC                      AND
                                   MD.CDTPREGFISC  = '50'
                             GROUP BY I.CDFILIAL, I.NRLANCTONF, N.NRNOTAFISC, N.CDSERIE, N.CDSERIEENTR, N.CDINSCESTA,
                                      N.CDFILIRECE, N.DTEMISSAO, I.VRALIQNF, I.CDFISCOPERNF,N.CDMODDOCFISC, N.IDORIGDESTNF) NF
                        WHERE FI.CDFILIAL(+)     = NF.CDFILIRECE               AND
                              F.CDFILIAL         = NF.CDFILIRECE               AND
                              EF.CDFILIAL(+)     = F.CDFILIAL                  AND
                              EF.IDTPENDEFILI(+) = NVL(F.IDENDEMISFIL,'P')     AND
                              NF.CDINSCESTA      = IE.CDINSCESTA               AND
                              IE.CDEMPRESA       = P_CDEMPRESA                 AND
                              NVL(EF.CDPAIS,IE.CDPAIS) = NVL(P_CDPAIS,NVL(EF.CDPAIS,IE.CDPAIS)) AND
                              (INSTR(NVL(P_SGESTADO, NVL(EF.SGESTADO,IE.SGESTADO)), NVL(EF.SGESTADO,IE.SGESTADO)) > 0)) LOOP

      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD(V_CONTADOR, 10, '0'), C_REGISTRO53.DSARQMAG, P_SEQUENCIAL);

      V_CONTADOR := NVL(V_CONTADOR,0)+1;
      V_CONTREG := NVL(V_CONTREG,0)+1;
      V_CONTGERAL := NVL(V_CONTGERAL,0)+1;
   END LOOP;

   IF V_CONTREG > 0 THEN
      V_STTOTAL := P_CONTTOTAL||'53'||LPAD(RTRIM(TO_CHAR(V_CONTREG)),8,'0');
      RETURN(TO_CHAR(LPAD(V_CONTGERAL,10,0))||TO_CHAR(LPAD(V_CONTADOR,10,0))||V_STTOTAL);
   END IF;

   IF V_CONTREG = 0 THEN
      RETURN('0');
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('ARQUIVO_DIC_53','5.30.000'); END;
/

