CREATE OR REPLACE PROCEDURE GRAVA_IMPOSTO(
   P_FILIAL IN NOTAFISCAL.CDFILIAL%TYPE,
   P_LANCTONF IN NOTAFISCAL.NRLANCTONF%TYPE)

AS

BEGIN

   DELETE ITEMPROIMPOS
   WHERE CDFILIAL   = P_FILIAL   AND
         NRLANCTONF = P_LANCTONF;

   DELETE IMPOSNFTOT
   WHERE CDFILIAL   = P_FILIAL   AND
         NRLANCTONF = P_LANCTONF;

   INSERT INTO ITEMPROIMPOS(
      CDFILIAL, NRLANCTONF, NRSEQITPROD, CDIMPOSTO, NRITPROIMPOS, CDFISCALIT, CDSITUTRIBIT,
      CDFISCOPERIT, CDSUFXCFOPIT, VRALIQITEM, VRPERREDBCIT, VRPARDEDIT, VRCONTITIMP,
      VRBASECALCIT, VRIMPOSTOIT, IDERROSIMPIT, IDAVISIMPIT, IDVALORINFIT,
      CDSITUTRIBITNFE, IDMODBCITNFE, IDMODBCSTITNFE, IDDIFALIQCUSPRI, VR1PERREDBCIT, VR1IMPREDBCIT,
      CDTPCREDPICOITE, CDNATUBCALPCIT, IDIMPDESONERAIT, IDMOTDESONERAIT, IDTRIMPSTII, CDREGRAIT, VRBCALCIRRF,
      CDDETALHEPROD,   CDENQLEGALIPI,
      VRPERDIFEREIT, VRIMPOSTOOPERAIT, VRIMPOSTODIFERIT, VRADICALIQFECP, VRADICFECP, CDBENEFFISCAL, IDCONSDIFALIQ,
      IDDESONERAREGRA, IDCONSDESONVRNF, VRIMPOSTODESON, CDCOMPST, CDRESTST, VRALIQFCP, VRIMPOFCP, VRBASECALCDEST, VRALIQINTEDEST,
      VRPERCPARTILHA, VRIMPOPARTDEST, VRIMPOPARTREME, VRALIQINTEREST, VRBASECALCREME, CDCEST, CDNATUREC, VRPERREDBCSTIT, VRIMPOSTOREP)
      (SELECT CDFILIMPOIT, NRLANCIMPOIT, NRSEQIMPOIT, CDIMPOSTOIT, NRSEQITPRO, CDFISCIMPOIT, CDSTRIBIMPOIT,
              CDFOPERIMPOIT, CDSCFOPIMPOIT, VRALIQIMPOIT, VRREDBCIMPOIT, VRPDEDIMPOIT, VRCONTIMPOIT,
              VRBCALCIMPOIT, VRIMPOSIT, IDERRIMPOIT, IDAVISIMPOIT, IDVALORINFO,
              CDSTRIBITNFE, IDMODBCITEMNFE, IDMODBCSTITENFE, IDDIFALIQCUSPTI, VR1REDBCIMPOIT, VR1IMPOSTOIT,
              CDTPCREDPICOITE, CDNATUBCALPCIT, IDIMPDESOTRABIT, IDMOTDESOTRABIT, IDTRIMPSTTI, CDREGRATRABIT, VRBCALCIRRF,
              CDDETALHEPROD,   CDENQLEGALIPI,
              VRPERDIFEREIT, VRIMPOSTOOPERAIT, VRIMPOSTODIFERIT, VRADICALIQFECP, VRADICFECP, CDBENEFFISCAL, IDCONSDIFALIQ,
              IDDESONERAREGRA, IDCONSDESONVRNF, VRIMPOSTODESON, CDCOMPST, CDRESTST, VRALIQFCP, VRIMPOFCP, VRBASECALCDEST,
              VRALIQINTEDEST, VRPERCPARTILHA, VRIMPOPARTDEST, VRIMPOPARTREME, VRALIQINTEREST, VRBASECALCREME, CDCEST, 
         			  CDNATUREC, VRPERREDBCSTIT, VRIMPOSTOREP
            FROM TRABITPROIMPOS T, ITEMPRODUTO I
            WHERE (CDFILIMPOIT  = P_FILIAL)     AND
                  (NRLANCIMPOIT = P_LANCTONF)   AND
                  (CDFILIMPOIT  = I.CDFILIAL)   AND
                  (NRLANCIMPOIT = I.NRLANCTONF) AND
                  (NRSEQIMPOIT  = I.NRSEQITPROD));                         

   INSERT INTO IMPOSNFTOT(
      CDFILIAL, NRLANCTONF, CDIMPOSTO, VRTOTIMPONF, VRTOTBCALCNF,
      IDERROSIMPTT, IDAVISIMPTT)
      (SELECT CDFILIMPTOT, NRLANCIMPTOT, CDIMPOSTOT, VRIMPOSTOT, VRBCALIMPTOT,
              IDERROSTOT, IDAVISOSTOT
       FROM TRABIMPOSNFTOT
       WHERE CDFILIMPTOT  = P_FILIAL AND
             NRLANCIMPTOT = P_LANCTONF);

   DELETE TRABITPROIMPOS
   WHERE CDFILIMPOIT  = P_FILIAL   AND
         NRLANCIMPOIT = P_LANCTONF;

   DELETE TRABIMPOSNFTOT
   WHERE CDFILIMPTOT  = P_FILIAL AND
         NRLANCIMPTOT = P_LANCTONF;

END;
/