create or replace FUNCTION ARQUIVO_DMA_DETALHAMENTO (
   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_NRSEQARQMAG  IN ARQMAGNETICO.NRSEQARQMAG%TYPE) RETURN NUMBER
AS
   V_CONTREGDET      NUMBER;
   V_NRSEQUMAPAPU    VARCHAR2(10);
   V_REG_TIPO40      VARCHAR2(60);
   V_REG_TIPO41      VARCHAR2(60);
   V_REG_TIPO42      VARCHAR2(60);
   V_REG_TIPO43      VARCHAR2(60);
   V_REG_TIPO44      VARCHAR2(60);
   V_REG_TIPO45      VARCHAR2(60);
   V_REG_TIPO46      VARCHAR2(60);
   V_REG_TIPO47      VARCHAR2(60);
   V_REG_TIPO48      VARCHAR2(60);
   V_REG_TIPO49      VARCHAR2(60);
   V_REG_TIPO50      VARCHAR2(60);
   V_REG_TIPO51      VARCHAR2(60);
   V_REG_TIPO52      VARCHAR2(60);
   V_REG_TIPO53      VARCHAR2(60);
   V_REG_TIPO54      VARCHAR2(60);
   V_REG_TIPO55      VARCHAR2(60);
   V_REG_TIPO56      VARCHAR2(60);
   V_REG_TIPO57      VARCHAR2(60);
   V_REG_TIPO58      VARCHAR2(60);
   V_REG_TIPO59      VARCHAR2(60);
   V_REG_TIPO60      VARCHAR2(60);
   V_REG_TIPO61      VARCHAR2(60);
   V_REG_TIPO62      VARCHAR2(60);
   V_REG_TIPO63      VARCHAR2(60);
   V_REG_TIPO64      VARCHAR2(60);
   V_REG_TIPO65      VARCHAR2(60);
   V_REG_TIPO66      VARCHAR2(60);
   V_REG_TIPO67      VARCHAR2(60);
   V_REG_TIPO68      VARCHAR2(60);
   V_REG_TIPO69      VARCHAR2(60);
   V_REG_TIPO70      VARCHAR2(60);
   V_REG_TIPO71      VARCHAR2(60);
   V_REG_TIPO72      VARCHAR2(60);
   V_REG_TIPO73      VARCHAR2(60);
   V_REG_TIPO74      VARCHAR2(60);
   V_REG_TIPO75      VARCHAR2(60);
   V_REG_TIPO76      VARCHAR2(60);
   V_REG_TIPO77      VARCHAR2(60);
   V_REG_TIPO78      VARCHAR2(60);
   V_REG_TIPO79      VARCHAR2(60);
   V_REG_TIPO80      VARCHAR2(60);
   V_REG_TIPO81      VARCHAR2(60);
   V_REG_TIPO82      VARCHAR2(60);
   V_REG_TIPO83      VARCHAR2(60);
   V_REG_TIPO84      VARCHAR2(60);
   V_REG_TIPO85      VARCHAR2(60);
   V_REG_TIPO86      VARCHAR2(60);
   V_REG_TIPO87      VARCHAR2(60);
   V_REG_TIPO88      VARCHAR2(60);
   V_REG_TIPO89      VARCHAR2(60);
   V_REG_TIPO90      VARCHAR2(60);
   V_REG_TIPO91      VARCHAR2(60);
   V_REG_TIPO92      VARCHAR2(60);
BEGIN
   V_CONTREGDET := 0;

   BEGIN
      SELECT MAX(NRSEQUMAPAPU)
        INTO V_NRSEQUMAPAPU
        FROM MAPAAPURAC M
       WHERE M.CDEMPRESA    = P_CDEMPRESA
         AND M.CDIMPOSTO    = P_CDIMPOSTO
         AND M.CDINSCESTA   = P_CDINSCESTA
         AND M.DTINIPERIODO = P_DTINICIAL
         AND M.DTFIMPERIODO = P_DTFINAL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_NRSEQUMAPAPU := NULL;
   END;

   /*TIPO 40*/
   BEGIN
      SELECT '40'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO40
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('1908','1909','1949');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO40 := '40'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO40, P_NRSEQARQMAG);

   /*TIPO 41*/
   BEGIN
      SELECT '41'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO41
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('1901','1902','1903','1915','1916');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO41 := '41'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO41, P_NRSEQARQMAG);

   /*TIPO 42*/
   BEGIN
      SELECT '42'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO42
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('1912','1913');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO42 := '42'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO42, P_NRSEQARQMAG);

   /*TIPO 43*/
   BEGIN
      SELECT '43'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO43
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('1914');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO43 := '43'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO43, P_NRSEQARQMAG);

   /*TIPO 44*/
   V_REG_TIPO44 := '44'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                   LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
                   
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO44, P_NRSEQARQMAG);

   /*TIPO 45*/
   BEGIN
      SELECT '45'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO45
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('1414','1415','1904');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO45 := '45'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO45, P_NRSEQARQMAG);

   /*TIPO 46*/
   BEGIN
      SELECT '46'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO46
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('1905','1906','1907');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO46 := '46'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO46, P_NRSEQARQMAG);

   /*TIPO 47*/
   BEGIN
      SELECT '47'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO47
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('1920','1921');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO47 := '47'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO47, P_NRSEQARQMAG);

   /*TIPO 48*/
   BEGIN
      SELECT '48'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO48
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('1553','1554','1555','1910','1911','1917','1918','1919','1922','1923','1924','1925','1926');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO48 := '48'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO48, P_NRSEQARQMAG);

   /*TIPO 49*/
   BEGIN
      SELECT '49'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO49
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('1451','1452');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO49 := '49'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO49, P_NRSEQARQMAG);

   /*TIPO 50*/
   BEGIN
      SELECT '50'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO50
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('2908','2909','2949');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO50 := '50'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO50, P_NRSEQARQMAG);

   /*TIPO 51*/
   BEGIN
      SELECT '51'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO51
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('2901','2902','2903','2915','2916');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO51 := '51'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO51, P_NRSEQARQMAG);

   /*TIPO 52*/
   BEGIN
      SELECT '52'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO52
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('2912','2913');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO52 := '52'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO52, P_NRSEQARQMAG);

   /*TIPO 53*/
   BEGIN
      SELECT '53'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO53
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('2914');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO53 := '53'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO53, P_NRSEQARQMAG);

   /*TIPO 54*/
   V_REG_TIPO54 := '54'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                   LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');

   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO54, P_NRSEQARQMAG);

   /*TIPO 55*/
   BEGIN
      SELECT '55'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO55
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('2414','2415','2904');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO55 := '55'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO55, P_NRSEQARQMAG);

   /*TIPO 56*/
   BEGIN
      SELECT '56'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO56
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('2905','2906','2907');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO56 := '56'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO56, P_NRSEQARQMAG);

   /*TIPO 57*/
   BEGIN
      SELECT '57'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO57
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('2920','2921');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO57 := '57'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO57, P_NRSEQARQMAG);

   /*TIPO 58*/
   BEGIN
      SELECT '58'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO58
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('2553','2554','2555','2910','2911','2917','2918','2919','2922','2923','2924','2925','2926');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO58 := '58'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO58, P_NRSEQARQMAG);

   /*TIPO 59*/
   BEGIN
      SELECT '59'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO59
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('2451','2452');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO59 := '59'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO59, P_NRSEQARQMAG);

   /*TIPO 60*/
   BEGIN
      SELECT '60'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO60
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('3949');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO60 := '60'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO60, P_NRSEQARQMAG);

   /*TIPO 61*/
   V_REG_TIPO61 := '61'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                   LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO61, P_NRSEQARQMAG);

   /*TIPO 62*/
   V_REG_TIPO62 := '62'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                   LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO62, P_NRSEQARQMAG);

   /*TIPO 63*/
   BEGIN
      SELECT '63'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO63
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('3553','3930');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO63 := '63'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO63, P_NRSEQARQMAG);

   /*TIPO 64*/
   V_REG_TIPO64 := '64'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                   LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO64, P_NRSEQARQMAG);

   /*TIPO 65*/
   BEGIN
      SELECT '65'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO65
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('5412','5413','5551','5552','5553','5556','5557');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO65 := '65'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO65, P_NRSEQARQMAG);

   /*TIPO 66*/
   BEGIN
      SELECT '66'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO66
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('5908','5909','5949');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO66 := '66'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO66, P_NRSEQARQMAG);

   /*TIPO 67*/
   BEGIN
      SELECT '67'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO67
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('5901','5902','5903','5915','5916');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO67 := '67'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO67, P_NRSEQARQMAG);

   /*TIPO 68*/
   BEGIN
      SELECT '68'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO68
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('5912','5913');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO68 := '68'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO68, P_NRSEQARQMAG);

   /*TIPO 69*/
   BEGIN
      SELECT '69'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO69
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('5914');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO69 := '69'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO69, P_NRSEQARQMAG);

   /*TIPO 70*/
   V_REG_TIPO70 := '70'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO70, P_NRSEQARQMAG);

   /*TIPO 71*/
   BEGIN
      SELECT '71'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO71
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('5414','5415','5904');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO71 := '71'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO71, P_NRSEQARQMAG);

   /*TIPO 72*/
   BEGIN
      SELECT '72'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO72
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('5905','5906','5907');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO72 := '72'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO72, P_NRSEQARQMAG);

   /*TIPO 73*/
   BEGIN
      SELECT '73'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO73
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('5920','5921');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO73 := '73'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO73, P_NRSEQARQMAG);

   /*TIPO 74*/
   BEGIN
      SELECT '74'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO74
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('5554','5555','5910','5911','5917','5918','5919','5922','5923','5924','5925','5926','5929');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO74 := '74'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO74, P_NRSEQARQMAG);

   /*TIPO 75*/
   BEGIN
      SELECT '75'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO75
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('5451','5927','5928','5931');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO75 := '75'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO75, P_NRSEQARQMAG);

   /*TIPO 76*/
   BEGIN
      SELECT '76'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO76
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('6412','6413','6551','6552','6553','6556','6557');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO76 := '76'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO76, P_NRSEQARQMAG);

   /*TIPO 77*/
   BEGIN
      SELECT '77'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO77
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('6908','6909','6949');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO77 := '77'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO77, P_NRSEQARQMAG);

   /*TIPO 78*/
   BEGIN
      SELECT '78'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO78
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('6901','6902','6903','6915','6916');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO78 := '78'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO78, P_NRSEQARQMAG);

   /*TIPO 79*/
   BEGIN
      SELECT '79'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO79
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('6912','6913');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO79 := '79'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO79, P_NRSEQARQMAG);

   /*TIPO 80*/
   BEGIN
      SELECT '80'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO80
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('6914');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO80 := '80'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO80, P_NRSEQARQMAG);

   /*TIPO 81*/
   V_REG_TIPO81 := '81'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                   LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO81, P_NRSEQARQMAG);

   /*TIPO 82*/
   BEGIN
      SELECT '82'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO82
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('6414','6415','6904');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO82 := '82'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO82, P_NRSEQARQMAG);

   /*TIPO 83*/
   BEGIN
      SELECT '83'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO83
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('6905','6907');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO83 := '83'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO83, P_NRSEQARQMAG);

   /*TIPO 84*/
   BEGIN
      SELECT '84'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO84
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('6920','6921');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO84 := '84'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO84, P_NRSEQARQMAG);

   /*TIPO 85*/
   BEGIN
      SELECT '85'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO85
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('6554','6555','6910','6911','6917','6918','6919','6922','6923','6924','6925','6929','6932');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO85 := '85'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO85, P_NRSEQARQMAG);

   /*TIPO 86*/
   BEGIN
      SELECT '86'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO86
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('6931');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO86 := '86'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO86, P_NRSEQARQMAG);

   /*TIPO 87*/
   BEGIN
      SELECT '87'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO87
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('7551','7553','7556');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO87 := '87'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO87, P_NRSEQARQMAG);

   /*TIPO 88*/
   BEGIN
      SELECT '88'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO88
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('7949');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO88 := '88'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO88, P_NRSEQARQMAG);

   /*TIPO 89*/
   V_REG_TIPO89 := '89'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO89, P_NRSEQARQMAG);

   /*TIPO 90*/
   V_REG_TIPO90 := '90'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO90, P_NRSEQARQMAG);

   /*TIPO 91*/
   BEGIN
      SELECT '91'
             ||TO_CHAR(P_DTINICIAL, 'YYYYMM')
             ||LPAD(P_CDINSCESTA, 9, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 1, ROUND(NVL(I.VRBASECALCNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(DECODE(I.CDFISCALNF, 2, ROUND(NVL(I.VRCONTABILNF,0),2), 0)), '9999999999.00'), '.', '')), 12, '0')
             ||LPAD(LTRIM(REPLACE(TO_CHAR(SUM(ROUND(I.VRCONTABILNF,2) - DECODE(I.CDFISCALNF,1,ROUND(I.VRBASECALCNF,2),0) - DECODE(I.CDFISCALNF,2,ROUND(I.VRCONTABILNF,2),0)), '9999999999.00'), '.', '')), 12, '0')
        INTO V_REG_TIPO91
        FROM IMPOSTONF I
       WHERE I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND I.CDFISCOPERNF IN ('7930');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_REG_TIPO91 := '91'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   END;
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO91, P_NRSEQARQMAG);

   /*TIPO 92*/
   V_REG_TIPO92 := '92'||TO_CHAR(P_DTINICIAL, 'YYYYMM')||LPAD(P_CDINSCESTA, 9, '0')||
                         LPAD('0', 12, '0')||LPAD('0', 12, '0')||LPAD('0', 12, '0');
   V_CONTREGDET := NVL(V_CONTREGDET, 0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (V_CONTREGDET, V_REG_TIPO92, P_NRSEQARQMAG);

   RETURN(V_CONTREGDET);
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('ARQUIVO_DMA_CS','5.65.000'); END;
/

