IF EXISTS(SELECT TOP 1 1  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'LCF_LX_PAIS' AND COLUMN_NAME = 'LX_STATUS_REGISTRO')
	BEGIN
	IF EXISTS(SELECT TOP 1 1  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'LCF_LX_PAIS' AND COLUMN_NAME = 'LX_STATUS_REGISTRO' and COLUMN_DEFAULT is null)
		BEGIN
			EXEC('ALTER TABLE [dbo].[LCF_LX_PAIS] ADD  CONSTRAINT [DF_LX_STATUS_REGISTRO]  DEFAULT ((1)) FOR [LX_STATUS_REGISTRO]')
		END
	END
GO

IF EXISTS(SELECT *  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'SERIES_NF' AND COLUMN_NAME = 'SEQUENCIAL' and CHARACTER_MAXIMUM_LENGTH <15)
	BEGIN
		EXEC('ALTER TABLE [dbo].[SERIES_NF] ALTER COLUMN [SEQUENCIAL] [varchar](15) NULL')
	END
	
GO

IF EXISTS(SELECT *  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'SERIES_NF' AND COLUMN_NAME = 'COD_SERIE_SINTEGRA' and CHARACTER_MAXIMUM_LENGTH <6)
	BEGIN
		EXEC('ALTER TABLE [dbo].[SERIES_NF] ALTER COLUMN [COD_SERIE_SINTEGRA] [varchar](6) NULL')
	END
	
GO

IF EXISTS(SELECT TOP 1 1  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'LCF_LX_UF' AND COLUMN_NAME = 'LX_STATUS_REGISTRO')
	BEGIN
	IF EXISTS(SELECT TOP 1 1  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'LCF_LX_UF' AND COLUMN_NAME = 'LX_STATUS_REGISTRO' and COLUMN_DEFAULT is null)
		BEGIN
			EXEC('ALTER TABLE [dbo].[LCF_LX_UF] ADD  CONSTRAINT [DF_UF_LX_STATUS_REGISTRO]  DEFAULT ((1)) FOR [LX_STATUS_REGISTRO]')
		END
	END
GO

IF OBJECT_ID('UNICO_VITRINE_PEDIDO') IS NULL
	BEGIN
		CREATE TABLE UNICO_VITRINE_PEDIDO
			(
				ID_PEDIDO INT IDENTITY(1, 1) NOT NULL, 
				CODIGO_FILIAL CHAR(6) NOT NULL, 
				TICKET CHAR(8) NOT NULL, 
				DATA_VENDA DATETIME NOT NULL, 
				PEDIDO VARCHAR(50) NOT NULL, 
				CODIGO_CLIENTE VARCHAR(14) NULL, 
				NSU VARCHAR(16) NULL, 
				VALOR_ORIGINAL_PEDIDO NUMERIC(9, 2) NOT NULL, 
				DESCONTO_LOJA NUMERIC(9, 2) NOT NULL, 
				CONFIRMADO BIT NOT NULL,
				CANCELADO BIT NOT NULL, 
				FRETE NUMERIC(9, 2) NULL,
			    CONSTRAINT PK_UNICO_VITRINE_PEDIDO PRIMARY KEY CLUSTERED (ID_PEDIDO)
			)
	END
GO
		
IF OBJECT_ID('UNICO_VITRINE_PEDIDO_ITEM') IS NULL
	BEGIN	
		CREATE TABLE UNICO_VITRINE_PEDIDO_ITEM
				(	ID_PEDIDO_ITEM INT IDENTITY(1, 1)NOT NULL, 
					ID_PEDIDO INT NOT NULL, 
					CODIGO_BARRA VARCHAR(25) NOT NULL, 
					QTDE INT NOT NULL, 
					PRECO_UNITARIO NUMERIC(9, 2) NOT NULL, 
					VENDEDOR CHAR(4) NULL,
					DESCONTO NUMERIC(9, 2) NULL,
					CONSTRAINT PK_UNICO_VITRINE_PEDIDO_ITEM PRIMARY KEY CLUSTERED (ID_PEDIDO_ITEM), 
					FOREIGN KEY (ID_PEDIDO) REFERENCES UNICO_VITRINE_PEDIDO (ID_PEDIDO) ON DELETE CASCADE 			
		)
	END
GO

IF NOT EXISTS(SELECT TOP 1 1  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'UNICO_VITRINE_PEDIDO_ITEM' AND COLUMN_NAME = 'VENDEDOR')
	BEGIN
		ALTER TABLE [dbo].[UNICO_VITRINE_PEDIDO_ITEM] ADD [VENDEDOR] CHAR(4) NULL
	END
GO

IF NOT EXISTS(SELECT TOP 1 1  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'UNICO_VITRINE_PEDIDO_ITEM' AND COLUMN_NAME = 'DESCONTO')
	BEGIN
			ALTER TABLE [dbo].[UNICO_VITRINE_PEDIDO_ITEM] ADD [DESCONTO] NUMERIC(9, 2) NULL
	END
GO

IF NOT EXISTS(SELECT TOP 1 1  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'UNICO_VITRINE_PEDIDO' AND COLUMN_NAME = 'FRETE')
	BEGIN
		ALTER TABLE [dbo].[UNICO_VITRINE_PEDIDO] ADD [FRETE] NUMERIC(9, 2) NULL
	END
GO

IF NOT EXISTS(SELECT TOP 1 1 FROM sys.tables A INNER JOIN sys.columns B ON A.Object_ID = B.Object_ID AND A.Name = 'TABELA_ALIQUOTA_IMPOSTO_ITEM' AND COLUMNPROPERTY(A.Object_ID, B.Name, 'IsIdentity') = 1)
BEGIN
begin try 

	BEGIN TRANSACTION
	CREATE TABLE dbo.Tmp_TABELA_ALIQUOTA_IMPOSTO_ITEM
					(
					ID_IMPOSTO_ITEM int NOT NULL IDENTITY (1, 1),
					UF char(2) NOT NULL,
					NCM_NBS char(10) NOT NULL,
					EXCESSAO_FISCAL_TABELA smallint NOT NULL,
					TABELA smallint NOT NULL,
					ALIQNAC_FEDERAL numeric(5, 2) NOT NULL,
					ALIQIMP_FEDERAL numeric(5, 2) NOT NULL,
					ALIQ_MUNICIPAL numeric(5, 2) NOT NULL,
					ALIQ_ESTADUAL numeric(5, 2) NOT NULL,
					INICIO_VIGENCIA smalldatetime NULL,
					FIM_VIGENCIA smalldatetime NULL,
					CHAVE varchar(6) NULL,
					VERSAO varchar(6) NOT NULL,
					FONTE varchar(34) NULL,
					DATA_PARA_TRANSFERENCIA datetime NULL,
					CONSTRAINT XPKTmp_TABELA_ALIQUOTA_IMPOSTO_ITEM PRIMARY KEY NONCLUSTERED (NCM_NBS, UF,VERSAO,EXCESSAO_FISCAL_TABELA),
					CONSTRAINT XAKTmp_TABELA_ALIQUOTA_IMPOSTO_ITEM UNIQUE CLUSTERED (ID_IMPOSTO_ITEM))  


	SET IDENTITY_INSERT dbo.Tmp_TABELA_ALIQUOTA_IMPOSTO_ITEM ON
	IF EXISTS(SELECT * FROM dbo.TABELA_ALIQUOTA_IMPOSTO_ITEM)
		EXEC('	INSERT INTO dbo.Tmp_TABELA_ALIQUOTA_IMPOSTO_ITEM (ID_IMPOSTO_ITEM, UF, NCM_NBS, EXCESSAO_FISCAL_TABELA, TABELA, ALIQNAC_FEDERAL, ALIQIMP_FEDERAL, ALIQ_MUNICIPAL, ALIQ_ESTADUAL, INICIO_VIGENCIA, FIM_VIGENCIA, CHAVE, VERSAO, FONTE, DATA_PARA_TRANSFERENCIA)
				SELECT ID_IMPOSTO_ITEM, UF, NCM_NBS, EXCESSAO_FISCAL_TABELA, TABELA, ALIQNAC_FEDERAL, ALIQIMP_FEDERAL, ALIQ_MUNICIPAL, ALIQ_ESTADUAL, INICIO_VIGENCIA, FIM_VIGENCIA, CHAVE, VERSAO, FONTE, DATA_PARA_TRANSFERENCIA FROM dbo.TABELA_ALIQUOTA_IMPOSTO_ITEM WITH (HOLDLOCK TABLOCKX)')
	SET IDENTITY_INSERT dbo.Tmp_TABELA_ALIQUOTA_IMPOSTO_ITEM OFF


	DROP TABLE dbo.TABELA_ALIQUOTA_IMPOSTO_ITEM

	EXECUTE sp_rename N'dbo.Tmp_TABELA_ALIQUOTA_IMPOSTO_ITEM', N'TABELA_ALIQUOTA_IMPOSTO_ITEM', 'OBJECT'
	EXECUTE sp_rename N'XPKTmp_TABELA_ALIQUOTA_IMPOSTO_ITEM', N'XPKTABELA_ALIQUOTA_IMPOSTO_ITEM', 'OBJECT'
	EXECUTE sp_rename N'dbo.XAKTmp_TABELA_ALIQUOTA_IMPOSTO_ITEM', N'XAKTABELA_ALIQUOTA_IMPOSTO_ITEM', 'OBJECT'

	commit tran

end try
begin catch

	declare @ErrorMessage  nvarchar(4000), @ErrorSeverity int, @ErrorState int

	select	@ErrorMessage = error_message(), @ErrorSeverity = error_severity(),	@ErrorState = error_state()
		
	if @@trancount > 0 rollback tran
		
	raiserror (@ErrorMessage, @ErrorSeverity, @ErrorState)

end catch

END
GO

IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'LOJA_VENDA_PGTO' AND COLUMN_NAME = 'NUMERO_NF_ORDEM')
begin
	ALTER TABLE DBO.LOJA_VENDA_PGTO ADD NUMERO_NF_ORDEM CHAR(15) NULL
end
go

IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'LOJA_VENDA_PGTO' AND COLUMN_NAME = 'SERIE_NF_ORDEM')
begin
	ALTER TABLE DBO.LOJA_VENDA_PGTO ADD SERIE_NF_ORDEM CHAR(6) NULL
END
go


/*
-----------------------------------------------------------------------------------------------------------------------------------------------------
--	ALTERAÇÕES DE BANCO PARA A VERSÃO 3.1.10.6
-----------------------------------------------------------------------------------------------------------------------------------------------------
*/

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS 
   WHERE TABLE_NAME = 'LOJA_PEDIDO' AND COLUMN_NAME = 'CHANNEL_ID' )
BEGIN
	ALTER TABLE [dbo].[LOJA_PEDIDO] ADD CHANNEL_ID VARCHAR(100)
END
go

IF NOT EXISTS (SELECT 1 FROM   INFORMATION_SCHEMA.COLUMNS
     WHERE  TABLE_NAME = 'LOJA_VENDA_TROCA_OMS' AND COLUMN_NAME = 'QTDE_DEVOLVIDA')
BEGIN
  ALTER TABLE [dbo].[LOJA_VENDA_TROCA_OMS]  ADD QTDE_DEVOLVIDA INT NULL
END
go

IF NOT EXISTS (SELECT 1 FROM   INFORMATION_SCHEMA.COLUMNS
     WHERE  TABLE_NAME = 'LOJA_VENDA_TROCA_OMS' AND COLUMN_NAME = 'QTDE_AVARIA')
BEGIN
  ALTER TABLE [dbo].[LOJA_VENDA_TROCA_OMS] ADD QTDE_AVARIA INT NULL
END
go
       
IF NOT EXISTS (SELECT 1 FROM   INFORMATION_SCHEMA.COLUMNS
    WHERE  TABLE_NAME = 'LOJA_VENDA_TROCA_OMS' AND COLUMN_NAME = 'QTDE_RECUSADA')
BEGIN
  ALTER TABLE [dbo].[LOJA_VENDA_TROCA_OMS] ADD QTDE_RECUSADA INT NULL
END
go
       
IF NOT EXISTS (SELECT 1 FROM   INFORMATION_SCHEMA.COLUMNS
      WHERE  TABLE_NAME = 'LOJA_VENDA_TROCA_OMS' AND COLUMN_NAME = 'QTDE_NAO_DEVOLVIDA')
BEGIN
  ALTER TABLE [dbo].[LOJA_VENDA_TROCA_OMS] ADD QTDE_NAO_DEVOLVIDA INT NULL
END
go
       
 IF NOT EXISTS (SELECT 1 FROM   INFORMATION_SCHEMA.COLUMNS
      WHERE  TABLE_NAME = 'LOJA_VENDA_TROCA_OMS' AND COLUMN_NAME = 'ID_EXCEPTION')
BEGIN
  ALTER TABLE [dbo].[LOJA_VENDA_TROCA_OMS] ADD ID_EXCEPTION VARCHAR(20) NULL
END
go
       
IF NOT EXISTS (SELECT 1  FROM   INFORMATION_SCHEMA.COLUMNS
      WHERE  TABLE_NAME = 'LOJA_VENDA_TROCA_OMS' AND COLUMN_NAME = 'INDICA_DEVOLUCAO')
BEGIN
  ALTER TABLE [dbo].[LOJA_VENDA_TROCA_OMS] ADD INDICA_DEVOLUCAO BIT NULL
END
go
       
/*
-----------------------------------------------------------------------------------------------------------------------------------------------------
--	ALTERAÇÕES DE BANCO PARA A VERSÃO 3.1.10.12
-----------------------------------------------------------------------------------------------------------------------------------------------------
*/

IF EXISTS (SELECT TOP 1 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'AREZZO_XML_COMPRAS_LP' )
	AND NOT EXISTS (SELECT TOP 1 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'AREZZO_XML_COMPRAS_LP' AND COLUMN_NAME = 'CODIGO_FILIAL')
BEGIN
	ALTER TABLE AREZZO_XML_COMPRAS_LP ADD CODIGO_FILIAL CHAR(06) NULL
END
GO

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'CLASSIF_FISCAL_IMPOSTO' AND COLUMN_NAME = 'INATIVO') 
BEGIN
	ALTER TABLE [dbo].[CLASSIF_FISCAL_IMPOSTO] ADD INATIVO BIT NULL DEFAULT (0)
END
GO

/*
-----------------------------------------------------------------------------------------------------------------------------------------------------
--	ALTERAÇÕES DE BANCO PARA A VERSÃO 3.1.10.14
-----------------------------------------------------------------------------------------------------------------------------------------------------
*/
IF NOT EXISTS(SELECT TOP 1 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'LOJA_INTERMEDIADORES_TRANSACAO')
BEGIN
	CREATE TABLE [dbo].[LOJA_INTERMEDIADORES_TRANSACAO](
		   [ID_INTERMEDIADOR] [int] NOT NULL,
		   [CNPJ] char(14) NOT NULL,
		   [NOME_FANTASIA] VARCHAR(40) NOT NULL,
		   [INATIVO] BIT DEFAULT 0 NOT NULl ,
		   [ENVIA_LOJA_VAREJO] [bit] DEFAULT 0 NOT NULL,
		   [DATA_PARA_TRANSFERENCIA]  DATETIME,
	CONSTRAINT [XPKLOJA_INTERMEDIADORES_TRANSACAO] PRIMARY KEY CLUSTERED 
	(
		   [ID_INTERMEDIADOR] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY],
    CONSTRAINT [XAKLOJA_INTERMEDIADORES_TRANSACAO] UNIQUE NONCLUSTERED 
    (
		        [CNPJ] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
END
GO

IF NOT EXISTS( SELECT 1 FROM SYS.sysobjects WHERE NAME = 'LOJA_INTERMEDIADORES_TRANSACAO_FILIAL' )
BEGIN
	CREATE TABLE [dbo].[LOJA_INTERMEDIADORES_TRANSACAO_FILIAL](
				[ID_INTERMEDIADOR] [int] NOT NULL,
				[COD_FILIAL] [char](6) NOT NULL,
				[IDCADINTTRAN] [varchar](60) NOT NULL,
				[INATIVO] [bit] DEFAULT 0 NOT NULL,
				[CODIGO_FILIAL] [char](6) NOT NULL,
	CONSTRAINT [XPKLOJA_INTERMEDIADORES_TRANSACAO_FILIAL] PRIMARY KEY CLUSTERED 
    (
                [ID_INTERMEDIADOR] ASC, [COD_FILIAL] ASC, [IDCADINTTRAN] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
    CONSTRAINT [XAKLOJA_INTERMEDIADORES_TRANSACAO_FILIAL] UNIQUE NONCLUSTERED 
	(
			[IDCADINTTRAN] ASC,
			[COD_FILIAL] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
	CONSTRAINT [XFK10001_LOJA_INTERMEDIADORES_TRANSACAO_FILIAL] FOREIGN KEY
	(
		[ID_INTERMEDIADOR]
	)
	REFERENCES [dbo].[LOJA_INTERMEDIADORES_TRANSACAO] ([ID_INTERMEDIADOR]) ON DELETE CASCADE	
	) ON [PRIMARY]
END