USE [TryNFeOnline]
GO

ALTER TABLE [dbo].[configuracoes] ADD 
[emailsAdicionaisPedido] [varchar] (500) COLLATE Latin1_General_CI_AS NULL,
[idnEnviarEmailPedido] [bit] NULL,
[idnEnviarEmailPedidoVendedor] [bit] NULL
GO
ALTER TABLE [dbo].[emitente] ADD 
[idnRealizarCienciaDownloadXmlAutomaticamente] [bit] NULL
GO
CREATE TABLE [dbo].[ParceiroEmails]
(
	[id] [int] IDENTITY (1,1) NOT NULL,
	[idParceiro] [int] NOT NULL,
	[enderecoEmail] [varchar] (500) COLLATE Latin1_General_CI_AS NULL,
	[idnDocumentoFiscal] [bit] NOT NULL,
	[idnBoleto] [bit] NOT NULL,
	[idnCobranca] [bit] NOT NULL,
	[idnMovimento] [bit] NOT NULL,
	[idnPedido] [bit] NOT NULL,
	[idnOrdemServico] [bit] NOT NULL,
	CONSTRAINT [PK_ParceiroEmails] 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 [_dta_index_nfEmitente_16_837578022__K2] ON [dbo].[nfEmitente]
(
	[idEmitente] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [_dta_index_pedidoStatus_13_375672386__K2_1_3_4_5] ON [dbo].[pedidoStatus]
(
	[idPedido] ASC
) INCLUDE ([id],[situacao],[dataHora],[idOperador]) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [_dta_index_Fatura_13_46623209__K43_1_2_3_4_5_6_7_8_9_10_11_12_13_14_15_16_17_18_19_20_21_22_23_24_25_26_27_28_29_30_31_32_33_] ON [dbo].[Fatura]
(
	[idPedido] ASC
) INCLUDE ([id],[idnAgrupada],[motivoBaixa],[idnTransferencia],[idnContrato],[idContrato],[lote],[parcelaContrato],[idnSaque],[idnDeposito],[idnTransferenciaEntreContas],[idAdiantamento],[idFaturaAdiantamentoOrigem],[idnLancamentoCartao],[idnTrocaNumerario],[dataUltimaAlteracao],[idnAdiantamentoOrdemServico],[idVeiculo],[idFaturaAgrupada],[idConta],[idNumerario],[idnLancamentoAVista],[idParceiro],[dataHoraEmissao],[dataFatura],[valor],[saldo],[numeroDocumento],[idOperador],[idCondicaoPagamento],[idTipoEquipamento],[idVendedor],[idEmitente],[idReceitaDespesa],[historico],[idFormaPagamento],[idnReceitaDespesa],[valorEntrada],[idOrdemServico],[idnPrevista],[idNotaFiscal]) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [_dta_index_FaturaParcela_5_411148510__K2_K4_K6_1_3_5_7_8_9_10_11_12_13_14_15_16_17_18] ON [dbo].[FaturaParcela]
(
	[idFatura] ASC,
	[dataVencimento] ASC,
	[saldo] ASC
) INCLUDE ([id],[saldoAnteriorAgrupamento],[idFaturaAgrupada],[dataBaixa],[idnBaixado],[numeroDuplicata],[dataCalculoJuro],[percentualJuro],[valorMulta],[saldoJuro],[idnQuitada],[valor],[parcela],[saldoAnteriorBaixa],[observacao]) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [_dta_index_produto_13_2117582582__K2_K18_K21_1_3] ON [dbo].[produto]
(
	[descricao] ASC,
	[idEmitente] ASC,
	[idnBaixa] ASC
) INCLUDE ([id],[ncm]) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [_dta_index_pedido_13_1598628738__K2D_K10_K6_K7_K3_K4_K5_K28_K11_K18_K30_K14_K15_K19_K26] ON [dbo].[pedido]
(
	[numeroPedido] DESC,
	[idOperacao] ASC,
	[dataPedido] ASC,
	[dataEntrega] ASC,
	[idEmitente] ASC,
	[idParceiro] ASC,
	[idParceiroVendedor] ASC,
	[idRepresentante] ASC,
	[tipoPedido] ASC,
	[idnProducao] ASC,
	[idnExpedicao] ASC,
	[idnFaturado] ASC,
	[idnCancelado] ASC,
	[idnPreVendaComanda] ASC,
	[idnPedidoDelivery] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [_dta_index_parceiro_5_763865788__K24_1_16_21_28_50_95] ON [dbo].[parceiro]
(
	[nascimentoConjugue] ASC
) INCLUDE ([id],[email],[nomeConjugue],[celular],[operadoraCelular],[telefoneConjuge]) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [_dta_index_parceiro_5_763865788__K23] ON [dbo].[parceiro]
(
	[dataNascimento] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [_dta_index_notafiscal_5_1874105717__K191_K82_K83_K157_K79_K37_K128_K69_K40_K165_K109] ON [dbo].[notafiscal]
(
	[terminalServerSessionName] ASC,
	[idOperador] ASC,
	[nomeComputadorEmissao] ASC,
	[dataAtualizacaoStatusNota] ASC,
	[idnDocumentoEmitido] ASC,
	[idnImpresso] ASC,
	[idnEmiteNFCe] ASC,
	[idnEmiteCte] ASC,
	[idnEmiteNF] ASC,
	[idnEmiteCTeOS] ASC,
	[idnEmiteMDFe] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [_dta_index_parceiro_13_1117247035__K1_K14] ON [dbo].[parceiro]
(
	[id] ASC,
	[idCidade] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [_dta_index_parceiro_13_1117247035__K1_2_3_5_15] ON [dbo].[parceiro]
(
	[id] ASC
) INCLUDE ([razaosocial],[fantasia],[cpfcnpj],[telefone]) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [_dta_index_notafiscal_16_613577224__K1_77] ON [dbo].[notafiscal]
(
	[id] ASC
) INCLUDE ([numeroMovimento]) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [_dta_index_notafiscal_13_613577224__K191_K79_K157_K82_K83_K37_K128_K69_K40_K165_K109_K1] ON [dbo].[notafiscal]
(
	[terminalServerSessionName] ASC,
	[idnDocumentoEmitido] ASC,
	[dataAtualizacaoStatusNota] ASC,
	[idOperador] ASC,
	[nomeComputadorEmissao] ASC,
	[idnImpresso] ASC,
	[idnEmiteNFCe] ASC,
	[idnEmiteCte] ASC,
	[idnEmiteNF] ASC,
	[idnEmiteCTeOS] ASC,
	[idnEmiteMDFe] ASC,
	[id] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [_dta_index_parceiro_13_1117247035__K38_1_2_3_5_15] ON [dbo].[parceiro]
(
	[idnVendedor] ASC
) INCLUDE ([id],[razaosocial],[fantasia],[cpfcnpj],[telefone]) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ParceiroEmails] ADD CONSTRAINT [FK_ParceiroEmails_parceiro] FOREIGN KEY
	(
		[idParceiro]
	)
	REFERENCES [dbo].[parceiro]
	(
		[id]
	)
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[ManutencoesBanco](
	[descricao] [varchar](50) NOT NULL,
	[dataUltimaExecucao] [datetime] NULL,
 CONSTRAINT [PK_ManutencoesBanco] PRIMARY KEY CLUSTERED 
(
	[descricao] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT INTO ManutencoesBanco(descricao) values ('INDICES')
GO
declare @diasSemManutencao int = 0;
SET @diasSemManutencao = (select DATEDIFF(DAY,coalesce(m.dataUltimaExecucao, DATEADD(week,-2, getdate())), GETDATE()) from ManutencoesBanco m where descricao = 'INDICES');
if (@diasSemManutencao >= 7)
begin
EXEC sp_msforeachtable 'SET QUOTED_IDENTIFIER ON ALTER INDEX ALL ON ? REBUILD WITH (FILLFACTOR = 90)';
EXEC sp_msforeachtable 'SET QUOTED_IDENTIFIER ON ALTER INDEX ALL ON ? REORGANIZE';
UPDATE ManutencoesBanco set dataUltimaExecucao = GETDATE() where descricao = 'INDICES';
end;
GO
INSERT INTO VERSAO(VERSAO, VERSAOATUALIZACAO, DATAATUALIZACAO) VALUES(425, 683, GETDATE());