
/*------ ATD 428269 ------*/

IF NOT EXISTS (SELECT 1 FROM SYSOBJECTS T WHERE T.NAME = 'POLITICA_COMERCIAL_ECOMMERCE' AND T.XTYPE = 'U')
begin
	CREATE TABLE dbo.POLITICA_COMERCIAL_ECOMMERCE(
		ID INT IDENTITY NOT NULL,
		RPLATAFORMA INT NOT NULL,
		CODIGO INT NOT NULL,
		NOME VARCHAR(100) NOT NULL,
		RAREADEVENDA INT NOT NULL,
		RFILIAL INT NOT NULL DEFAULT 7,
		CODVEND CHAR(5) NULL,
		RTPO INT NOT NULL DEFAULT 7,
		PRINCIPAL NUMERIC(1) NOT NULL DEFAULT 0)

	ALTER TABLE POLITICA_COMERCIAL_ECOMMERCE ADD CONSTRAINT PK_POLITICA_COMERCIAL_ECOMMERCE PRIMARY KEY (ID)

	CREATE INDEX POLITICA_COMERCIAL_ECOMMERCE_IDX1 ON POLITICA_COMERCIAL_ECOMMERCE(RPLATAFORMA)
end
go


IF NOT EXISTS (SELECT 1 FROM SYSOBJECTS T WHERE T.NAME = 'PRODUTO_POLITICA_ECOMMERCE' AND T.XTYPE = 'U')
begin
	CREATE TABLE dbo.PRODUTO_POLITICA_ECOMMERCE(
		ID INT IDENTITY NOT NULL,
		IDPRODUTOECOMMERCE INT NOT NULL,
		IDPOLITICAECOMMERCE INT NOT NULL)

	ALTER TABLE PRODUTO_POLITICA_ECOMMERCE ADD CONSTRAINT PK_PRODUTO_POLITICA_ECOMMERCE PRIMARY KEY (ID)

	CREATE INDEX PRODUTO_POLITICA_ECOMMERCE_IDX1 ON PRODUTO_POLITICA_ECOMMERCE(IDPRODUTOECOMMERCE)
end
go


IF NOT EXISTS (SELECT 1 FROM SYSOBJECTS T WHERE T.NAME = 'ARMAZEM_SKU_ECOMMERCE' AND T.XTYPE = 'U')
begin
	CREATE TABLE dbo.ARMAZEM_SKU_ECOMMERCE(
		ID INT IDENTITY NOT NULL,
		IDARMAZEMECOMMERCE INT NOT NULL,
		IDSKUPRODUTOECOMMERCE INT NOT NULL)

	ALTER TABLE ARMAZEM_SKU_ECOMMERCE ADD CONSTRAINT PK_ARMAZEM_SKU_ECOMMERCE PRIMARY KEY (ID)

	CREATE INDEX ARMAZEM_SKU_ECOMMERCE_IDX1 ON ARMAZEM_SKU_ECOMMERCE(IDARMAZEMECOMMERCE)
end
go

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'PRECO_ECOMMERCE' AND COLUMN_NAME = 'PRECOERP')
begin
ALTER TABLE PRECO_ECOMMERCE ADD PRECOERP DECIMAL(15,2) NOT NULL DEFAULT 0
EXEC('UPDATE PRECO_ECOMMERCE SET PRECOERP = PRECO WHERE PRECOERP = 0')
end
go

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'PRECO_ECOMMERCE' AND COLUMN_NAME = 'PRECODELISTAERP')
begin
ALTER TABLE PRECO_ECOMMERCE ADD PRECODELISTAERP DECIMAL(15,2) NOT NULL DEFAULT 0
EXEC('UPDATE PRECO_ECOMMERCE SET PRECODELISTAERP = PRECODELISTA WHERE PRECODELISTAERP = 0')
end
go

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'PRECO_ECOMMERCE' AND COLUMN_NAME = 'IDPOLITICAECOMMERCE')
begin
ALTER TABLE PRECO_ECOMMERCE ADD IDPOLITICAECOMMERCE INT NOT NULL DEFAULT 0

if exists(SELECT 1 FROM sys.indexes i
	WHERE name = 'PRECO_ECOMMERCE_IDX2' and type = 2 and object_id = object_id('PRECO_ECOMMERCE'))
begin
drop index PRECO_ECOMMERCE.PRECO_ECOMMERCE_IDX2
end
CREATE UNIQUE INDEX PRECO_ECOMMERCE_IDX2 ON PRECO_ECOMMERCE(RSKUECOMMERCE, RTABELAPRECO, IDPOLITICAECOMMERCE)

end
GO

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'PEDIDO_ECOMMERCE' AND COLUMN_NAME = 'CODIGOPOLITICACOMERCIAL')
ALTER TABLE PEDIDO_ECOMMERCE ADD CODIGOPOLITICACOMERCIAL VARCHAR(20)
go

IF EXISTS (SELECT 1 FROM SYSOBJECTS T WHERE T.NAME = 'MULOGPEDIDOSITE' AND T.XTYPE = 'U') AND
	NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MULOGPEDIDOSITE' AND COLUMN_NAME = 'IDPOLITICACOMERCIAL')
ALTER TABLE MULOGPEDIDOSITE ADD IDPOLITICACOMERCIAL VARCHAR(20)
go

IF EXISTS (SELECT 1 FROM SYSOBJECTS T WHERE T.NAME = 'MULOGERROSITE' AND T.XTYPE = 'U') AND
	NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MULOGERROSITE' AND COLUMN_NAME = 'IDPOLITICACOMERCIAL')
ALTER TABLE MULOGERROSITE ADD IDPOLITICACOMERCIAL VARCHAR(20)
go

IF EXISTS (SELECT 1 FROM SYSOBJECTS T WHERE T.NAME = 'MULOGPRECOECOMMERCE' AND T.XTYPE = 'U') AND
	NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MULOGPRECOECOMMERCE' AND COLUMN_NAME = 'IDPOLITICACOMERCIAL')
ALTER TABLE MULOGPRECOECOMMERCE ADD IDPOLITICACOMERCIAL VARCHAR(20)
go


-- Corrigir um par metro criado errado
update DADOADICIONAL SET INFORMACAOMU = 'PA 362643' 
where OID = 39250 AND INFORMACAOMU != 'PA 362643'
go
update ITEM SET OBSERVACAO = 'Se o par metro estiver como S, ao importar o pedido n o vai atualizar esses dados do cliente.' 
where OID = 39251 AND OBSERVACAO LIKE '%como N,%'
go
