CREATE OR ALTER trigger ttbnfs_item_b05d for tbnfs_item
active before delete position 5
AS
BEGIN

    DELETE FROM TBNFS_ITEM_SALDO WHERE NFS_ITEM_CONTROLE = OLD.CONTROLE;

    IF(OLD.GC_WEB = 1) THEN
    BEGIN
      IF(OLD.NF_NATUREZA = 2) THEN
      BEGIN
         -- DELETAR ESTOQUE PARA NATUREZA LANÇAMENTO
          EXECUTE PROCEDURE SP_NF_ITEM_ESTOQUE('NFS', OLD.CONTROLE, OLD.NFS_CONTROLE, 2);

          -- DESFAZER BAIXA DE PEDIDO
          EXECUTE PROCEDURE SPD_NFS_PEDIDO(OLD.NFS_CONTROLE, OLD.CONTROLE);
      END

      UPDATE TBLOTE_PECA_ITEM T SET T.NFS_ITEM_ID = NULL WHERE T.NFS_ITEM_ID = OLD.CONTROLE;

      IF(COALESCE(OLD.PDV_CONFERENCIA_ITEM_ID, 0) > 0) THEN
      BEGIN
          EXECUTE PROCEDURE SP_NFS_ITEM_PRE_PEDIDO(OLD.PDV_CONFERENCIA_ITEM_ID, 2);
      END
    END
END;