CREATE OR REPLACE PROCEDURE GERA_INFADIC_GIA_RS(
   P_NRSEQUMAPAPU IN MAPAAPURAC.NRSEQUMAPAPU%TYPE,
   P_CDLIVROMAG   IN LIVROMAGNETICO.CDLIVROMAG%TYPE,
   P_NREMPREGADOS IN NUMBER,
   P_VRFOLHA      IN FLOAT,
   P_INSCRCENTRAL IN VARCHAR2,
   P_ATODECLARA   IN VARCHAR2)
AS
   V_NRSEQINFADIC  NUMBER;
   V_VRFATURAMENTO NUMBER;
   V_INFADICOBSERV VARCHAR2(01);
   V_VRENTRADAS    NUMBER;
   V_DTFIMPERIODO  DATE;
BEGIN

   /******** Deleta os registros na tabela, devido a regeração da guia ********/
   DELETE FROM INFADICMAPA
    WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU;

   BEGIN
      SELECT M.DTFIMPERIODO
        INTO V_DTFIMPERIODO
        FROM MAPAAPURAC M
       WHERE M.NRSEQUMAPAPU = P_NRSEQUMAPAPU;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_DTFIMPERIODO := NULL;
   END;

   V_NRSEQINFADIC := 0;

   IF V_DTFIMPERIODO <= '29/02/2020' THEN
      BEGIN
         SELECT NVL(VRFATU,0)-NVL(VRNFATU,0)
           INTO V_VRFATURAMENTO
          FROM (SELECT SUM(ROUND(CASE WHEN SUBSTR(I.CDFISCOPERNF,1,1) IN ('5','6','7') THEN I.VRCONTABILNF ELSE 0 END,2)) AS VRFATU,
                        SUM(ROUND(CASE WHEN SUBSTR(I.CDFISCOPERNF,1,1) IN ('1','2','3') THEN I.VRCONTABILNF ELSE 0 END,2)) AS VRNFATU
                   FROM IMPOSTONF I, CFOPLIVROMAG L, IMPOSTO P
                  WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                    AND I.CDIMPOSTO    = P.CDIMPOSTO
                    AND P.CDIMPOSTONFE = '00'
                    AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL))
                    AND L.CDLIVROMAG   = P_CDLIVROMAG
                    AND L.CDLAYOUTLMAG = '004'
                    AND I.CDFISCOPERNF = L.CDCFOPLMAG);
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_VRFATURAMENTO := 0;
      END;

      IF V_VRFATURAMENTO > 0 THEN
         V_NRSEQINFADIC := V_NRSEQINFADIC + 1;
         INSERT INTO INFADICMAPA
               (NRSEQUMAPAPU, NRSEQINFADIC, CDINFADICMAPA, DSINFADICMAPA, VRINFADICMAPA)
         VALUES
            (P_NRSEQUMAPAPU, LPAD(V_NRSEQINFADIC,5,'0'), 'RS000031', 'Faturamento no Mês', V_VRFATURAMENTO);
      END IF;
   END IF;

	IF P_NREMPREGADOS > 0 THEN
	   V_NRSEQINFADIC := V_NRSEQINFADIC + 1;
	   INSERT INTO INFADICMAPA
   	      (NRSEQUMAPAPU, NRSEQINFADIC, CDINFADICMAPA, DSINFADICMAPA, VRINFADICMAPA)
	   VALUES
	      (P_NRSEQUMAPAPU, LPAD(V_NRSEQINFADIC,5,'0'), 'RS000032', 'Número de Empregados no Último dia do Mês', P_NREMPREGADOS);
	END IF;
	
	IF P_VRFOLHA > 0 THEN
	   V_NRSEQINFADIC := V_NRSEQINFADIC + 1;
	   INSERT INTO INFADICMAPA
   	      (NRSEQUMAPAPU, NRSEQINFADIC, CDINFADICMAPA, DSINFADICMAPA, VRINFADICMAPA)
	   VALUES
	      (P_NRSEQUMAPAPU, LPAD(V_NRSEQINFADIC,5,'0'), 'RS000033', 'Valor da Folha de Pagamentos do Mês', P_VRFOLHA);
	END IF;
	
    V_INFADICOBSERV	:= 'N';
	
	BEGIN
	   SELECT MAX('S')
	     INTO V_INFADICOBSERV
		 FROM IMPOSTONF I
		WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
		  AND I.CDFISCOPERNF IN ('1949','2949','3949','5949','6949','7949');		 
	EXCEPTION
	   WHEN NO_DATA_FOUND THEN
	      V_INFADICOBSERV := 'N';
	END;
	
	IF V_INFADICOBSERV = 'S' THEN
	   V_NRSEQINFADIC := V_NRSEQINFADIC + 1;
	   INSERT INTO INFADICMAPA
   	      (NRSEQUMAPAPU, NRSEQINFADIC, CDINFADICMAPA, DSINFADICMAPA, VRINFADICMAPA)
	   VALUES
	      (P_NRSEQUMAPAPU, LPAD(V_NRSEQINFADIC,5,'0'), 'RS000090', ' ', 0);
	END IF;
	
	FOR C_ANEXOVA IN (SELECT T.CDFISCOPERIT, LPAD(NVL(T.CDDETALHEPROD,'0'),3,'0') AS CDDETALHEPROD, SUM(T.VRCONTSAI) AS VRCONTABIL
                       FROM (SELECT IT.CDFISCOPERIT, IT.CDDETALHEPROD, SUM(ROUND(IT.VRCONTITIMP,2)) AS VRCONTSAI
                               FROM ITEMPROIMPOS IT, ITEMPRODUTO IP, IMPOSTO IM,
                                    (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF
                                       FROM IMPOSTONF I, NOTAFISCAL N, IMPOSTO P
                                      WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                                AND I.NRLANCTONF   = N.NRLANCTONF
                                AND I.CDFILIAL     = N.CDFILIAL
                                AND I.CDIMPOSTO    = P.CDIMPOSTO
                                AND P.CDIMPOSTONFE = '00'
                                AND N.IDENTRSAIDOP = 'S') NF
                     WHERE NF.CDFILIAL     = IT.CDFILIAL
                       AND NF.NRLANCTONF   = IT.NRLANCTONF
                       AND IT.CDFILIAL     = IP.CDFILIAL
                       AND IT.NRLANCTONF   = IP.NRLANCTONF
                       AND IT.NRSEQITPROD  = IP.NRSEQITPROD
                       AND IT.CDIMPOSTO    = IM.CDIMPOSTO
                       AND IM.CDIMPOSTONFE = '00'
                       AND IT.CDFISCALIT   = '2'
                       AND IT.CDFISCOPERIT NOT IN ('5601','5602','5605')
                       AND ((IT.NRITPROIMPOS <> '3') OR (IT.NRITPROIMPOS IS NULL))
                     GROUP BY IT.CDFISCOPERIT, IT.CDDETALHEPROD
                     ORDER BY IT.CDFISCOPERIT, IT.CDDETALHEPROD)T
               GROUP BY T.CDFISCOPERIT, T.CDDETALHEPROD
               ORDER BY T.CDFISCOPERIT, T.CDDETALHEPROD)LOOP
         
      V_NRSEQINFADIC := V_NRSEQINFADIC + 1;
	  INSERT INTO INFADICMAPA
   	     (NRSEQUMAPAPU, NRSEQINFADIC, CDINFADICMAPA, DSINFADICMAPA, VRINFADICMAPA)
	  VALUES
	     (P_NRSEQUMAPAPU, LPAD(V_NRSEQINFADIC,5,'0'), 'RS051'||C_ANEXOVA.CDDETALHEPROD, C_ANEXOVA.CDFISCOPERIT||' - Valor Isentas', C_ANEXOVA.VRCONTABIL);
   END LOOP;

   FOR C_ANEXOVB IN (SELECT T.CDFISCOPERIT, LPAD(NVL(T.CDDETALHEPROD,'0'),3,'0') AS CDDETALHEPROD, SUM(T.VRCONTSAI) AS VRCONTABIL
                       FROM (SELECT IT.CDFISCOPERIT, IT.CDDETALHEPROD,
                                    SUM((CASE WHEN C.CDLAYOUTLMAG = '003' THEN ROUND(IT.VRCONTITIMP-DECODE(IT.CDFISCALIT,'1',IT.VRBASECALCIT,0)-DECODE(IT.CDFISCALIT,'2',IT.VRCONTITIMP,0),2) ELSE
                                        (CASE WHEN C.CDLAYOUTLMAG IN ('002','005') THEN ROUND(DECODE(IT.CDFISCALIT,'3',IT.VRCONTITIMP,0),2) ELSE 0
                                        END)END)) AS VRCONTSAI
                               FROM ITEMPROIMPOS IT, ITEMPRODUTO IP, IMPOSTO IM,  CFOPLIVROMAG C,
                                    (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF
                                       FROM IMPOSTONF I, NOTAFISCAL N, IMPOSTO P
                                      WHERE I.NRSEQUMAPAPU  = P_NRSEQUMAPAPU   AND
                                            I.NRLANCTONF    = N.NRLANCTONF     AND
                                            I.CDFILIAL      = N.CDFILIAL       AND
                                            I.CDIMPOSTO     = P.CDIMPOSTO      AND
                                            P.CDIMPOSTONFE  = '00'      AND
                                            N.IDENTRSAIDOP  = 'S') NF
                              WHERE NF.CDFILIAL     = IT.CDFILIAL
                                AND NF.NRLANCTONF   = IT.NRLANCTONF
                                AND IT.CDFILIAL     = IP.CDFILIAL
                                AND IT.NRLANCTONF   = IP.NRLANCTONF
                                AND IT.NRSEQITPROD  = IP.NRSEQITPROD
                                AND IT.CDIMPOSTO    = IM.CDIMPOSTO
                                AND IM.CDIMPOSTONFE = '00'
                                AND IT.CDFISCOPERIT  = C.CDCFOPLMAG
                                AND C.CDLIVROMAG    = P_CDLIVROMAG
                                AND IT.CDFISCOPERIT NOT IN ('5601','5602','5605')
                                AND ((IT.NRITPROIMPOS <> '3') OR (IT.NRITPROIMPOS IS NULL))
                              GROUP BY IT.CDFISCOPERIT, IT.CDDETALHEPROD
                              ORDER BY IT.CDFISCOPERIT, IT.CDDETALHEPROD)T
                      WHERE T.VRCONTSAI > 0
                      GROUP BY T.CDFISCOPERIT, T.CDDETALHEPROD
                      ORDER BY T.CDFISCOPERIT, T.CDDETALHEPROD) LOOP

	  V_NRSEQINFADIC := V_NRSEQINFADIC + 1;
	  INSERT INTO INFADICMAPA
   	     (NRSEQUMAPAPU, NRSEQINFADIC, CDINFADICMAPA, DSINFADICMAPA, VRINFADICMAPA)
	  VALUES
	     (P_NRSEQUMAPAPU, LPAD(V_NRSEQINFADIC,5,'0'), 'RS051'||C_ANEXOVB.CDDETALHEPROD, C_ANEXOVB.CDFISCOPERIT||' - Valor Outras', C_ANEXOVB.VRCONTABIL);

	END LOOP;

   IF P_INSCRCENTRAL = 'S' THEN
	   V_VRENTRADAS := 0;
	   BEGIN
	      SELECT SUM(T.VRIMPORTEXCL)
            INTO V_VRENTRADAS
            FROM (/*Preencher Excluídas com diferença Valor Contábil - Valores Fiscais PARCIAL*/
                  /*Requer cadastro de cfops, tela FIS07300, em layout código 005 - CFOP - EXCLUÍDAS VALOR PARCIAL*/
                  SELECT SUM(VRIMPORTEXCL) AS VRIMPORTEXCL
				        FROM (SELECT SUM(NVL(VALOR_ENCARGO(N.CDFILIAL, N.NRLANCTONF,'A','10'),0)) AS VRIMPORTEXCL
                            FROM IMPOSTONF I, NOTAFISCAL N, IMPOSTO P, CFOPLIVROMAG C
                           WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                             AND I.NRLANCTONF   = N.NRLANCTONF
                             AND I.CDFILIAL     = N.CDFILIAL
                             AND I.CDIMPOSTO    = P.CDIMPOSTO
                             AND P.CDIMPOSTONFE = '00'
                             AND N.IDENTRSAIDOP = 'E'
                             AND I.CDFISCOPERNF = C.CDCFOPLMAG
                             AND C.CDLIVROMAG   = P_CDLIVROMAG
                             AND C.CDLAYOUTLMAG = '005'
                             AND I.CDFISCOPERNF NOT IN ('1601','1602','1605')
                             AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL))
                           UNION ALL
                          SELECT SUM(CASE WHEN CDFISCOPERNF IN ('1403','2403') THEN NVL(IT.VRITEMENCA,0) ELSE 0 END) AS VRIMPORTEXCL
                            FROM ITEMENCARGO IT, (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF, I.CDFISCOPERNF
                                                  FROM IMPOSTONF I, NOTAFISCAL N, IMPOSTO P, CFOPLIVROMAG C
                                                 WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU           AND
                                                       I.NRLANCTONF   = N.NRLANCTONF                  AND
                                                       I.CDFILIAL     = N.CDFILIAL                    AND
                                                       I.CDIMPOSTO    = P.CDIMPOSTO                   AND
                                                       P.CDIMPOSTONFE = '00'                   AND
                                                       N.IDENTRSAIDOP = 'E'                           AND
                                                       I.CDFISCOPERNF = C.CDCFOPLMAG                  AND
                                                       C.CDLIVROMAG   = P_CDLIVROMAG                  AND
                                                       C.CDLAYOUTLMAG IN ('005')                      AND
                                                       I.CDFISCOPERNF NOT IN ('1601','1602','1605')   AND
                                                      ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL))) A
                         WHERE IT.CDFILIAL   = A.CDFILIAL     AND
                               IT.NRLANCTONF = A.NRLANCTONF   AND
                               IT.CDENCARGO = '05')
                   UNION ALL
                  /*Preencher apenas Valor Excluídas com a soma da Base de Cálculo+Isentas+Outras*/
                  /*Requer cadastro de cfops, tela FIS07300, em layout código 002 - CFOP - EXCLUÍDAS VALOR INTEGRAL*/
                  SELECT SUM(VRIMPORTEXCL) AS VRIMPORTEXCL
                    FROM (SELECT SUM(ROUND(I.VRBASECALCNF,2)+ROUND(DECODE(I.CDFISCALNF,'2',I.VRCONTABILNF,0),2)+ROUND(DECODE(I.CDFISCALNF,'3',I.VRCONTABILNF,0),2)) AS VRIMPORTEXCL
                            FROM IMPOSTONF I, NOTAFISCAL N, IMPOSTO P, CFOPLIVROMAG C
                           WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU                AND
                                 I.NRLANCTONF   = N.NRLANCTONF                  AND
                                 I.CDFILIAL     = N.CDFILIAL                    AND
                                 I.CDIMPOSTO    = P.CDIMPOSTO                   AND
                                 P.CDIMPOSTONFE = '00'                   AND
                                 N.IDENTRSAIDOP = 'E'                           AND
                                 I.CDFISCOPERNF = C.CDCFOPLMAG                  AND
                                 C.CDLIVROMAG   = P_CDLIVROMAG                  AND
                                 C.CDLAYOUTLMAG IN ('002')          AND
                                 I.CDFISCOPERNF NOT IN ('1601','1602','1605')   AND
                                 ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL))
                           UNION ALL
                          SELECT SUM(CASE WHEN A.CDFISCOPERNF IN('1403','2403') THEN NVL(IT.VRITEMENCA,0) ELSE 0 END) AS VRIMPORTEXCL
                            FROM ITEMENCARGO IT, (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF, I.CDFISCOPERNF
                                                    FROM IMPOSTONF I, NOTAFISCAL N, IMPOSTO P, CFOPLIVROMAG C
                                                   WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU                AND
                                                         I.NRLANCTONF   = N.NRLANCTONF                  AND
                                                         I.CDFILIAL     = N.CDFILIAL                    AND
                                                         I.CDIMPOSTO    = P.CDIMPOSTO                   AND
                                                         P.CDIMPOSTONFE = '00'                   AND
                                                         N.IDENTRSAIDOP = 'E'                           AND
                                                         I.CDFISCOPERNF = C.CDCFOPLMAG                  AND
                                                         C.CDLIVROMAG   = P_CDLIVROMAG                  AND
                                                         C.CDLAYOUTLMAG IN ('002')                      AND
                                                         I.CDFISCOPERNF NOT IN ('1601','1602','1605')   AND
                                                        ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL))) A
                           WHERE IT.CDFILIAL   = A.CDFILIAL     AND
                                 IT.NRLANCTONF = A.NRLANCTONF   AND
                                 IT.CDENCARGO = '05')
                   UNION ALL
                  /*SELECT PARA SOMAR OS VALORES DOS COD. AJUSTE 1,2,3,4 DO ANEXO V.C*/
                  SELECT SUM(ROUND((CASE WHEN CA.CDAJUSTE = '001' THEN (NVL(VALOR_IMPOSTO_NFE(N.CDFILIAL, N.NRLANCTONF,'80','I','1'),0)*0.699) ELSE
                                   (CASE WHEN CA.CDAJUSTE = '002' THEN (NVL(VALOR_IMPOSTO_NFE(N.CDFILIAL, N.NRLANCTONF,'10','I','1'),0)*0.699) ELSE
                                   (CASE WHEN CA.CDAJUSTE = '003' THEN (NVL(VALOR_ENCARGO_NFE(N.CDFILIAL, N.NRLANCTONF, 'A','01'),0)*0.699)    ELSE
                                   (CASE WHEN CA.CDAJUSTE = '004' THEN (CASE WHEN (ROUND(I.VRBASECALCNF,2) - ROUND(I.VRCONTABILNF,2)) > 0
                                                                             THEN NVL(ROUND(I.VRBASECALCNF,2)-ROUND(I.VRCONTABILNF,2),0) ELSE 0 END) ELSE 0
                                     END)END)END)END),2)) AS VRIMPORTEXCL
                    FROM IMPOSTONF I, NOTAFISCAL N, IMPOSTO P, CFOPLIVROMAG C, CODAJUSTECFOP CA
                   WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU                AND
                         I.NRLANCTONF   = N.NRLANCTONF                  AND
                         I.CDFILIAL     = N.CDFILIAL                    AND
                         I.CDIMPOSTO    = P.CDIMPOSTO                   AND
                         P.CDIMPOSTONFE = '00'                   AND
                         N.IDENTRSAIDOP = 'E'                           AND
                         I.CDFISCOPERNF = C.CDCFOPLMAG                  AND
                         C.CDLIVROMAG   = P_CDLIVROMAG                  AND
                         C.CDLAYOUTLMAG IN ('002','005')                AND
                         CA.CDAJUSTE    IN ('001','002','003','004')    AND
                         I.CDFISCOPERNF NOT IN ('1601','1602','1605')   AND
                         I.CDFISCOPERNF = CA.CDFISCOPERNF(+)            AND
                         ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL)))T;
	   EXCEPTION
	      WHEN NO_DATA_FOUND THEN
		     V_VRENTRADAS := 0;
	   END;

	   IF V_VRENTRADAS > 0 THEN
		  V_NRSEQINFADIC := V_NRSEQINFADIC + 1;
		  INSERT INTO INFADICMAPA
			 (NRSEQUMAPAPU, NRSEQINFADIC, CDINFADICMAPA, DSINFADICMAPA, VRINFADICMAPA)
		  VALUES
			 (P_NRSEQUMAPAPU, LPAD(V_NRSEQINFADIC,5,'0'), 'RS160087', 'Coluna Entrada - Custos', V_VRENTRADAS);
	   END IF;

	   V_NRSEQINFADIC := V_NRSEQINFADIC + 1;
		INSERT INTO INFADICMAPA
		   (NRSEQUMAPAPU, NRSEQINFADIC, CDINFADICMAPA, DSINFADICMAPA, VRINFADICMAPA)
		VALUES
		   (P_NRSEQUMAPAPU, LPAD(V_NRSEQINFADIC,5,'0'), 'RS160001', 'Ato Declaratório', P_ATODECLARA);
   END IF;

   /*ANEXO I.C - IMPORTÂNCIAS EXCLUÍDAS / AJUSTES VA (ENTRADAS)*/
   FOR C_ANEXOIC IN (SELECT CDAJUSTE, VRIMPORTEXCL, CDFISCOPERNF
                       FROM (/*Cód. 1 -> ST - Valor da Substituição Tributária Destacada na NF*/
                            SELECT DECODE(A.CDFISCALNF,'2','001','091') AS CDAJUSTE,
                                   SUM(IT.VRITEMENCA) AS VRIMPORTEXCL ,
                                   A.CDFISCOPERNF
                              FROM ITEMENCARGO IT, (SELECT I.CDFILIAL, I.NRLANCTONF, MAX(I.CDFISCOPERNF) AS CDFISCOPERNF, MAX(I.CDFISCALNF) AS CDFISCALNF
                                                      FROM IMPOSTONF I, NOTAFISCAL N, IMPOSTO P
                                                     WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                                                       AND I.NRLANCTONF   = N.NRLANCTONF
                                                       AND I.CDFILIAL     = N.CDFILIAL
                                                       AND I.CDIMPOSTO    = P.CDIMPOSTO
                                                       AND P.CDIMPOSTONFE = '00'
                                                       AND N.IDENTRSAIDOP = 'E'
                                                       AND I.CDFISCOPERNF IN ('1403','2403')
                                                       AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL))
                                                     GROUP BY I.CDFILIAL, I.NRLANCTONF) A
                             WHERE IT.CDFILIAL   = A.CDFILIAL
                               AND IT.NRLANCTONF = A.NRLANCTONF
                               AND IT.CDENCARGO  IN (SELECT I.CDENCARGOIMPXML FROM IMPOSTO I WHERE I.CDIMPOSTONFE = '80')
                             GROUP BY A.CDFISCOPERNF, A.CDFISCALNF
                             UNION ALL
                            /*Cód. 2 -> ST - Valor do IPI Destacado na NF*/
                            SELECT DECODE(A.CDFISCALNF,'2','002','092') AS CDAJUSTE,
                                   SUM(IT.VRITEMENCA) AS VRIMPORTEXCL ,
                                   A.CDFISCOPERNF
                              FROM ITEMENCARGO IT, (SELECT I.CDFILIAL, I.NRLANCTONF, MAX(I.CDFISCOPERNF) AS CDFISCOPERNF, MAX(I.CDFISCALNF) AS CDFISCALNF
                                                      FROM IMPOSTONF I, NOTAFISCAL N, IMPOSTO P, CFOPLIVROMAG C, CODAJUSTECFOP CA
                                                     WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                                                       AND I.NRLANCTONF   = N.NRLANCTONF
                                                       AND I.CDFILIAL     = N.CDFILIAL
                                                       AND I.CDIMPOSTO    = P.CDIMPOSTO
                                                       AND P.CDIMPOSTONFE = '00'
                                                       AND N.IDENTRSAIDOP = 'E'
                                                       AND I.CDFISCOPERNF = C.CDCFOPLMAG
                                                       AND C.CDLIVROMAG   = P_CDLIVROMAG
                                                       AND C.CDLAYOUTLMAG IN ('002','005')
                                                       AND I.CDFISCOPERNF NOT IN ('1601','1602','1605')
                                                       AND CA.CDFISCOPERNF = I.CDFISCOPERNF
                                                       AND CA.CDAJUSTE    = '002'
                                                       AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL))
                                                     GROUP BY I.CDFILIAL, I.NRLANCTONF) A
                             WHERE IT.CDFILIAL   = A.CDFILIAL
                               AND IT.NRLANCTONF = A.NRLANCTONF
                               AND IT.CDENCARGO  IN (SELECT I.CDENCARGOIMPXML FROM IMPOSTO I WHERE I.CDIMPOSTONFE = '10')
                             GROUP BY A.CDFISCOPERNF, A.CDFISCALNF
                             UNION ALL
                            /*Cód. 3 -> Frete sobre ativo imobilizado ou material de uso e consumo*/
                            SELECT '003' AS CDAJUSTE,
                                   SUM(NVL(VALOR_ENCARGO_NFE(N.CDFILIAL, N.NRLANCTONF, 'A','01'),0)) AS VRIMPORTEXCL,
                                   I.CDFISCOPERNF
                              FROM IMPOSTONF I, NOTAFISCAL N, IMPOSTO P, CFOPLIVROMAG C, CODAJUSTECFOP CA
                             WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                               AND I.NRLANCTONF   = N.NRLANCTONF
                               AND I.CDFILIAL     = N.CDFILIAL
                               AND I.CDIMPOSTO    = P.CDIMPOSTO
                               AND P.CDIMPOSTONFE = '00'
                               AND N.IDENTRSAIDOP = 'E'
                               AND I.CDFISCOPERNF = C.CDCFOPLMAG
                               AND C.CDLIVROMAG   = P_CDLIVROMAG
                               AND C.CDLAYOUTLMAG IN ('002','005')
                               AND I.CDFISCOPERNF NOT IN ('1601','1602','1605')
                               AND CA.CDFISCOPERNF = I.CDFISCOPERNF
                               AND CA.CDAJUSTE    = '003'
                               AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL))
                             GROUP BY I.CDFISCOPERNF
                             UNION ALL
                            /*Cód. 6 -> Frete sobre ativo imobilizado ou material de uso e consumo*/
                            SELECT '006' AS CDAJUSTE,
                                   SUM(CASE WHEN NVL(ROUND(I.VRCONTABILNF-I.VRBASECALCNF-DECODE(I.CDFISCALNF,'2',I.VRCONTABILNF,0)-DECODE(I.CDFISCALNF,'3',I.VRCONTABILNF,0),2),0) > 0
                                            THEN NVL(ROUND(I.VRCONTABILNF-I.VRBASECALCNF-DECODE(I.CDFISCALNF,'2',I.VRCONTABILNF,0)-DECODE(I.CDFISCALNF,'3',I.VRCONTABILNF,0),2),0)
                                            ELSE 0 END) AS VRIMPORTEXCL,
                                   I.CDFISCOPERNF
                              FROM IMPOSTONF I, NOTAFISCAL N, IMPOSTO P, CFOPLIVROMAG C, CODAJUSTECFOP CA
                             WHERE I.NRSEQUMAPAPU  = P_NRSEQUMAPAPU
                               AND I.NRLANCTONF    = N.NRLANCTONF
                               AND I.CDFILIAL      = N.CDFILIAL
                               AND I.CDIMPOSTO     = P.CDIMPOSTO
                               AND P.CDIMPOSTONFE  = '00'
                               AND N.IDENTRSAIDOP  = 'E'
                               AND I.CDFISCOPERNF  = C.CDCFOPLMAG
                               AND C.CDLIVROMAG    = P_CDLIVROMAG
                               AND C.CDLAYOUTLMAG  IN ('002','005')
                               AND I.CDFISCOPERNF  NOT IN ('1601','1602','1605')
                               AND CA.CDFISCOPERNF = I.CDFISCOPERNF
                               AND CA.CDAJUSTE     = '006'
                               AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL))
                             GROUP BY I.CDFISCOPERNF) A
                      WHERE A.VRIMPORTEXCL > 0) LOOP

      V_NRSEQINFADIC := V_NRSEQINFADIC + 1;
      INSERT INTO INFADICMAPA
         (NRSEQUMAPAPU, NRSEQINFADIC, CDINFADICMAPA, DSINFADICMAPA, VRINFADICMAPA)
      VALUES
         (P_NRSEQUMAPAPU, LPAD(V_NRSEQINFADIC,5,'0'), 'RS013'||C_ANEXOIC.CDAJUSTE, C_ANEXOIC.CDFISCOPERNF||' - Importâncias Excluídas VA (Entradas)', C_ANEXOIC.VRIMPORTEXCL);
   END LOOP;

   /*ANEXO V.C - IMPORTÂNCIAS EXCLUÍDAS / AJUSTES VA (SAÍDAS)*/
   FOR C_ANEXOVC IN (SELECT CDAJUSTE, VRIMPORTEXCL, CDFISCOPERNF
                       FROM (/*Cód. 1 -> ST - Valor da Substituição Tributária Destacada na NF*/
                            SELECT DECODE(A.CDFISCALNF,'2','001','091') AS CDAJUSTE,
                                   SUM(IT.VRITEMENCA) AS VRIMPORTEXCL ,
                                   A.CDFISCOPERNF
                              FROM ITEMENCARGO IT, (SELECT I.CDFILIAL, I.NRLANCTONF, MAX(I.CDFISCOPERNF) AS CDFISCOPERNF, MAX(I.CDFISCALNF) AS CDFISCALNF
                                                      FROM IMPOSTONF I, NOTAFISCAL N, IMPOSTO P, CFOPLIVROMAG C, CODAJUSTECFOP CA
                                                     WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                                                       AND I.NRLANCTONF   = N.NRLANCTONF
                                                       AND I.CDFILIAL     = N.CDFILIAL
                                                       AND I.CDIMPOSTO    = P.CDIMPOSTO
                                                       AND P.CDIMPOSTONFE = '00'
                                                       AND N.IDENTRSAIDOP = 'S'
                                                       AND I.CDFISCOPERNF = C.CDCFOPLMAG
                                                       AND C.CDLIVROMAG   = P_CDLIVROMAG
                                                       AND C.CDLAYOUTLMAG IN ('002','005')
                                                       AND I.CDFISCOPERNF NOT IN ('5601','5602','5605')
                                                       AND CA.CDFISCOPERNF = I.CDFISCOPERNF
                                                       AND CA.CDAJUSTE    = '001'
                                                       AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL))
                                                     GROUP BY I.CDFILIAL, I.NRLANCTONF) A
                             WHERE IT.CDFILIAL   = A.CDFILIAL
                               AND IT.NRLANCTONF = A.NRLANCTONF
                               AND IT.CDENCARGO  IN (SELECT I.CDENCARGOIMPXML FROM IMPOSTO I WHERE I.CDIMPOSTONFE = '80')
                             GROUP BY A.CDFISCOPERNF, A.CDFISCALNF
                             UNION ALL
                            /*Cód. 2 -> ST - Valor do IPI Destacado na NF*/
                            SELECT DECODE(A.CDFISCALNF,'2','002','092') AS CDAJUSTE,
                                   SUM(IT.VRITEMENCA) AS VRIMPORTEXCL ,
                                   A.CDFISCOPERNF
                              FROM ITEMENCARGO IT, (SELECT I.CDFILIAL, I.NRLANCTONF, MAX(I.CDFISCOPERNF) AS CDFISCOPERNF, MAX(I.CDFISCALNF) AS CDFISCALNF
                                                      FROM IMPOSTONF I, NOTAFISCAL N, IMPOSTO P, CFOPLIVROMAG C, CODAJUSTECFOP CA
                                                     WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                                                       AND I.NRLANCTONF   = N.NRLANCTONF
                                                       AND I.CDFILIAL     = N.CDFILIAL
                                                       AND I.CDIMPOSTO    = P.CDIMPOSTO
                                                       AND P.CDIMPOSTONFE = '00'
                                                       AND N.IDENTRSAIDOP = 'S'
                                                       AND I.CDFISCOPERNF = C.CDCFOPLMAG
                                                       AND C.CDLIVROMAG   = P_CDLIVROMAG
                                                       AND C.CDLAYOUTLMAG IN ('002','005')
                                                       AND I.CDFISCOPERNF NOT IN ('5601','5602','5605')
                                                       AND CA.CDFISCOPERNF = I.CDFISCOPERNF
                                                       AND CA.CDAJUSTE    = '002'
                                                       AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL))
                                                     GROUP BY I.CDFILIAL, I.NRLANCTONF) A
                             WHERE IT.CDFILIAL   = A.CDFILIAL
                               AND IT.NRLANCTONF = A.NRLANCTONF
                               AND IT.CDENCARGO  IN (SELECT I.CDENCARGOIMPXML FROM IMPOSTO I WHERE I.CDIMPOSTONFE = '10')
                             GROUP BY A.CDFISCOPERNF, A.CDFISCALNF
                             UNION ALL
                            /*Cód. 6 -> Frete sobre ativo imobilizado ou material de uso e consumo*/
                            SELECT '006' AS CDAJUSTE,
                                   SUM(CASE WHEN NVL(ROUND(I.VRCONTABILNF-I.VRBASECALCNF-DECODE(I.CDFISCALNF,'2',I.VRCONTABILNF,0)-DECODE(I.CDFISCALNF,'3',I.VRCONTABILNF,0),2),0) > 0
                                            THEN NVL(ROUND(I.VRCONTABILNF-I.VRBASECALCNF-DECODE(I.CDFISCALNF,'2',I.VRCONTABILNF,0)-DECODE(I.CDFISCALNF,'3',I.VRCONTABILNF,0),2),0)
                                            ELSE 0 END) AS VRIMPORTEXCL,
                                   I.CDFISCOPERNF
                              FROM IMPOSTONF I, NOTAFISCAL N, IMPOSTO P, CFOPLIVROMAG C, CODAJUSTECFOP CA
                             WHERE I.NRSEQUMAPAPU  = P_NRSEQUMAPAPU
                               AND I.NRLANCTONF    = N.NRLANCTONF
                               AND I.CDFILIAL      = N.CDFILIAL
                               AND I.CDIMPOSTO     = P.CDIMPOSTO
                               AND P.CDIMPOSTONFE  = '00'
                               AND N.IDENTRSAIDOP  = 'S'
                               AND I.CDFISCOPERNF  = C.CDCFOPLMAG
                               AND C.CDLIVROMAG    = P_CDLIVROMAG
                               AND C.CDLAYOUTLMAG  IN ('002','005')
                               AND I.CDFISCOPERNF  NOT IN ('5601','5602','5605')
                               AND CA.CDFISCOPERNF = I.CDFISCOPERNF
                               AND CA.CDAJUSTE     = '006'
                               AND ((I.IDIMPOSTONF <> '3') OR (I.IDIMPOSTONF IS NULL))
                             GROUP BY I.CDFISCOPERNF) A
                      WHERE A.VRIMPORTEXCL > 0) LOOP

      V_NRSEQINFADIC := V_NRSEQINFADIC + 1;
      INSERT INTO INFADICMAPA
         (NRSEQUMAPAPU, NRSEQINFADIC, CDINFADICMAPA, DSINFADICMAPA, VRINFADICMAPA)
      VALUES
         (P_NRSEQUMAPAPU, LPAD(V_NRSEQINFADIC,5,'0'), 'RS053'||C_ANEXOVC.CDAJUSTE, C_ANEXOVC.CDFISCOPERNF||' - Importâncias Excluídas VA (Saídas)', C_ANEXOVC.VRIMPORTEXCL);
   END LOOP;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('GERA_INFADIC_GIA_RS','0.00.000'); END;
/
