
/*------ ATD 368286 ------*/
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'PLATAFORMA_ECOMMERCE' and column_name = 'REGRAATUALIZACAOPRECO')
	ALTER TABLE PLATAFORMA_ECOMMERCE ADD REGRAATUALIZACAOPRECO NUMERIC(1) NOT NULL DEFAULT 1
go

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'OFERTASEST' and column_name = 'PERMITIDOECOMMERCE')
	ALTER TABLE OFERTASEST ADD PERMITIDOECOMMERCE NUMERIC(1) NOT NULL DEFAULT 0
go
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'PROMOCAO' and column_name = 'PERMITIDOECOMMERCE')
	ALTER TABLE PROMOCAO ADD PERMITIDOECOMMERCE NUMERIC(1) NOT NULL DEFAULT 0
go

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'PROMOCAO_R' and column_name = 'PERMITIDOECOMMERCE')
   AND EXISTS (SELECT 1 FROM SYSOBJECTS T WHERE T.NAME = 'PROMOCAO_R' AND T.XTYPE = 'V')
	drop view PROMOCAO_R
go
IF NOT EXISTS (SELECT 1 FROM SYSOBJECTS T WHERE T.NAME = 'PROMOCAO_R' AND T.XTYPE = 'V')
	EXECUTE('create view PROMOCAO_R (
		COR,
		INICIO,
		TERMINO,
		VALORMINIMO,
		DESCONTOGERENTE,
		DESCONTOVENDEDOR,
		NUMEROPARCELAS,
		CARENCIA,
		FATORPROMOCAO,
		TAXAPROMOCAO,
		BRINDEUNICO,
		CONDICAOPARCELA,
		CONDICAOCARENCIA,
		FATORENTRADA,
		ENTRADA,
		PERMITIDOECOMMERCE,
		RTIPOPROMOCAO,
		RTIPOPLANO,
		RTIPOPRODUTO,
		CRIADOEM,
		CID,
		ATUALIZADOEM,
		CODIGO,
		OBSERVACAO,
		NOME,
		OID,
		RESCOPO,
		CRIADOPOR,
		ATUALIZADOPOR)
	as select
		PROMOCAO.COR,
		PROMOCAO.INICIO,
		PROMOCAO.TERMINO,
		PROMOCAO.VALORMINIMO,
		PROMOCAO.DESCONTOGERENTE,
		PROMOCAO.DESCONTOVENDEDOR,
		PROMOCAO.NUMEROPARCELAS,
		PROMOCAO.CARENCIA,
		PROMOCAO.FATORPROMOCAO,
		PROMOCAO.TAXAPROMOCAO,
		PROMOCAO.BRINDEUNICO,
		PROMOCAO.CONDICAOPARCELA,
		PROMOCAO.CONDICAOCARENCIA,
		PROMOCAO.FATORENTRADA,
		PROMOCAO.ENTRADA,
		PROMOCAO.PERMITIDOECOMMERCE,
		PROMOCAO.RTIPOPROMOCAO,
		PROMOCAO.RTIPOPLANO,
		PROMOCAO.RTIPOPRODUTO,
		ITEM.CRIADOEM,
		ITEM.CID,
		ITEM.ATUALIZADOEM,
		ITEM.CODIGO,
		ITEM.OBSERVACAO,
		ITEM.NOME,
		ITEM.OID,
		ITEM.RESCOPO,
		ITEM.CRIADOPOR,
		ITEM.ATUALIZADOPOR
	from
		PROMOCAO,ITEM
	where (ITEM.EXCLUIDO = 0) AND
		PROMOCAO.OID = ITEM.OID')
go

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'PROMOCAO_V' and column_name = 'PERMITIDOECOMMERCE')
   AND EXISTS (SELECT 1 FROM SYSOBJECTS T WHERE T.NAME = 'PROMOCAO_V' AND T.XTYPE = 'V')
	drop view PROMOCAO_V
go
IF NOT EXISTS (SELECT 1 FROM SYSOBJECTS T WHERE T.NAME = 'PROMOCAO_V' AND T.XTYPE = 'V')
	EXECUTE('create view PROMOCAO_V (
		RTIPOPROMOCAO,
		RTIPOPLANO,
		RTIPOPRODUTO,
		COR,
		INICIO,
		TERMINO,
		VALORMINIMO,
		DESCONTOGERENTE,
		DESCONTOVENDEDOR,
		NUMEROPARCELAS,
		CARENCIA,
		FATORPROMOCAO,
		TAXAPROMOCAO,
		BRINDEUNICO,
		CONDICAOPARCELA,
		CONDICAOCARENCIA,
		FATORENTRADA,
		ENTRADA,
		PERMITIDOECOMMERCE,
		RESCOPO,
		CRIADOEM,
		CID,
		ATUALIZADOEM,
		CODIGO,
		OBSERVACAO,
		NOME,
		OID,
		NTIPOPROMOCAO,
		NTIPOPLANO,
		NTIPOPRODUTO,
		NESCOPO
	) as select
		PROMOCAO.RTIPOPROMOCAO,
		PROMOCAO.RTIPOPLANO,
		PROMOCAO.RTIPOPRODUTO,
		PROMOCAO.COR,
		PROMOCAO.INICIO,
		PROMOCAO.TERMINO,
		PROMOCAO.VALORMINIMO,
		PROMOCAO.DESCONTOGERENTE,
		PROMOCAO.DESCONTOVENDEDOR,
		PROMOCAO.NUMEROPARCELAS,
		PROMOCAO.CARENCIA,
		PROMOCAO.FATORPROMOCAO,
		PROMOCAO.TAXAPROMOCAO,
		PROMOCAO.BRINDEUNICO,
		PROMOCAO.CONDICAOPARCELA,
		PROMOCAO.CONDICAOCARENCIA,
		PROMOCAO.FATORENTRADA,
		PROMOCAO.ENTRADA,
		PROMOCAO.PERMITIDOECOMMERCE,
		ITEM.RESCOPO,
		ITEM.CRIADOEM,
		ITEM.CID,
		ITEM.ATUALIZADOEM,
		ITEM.CODIGO,
		ITEM.OBSERVACAO,
		ITEM.NOME,
		ITEM.OID,
		T1.NOME,
		T2.NOME,
		T3.NOME,
		T4.NOME
	from
		PROMOCAO,ITEM,ITEM T1,ITEM T2,ITEM T3,ITEM T4
	where (ITEM.EXCLUIDO = 0) AND
		PROMOCAO.OID = ITEM.OID AND
		PROMOCAO.RTIPOPROMOCAO = T1.OID AND
		PROMOCAO.RTIPOPLANO = T2.OID AND
		PROMOCAO.RTIPOPRODUTO = T3.OID AND
		ITEM.RESCOPO = T4.OID')
go