BEGIN
   IF 1 = 2 THEN
      /*ENTRADAS*/
      --REGRA GERAL
      UPDATE ITEMPROIMPOS
         SET CDFISCALIT = '1',
             VRCONTITIMP = ROUND(VRCONTITIMP,2),
             VRBASECALCIT = ROUND(VRCONTITIMP,2),
             VRALIQITEM = DECODE(CDIMPOSTO,'02',1.65,7.60),
             VRIMPOSTOIT = ROUND(ROUND(VRCONTITIMP,2) * DECODE(CDIMPOSTO,'02',1.65,7.60) / 100,2),
             CDSITUTRIBITNFE = DECODE(CDFISCOPERIT,'1411','98','1202','98','2202','98','50')
       WHERE (CDFILIAL, NRLANCTONF, NRSEQITPROD, CDIMPOSTO, NRITPROIMPOS) IN (SELECT T.CDFILIAL, T.NRLANCTONF, T.NRSEQITPROD, T.CDIMPOSTO, T.NRITPROIMPOS
                                                                                FROM ITEMPROIMPOS T, CFOPPADRAO C
                                                                               WHERE (T.CDFILIAL, T.NRLANCTONF) IN (SELECT DISTINCT N.CDFILIAL, N.NRLANCTONF
                                                                                                                      FROM NOTAFISCAL N, IMPOSTONF I
                                                                                                                     WHERE N.CDFILIAL = I.CDFILIAL
                                                                                                                       AND N.NRLANCTONF = I.NRLANCTONF
                                                                                                                       AND DECODE(N.IDENTRSAIDOP,'E',N.DTENTRSAID,N.DTEMISSAO) BETWEEN '01/01/2020' AND '31/01/2021'
                                                                                                                       AND N.IDENTRSAIDOP = 'E'
                                                                                                                       AND N.IDSTATUS IN ('A','E')
                                                                                                                       AND I.CDIMPOSTO IN ('02','03')
                                                                                                                       AND N.CDEMPRESA = '20')
                                                                                                                       AND T.CDIMPOSTO IN ('02','03')
                                                                                                                       AND T.VRALIQITEM NOT IN (1.65,7.6)
                                                                                                                       AND T.CDFISCOPERIT IN ('1202','1253','1353','1411','1653','2102','2202','2353','2403')
                                                                                                                       AND T.CDFISCOPERIT = C.CDCFOPPADRAO);

      --EXCECOES DAS DEVOLUCOES--=
      UPDATE ITEMPROIMPOS
         SET CDFISCALIT = '3',
             VRCONTITIMP = ROUND(VRCONTITIMP,2),
             VRBASECALCIT = 0,
             VRALIQITEM = 0,
             VRIMPOSTOIT = 0,
             CDSITUTRIBITNFE = '08'
       WHERE (CDFILIAL, NRLANCTONF, NRSEQITPROD, CDIMPOSTO, NRITPROIMPOS) IN (SELECT T.CDFILIAL, T.NRLANCTONF, T.NRSEQITPROD, T.CDIMPOSTO, T.NRITPROIMPOS
                                                                                FROM ITEMPROIMPOS T, ITEMPRODUTO P
                                                                               WHERE (T.CDFILIAL, T.NRLANCTONF) IN (SELECT DISTINCT N.CDFILIAL, N.NRLANCTONF
                                                                                                                      FROM NOTAFISCAL N, IMPOSTONF I
                                                                                                                     WHERE N.CDFILIAL = I.CDFILIAL
                                                                                                                       AND N.NRLANCTONF = I.NRLANCTONF
                                                                                                                       AND DECODE(N.IDENTRSAIDOP,'E',N.DTENTRSAID,N.DTEMISSAO) BETWEEN '01/01/2020' AND '31/01/2021'
                                                                                                                       AND N.IDENTRSAIDOP = 'E'
                                                                                                                       AND N.IDSTATUS IN ('A','E')
                                                                                                                       AND I.CDIMPOSTO IN ('02','03')
                                                                                                                       AND N.CDEMPRESA = '20')
                                                                                                                       AND T.CDIMPOSTO IN ('02','03')
                                                                                                                       AND T.CDFISCOPERIT IN ('1202','1411','2202','2411')
                                                                                                                       AND T.CDFILIAL = P.CDFILIAL
                                                                                                                       AND T.NRLANCTONF = P.NRLANCTONF
                                                                                                                       AND T.NRSEQITPROD = P.NRSEQITPROD
                                                                                                                       AND P.CDPRODUTO IN (SELECT DISTINCT R.CDPRODUTO
                                                                                                                                             FROM DADOSFISCPROD D, PRODUTO P, PRODUTO R
                                                                                                                                            WHERE D.CDREGRAFISC = '0000000109'
                                                                                                                                              AND P.CDPRODUTO = D.CDPRODUTO
                                                                                                                                              AND R.CDARVPROD LIKE P.CDARVPROD||'%'
                                                                                                                                              AND D.CDIMPOSTO IN ('02','03')
                                                                                                                                              AND R.CDNVPRODUTO = 5));

      --CFOP'S 1.102 E 1.403--
      UPDATE ITEMPROIMPOS
         SET CDFISCALIT = '1',
             VRCONTITIMP = ROUND(VRCONTITIMP,2),
             VRBASECALCIT = ROUND(VRCONTITIMP,2),
             VRALIQITEM = DECODE(CDIMPOSTO,'02',1.65,7.60),
             VRIMPOSTOIT = ROUND(ROUND(VRCONTITIMP,2) * DECODE(CDIMPOSTO,'02',1.65,7.60) / 100,2),
             CDSITUTRIBITNFE = '50'
       WHERE (CDFILIAL, NRLANCTONF, NRSEQITPROD, CDIMPOSTO, NRITPROIMPOS) IN (SELECT T.CDFILIAL, T.NRLANCTONF, T.NRSEQITPROD, T.CDIMPOSTO, T.NRITPROIMPOS
                                                                                FROM ITEMPROIMPOS T, ITEMPRODUTO P
                                                                               WHERE (T.CDFILIAL, T.NRLANCTONF) IN (SELECT DISTINCT N.CDFILIAL, N.NRLANCTONF
                                                                                                                      FROM NOTAFISCAL N, IMPOSTONF I
                                                                                                                     WHERE N.CDFILIAL = I.CDFILIAL
                                                                                                                       AND N.NRLANCTONF = I.NRLANCTONF
                                                                                                                       AND DECODE(N.IDENTRSAIDOP,'E',N.DTENTRSAID,N.DTEMISSAO) BETWEEN '01/01/2020' AND '31/01/2021'
                                                                                                                       AND N.IDENTRSAIDOP = 'E'
                                                                                                                       AND N.IDSTATUS IN ('A','E')
                                                                                                                       AND I.CDIMPOSTO IN ('02','03')
                                                                                                                       AND N.CDEMPRESA = '20')
                                                                                                                       AND T.CDIMPOSTO IN ('02','03')
                                                                                                                       AND T.VRALIQITEM NOT IN (1.65,7.6)
                                                                                                                       AND T.CDFISCOPERIT IN ('1102','1403')
                                                                                                                       AND T.CDFILIAL = P.CDFILIAL
                                                                                                                       AND T.NRLANCTONF = P.NRLANCTONF
                                                                                                                       AND T.NRSEQITPROD = P.NRSEQITPROD
                                                                                                                       AND P.CDPRODUTO NOT IN (SELECT DISTINCT R.CDPRODUTO
                                                                                                                                                 FROM DADOSFISCPROD D, PRODUTO P, PRODUTO R
                                                                                                                                                WHERE D.CDREGRAFISC = '0000000106'
                                                                                                                                                  AND P.CDPRODUTO = D.CDPRODUTO
                                                                                                                                                  AND R.CDARVPROD LIKE P.CDARVPROD||'%'
                                                                                                                                                  AND D.CDIMPOSTO IN ('02','03')
                                                                                                                                                  AND R.CDNVPRODUTO = 5));

      --SERVICOS --
      UPDATE ITEMPROIMPOS
         SET CDFISCALIT = '1',
             VRCONTITIMP = ROUND(VRCONTITIMP,2),
             VRBASECALCIT = ROUND(VRCONTITIMP,2),
             VRALIQITEM = DECODE(CDIMPOSTO,'02',1.65,7.60),
             VRIMPOSTOIT = ROUND(ROUND(VRCONTITIMP,2) * DECODE(CDIMPOSTO,'02',1.65,7.60) / 100,2),
             CDSITUTRIBITNFE = '50'
       WHERE (CDFILIAL, NRLANCTONF, NRSEQITPROD, CDIMPOSTO, NRITPROIMPOS) IN (SELECT I.CDFILIAL, I.NRLANCTONF, I.NRSEQITPROD, I.CDIMPOSTO, I.NRITPROIMPOS
                                                                                FROM NOTAFISCAL N, ITEMPROIMPOS I, ITEMPRODUTO P
                                                                               WHERE IDENTRSAIDOP = 'E'
                                                                                 AND N.IDSTATUS IN ('A','E')
                                                                                 AND CDTIPOOPER = '18'
                                                                                 AND DECODE(N.IDENTRSAIDOP,'E',N.DTENTRSAID,N.DTEMISSAO) BETWEEN '01/01/2020' AND '31/01/2021'
                                                                                 AND CDEMPRESA = '20'
                                                                                 AND N.CDFILIAL = I.CDFILIAL
                                                                                 AND N.NRLANCTONF = I.NRLANCTONF
                                                                                 AND I.CDIMPOSTO IN ('02','03')
                                                                                 AND I.CDFILIAL = P.CDFILIAL
                                                                                 AND I.NRLANCTONF = P.NRLANCTONF
                                                                                 AND I.NRSEQITPROD = P.NRSEQITPROD
                                                                                 AND P.CDPRODUTO IN (SELECT DISTINCT R.CDPRODUTO
                                                                                                       FROM DADOSFISCPROD D, PRODUTO P, PRODUTO R
                                                                                                      WHERE D.CDREGRAFISC = '0000000058'
                                                                                                        AND P.CDPRODUTO = D.CDPRODUTO
                                                                                                        AND R.CDARVPROD LIKE P.CDARVPROD||'%'
                                                                                                        AND D.CDIMPOSTO IN ('02','03')
                                                                                                        AND R.CDNVPRODUTO = 5));

      --ZERAR 1303--
      UPDATE ITEMPROIMPOS
         SET CDFISCALIT = '3',
             VRCONTITIMP = ROUND(VRCONTITIMP,2),
             VRBASECALCIT = 0,
             VRALIQITEM = 0,
             VRIMPOSTOIT = 0,
             CDSITUTRIBITNFE = '70'
       WHERE (CDFILIAL, NRLANCTONF, NRSEQITPROD, CDIMPOSTO, NRITPROIMPOS) IN (SELECT T.CDFILIAL, T.NRLANCTONF, T.NRSEQITPROD, T.CDIMPOSTO, T.NRITPROIMPOS
                                                                                FROM ITEMPROIMPOS T, NOTAFISCAL N
                                                                               WHERE (T.CDFILIAL, T.NRLANCTONF) IN (SELECT DISTINCT N.CDFILIAL, N.NRLANCTONF
                                                                                                                      FROM NOTAFISCAL N, IMPOSTONF I
                                                                                                                     WHERE N.CDFILIAL = I.CDFILIAL
                                                                                                                       AND N.NRLANCTONF = I.NRLANCTONF
                                                                                                                       AND DECODE(N.IDENTRSAIDOP,'E',N.DTENTRSAID,N.DTEMISSAO) BETWEEN '01/01/2020' AND '31/01/2021'
                                                                                                                       AND N.IDSTATUS IN ('A','E')
                                                                                                                       AND I.CDIMPOSTO IN ('02','03')
                                                                                                                       AND N.CDEMPRESA = '20')
                                                                                                                       AND T.CDIMPOSTO IN ('02','03')
                                                                                                                       AND N.CDFILIAL = T.CDFILIAL
                                                                                                                       AND N.NRLANCTONF = T.NRLANCTONF
                                                                                                                       AND CDFISCOPERIT IN ('1303')
                                                                                                                       AND T.VRALIQITEM NOT IN (0, 1.65,7.6));

     /* FOR C_IMP IN (SELECT SUM(VRCONTITIMP) AS VRCONT, SUM(VRBASECALCIT)AS VRBASE, SUM(VRIMPOSTOIT) AS VRIMP,
                           T.CDFILIAL, T.NRLANCTONF, T.CDIMPOSTO
                      FROM ITEMPROIMPOS T, NOTAFISCAL N
                     WHERE (T.CDFILIAL, T.NRLANCTONF) IN (SELECT DISTINCT N.CDFILIAL, N.NRLANCTONF
                                                            FROM NOTAFISCAL N, IMPOSTONF I
                                                           WHERE N.CDFILIAL = I.CDFILIAL
                                                             AND N.NRLANCTONF = I.NRLANCTONF
                                                             AND DECODE(N.IDENTRSAIDOP,'E',N.DTENTRSAID,N.DTEMISSAO) BETWEEN '01/01/2020' AND '31/01/2021'
                                                             AND N.IDSTATUS IN ('A','E')
                                                             AND I.CDIMPOSTO IN ('02','03')
                                                             AND N.CDEMPRESA = '20')
                                                             AND T.CDIMPOSTO IN ('02','03')
                                                             AND N.CDFILIAL = T.CDFILIAL
                                                             AND N.NRLANCTONF = T.NRLANCTONF
                                                             AND N.IDSISTEMA <> 'FIS'
                                                             AND N.IDENTRSAIDOP = 'E'
                                                           GROUP BY  T.CDFILIAL, T.NRLANCTONF, T.CDIMPOSTO) LOOP
         RECRIA_IMPOSTONF_TESTE(C_IMP.CDFILIAL, C_IMP.NRLANCTONF, C_IMP.CDIMPOSTO);
      END LOOP;*/

      --SAIDAS--
      --VENDA NORMAL--
      UPDATE ITEMPROIMPOS
         SET CDFISCALIT = '1',
             VRCONTITIMP = ROUND(VRCONTITIMP,2),
             VRBASECALCIT = ROUND(VRCONTITIMP,2),
             VRALIQITEM = DECODE(CDIMPOSTO,'02',1.65,7.60),
             VRIMPOSTOIT = ROUND(ROUND(VRCONTITIMP,2) * DECODE(CDIMPOSTO,'02',1.65,7.60) / 100,2)
       WHERE (CDFILIAL, NRLANCTONF, NRSEQITPROD, CDIMPOSTO, NRITPROIMPOS) IN (SELECT T.CDFILIAL, T.NRLANCTONF, T.NRSEQITPROD, T.CDIMPOSTO, T.NRITPROIMPOS
                                                                                FROM ITEMPROIMPOS T, CFOPPADRAO C
                                                                               WHERE (T.CDFILIAL, T.NRLANCTONF) IN (SELECT DISTINCT N.CDFILIAL, N.NRLANCTONF
                                                                                                                      FROM NOTAFISCAL N, IMPOSTONF I
                                                                                                                     WHERE N.CDFILIAL = I.CDFILIAL
                                                                                                                       AND N.NRLANCTONF = I.NRLANCTONF
                                                                                                                       AND DECODE(N.IDENTRSAIDOP,'E',N.DTENTRSAID,N.DTEMISSAO) BETWEEN '01/01/2020' AND '31/01/2021'
                                                                                                                       AND N.IDENTRSAIDOP = 'S'
                                                                                                                       AND N.IDSTATUS IN ('A','E')
                                                                                                                       AND I.CDIMPOSTO IN ('02','03')
                                                                                                                       AND N.CDEMPRESA = '20')
                                                                                                                       AND T.CDIMPOSTO IN ('02','03')
                                                                                                                       AND T.CDFISCOPERIT IN ('5102','5202','5405','5411','5556','6102','6202','6403','6411')
                                                                                                                       AND T.CDFISCOPERIT = C.CDCFOPPADRAO
                                                                                                                       AND T.CDSITUTRIBITNFE IN ('01','49'));

      --VENDA SERVICO--
      UPDATE ITEMPROIMPOS
         SET CDFISCALIT = '1',
             VRCONTITIMP = ROUND(VRCONTITIMP,2),
             VRBASECALCIT = ROUND(VRCONTITIMP,2),
             VRALIQITEM = DECODE(CDIMPOSTO,'02',1.65,7.60),
             VRIMPOSTOIT = ROUND(ROUND(VRCONTITIMP,2) * DECODE(CDIMPOSTO,'02',1.65,7.60) / 100,2)
       WHERE (CDFILIAL, NRLANCTONF, NRSEQITPROD, CDIMPOSTO, NRITPROIMPOS) IN (SELECT T.CDFILIAL, T.NRLANCTONF, T.NRSEQITPROD, T.CDIMPOSTO, T.NRITPROIMPOS
                                                                                FROM ITEMPROIMPOS T
                                                                               WHERE (T.CDFILIAL, T.NRLANCTONF) IN (SELECT DISTINCT N.CDFILIAL, N.NRLANCTONF
                                                                                                                      FROM NOTAFISCAL N, IMPOSTONF I
                                                                                                                     WHERE N.CDFILIAL = I.CDFILIAL
                                                                                                                       AND N.NRLANCTONF = I.NRLANCTONF
                                                                                                                       AND DECODE(N.IDENTRSAIDOP,'E',N.DTENTRSAID,N.DTEMISSAO) BETWEEN '01/01/2020' AND '31/01/2021'
                                                                                                                       AND N.IDENTRSAIDOP = 'S'
                                                                                                                       AND N.IDSTATUS IN ('A','E')
                                                                                                                       AND I.CDIMPOSTO IN ('02','03')
                                                                                                                       AND N.CDEMPRESA = '20')
                                                                                                                       AND T.CDIMPOSTO IN ('02','03')
                                                                                                                       AND (T.CDFISCOPERIT IS NULL OR T.CDFISCOPERIT = '0000')
                                                                                                                       AND T.CDSITUTRIBITNFE = '01');

      /*FOR C_IMP IN (SELECT SUM(VRCONTITIMP) AS VRCONT, SUM(VRBASECALCIT)AS VRBASE, SUM(VRIMPOSTOIT) AS VRIMP,
                           T.CDFILIAL, T.NRLANCTONF,  T.CDIMPOSTO
                      FROM ITEMPROIMPOS T, NOTAFISCAL N
                     WHERE (T.CDFILIAL, T.NRLANCTONF) IN (SELECT DISTINCT N.CDFILIAL, N.NRLANCTONF
                                                            FROM NOTAFISCAL N, IMPOSTONF I
                                                           WHERE N.CDFILIAL = I.CDFILIAL
                                                             AND N.NRLANCTONF = I.NRLANCTONF
                                                             AND DECODE(N.IDENTRSAIDOP,'E',N.DTENTRSAID,N.DTEMISSAO) BETWEEN '01/01/2020' AND '31/01/2021'
                                                             AND N.IDSTATUS IN ('A','E')
                                                             AND I.CDIMPOSTO IN ('02','03')
                                                             AND N.CDEMPRESA = '20')
                                                             AND T.CDIMPOSTO IN ('02','03')
                                                             AND N.CDFILIAL = T.CDFILIAL
                                                             AND N.NRLANCTONF = T.NRLANCTONF
                                                             AND N.IDSISTEMA <> 'FIS'
                                                             AND N.IDENTRSAIDOP = 'S'
                                                           GROUP BY  T.CDFILIAL, T.NRLANCTONF, T.CDIMPOSTO) LOOP
         RECRIA_IMPOSTONF_TESTE(C_IMP.CDFILIAL, C_IMP.NRLANCTONF, C_IMP.CDIMPOSTO);
      END LOOP;*/

      UPDATE ITVENDAIMPOS
         SET VRIMPCOFINS = ROUND_ABNT(VRBCPISCOFINS*7.6/100,2),
             VRIMPPIS = ROUND_ABNT(VRBCPISCOFINS*1.65/100,2),
             VRPERPIS = 1.65,
             VRPERCOFINS = 7.6
       WHERE (CDFILIAL, CDCAIXA, NRSEQVENDA, NRSEQUITVEND, NRSEQITIMPOS) IN (SELECT I.CDFILIAL, I.CDCAIXA, I.NRSEQVENDA, I.NRSEQUITVEND, I.NRSEQITIMPOS
                                                                               FROM VENDA V, ITEMVENDA T, ITVENDAIMPOS I, FILIAL F
                                                                              WHERE V.CDFILIAL = T.CDFILIAL
                                                                                AND V.CDCAIXA = T.CDCAIXA
                                                                                AND V.NRSEQVENDA = T.NRSEQVENDA
                                                                                AND I.CDFILIAL = T.CDFILIAL
                                                                                AND I.CDCAIXA = T.CDCAIXA
                                                                                AND I.NRSEQVENDA = T.NRSEQVENDA
                                                                                AND I.NRSEQUITVEND = T.NRSEQUITVEND
                                                                                AND V.CDFILIAL = F.CDFILIAL
                                                                                AND F.CDEMPRESA = '20'
                                                                                AND (VRPERPIS > 0 OR VRPERCOFINS > 0)
                                                                                AND V.DTENTRVENDA BETWEEN '01/01/2020' AND '31/03/2020');

      --APAGA ITEMVENOUTIMPOS--
      DELETE FROM ITEMVENOUTIMPOS
       WHERE NRSEQITVENOUTIMP IN (SELECT NRSEQITVENOUTIMP
                                    FROM ITEMVENOUTIMPOS I, FILIAL F
                                   WHERE I.CDFILIAL = F.CDFILIAL
                                     AND F.CDEMPRESA = '20'
                                     AND I.DTREDUCAOZ BETWEEN '01/01/2020' AND '31/01/2021');

      FOR C_VENDA IN (SELECT DISTINCT V.CDFILIAL, M.CDCLIENTE, M.CDTIPOOPER, V.CDCAIXA ,
                             DECODE(IDTPEMISVEND,'S','SAT','N','NFCE','ECF') AS IDORIGEMVENDA,
                             V.DTENTRVENDA,  TRUNC(V.DTEMISSAONFCE) AS DTEMISSAO,
                             M.CDFILIALNFFIS, M.NRLANCTONFFIS
                        FROM VENDA V, ITEMVENDA T, ITVENDAIMPOS I, FILIAL F, (SELECT DISTINCT M.CDFILIAL, M.CDCAIXA, M.NRSEQVENDA,
                                                                                     M.CDFILIALNFFIS, M.NRLANCTONFFIS,
                                                                                     N.CDCLIENTE, N.CDTIPOOPER
                                                                                FROM MOVCAIXA M, NOTAFISCAL N
                                                                               WHERE N.CDEMPRESA = '20'
                                                                                 AND N.DTENTRSAID BETWEEN '01/01/2020' AND '31/01/2021'
                                                                                 AND M.CDFILIALNFFIS = N.CDFILIAL
                                                                                 AND N.NRLANCTONF = N.NRLANCTONF
                                                                                 AND N.IDSISTEMA = 'FIS') M
                       WHERE V.CDFILIAL = T.CDFILIAL
                         AND V.CDCAIXA = T.CDCAIXA
                         AND V.NRSEQVENDA = T.NRSEQVENDA
                         AND I.CDFILIAL = T.CDFILIAL
                         AND I.CDCAIXA = T.CDCAIXA
                         AND I.NRSEQVENDA = T.NRSEQVENDA
                         AND I.NRSEQUITVEND = T.NRSEQUITVEND
                         AND V.CDFILIAL = F.CDFILIAL
                         AND F.CDEMPRESA = '20'
                         AND V.IDSITUVENDA = 'O'
                         AND V.IDAPROVACAONFCE = 'A'
                         AND M.CDFILIAL = V.CDFILIAL
                         AND M.CDCAIXA = V.CDCAIXA
                         AND M.NRSEQVENDA = V.NRSEQVENDA
                         AND V.DTENTRVENDA BETWEEN '01/01/2020' AND '31/01/2021') LOOP

         GRAVA_IMP_FEDERAIS_VND/*_AUX*/(C_VENDA.CDFILIAL,
                                      C_VENDA.CDCLIENTE,
                                      C_VENDA.CDTIPOOPER,
                                      C_VENDA.CDCAIXA,
                                      '',
                                      C_VENDA.IDORIGEMVENDA,
                                      C_VENDA.DTENTRVENDA,
                                      C_VENDA.DTEMISSAO,
                                      C_VENDA.CDFILIALNFFIS,
                                      C_VENDA.NRLANCTONFFIS,
                                      'N',
                                      SYSDATE);
      END LOOP;
   END IF;
END;
/