
/*------ ATD 428269 ------*/
DECLARE v_count NUMBER:=0;

BEGIN

SELECT Count(1) INTO v_count FROM USER_TABLES WHERE UPPER(table_name) = 'POLITICA_COMERCIAL_ECOMMERCE';

IF v_count = 0 THEN

EXECUTE IMMEDIATE 'CREATE TABLE POLITICA_COMERCIAL_ECOMMERCE(
		ID INT NOT NULL,
		RPLATAFORMA INT NOT NULL,
		CODIGO INT NOT NULL,
		NOME VARCHAR(100) NOT NULL,
		RAREADEVENDA INT NOT NULL,
		RFILIAL INT DEFAULT 7 NOT NULL,
		CODVEND CHAR(5) NULL,
		RTPO INT DEFAULT 7 NOT NULL,
		PRINCIPAL NUMERIC(1) DEFAULT 0 NOT NULL)';
EXECUTE IMMEDIATE 'ALTER TABLE POLITICA_COMERCIAL_ECOMMERCE ADD CONSTRAINT PK_POLITICA_COMERCIAL_ECOMMERCE PRIMARY KEY (ID)';
EXECUTE IMMEDIATE 'CREATE POLITICA_COMERCIAL_ECOMMERCE_IDX1 ON POLITICA_COMERCIAL_ECOMMERCE(RPLATAFORMA)';
EXECUTE IMMEDIATE 'CREATE SEQUENCE POLITICA_COMERCIAL_ECOMMERCE_SEQ MINVALUE 1 MAXVALUE 999999999999999999999999999 INCREMENT BY 1 NOCYCLE NOORDER' ;
EXECUTE IMMEDIATE 'CREATE OR REPLACE TRIGGER POLITICA_COMERCIAL_ECOMMERCE_I
BEFORE INSERT ON POLITICA_COMERCIAL_ECOMMERCE FOR EACH ROW
WHEN (new.ID IS NULL)
BEGIN
SELECT POLITICA_COMERCIAL_ECOMMERCE_SEQ.NEXTVAL INTO :new.ID FROM dual;
END;';

END IF;

SELECT Count(1) INTO v_count FROM USER_TABLES WHERE UPPER(table_name) = 'PRODUTO_POLITICA_ECOMMERCE';

IF v_count = 0 THEN

EXECUTE IMMEDIATE 'CREATE TABLE PRODUTO_POLITICA_ECOMMERCE(
		ID INT IDENTITY NOT NULL,
		IDPRODUTOECOMMERCE INT NOT NULL,
		IDPOLITICAECOMMERCE INT NOT NULL)';
EXECUTE IMMEDIATE 'ALTER TABLE PRODUTO_POLITICA_ECOMMERCE ADD CONSTRAINT PK_PRODUTO_POLITICA_ECOMMERCE PRIMARY KEY (ID)';
EXECUTE IMMEDIATE 'CREATE PRODUTO_POLITICA_ECOMMERCE_IDX1 ON PRODUTO_POLITICA_ECOMMERCE(IDPRODUTOECOMMERCE)';
EXECUTE IMMEDIATE 'CREATE SEQUENCE PRODUTO_POLITICA_ECOMMERCE_SEQ MINVALUE 1 MAXVALUE 999999999999999999999999999 INCREMENT BY 1 NOCYCLE NOORDER' ;
EXECUTE IMMEDIATE 'CREATE OR REPLACE TRIGGER PRODUTO_POLITICA_ECOMMERCE_I
BEFORE INSERT ON PRODUTO_POLITICA_ECOMMERCE FOR EACH ROW
WHEN (new.ID IS NULL)
BEGIN
SELECT PRODUTO_POLITICA_ECOMMERCE_SEQ.NEXTVAL INTO :new.ID FROM dual;
END;';

END IF;

SELECT Count(1) INTO v_count FROM USER_TABLES WHERE UPPER(table_name) = 'ARMAZEM_SKU_ECOMMERCE';

IF v_count = 0 THEN

EXECUTE IMMEDIATE 'CREATE TABLE ARMAZEM_SKU_ECOMMERCE(
		ID INT IDENTITY NOT NULL,
		IDARMAZEMECOMMERCE INT NOT NULL,
		IDSKUPRODUTOECOMMERCE INT NOT NULL)';
EXECUTE IMMEDIATE 'ALTER TABLE ARMAZEM_SKU_ECOMMERCE ADD CONSTRAINT PK_ARMAZEM_SKU_ECOMMERCE PRIMARY KEY (ID)';
EXECUTE IMMEDIATE 'CREATE ARMAZEM_SKU_ECOMMERCE_IDX1 ON PRODUTO_POLITICA_ECOMMERCE(IDARMAZEMECOMMERCE)';
EXECUTE IMMEDIATE 'CREATE SEQUENCE ARMAZEM_SKU_ECOMMERCE_SEQ MINVALUE 1 MAXVALUE 999999999999999999999999999 INCREMENT BY 1 NOCYCLE NOORDER' ;
EXECUTE IMMEDIATE 'CREATE OR REPLACE TRIGGER ARMAZEM_SKU_ECOMMERCE_I
BEFORE INSERT ON ARMAZEM_SKU_ECOMMERCE FOR EACH ROW
WHEN (new.ID IS NULL)
BEGIN
SELECT ARMAZEM_SKU_ECOMMERCE_SEQ.NEXTVAL INTO :new.ID FROM dual;
END;';

END IF;

SELECT Count(1) INTO v_count FROM USER_TAB_COLUMNS WHERE UPPER(table_name) = 'PRECO_ECOMMERCE' and UPPER(column_name) = 'PRECOERP';

IF v_count = 0 THEN
EXECUTE IMMEDIATE 'ALTER TABLE PRECO_ECOMMERCE ADD PRECOERP NUMBER(15,2) DEFAULT 0 NOT NULL';
ENDIF

SELECT Count(1) INTO v_count FROM USER_TAB_COLUMNS WHERE UPPER(table_name) = 'PRECO_ECOMMERCE' and UPPER(column_name) = 'PRECODELISTAERP';

IF v_count = 0 THEN
EXECUTE IMMEDIATE 'ALTER TABLE PRECO_ECOMMERCE ADD PRECODELISTAERP NUMBER(15,2) DEFAULT 0 NOT NULL';
ENDIF

SELECT Count(1) INTO v_count FROM USER_TAB_COLUMNS WHERE UPPER(table_name) = 'PRECO_ECOMMERCE' and UPPER(column_name) = 'IDPOLITICAECOMMERCE';

IF v_count = 0 THEN

EXECUTE IMMEDIATE 'ALTER TABLE PRECO_ECOMMERCE ADD IDPOLITICAECOMMERCE INT NOT NULL DEFAULT 0';

SELECT COUNT(1) INTO v_count FROM USER_INDEXES WHERE UPPER(INDEX_NAME) = 'PRECO_ECOMMERCE_IDX2';
IF v_count > 0 THEN
    EXECUTE IMMEDIATE 'DROP INDEX PRECO_ECOMMERCE_IDX2';
END IF;
EXECUTE IMMEDIATE 'CREATE UNIQUE INDEX PRECO_ECOMMERCE_IDX2 ON PRECO_ECOMMERCE(RSKUECOMMERCE, RTABELAPRECO, IDPOLITICAECOMMERCE)';

END IF;


SELECT Count(1) INTO v_count FROM USER_TAB_COLUMNS WHERE UPPER(table_name) = 'PEDIDO_ECOMMERCE' and UPPER(column_name) = 'CODIGOPOLITICACOMERCIAL';

IF v_count = 0 THEN
EXECUTE IMMEDIATE 'ALTER TABLE PEDIDO_ECOMMERCE ADD CODIGOPOLITICACOMERCIAL VARCHAR2(20)';
ENDIF


SELECT Count(1) INTO v_count FROM USER_TAB_COLUMNS WHERE UPPER(table_name) = 'MULOGPEDIDOSITE' and UPPER(column_name) = 'IDPOLITICACOMERCIAL';

IF v_count = 0 THEN
	SELECT Count(1) INTO v_count FROM USER_TABLES WHERE UPPER(table_name) = 'MULOGPEDIDOSITE';
	IF v_count > 0 THEN
		EXECUTE IMMEDIATE 'ALTER TABLE MULOGPEDIDOSITE ADD IDPOLITICACOMERCIAL VARCHAR2(20)';
	ENDIF
ENDIF

SELECT Count(1) INTO v_count FROM USER_TAB_COLUMNS WHERE UPPER(table_name) = 'MULOGERROSITE' and UPPER(column_name) = 'IDPOLITICACOMERCIAL';

IF v_count = 0 THEN
	SELECT Count(1) INTO v_count FROM USER_TABLES WHERE UPPER(table_name) = 'MULOGERROSITE';
	IF v_count > 0 THEN
		EXECUTE IMMEDIATE 'ALTER TABLE MULOGERROSITE ADD IDPOLITICACOMERCIAL VARCHAR2(20)';
	ENDIF
ENDIF

SELECT Count(1) INTO v_count FROM USER_TAB_COLUMNS WHERE UPPER(table_name) = 'MULOGPRECOECOMMERCE' and UPPER(column_name) = 'IDPOLITICACOMERCIAL';

IF v_count = 0 THEN
	SELECT Count(1) INTO v_count FROM USER_TABLES WHERE UPPER(table_name) = 'MULOGPRECOECOMMERCE';
	IF v_count > 0 THEN
		EXECUTE IMMEDIATE 'ALTER TABLE MULOGPRECOECOMMERCE ADD IDPOLITICACOMERCIAL VARCHAR2(20)';
	ENDIF
ENDIF


END;
/