USE [TryNFeOnline]
GO

ALTER TABLE [dbo].[MDeNotas] ADD 
[idEmitente] [int] NULL
GO
ALTER TABLE [dbo].[notafiscal] ADD 
[idnNFSeSubstituta] [bit] NULL
GO
CREATE TABLE [dbo].[NFSeSubstituicao]
(
	[id] [int] IDENTITY (1,1) NOT NULL,
	[idNotaSubstituida] [int] NOT NULL,
	[idNotaSubstituta] [int] NOT NULL,
	CONSTRAINT [PK_NFSeSubstituicao] PRIMARY KEY CLUSTERED
	(
		[id] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE NONCLUSTERED INDEX [IX_idMDeConsulta] ON [dbo].[MDeNotas]
(
	[idMDeConsulta] ASC,
	[tpAmb] ASC,
	[numeroNFe] ASC,
	[cpfCnpjEmitente] ASC,
	[dataEmissao] ASC,
	[tipoNFe] ASC,
	[situacaoNFe] ASC,
	[idnTransportadoraOuTerceiros] ASC,
	[idEmitente] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE TRIGGER [dbo].[TG_AI_MDeNotas]
   ON  dbo.MDeNotas
   AFTER INSERT
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;	
    -- Insert statements for trigger here	
	 
	update MDeNotas set idEmitente = (select top 1 m.idEmitente from MDeConsultaNFe m where m.id = (select top 1 idMDeConsulta from INSERTED)) where id = (select top 1 id from INSERTED)
END
GO
INSERT INTO [selecao] ([tabela],[descricao],[camposListar],[campoChave],[campoRetorno],[nomeTela]) VALUES ('DashboardGrupos', 'Grupo da Dashboard', 'upper(descricao) as descricao = Descrição, id = Código', 'id', 'descricao', 'ucCadGrupoDashboard')
GO
IF COL_LENGTH('dbo.CartaoLote', 'historico') IS NULL
BEGIN
    ALTER TABLE [dbo].[CartaoLote] ADD
	[historico] [varchar] (max) COLLATE Latin1_General_CI_AS NULL
END
GO
INSERT INTO VERSAO(VERSAO, VERSAOATUALIZACAO, DATAATUALIZACAO) VALUES(395, 653, GETDATE());