CREATE OR REPLACE FUNCTION ARQUIVO_DACON_R06(
   P_CDEMPRESA              IN EMPRESA.CDEMPRESA%TYPE,
   P_DTINICIO               IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_DTFIM                  IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_NRSEQARQMAG            IN ARQMAGNETICO.NRSEQARQMAG%TYPE,
   P_LIVRO                  IN RELACPRODITGEN.CDLIVROMAG%TYPE) RETURN NUMBER

AS
   V_CONTREG06        NUMBER;

BEGIN

 /*----------------------------------TIPO R06A-------------------------------------*/

   FOR C_REG_R06A IN (SELECT (RPAD('R06A',4,' ')                                                                                    ||
                              TO_CHAR(P_DTINICIO,'MM')                                                                              ||
                              '01'                                                                                                  ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR01,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR02,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR03,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR04,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR05,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR06,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR07,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR08,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR09,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR10,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR11,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR12,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR13,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR14,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR15,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR16,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR17,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR18,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR19,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR20,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR21,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR22,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR23,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR24,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR25,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR26,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR27,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR28,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR25+B.VR26+B.VR27+B.VR28,0)),'000000000000.00'),'.')),'14','0') ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR30,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR31,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR32,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR33,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD('0',38,'0')                                                                                      ||
                              LPAD('0',10,'0')  ) AS REG_R06A
                        FROM
                          (SELECT
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '01') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '1')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR01,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '02') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '1')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR02,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '03') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '1')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR03,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '04') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '1')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR04,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '05') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '1')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR05,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '06') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '1')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR06,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '07') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '1')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR07,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '08') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '1')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR08,
                                  LPAD('0',14,'0') AS VR09,
                                  LPAD('0',14,'0') AS VR10,
                                  LPAD('0',14,'0') AS VR11,
                                 (CASE WHEN ((A.CDNATUBCALPCIT  = '12') AND ((SUBSTR(A.CDTPCREDPICOITE,1,1)= '1'))) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR12,
                                 (CASE WHEN ((A.CDNATUBCALPCIT  = '13') AND ((SUBSTR(A.CDTPCREDPICOITE,1,1)= '1'))) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR13,
                                 (CASE WHEN ((A.CDNATUBCALPCIT  IN ('01','02','03','04','05','06','07','08','12','13')) AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '1')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR14,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '101') THEN TO_CHAR(A.VRIMPOSTOIT) ELSE LPAD('0',14,'0') END) AS VR15,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '102') THEN TO_CHAR(A.VRIMPOSTOIT) ELSE LPAD('0',14,'0') END) AS VR16,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '103') THEN TO_CHAR(A.VRIMPOSTOIT) ELSE LPAD('0',14,'0') END) AS VR17,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '14') AND ((SUBSTR(A.CDTPCREDPICOITE,1,1)= '1'))) THEN TO_CHAR(A.VRIMPOSTOIT) ELSE LPAD('0',14,'0') END) AS VR18,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '104') THEN TO_CHAR(A.VRIMPOSTOIT) ELSE LPAD('0',14,'0') END) AS VR19,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '109') THEN TO_CHAR(A.VRIMPOSTOIT) ELSE LPAD('0',14,'0') END) AS VR20,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '199') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR21,
                                  LPAD('0',14,'0')  AS VR22,
                                  LPAD('0',14,'0')  AS VR23,
                                  TO_CHAR(A.VRIMPOSTOIT) AS VR24,
                                  /*(CASE WHEN ((A.CDNATUBCALPCIT IN ('12','13','14')) AND(SUBSTR(A.CDTPCREDPICOITE,1,1)= '1')) OR (A.CDTPCREDPICOITE IN ('101','102','103','104','109','199'))THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR24,*/
                                  DECODE(CLASSIFICA_PRODUTOS_DACON('I',CDPRODUTO,P_LIVRO,CDNATUBCALPCIT,CDSITUTRIBITNFE),'A',VRIMPOSTOIT,0) AS VR25,
                                  DECODE(CLASSIFICA_PRODUTOS_DACON('I',CDPRODUTO,P_LIVRO,CDNATUBCALPCIT,CDSITUTRIBITNFE),'V',VRIMPOSTOIT,0) AS VR26,
                                  LPAD('0',14,'0')  AS VR27,
                                  LPAD('0',14,'0')  AS VR28,
                                  LPAD('0',14,'0')  AS VR30,
                                  LPAD('0',14,'0')  AS VR31,
                                  LPAD('0',14,'0')  AS VR32,
                                  LPAD('0',14,'0')  AS VR33
                             FROM
                                (SELECT VRCONTITIMP, CDNATUBCALPCIT,CDTPCREDPICOITE, VRBASECALCIT, CDPRODUTO,CDSITUTRIBITNFE,VRIMPOSTOIT
                                   FROM ITEMPROIMPOS IT, ITEMPRODUTO IP,IMPOSTO IM,NOTAFISCAL N, FILIAL F,
                                      (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF
                                         FROM IMPOSTONF I, MAPAAPURAC M
                                        WHERE M.CDIMPOSTO     =  '50'
                                          AND M.CDEMPRESA     =  P_CDEMPRESA
                                          AND M.DTINIPERIODO  >= P_DTINICIO
                                          AND M.DTFIMPERIODO  <= P_DTFIM
                                          AND M.NRSEQUMAPAPU  =  I.NRSEQUMAPAPU
                                          /*AND SUBSTR(I.CDFISCOPERNF) IN ('1','2','3')*/ ) NOTA
                            WHERE IT.CDFILIAL        = NOTA.CDFILIAL
                              AND IT.NRLANCTONF      = NOTA.NRLANCTONF
                              AND IT.CDIMPOSTO       = IM.CDIMPOSTO
                              AND IM.CDIMPOSTONFE = '50'
                              AND IT.CDFILIAL        = IP.CDFILIAL
                              AND IT.NRLANCTONF      = IP.NRLANCTONF
                              AND IT.NRSEQITPROD     = IP.NRSEQITPROD
                              AND N.CDFILIAL         = IT.CDFILIAL
                              AND N.NRLANCTONF       = IT.NRLANCTONF
                              AND N.CDFILIAL         = NOTA.CDFILIAL
                              AND N.NRLANCTONF       = NOTA.NRLANCTONF
                              AND DECODE(IDENTRSAIDOP,'S',N.CDFILIEMIT,N.CDFILIRECE) = F.CDFILIAL
                              AND F.IDCUMUPISCOFIL   = 'S'
                              AND IT.CDSITUTRIBITNFE BETWEEN '50' AND '67'
                              AND SUBSTR(CDTPCREDPICOITE,1,1)= '1'

                          )A
                     )B
                      GROUP BY 'R06A', TO_CHAR(P_DTINICIO,'MM'),'01'
                      UNION ALL

                       SELECT (RPAD('R06A',4,' ')                                                                                   ||
                              TO_CHAR(P_DTINICIO,'MM')                                                                              ||
                              '02'                                                                                                  ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR01,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR02,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR03,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR04,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR05,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR06,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR07,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR08,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR09,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR10,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR11,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR12,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR13,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR14,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR15,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR16,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR17,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR18,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR19,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR20,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR21,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR22,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR23,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR24,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR25,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR26,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR27,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR28,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR25+B.VR26+B.VR27+B.VR28,0)),'000000000000.00'),'.')),'14','0') ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR30,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR31,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR32,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR33,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD('0',38,'0')                                                                                      ||
                              LPAD('0',10,'0')  ) AS REG_R06A
                        FROM
                          (SELECT
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '01') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '2')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR01,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '02') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '2')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR02,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '03') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '2')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR03,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '04') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '2')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR04,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '05') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '2')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR05,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '06') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '2')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR06,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '07') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '2')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR07,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '08') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '2')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR08,
                                  LPAD('0',14,'0') AS VR09,
                                  LPAD('0',14,'0') AS VR10,
                                  LPAD('0',14,'0') AS VR11,
                                 (CASE WHEN ((A.CDNATUBCALPCIT  = '12') AND ((SUBSTR(A.CDTPCREDPICOITE,1,1)= '2'))) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR12,
                                 (CASE WHEN ((A.CDNATUBCALPCIT  = '13') AND ((SUBSTR(A.CDTPCREDPICOITE,1,1)= '2'))) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR13,
                                 (CASE WHEN ((A.CDNATUBCALPCIT  IN ('01','02','03','04','05','06','07','08','12','13')) AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '2')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR14,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '201') THEN TO_CHAR(A.VRIMPOSTOIT) ELSE LPAD('0',14,'0') END) AS VR15,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '202') THEN TO_CHAR(A.VRIMPOSTOIT) ELSE LPAD('0',14,'0') END) AS VR16,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '203') THEN TO_CHAR(A.VRIMPOSTOIT) ELSE LPAD('0',14,'0') END) AS VR17,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '14') AND ((SUBSTR(A.CDTPCREDPICOITE,1,1)= '2'))) THEN TO_CHAR(A.VRIMPOSTOIT) ELSE LPAD('0',14,'0') END) AS VR18,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '204') THEN TO_CHAR(A.VRIMPOSTOIT) ELSE LPAD('0',14,'0') END) AS VR19,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '209') THEN TO_CHAR(A.VRIMPOSTOIT) ELSE LPAD('0',14,'0') END) AS VR20,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '299') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR21,
                                  LPAD('0',14,'0')  AS VR22,
                                  LPAD('0',14,'0')  AS VR23,
                                  TO_CHAR(A.VRIMPOSTOIT) AS VR24,
                                  /*(CASE WHEN ((A.CDNATUBCALPCIT IN ('12','13','14')) AND(SUBSTR(A.CDTPCREDPICOITE,1,1)= '2')) OR (A.CDTPCREDPICOITE IN ('201','202','203','204','209','299'))THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR24,*/
                                  DECODE(CLASSIFICA_PRODUTOS_DACON('I',CDPRODUTO,P_LIVRO,CDNATUBCALPCIT,CDSITUTRIBITNFE),'A',VRIMPOSTOIT,0) AS VR25,
                                  DECODE(CLASSIFICA_PRODUTOS_DACON('I',CDPRODUTO,P_LIVRO,CDNATUBCALPCIT,CDSITUTRIBITNFE),'V',VRIMPOSTOIT,0) AS VR26,
                                  LPAD('0',14,'0')  AS VR27,
                                  LPAD('0',14,'0')  AS VR28,
                                  LPAD('0',14,'0')  AS VR30,
                                  LPAD('0',14,'0')  AS VR31,
                                  LPAD('0',14,'0')  AS VR32,
                                  LPAD('0',14,'0')  AS VR33
                             FROM
                                (SELECT VRCONTITIMP, CDNATUBCALPCIT,CDTPCREDPICOITE, VRBASECALCIT,VRIMPOSTOIT,CDSITUTRIBITNFE, CDPRODUTO
                                   FROM ITEMPROIMPOS IT, ITEMPRODUTO IP,IMPOSTO IM,NOTAFISCAL N, FILIAL F,
                                      (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF
                                         FROM IMPOSTONF I, MAPAAPURAC M
                                        WHERE M.CDIMPOSTO     =  '50'
                                          AND M.CDEMPRESA     =  P_CDEMPRESA
                                          AND M.DTINIPERIODO  >= P_DTINICIO
                                          AND M.DTFIMPERIODO  <= P_DTFIM
                                          AND M.NRSEQUMAPAPU  =  I.NRSEQUMAPAPU
                                          /*AND SUBSTR(I.CDFISCOPERNF) IN ('1','2','3')*/) NOTA
                            WHERE IT.CDFILIAL        = NOTA.CDFILIAL
                              AND IT.NRLANCTONF      = NOTA.NRLANCTONF
                              AND IT.CDIMPOSTO       = IM.CDIMPOSTO
                              AND IM.CDIMPOSTONFE = '50'
                              AND IT.CDFILIAL        = IP.CDFILIAL
                              AND IT.NRLANCTONF      = IP.NRLANCTONF
                              AND IT.NRSEQITPROD     = IP.NRSEQITPROD
                              AND N.CDFILIAL         = IT.CDFILIAL
                              AND N.NRLANCTONF       = IT.NRLANCTONF
                              AND N.CDFILIAL         = NOTA.CDFILIAL
                              AND N.NRLANCTONF       = NOTA.NRLANCTONF
                              AND DECODE(IDENTRSAIDOP,'S',N.CDFILIEMIT,N.CDFILIRECE) = F.CDFILIAL
                              AND F.IDCUMUPISCOFIL   = 'S'
                              AND IT.CDSITUTRIBITNFE BETWEEN '50' AND '67'
                              AND SUBSTR(CDTPCREDPICOITE,1,1)= '2'
                          )A
                     )B
                     GROUP BY 'R06A', TO_CHAR(P_DTINICIO,'MM'),'01'
                     UNION ALL
                       SELECT (RPAD('R06A',4,' ')                                                                                   ||
                              TO_CHAR(P_DTINICIO,'MM')                                                                              ||
                              '03'                                                                                                  ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR01,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR02,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR03,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR04,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR05,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR06,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR07,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR08,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR09,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR10,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR11,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR12,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR13,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR14,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR15,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR16,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR17,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR18,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR19,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR20,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR21,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR22,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR23,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD('0',14,'0')                                                                                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR25,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR26,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR27,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR28,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR25+B.VR26+B.VR27+B.VR28,0)),'000000000000.00'),'.')),'14','0') ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR30,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR31,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR32,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(B.VR33,0)),'000000000000.00'),'.')),'14','0')                      ||
                              LPAD('0',38,'0')                                                                                      ||
                              LPAD('0',10,'0')  ) AS REG_R06A                        FROM
                          (SELECT
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '01') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '3')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR01,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '02') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '3')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR02,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '03') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '3')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR03,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '04') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '3')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR04,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '05') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '3')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR05,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '06') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '3')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR06,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '07') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '3')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR07,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '08') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '3')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR08,
                                  LPAD('0',14,'0') AS VR09,
                                  LPAD('0',14,'0') AS VR10,
                                  LPAD('0',14,'0') AS VR11,
                                 (CASE WHEN ((A.CDNATUBCALPCIT  = '12') AND ((SUBSTR(A.CDTPCREDPICOITE,1,1)= '3'))) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR12,
                                 (CASE WHEN ((A.CDNATUBCALPCIT  = '13') AND ((SUBSTR(A.CDTPCREDPICOITE,1,1)= '3'))) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR13,
                                 (CASE WHEN ((A.CDNATUBCALPCIT  IN ('01','02','03','04','05','06','07','08','12','13')) AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '3')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR14,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '301') THEN TO_CHAR(A.VRIMPOSTOIT) ELSE LPAD('0',14,'0') END) AS VR15,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '302') THEN TO_CHAR(A.VRIMPOSTOIT) ELSE LPAD('0',14,'0') END) AS VR16,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '303') THEN TO_CHAR(A.VRIMPOSTOIT) ELSE LPAD('0',14,'0') END) AS VR17,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '14') AND ((SUBSTR(A.CDTPCREDPICOITE,1,1)= '3'))) THEN TO_CHAR(A.VRIMPOSTOIT) ELSE LPAD('0',14,'0') END) AS VR18,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '304') THEN TO_CHAR(A.VRIMPOSTOIT) ELSE LPAD('0',14,'0') END) AS VR19,
                                  LPAD('0',14,'0')AS VR20,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '399') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR21,
                                  LPAD('0',14,'0')  AS VR22,
                                  LPAD('0',14,'0')  AS VR23,
                                  /*(CASE WHEN ((A.CDNATUBCALPCIT IN ('12','13','14')) AND(SUBSTR(A.CDTPCREDPICOITE,1,1)= '3')) OR (A.CDTPCREDPICOITE IN ('301','302','303','304','309','399'))THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR24,*/
                                  DECODE(CLASSIFICA_PRODUTOS_DACON('I',CDPRODUTO,P_LIVRO,CDNATUBCALPCIT,CDSITUTRIBITNFE),'A',VRIMPOSTOIT,0) AS VR25,
                                  DECODE(CLASSIFICA_PRODUTOS_DACON('I',CDPRODUTO,P_LIVRO,CDNATUBCALPCIT,CDSITUTRIBITNFE),'V',VRIMPOSTOIT,0) AS VR26,
                                  LPAD('0',14,'0')  AS VR27,
                                  LPAD('0',14,'0')  AS VR28,
                                  LPAD('0',14,'0')  AS VR30,
                                  LPAD('0',14,'0')  AS VR31,
                                  LPAD('0',14,'0')  AS VR32,
                                  LPAD('0',14,'0')  AS VR33
                             FROM
                                (SELECT VRCONTITIMP, CDNATUBCALPCIT,CDTPCREDPICOITE, VRBASECALCIT,VRIMPOSTOIT,CDSITUTRIBITNFE, CDPRODUTO
                                   FROM ITEMPROIMPOS IT, ITEMPRODUTO IP,IMPOSTO IM,NOTAFISCAL N, FILIAL F,
                                      (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF
                                         FROM IMPOSTONF I, MAPAAPURAC M
                                        WHERE M.CDIMPOSTO     =  '50'
                                          AND M.CDEMPRESA     =  P_CDEMPRESA
                                          AND M.DTINIPERIODO  >= P_DTINICIO
                                          AND M.DTFIMPERIODO  <= P_DTFIM
                                          AND M.NRSEQUMAPAPU  =  I.NRSEQUMAPAPU
                                          /*AND SUBSTR(I.CDFISCOPERNF) IN ('1','2','3')*/) NOTA
                            WHERE IT.CDFILIAL        = NOTA.CDFILIAL
                              AND IT.NRLANCTONF      = NOTA.NRLANCTONF
                              AND IT.CDIMPOSTO       = IM.CDIMPOSTO
                              AND IM.CDIMPOSTONFE = '50'
                              AND IT.CDFILIAL        = IP.CDFILIAL
                              AND IT.NRLANCTONF      = IP.NRLANCTONF
                              AND IT.NRSEQITPROD     = IP.NRSEQITPROD
                              AND N.CDFILIAL         = IT.CDFILIAL
                              AND N.NRLANCTONF       = IT.NRLANCTONF
                              AND N.CDFILIAL         = NOTA.CDFILIAL
                              AND N.NRLANCTONF       = NOTA.NRLANCTONF
                              AND DECODE(IDENTRSAIDOP,'S',N.CDFILIEMIT,N.CDFILIRECE) = F.CDFILIAL
                              AND F.IDCUMUPISCOFIL   = 'S'
                              AND IT.CDSITUTRIBITNFE BETWEEN '50' AND '67'
                              AND SUBSTR(CDTPCREDPICOITE,1,1)= '3'
                          )A
                     )B
                      GROUP BY 'R06A', TO_CHAR(P_DTINICIO,'MM'),'01'                       ) LOOP

      V_CONTREG06 := NVL(V_CONTREG06,0) + 1;

      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
         ('07'||LPAD(RTRIM(V_CONTREG06), 8, '0'), C_REG_R06A.REG_R06A, P_NRSEQARQMAG);

   END LOOP;

 /*----------------------------------TIPO R06B-------------------------------------*/

/*   FOR C_REG_R06A IN (SELECT (RPAD('R06B',4,' ')                                                                                                ||
                              TO_CHAR(P_DTINICIO,'MM')                                                                              ||
                              '01'                                                                                                  ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR01,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR02,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR03,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR04,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR05,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR06,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR07,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR08,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR09,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR10,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR11,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR12,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR13,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR14,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR15,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR16,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR17,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR18,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR19,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR20,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR21,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR22,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR23,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR24,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ) AS REG_R06B
                        FROM
                          (SELECT
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '01') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '1')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR01,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '02') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '1')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR02,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '03') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '1')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR03,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '04') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '1')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR04,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '05') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '1')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR05,
                                  0 AS VR06,
                                  0 AS VR07,
                                 (CASE WHEN ((A.CDNATUBCALPCIT  = '13') AND ((SUBSTR(A.CDTPCREDPICOITE,1,1)= '1'))) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR13,
                                 (CASE WHEN ((A.CDNATUBCALPCIT  IN ('01','02','03','04','05','06','07','08','12','13')) AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '1')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR14,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '101') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR15,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '102') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR16,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '103') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR17,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '104') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR19,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '109') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR20,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '199') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR21,
                                  LPAD('0',14,'0')  AS VR22,
                                  LPAD('0',14,'0')  AS VR23,
                                 (CASE WHEN ((A.CDNATUBCALPCIT IN ('12','13','14')) AND(SUBSTR(A.CDTPCREDPICOITE,1,1)= '1')) OR (A.CDTPCREDPICOITE IN ('101','102','103','104','109','199'))THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END)AS VR24
                         FROM
                                (SELECT VRCONTITIMP, CDNATUBCALPCIT,CDTPCREDPICOITE, VRBASECALCIT, CDPRODUTO,CDSITUTRIBITNFE
                                   FROM ITEMPROIMPOS IT, ITEMPRODUTO IP,IMPOSTO IM,
                                      (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF
                                         FROM IMPOSTONF I, MAPAAPURAC M
                                        WHERE M.CDIMPOSTO     =  '50'
                                          AND M.CDEMPRESA     =  P_CDEMPRESA
                                          AND M.DTINIPERIODO  >= P_DTINICIO
                                          AND M.DTFIMPERIODO  <= P_DTFIM
                                          AND M.NRSEQUMAPAPU  =  I.NRSEQUMAPAPU) NOTA
                            WHERE IT.CDFILIAL        = NOTA.CDFILIAL
                              AND IT.NRLANCTONF      = NOTA.NRLANCTONF
                              AND IT.CDIMPOSTO       = IM.CDIMPOSTO
                              AND IM.CDIMPOSTONFE = '50'
                              AND IT.CDFILIAL        = IP.CDFILIAL
                              AND IT.NRLANCTONF      = IP.NRLANCTONF
                              AND IT.NRSEQITPROD     = IP.NRSEQITPROD
                              AND SUBSTR(IT.CDFISCOPERIT,1,1)= '7'
                          )A
                     )B
                      GROUP BY 'R06B', TO_CHAR(P_DTINICIO,'MM'),'01'
                      UNION ALL

                       SELECT (RPAD('R06B',4,' ')                                                                                                      ||
                              TO_CHAR(P_DTINICIO,'MM')                                                                                                 ||
                              '01'                                                                                                                     ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR01,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR02,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR03,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR04,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR05,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR06,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR07,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR08,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR09,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR10,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR11,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR12,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR13,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR14,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR15,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR16,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR17,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR18,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR19,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR20,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR21,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR22,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR23,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR24,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR25,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR26,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR27,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR28,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR25+B.VR26+B.VR27+B.VR28,0)),'000000000000.00'),',',''),'.',''),'14','0')) ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR30,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR31,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR32,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR33,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ) AS REG_R06B
                        FROM
                          (SELECT
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '01') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '2')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR01,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '02') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '2')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR02,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '03') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '2')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR03,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '04') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '2')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR04,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '05') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '2')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR05,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '06') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '2')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR06,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '07') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '2')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR07,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '08') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '2')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR08,
                                  LPAD('0',14,'0') AS VR09,
                                  LPAD('0',14,'0') AS VR10,
                                  LPAD('0',14,'0') AS VR11,
                                 (CASE WHEN ((A.CDNATUBCALPCIT  = '12') AND ((SUBSTR(A.CDTPCREDPICOITE,1,1)= '2'))) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR12,
                                 (CASE WHEN ((A.CDNATUBCALPCIT  = '13') AND ((SUBSTR(A.CDTPCREDPICOITE,1,1)= '2'))) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR13,
                                 (CASE WHEN ((A.CDNATUBCALPCIT  IN ('01','02','03','04','05','06','07','08','12','13')) AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '2')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR14,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '201') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR15,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '202') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR16,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '203') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR17,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '14') AND ((SUBSTR(A.CDTPCREDPICOITE,1,1)= '2'))) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR18,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '204') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR19,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '209') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR20,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '299') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR21,
                                  LPAD('0',14,'0')  AS VR22,
                                  LPAD('0',14,'0')  AS VR23,
                                  (CASE WHEN ((A.CDNATUBCALPCIT IN ('12','13','14')) AND(SUBSTR(A.CDTPCREDPICOITE,1,1)= '2')) OR (A.CDTPCREDPICOITE IN ('201','202','203','204','209','299'))THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR24,
                                  DECODE(CLASSIFICA_PRODUTOS_DACON('I',CDPRODUTO,P_LIVRO,CDNATUBCALPCIT,CDSITUTRIBITNFE),'A',VRIMPOSTOIT,0) AS VR25,
                                  DECODE(CLASSIFICA_PRODUTOS_DACON('I',CDPRODUTO,P_LIVRO,CDNATUBCALPCIT,CDSITUTRIBITNFE),'V',VRIMPOSTOIT,0) AS VR26,
                                  LPAD('0',14,'0')  AS VR27,
                                  LPAD('0',14,'0')  AS VR28,
                                  LPAD('0',14,'0')  AS VR30,
                                  LPAD('0',14,'0')  AS VR31,
                                  LPAD('0',14,'0')  AS VR32,
                                  LPAD('0',14,'0')  AS VR33
                             FROM
                                (SELECT VRCONTITIMP, CDNATUBCALPCIT,CDTPCREDPICOITE, VRBASECALCIT
                                   FROM ITEMPROIMPOS IT, ITEMPRODUTO IP,IMPOSTO IM,
                                      (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF
                                         FROM IMPOSTONF I, MAPAAPURAC M
                                        WHERE M.CDIMPOSTO     =  '50'
                                          AND M.CDEMPRESA     =  P_CDEMPRESA
                                          AND M.DTINIPERIODO  >= P_DTINICIO
                                          AND M.DTFIMPERIODO  <= P_DTFIM
                                          AND M.NRSEQUMAPAPU  =  I.NRSEQUMAPAPU) NOTA
                            WHERE IT.CDFILIAL        = NOTA.CDFILIAL
                              AND IT.NRLANCTONF      = NOTA.NRLANCTONF
                              AND IT.CDIMPOSTO       = IM.CDIMPOSTO
                              AND IM.CDIMPOSTONFE = '50'
                              AND IT.CDFILIAL        = IP.CDFILIAL
                              AND IT.NRLANCTONF      = IP.NRLANCTONF
                              AND IT.NRSEQITPROD     = IP.NRSEQITPROD
                          )A
                     )B
                     GROUP BY 'R06B', TO_CHAR(P_DTINICIO,'MM'),'01'
                     UNION ALL
                       SELECT (RPAD('R06B',4,' ')                                                                                                      ||
                              TO_CHAR(P_DTINICIO,'MM')                                                                                                 ||
                              '01'                                                                                                                     ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR01,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR02,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR03,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR04,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR05,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR06,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR07,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR08,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR09,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR10,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR11,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR12,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR13,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR14,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR15,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR16,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR17,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR18,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR19,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR20,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR21,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR22,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR23,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR24,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR25,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR26,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR27,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR28,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR25+B.VR26+B.VR27+B.VR28,0)),'000000000000.00'),',',''),'.',''),'14','0')) ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR30,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR31,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR32,0)),'000000000000.00'),',',''),'.',''),'14','0'))                      ||
                              LTRIM(LPAD(REPLACE(REPLACE(TO_CHAR(SUM(NVL(B.VR33,0)),'000000000000.00'),',',''),'.',''),'14','0'))   ) AS REG_R06B
                        FROM
                          (SELECT
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '01') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '3')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR01,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '02') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '3')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR02,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '03') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '3')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR03,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '04') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '3')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR04,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '05') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '3')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR05,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '06') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '3')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR06,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '07') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '3')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR07,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '08') AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '3')) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR08,
                                  LPAD('0',14,'0') AS VR09,
                                  LPAD('0',14,'0') AS VR10,
                                  LPAD('0',14,'0') AS VR11,
                                 (CASE WHEN ((A.CDNATUBCALPCIT  = '12') AND ((SUBSTR(A.CDTPCREDPICOITE,1,1)= '3'))) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR12,
                                 (CASE WHEN ((A.CDNATUBCALPCIT  = '13') AND ((SUBSTR(A.CDTPCREDPICOITE,1,1)= '3'))) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR13,
                                 (CASE WHEN ((A.CDNATUBCALPCIT  IN ('01','02','03','04','05','06','07','08','12','13')) AND (SUBSTR(A.CDTPCREDPICOITE,1,1)= '3')) THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END) AS VR14,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '301') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR15,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '302') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR16,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '303') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR17,
                                 (CASE WHEN ((A.CDNATUBCALPCIT = '14') AND ((SUBSTR(A.CDTPCREDPICOITE,1,1)= '3'))) THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR18,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '304') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR19,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '309') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR20,
                                 (CASE WHEN (A.CDTPCREDPICOITE  = '399') THEN TO_CHAR(A.VRCONTITIMP) ELSE LPAD('0',14,'0') END) AS VR21,
                                  LPAD('0',14,'0')  AS VR22,
                                  LPAD('0',14,'0')  AS VR23,
                                  (CASE WHEN ((A.CDNATUBCALPCIT IN ('12','13','14')) AND(SUBSTR(A.CDTPCREDPICOITE,1,1)= '3')) OR (A.CDTPCREDPICOITE IN ('301','302','303','304','309','399'))THEN TO_CHAR(A.VRBASECALCIT) ELSE LPAD('0',14,'0') END)AS VR24,
                                  DECODE(CLASSIFICA_PRODUTOS_DACON('I',CDPRODUTO,P_LIVRO,CDNATUBCALPCIT,CDSITUTRIBITNFE),'A',VRIMPOSTOIT,0) AS VR25,
                                  DECODE(CLASSIFICA_PRODUTOS_DACON('I',CDPRODUTO,P_LIVRO,CDNATUBCALPCIT,CDSITUTRIBITNFE),'V',VRIMPOSTOIT,0) AS VR26,
                                  LPAD('0',14,'0')  AS VR27,
                                  LPAD('0',14,'0')  AS VR28,
                                  LPAD('0',14,'0')  AS VR30,
                                  LPAD('0',14,'0')  AS VR31,
                                  LPAD('0',14,'0')  AS VR32,
                                  LPAD('0',14,'0')  AS VR33
                             FROM
                                (SELECT VRCONTITIMP, CDNATUBCALPCIT,CDTPCREDPICOITE, VRBASECALCIT
                                   FROM ITEMPROIMPOS IT, ITEMPRODUTO IP,IMPOSTO IM,
                                      (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF
                                         FROM IMPOSTONF I, MAPAAPURAC M
                                        WHERE M.CDIMPOSTO     =  '50'
                                          AND M.CDEMPRESA     =  P_CDEMPRESA
                                          AND M.DTINIPERIODO  >= P_DTINICIO
                                          AND M.DTFIMPERIODO  <= P_DTFIM
                                          AND M.NRSEQUMAPAPU  =  I.NRSEQUMAPAPU) NOTA
                            WHERE IT.CDFILIAL        = NOTA.CDFILIAL
                              AND IT.NRLANCTONF      = NOTA.NRLANCTONF
                              AND IT.CDIMPOSTO       = IM.CDIMPOSTO
                              AND IM.CDIMPOSTONFE = '50'
                              AND IT.CDFILIAL        = IP.CDFILIAL
                              AND IT.NRLANCTONF      = IP.NRLANCTONF
                              AND IT.NRSEQITPROD     = IP.NRSEQITPROD
                          )A
                     )B
                      GROUP BY 'R06B', TO_CHAR(P_DTINICIO,'MM'),'01'                       ) LOOP

      V_CONTREG06 := NVL(V_CONTREG06,0) + 1;

      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
         ('07'||LPAD(RTRIM(V_CONTREG06), 8, '0'), C_REG_R06B.REG_R06B, P_NRSEQARQMAG);

   END LOOP;*/


   RETURN(V_CONTREG06);

END;
/