CREATE OR REPLACE PROCEDURE DELETA_NF(
    P_FILIAL IN NOTAFISCAL.CDFILIAL%TYPE,
    P_LANCTONF IN NOTAFISCAL.NRLANCTONF%TYPE,
    P_IDCORPITEM IN VARCHAR2 DEFAULT NULL)

IS
    V_COUNT        NUMBER;
    V_AUTOFORN     VARCHAR2(10);
    V_CDFILIAL     VARCHAR2(04);
    V_NRLANCTONF   VARCHAR2(10);
    V_IDSISTEMA    VARCHAR2(03);

    V_CDTIPOOPER   VARCHAR2(02);
    V_VRNOTAFISC   NUMBER;

    V_CONTADOR     NUMBER;
    V_FILIAFATU    VARCHAR2(04); 
    V_NRAPURACFAT  VARCHAR2(10);
    V_IDENTRSAIDOP VARCHAR2(01);

    V_NRCTRLMOVEST CTRLMOVIESTQ.NRCTRLMOVEST%TYPE;
    V_CDFILIALENTR CTRLMOVIESTQ.CDFILIALENTR%TYPE;
    V_NRLANESTENTR CTRLMOVIESTQ.NRLANESTENTR%TYPE;

    V_CDFILIRECE     VARCHAR2(04);
    V_CDFILIEMIT     VARCHAR2(04); 
    V_IDCTRLDISTRIB  VARCHAR2(01);

    V_CDFORNECED       FORNECEDOR.CDFORNECED%TYPE;
    V_NRACESSONFE      NOTAFISCAL.NRACESSONFE%TYPE;
    V_CDFILIAGENT      VARCHAR2(04);
    V_CDFILIOLAGETERC  VARCHAR2(04);
BEGIN
   DELETE FROM GUIAPAGDOC
    WHERE (CDFILIAL   = P_FILIAL)
      AND (NRLANCTONF = P_LANCTONF);

   DELETE FROM ETIQUETAPRODOL
    WHERE (CDFILIAL   = P_FILIAL)
      AND (NRLANCTONF = P_LANCTONF);

   BEGIN
     SELECT IDENTRSAIDOP, CDFILIRECE, CDFILIEMIT, CDFORNECED, NRACESSONFE
       INTO V_IDENTRSAIDOP, V_CDFILIRECE, V_CDFILIEMIT, V_CDFORNECED, V_NRACESSONFE
       FROM NOTAFISCAL
      WHERE (CDFILIAL = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);
   EXCEPTION
     WHEN NO_DATA_FOUND THEN
       V_IDENTRSAIDOP := NULL; V_CDFILIRECE := NULL; V_CDFILIEMIT := NULL; V_CDFORNECED := NULL; V_NRACESSONFE := NULL;
   END;

   IF V_IDENTRSAIDOP = 'S' THEN
      UPDATE ETIQUETAPRODOL
         SET IDSTATUSETIQ = 'A',
             DSNOTAFISORIGET = NULL
       WHERE DSNOTAFISORIGET = P_FILIAL || P_LANCTONF;

      FOR C_ETIQPED IN (SELECT E.CDFILLANC, E.CDFILDEST, E.NRPEDEXOL
                          FROM NOTAFISCAL NF INNER JOIN NFRELACPEXOL XOL ON NF.CDFILIAL   = XOL.CDFILIAL
                                                                        AND NF.NRLANCTONF = XOL.NRLANCTONF
                                             INNER JOIN ETIQUETAPRODOL E ON XOL.CDFILLANC = E.CDFILLANC
                                                                        AND XOL.CDFILDEST = E.CDFILDEST
                                                                        AND XOL.NRPEDEXOL = E.NRPEDEXOL
                                             INNER JOIN PARAMFILIAL P ON P.CDFILIAL = NF.CDFILIEMIT
                         WHERE NF.CDFILIAL   = P_FILIAL
                           AND NF.NRLANCTONF = P_LANCTONF
                           AND P.IDETIQPRODOL= 'S'
                         GROUP BY E.CDFILLANC, E.CDFILDEST, E.NRPEDEXOL) LOOP

         UPDATE ETIQUETAPRODOL
            SET IDSTATUSETIQ = 'R'
          WHERE CDFILDEST = C_ETIQPED.CDFILDEST
            AND CDFILLANC = C_ETIQPED.CDFILLANC
            AND NRPEDEXOL = C_ETIQPED.NRPEDEXOL
            AND IDSTATUSETIQ = 'I';
      END LOOP;
   END IF;

   UPDATE GNRENF
      SET NRLANCTONFPAG = NULL
    WHERE (CDFILIAL      = P_FILIAL)
      AND (NRLANCTONFPAG = P_LANCTONF);

   DELETE FROM GNRENF
    WHERE (CDFILIAL   = P_FILIAL)
      AND (NRLANCTONF = P_LANCTONF);

   DELETE FROM RELACONTAINERNF
    WHERE (CDFILIAL   = P_FILIAL)
      AND (NRLANCTONF = P_LANCTONF);

   DELETE FROM COLETAVOL
    WHERE (CDFILIAL   = P_FILIAL)
      AND (NRLANCTONF = P_LANCTONF);

   IF (P_IDCORPITEM = 'I') OR (P_IDCORPITEM IS NULL) THEN

      UPDATE ITEMVENDA
      SET CDFILIALNF  = NULL,
          NRLANCTONF  = NULL,
          NRSEQITPROD = NULL
      WHERE CDFILIALNF = P_FILIAL
        AND NRLANCTONF = P_LANCTONF;

      BEGIN
        SELECT MAX(CDTIPOOPER)
        INTO V_CDTIPOOPER
        FROM NOTAFISCAL
        WHERE (CDFILIAL = P_FILIAL)
          AND (NRLANCTONF = P_LANCTONF);
      EXCEPTION
        WHEN NO_DATA_FOUND THEN
          NULL;
      END;

      IF V_CDTIPOOPER = '03' THEN
        V_VRNOTAFISC := 0;

        FOR C_ENCARGO IN (SELECT EN.IDENCARGO, SUM(VRITEMENCA) AS VRITEMENCA
                          FROM ITEMENCARGO IE, ENCARGO EN
                          WHERE (IE.CDFILIAL     = P_FILIAL)
                            AND (IE.NRLANCTONF   = P_LANCTONF)
                            AND (IE.VRITEMENCA   > 0)
                            AND (EN.CDENCARGO    = IE.CDENCARGO)
                            AND (EN.CDCTBLENTREN IS NULL)
                          GROUP BY IDENCARGO) LOOP

          IF C_ENCARGO.IDENCARGO = 'A' THEN
            V_VRNOTAFISC := V_VRNOTAFISC - C_ENCARGO.VRITEMENCA;
          ELSIF C_ENCARGO.IDENCARGO = 'D' THEN
              V_VRNOTAFISC := V_VRNOTAFISC + C_ENCARGO.VRITEMENCA;
          END IF;

        END LOOP;

        ATUALIZA_CUSTO_COMPLEMENTO(P_FILIAL, P_LANCTONF, V_VRNOTAFISC);
      END IF;

      DELETE FROM ITEMPROIMPOS
      WHERE (CDFILIAL = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);

      DELETE FROM ITEMPROIMPOSORIG
      WHERE (CDFILIAL = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);

      DELETE FROM ITEMENCARGO
      WHERE (CDFILIAL = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);

      DELETE FROM IMPOSTONF
      WHERE (CDFILIAL = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);

      DELETE FROM IMPOSNFTOT
      WHERE (CDFILIAL = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);

      DELETE FROM DADOSADIC
      WHERE (CDFILIAL = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);

      DELETE FROM IMPOSINFUSU
      WHERE (CDFILIAL = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);

      DELETE FROM ADICDECIMPNFE
      WHERE (CDFILIAL   = P_FILIAL) AND
            (NRLANCTONF = P_LANCTONF);

      DELETE FROM DECIMPNFE
      WHERE (CDFILIAL   = P_FILIAL) AND
            (NRLANCTONF = P_LANCTONF);

      DELETE FROM NFPRODRURAL
      WHERE (CDFILIAL   = P_FILIAL) AND
            (NRLANCTONF = P_LANCTONF);

      ATUALIZA_QTRELAC_ITNFORIGEM(P_FILIAL, P_LANCTONF, '0001', 0, 'E', 'S');

      DELETE FROM INFICMSST
      WHERE (CDFILIAL = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);

      DELETE FROM CTRLSENHADEVOL
       WHERE (CDFILIAL = P_FILIAL)
         AND (NRLANCTONF = P_LANCTONF);

      DELETE FROM ITEMPRODUTO
      WHERE (CDFILIAL = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);

      DELETE FROM ITEMNOTAFISC
      WHERE (CDFILIAL = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);

      DELETE FROM HISTNEGOCIACAO
      WHERE (CDFILIAL = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);

      DELETE FROM CONTARECEBER
      WHERE (CDFILIAL = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);

      DELETE FROM NFRELACPEXOL
      WHERE (CDFILIAL = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);

      DELETE FROM RESERITEMPROD
      WHERE (CDFILIAL = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);

      UPDATE LANCDESPFATU
      SET CDFILILANC = NULL,
        NRLANCTONF = NULL
      WHERE (CDFILILANC = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);

      UPDATE PREVRETI
      SET CDFILIALNF = NULL,
        NRLANCTONF = NULL
      WHERE (CDFILIALNF = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);

      UPDATE NOTADESTINADA
      SET CDFILIAL = NULL,
          NRLANCTONF = NULL
      WHERE (CDFILIAL = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);

   END IF;

   SELECT COUNT(*)
   INTO V_COUNT
   FROM LANCTOESTOQ
   WHERE CDFILIAL = P_FILIAL
    AND NRLANCTONF = P_LANCTONF;

   IF V_COUNT > 0 THEN
      FOR C_LANCTOESTOQ IN (SELECT NRLANCESTQ
                              FROM LANCTOESTOQ
                             WHERE CDFILIAL = P_FILIAL
                               AND NRLANCTONF = P_LANCTONF)
      LOOP

         BEGIN
            SELECT CDFILIAL, NRCTRLMOVEST, CDFILIALENTR, NRLANESTENTR
              INTO V_CDFILIAL, V_NRCTRLMOVEST, V_CDFILIALENTR, V_NRLANESTENTR
              FROM CTRLMOVIESTQ
             WHERE CDFILIALSAID = P_FILIAL
               AND NRLANESTSAID = C_LANCTOESTOQ.NRLANCESTQ;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_CDFILIAL     := NULL;
               V_NRCTRLMOVEST := NULL;
               V_CDFILIALENTR := NULL;
               V_NRLANESTENTR := NULL;
         END;

         IF V_IDENTRSAIDOP = 'E' THEN
            BEGIN
               SELECT NVL(IDCTRLDISTRIB, 'N')
                 INTO V_IDCTRLDISTRIB
                 FROM PARAMFILIAL
                WHERE CDFILIAL = V_CDFILIRECE;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDCTRLDISTRIB := 'N';
            END;
         ELSE
            BEGIN
               SELECT NVL(IDCTRLDISTRIB, 'N')
                 INTO V_IDCTRLDISTRIB
                 FROM PARAMFILIAL
                WHERE CDFILIAL = V_CDFILIEMIT;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDCTRLDISTRIB := 'N';
            END;
         END IF;

         -- Se a filial utiliza gestao de estoque e distribuicao
         IF V_IDCTRLDISTRIB = 'S' THEN
            FOR C_ITENS IN (SELECT I.CDALMOXARIFE, I.CDLOCALESTOQ, DECODE(TRUNC(I.QTTOTLANCTO / P.VRCAPACPALETE), 0, 1, TRUNC
(I.QTTOTLANCTO / P.VRCAPACPALETE)) AS QTPALETEUTIL
                              FROM ITLANCTOEST I, PRODPALETE P
                             WHERE I.CDFILIAL   = P_FILIAL
                               AND I.NRLANCESTQ = C_LANCTOESTOQ.NRLANCESTQ
                               AND P.CDFILIAL = I.CDFILIMOVI
                               AND P.CDPRODUTO = I.CDPRODUTO)
            LOOP
               -- Atualiza a quantidade de paletes utilizados na localizacao (rua)
               IF V_IDENTRSAIDOP = 'E' THEN
                  UPDATE LOCALESTOQUE
                     SET VRCAPACLOCUTIL = NVL(VRCAPACLOCUTIL,0) - C_ITENS.QTPALETEUTIL
                   WHERE CDFILIAL     = V_CDFILIRECE
                     AND CDALMOXARIFE = C_ITENS.CDALMOXARIFE
                     AND CDLOCALESTOQ = C_ITENS.CDLOCALESTOQ;

                  UPDATE LOCALESTOQUE
                     SET VRCAPACLOCUTIL = 0
                   WHERE CDFILIAL     = V_CDFILIRECE
                     AND CDALMOXARIFE = C_ITENS.CDALMOXARIFE
                     AND CDLOCALESTOQ = C_ITENS.CDLOCALESTOQ
                     AND VRCAPACLOCUTIL < 0;
               ELSE
                  UPDATE LOCALESTOQUE
                     SET VRCAPACLOCUTIL = NVL(VRCAPACLOCUTIL,0) + C_ITENS.QTPALETEUTIL
                   WHERE CDFILIAL     = V_CDFILIEMIT
                     AND CDALMOXARIFE = C_ITENS.CDALMOXARIFE
                     AND CDLOCALESTOQ = C_ITENS.CDLOCALESTOQ;
               END IF;
            END LOOP;
         END IF;

         IF (P_IDCORPITEM = 'I') OR (P_IDCORPITEM IS NULL) THEN
            IF V_NRCTRLMOVEST IS NOT NULL THEN
               DELETE
                 FROM ITLANCTOEST
                WHERE CDFILIAL = V_CDFILIALENTR
                  AND NRLANCESTQ = V_NRLANESTENTR;
            END IF;

            DELETE
              FROM ITLANCTOEST
             WHERE CDFILIAL = P_FILIAL
               AND NRLANCESTQ = C_LANCTOESTOQ.NRLANCESTQ;

            DELETE
              FROM CUSTOCMPPROD
             WHERE CDFILLANCEST = P_FILIAL
               AND NRLANCESTQ = C_LANCTOESTOQ.NRLANCESTQ;
         END IF;

         IF (P_IDCORPITEM = 'C') OR (P_IDCORPITEM IS NULL) THEN
            IF V_NRCTRLMOVEST IS NOT NULL THEN
               DELETE
                 FROM CTRLMOVIESTQ
                WHERE CDFILIAL = V_CDFILIAL
                  AND NRCTRLMOVEST = V_NRCTRLMOVEST;

               DELETE
                 FROM LANCTOESTOQ
                WHERE CDFILIAL = V_CDFILIALENTR
                  AND NRLANCESTQ = V_NRLANESTENTR;
            END IF;

            DELETE
              FROM LANCTOESTOQ
             WHERE CDFILIAL = P_FILIAL
               AND NRLANCTONF = P_LANCTONF;
         END IF;
      END LOOP;
   END IF;

   BEGIN
     SELECT MAX(CDFILIAL),
            MAX(NRAPURACFAT)
     INTO V_FILIAFATU,
          V_NRAPURACFAT
     FROM NOTAFISCAL
     WHERE (CDFILIAL = P_FILIAL)
       AND (NRLANCTONF = P_LANCTONF);
   EXCEPTION
     WHEN NO_DATA_FOUND THEN
       NULL;
   END;

   UPDATE EFECLIFAT
   SET CDFILIFATU  = NULL,
       NRAPURACFAT = NULL
   WHERE (CDFILIFATU = V_FILIAFATU)
     AND (NRAPURACFAT = V_NRAPURACFAT);

   UPDATE EFETCLIE
   SET CDFILIFATU = NULL,
     NRAPURACFAT = NULL
   WHERE (CDFILIFATU = V_FILIAFATU)
     AND (NRAPURACFAT = V_NRAPURACFAT);

   IF (P_IDCORPITEM = 'C') OR(P_IDCORPITEM IS NULL) THEN

      -- se possuir agente de terceirizacao e filial OL sera efetuada alteracao no pedido OL e na reserva do pedido
      IF V_IDENTRSAIDOP = 'E' THEN
         BEGIN
            SELECT CDFILIAL, CDFILIOLAGETERC
              INTO V_CDFILIAGENT, V_CDFILIOLAGETERC
              FROM FILIAL
             WHERE CDFORNAGETERC = V_CDFORNECED;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_CDFILIAGENT := NULL; V_CDFILIOLAGETERC := NULL;
         END;

         IF (NOT V_CDFILIAGENT IS NULL) AND (NOT V_CDFILIOLAGETERC IS NULL) THEN

            FOR C_INTG_ITENS IN (SELECT I.NRAUTOFORN, P.CDPRODUTO, SUM(I.QTITEMPROD) AS QTITEMPROD
                                   FROM INTG_NOTAFISCAL N, INTG_ITEMPRODUTO I, PRODFORNXML P
                                  WHERE N.NRACESSONFE = V_NRACESSONFE
                                    AND I.NRACESSONFE = N.NRACESSONFE
                                    AND P.CDFORNECED   = N.CDFORNECED
                                    AND P.CDPRODUTOXML = I.CDPRODUTO
                                  GROUP BY I.NRAUTOFORN, P.CDPRODUTO)
            LOOP
               FOR C_ITEM_PED IN (SELECT IT.CDFILDEST, IT.CDFILLANC, IT.NRPEDEXOL, IT.CDMARCA, IT.CDPRODUTO, IT.DTPROGENPEOL
                                    FROM PEDIDOEXOL P, ITEMPEDEXOL IT, RELACPRODOL R
                                   WHERE P.USR_NRPEDIDO = C_INTG_ITENS.NRAUTOFORN
                                     AND IT.CDFILDEST = P.CDFILDEST
                                     AND IT.CDFILLANC = P.CDFILLANC
                                     AND IT.NRPEDEXOL = P.NRPEDEXOL
                                     AND IT.IDBAIXENPEOL IS NULL
                                     AND R.CDPRARVORIG = C_INTG_ITENS.CDPRODUTO
                                     AND IT.CDPRODUTO  = R.CDPRARVORIG
                                     AND IT.CDMARCA    = R.CDMARCA
                                   ORDER BY IT.CDPRODUTO, IT.DTPROGENPEOL)
               LOOP
                  -- altera a qtde atendida do pedido OL com a qtde. da nota fiscal somente para a primeira data de entrega
                  UPDATE ITEMPEDEXOL
                     SET QTATENDPEDOL = (QTATENDPEDOL - C_INTG_ITENS.QTITEMPROD)
                   WHERE CDFILDEST    = C_ITEM_PED.CDFILDEST
                     AND CDFILLANC    = C_ITEM_PED.CDFILLANC
                     AND NRPEDEXOL    = C_ITEM_PED.NRPEDEXOL
                     AND CDMARCA      = C_ITEM_PED.CDMARCA
                     AND CDPRODUTO    = C_ITEM_PED.CDPRODUTO
                     AND DTPROGENPEOL = C_ITEM_PED.DTPROGENPEOL;

                  -- altera a qtde reservada do pedido OL e a filial da reserva do pedido para a filial relacionada ao agente de 
--terceirizacao
                  UPDATE RESERVAPEDEXOL
                     SET CDFILRESOL    = V_CDFILIAGENT,
                         QTRESERVESTOL = (QTRESERVESTOL - C_INTG_ITENS.QTITEMPROD),
                         CDFILIAL      = NULL,
                         NRLANCTONF    = NULL
                   WHERE CDFILDEST    = C_ITEM_PED.CDFILDEST
                     AND CDFILLANC    = C_ITEM_PED.CDFILLANC
                     AND NRPEDEXOL    = C_ITEM_PED.NRPEDEXOL
                     AND CDMARCA      = C_ITEM_PED.CDMARCA
                     AND CDPRODUTO    = C_ITEM_PED.CDPRODUTO
                     AND DTPROGENPEOL = C_ITEM_PED.DTPROGENPEOL;

                  EXIT;
               END LOOP;
            END LOOP;
         END IF;
      END IF;

     DELETE FROM CONTAPAGAR
     WHERE (CDFILIAL = P_FILIAL)
       AND (NRLANCTONF = P_LANCTONF);

     DELETE FROM NFRELACAF
     WHERE (CDFILIAL = P_FILIAL)
       AND (NRLANCTONF = P_LANCTONF);

     DELETE FROM ARQXMLNFE
      WHERE (CDFILIAL = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);

     DELETE FROM ERRONFPREVALID
      WHERE (CDFILIAL = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);

     DELETE FROM NFPREVALIDFIS
      WHERE (CDFILIAL = P_FILIAL)
        AND (NRLANCTONF = P_LANCTONF);

     DELETE FROM NOTABCMENSAL
     WHERE (CDFILIAL = P_FILIAL)
       AND (NRLANCTONF = P_LANCTONF);

     DELETE FROM NOTAFISCAL
     WHERE (CDFILIAL = P_FILIAL)
       AND (NRLANCTONF = P_LANCTONF);

     UPDATE INTG_NOTAFISCAL
        SET IDSTATUSNFE  = 'N',
            IDIMPORTADA  = 'N',
            DTUTILIZAXML = NULL,
            CDFILIAL     = '',
            NRLANCTONF   = '',
            IDSTATUS     = 'P'
      WHERE NRACESSONFE  = V_NRACESSONFE;
      
   END IF;
END;