CREATE TABLE CRM_GRUPORESPADRAO
(
    NRGRUPORESPADRAO  		NUMBER                 		NOT NULL,
    NMGRUPORESPADRAO  		VARCHAR2(200)         	NOT NULL,
    NRORG             			NUMBER                 		DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    DTINCLUSAO        		DATE                   		DEFAULT SYSDATE NOT NULL,
    DTULTATU          			DATE                   		NULL    ,
    NRORGINCLUSAO     		NUMBER                 		DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    CDOPERINCLUSAO    		VARCHAR2(12)           	DEFAULT '1' NOT NULL,
    NRORGULTATU       		NUMBER                 		DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    CDOPERULTATU      		VARCHAR2(12)           	DEFAULT '1' NOT NULL,
    IDATIVO           			VARCHAR2(1)            	DEFAULT 'S' NOT NULL
	CONSTRAINT CKC_IDATIVO_CRM_GRPRESPPAD CHECK (IDATIVO IN ('S','N')),
	CONSTRAINT CRM_GRUPORESPADRAO_PK PRIMARY KEY (NRGRUPORESPADRAO)
)
/
CREATE TABLE CRM_RESPOSTAPADRAO
(
    NRGRUPORESPADRAO  	NUMBER                 		NOT NULL,
    NRRESPOSTAPADRAO  	NUMBER                 		NOT NULL,
    NMRESPOSTAPADRAO  	VARCHAR2(200)          	NOT NULL,
    TXRESPOSTAPADRAO  	CLOB                   		NOT NULL,
    NRORG             		NUMBER                 		DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    DTINCLUSAO        	DATE                   		DEFAULT SYSDATE NOT NULL,
    DTULTATU          		DATE                   		NULL    ,
    NRORGINCLUSAO     	NUMBER                 		DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    CDOPERINCLUSAO    	VARCHAR2(12)           	DEFAULT '1' NOT NULL,
    NRORGULTATU       	NUMBER                 		DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    CDOPERULTATU      	VARCHAR2(12)           	DEFAULT '1' NOT NULL,
    IDATIVO           		VARCHAR2(1)            	DEFAULT 'S' NOT NULL
	CONSTRAINT CKC_IDATIVO_RESPPAD CHECK (IDATIVO IN ('S','N')),
    DSPATHANEXO       	VARCHAR2(2000)         	NOT NULL,
	CONSTRAINT CRM_RESPOSTAPADRAO_PK PRIMARY KEY (NRGRUPORESPADRAO, NRRESPOSTAPADRAO)
)
/
CREATE TABLE CRM_OPERADORRESPADRAO
(
    NRGRUPORESPADRAO  	NUMBER                		NOT NULL,
    NRRESPOSTAPADRAO  	NUMBER                 		NOT NULL,
    CDOPERADOR        	VARCHAR2(12)           	NOT NULL,
    NRORG             		NUMBER                 		DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    DTINCLUSAO        	DATE                   		DEFAULT SYSDATE NOT NULL,
    DTULTATU          		DATE                   		NULL    ,
    NRORGINCLUSAO     	NUMBER                 		DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    CDOPERINCLUSAO   	VARCHAR2(12)           	DEFAULT '1' NOT NULL,
    NRORGULTATU       	NUMBER                 		DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    CDOPERULTATU      	VARCHAR2(12)           	DEFAULT '1' NOT NULL,
    IDATIVO           		VARCHAR2(1)            	DEFAULT 'S' NOT NULL
	CONSTRAINT CKC_IDATIVO_OPERPADRAO CHECK (IDATIVO IN ('S','N')),
	CONSTRAINT CRM_OPERADORRESPADRAO_PK PRIMARY KEY (NRGRUPORESPADRAO, NRRESPOSTAPADRAO, CDOPERADOR)
)
/
CREATE INDEX CRM00056_FK ON CRM_RESPOSTAPADRAO (NRGRUPORESPADRAO ASC)
/
ALTER TABLE CRM_RESPOSTAPADRAO
    ADD CONSTRAINT FKCRM00056GRUPOPADRAO FOREIGN KEY  (NRGRUPORESPADRAO)
       REFERENCES CRM_GRUPORESPADRAO (NRGRUPORESPADRAO)
/
CREATE INDEX CRM00057_FK ON CRM_OPERADORRESPADRAO (NRGRUPORESPADRAO ASC, NRRESPOSTAPADRAO ASC)
/
ALTER TABLE CRM_OPERADORRESPADRAO
    ADD CONSTRAINT FKCRM00057_RESPOSTAPADRAO FOREIGN KEY  (NRGRUPORESPADRAO, NRRESPOSTAPADRAO)
       REFERENCES CRM_RESPOSTAPADRAO (NRGRUPORESPADRAO, NRRESPOSTAPADRAO)
/
ALTER TABLE CRM_RESPOSTAPADRAO ADD NMANEXO VARCHAR2(100) NULL
/
ALTER TABLE EVOLCONT ADD IDEVOLINTERNA VARCHAR2(1) DEFAULT 'S' NOT NULL
/
CREATE TABLE CRM_REGRADISTRIBUIDOR
(
    NRREGRADISTRIB     	NUMBER                 		NOT NULL,
    NMREGRADISTRIB     	VARCHAR2(200)          	NOT NULL,
    NRORDEM            	NUMBER                 		NOT NULL,
    DSREGRADISTIB      	VARCHAR2(2000)         	NULL    ,
    IDTIPOCOMBINACAO   	VARCHAR2(23)           	NOT NULL,
        CONSTRAINT CKC_IDTIPOCOM_REGDIST    CHECK (IDTIPOCOMBINACAO IN ('AND','OR')),
    NRORG              		NUMBER                 		DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    DTINCLUSAO         	DATE                   		DEFAULT SYSDATE NOT NULL,
    DTULTATU           		DATE                   		NULL    ,
    NRORGINCLUSAO      	NUMBER                 		DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    CDOPERINCLUSAO     	VARCHAR2(12)           	DEFAULT '1' NOT NULL,
    NRORGULTATU        	NUMBER                 		DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    CDOPERULTATU       	VARCHAR2(12)           	DEFAULT '1' NOT NULL,
    IDATIVO            		VARCHAR2(1)            	DEFAULT 'S' NOT NULL
	CONSTRAINT CKC_IDATIVO_REGDIST CHECK (IDATIVO IN ('S','N')),
	CONSTRAINT CRM_REGRADISTRIBUIDOR_PK PRIMARY KEY (NRREGRADISTRIB)
)
/
CREATE TABLE CRM_CONDICAODISTRIB
(
    NRREGRADISTRIB     	NUMBER                 		NOT NULL,
    NRCONDICAODISTRIB  	NUMBER                 		NOT NULL,
    NMCONDICAODISTRIB  	VARCHAR2(1000)         	NOT NULL,
    IDOPERCONDICAO     	NUMBER                 		NOT NULL
        CONSTRAINT CKC_IDOPERCOND_CONDDIST CHECK (IDOPERCONDICAO IN (1,2,3,4,5,6,7,8,9)),
    DSINFORMACAO       	CLOB                   		NOT NULL,
    NRORG              		NUMBER                 		DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    DTINCLUSAO         	DATE                   		DEFAULT SYSDATE NOT NULL,
    DTULTATU           		DATE                   		NULL    ,
    NRORGINCLUSAO      	NUMBER                		DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    CDOPERINCLUSAO     	VARCHAR2(12)           	DEFAULT '1' NOT NULL,
    NRORGULTATU        	NUMBER                 		DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    CDOPERULTATU       	VARCHAR2(12)           	DEFAULT '1' NOT NULL,
    TXINFORMACAO        	CLOB                  		NULL,
    IDATIVO           		VARCHAR2(1)            	DEFAULT 'S' NOT NULL
	CONSTRAINT CKC_IDATIVO_CONDDIST CHECK (IDATIVO IN ('S','N')),
	CONSTRAINT CRM_CONDICAODISTRIB_PK PRIMARY KEY (NRREGRADISTRIB, NRCONDICAODISTRIB)
)
/
CREATE TABLE CRM_ACAODISTRIB
(
    NRREGRADISTRIB     	NUMBER                 		NOT NULL,
    ACAODISTRIB        	NUMBER                 		NOT NULL,
    NRGRUPORESPADRAO   	NUMBER                 		NULL    ,
    NRRESPOSTAPADRAO   	NUMBER                 		NULL    ,
    NMACAODISTRIB      	VARCHAR2(1000)         	NOT NULL,
    DSACAODISTRIB      	CLOB                   		NOT NULL,
    NRORG              		NUMBER                 		DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    DTINCLUSAO         	DATE                   		DEFAULT SYSDATE NOT NULL,
    DTULTATU           		DATE                   		NULL    ,
    NRORGINCLUSAO      	NUMBER                 		DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    CDOPERINCLUSAO     	VARCHAR2(12)           	DEFAULT '1' NOT NULL,
    NRORGULTATU        	NUMBER                 		DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    CDOPERULTATU       	VARCHAR2(12)           	DEFAULT '1' NOT NULL,
    TXINFORMACAO        	CLOB                  		NULL,
    IDATIVO            		VARCHAR2(1)            	DEFAULT 'S' NOT NULL
	CONSTRAINT CKC_IDATIVO_CRM_ACAODIST CHECK (IDATIVO IN ('S','N')),
	CONSTRAINT CRM_ACAODISTRIB_PK PRIMARY KEY (NRREGRADISTRIB, ACAODISTRIB)
)
/
CREATE INDEX CRM00058_FK ON CRM_CONDICAODISTRIB (NRREGRADISTRIB ASC)
/
ALTER TABLE CRM_CONDICAODISTRIB
    ADD CONSTRAINT FKCRM00058_CRM_REGDIST FOREIGN KEY  (NRREGRADISTRIB)
       REFERENCES CRM_REGRADISTRIBUIDOR (NRREGRADISTRIB)
/
CREATE INDEX CRM00059_FK ON CRM_ACAODISTRIB (NRREGRADISTRIB ASC)
/
ALTER TABLE CRM_ACAODISTRIB
    ADD CONSTRAINT FKCRM00059_REGDIST FOREIGN KEY  (NRREGRADISTRIB)
       REFERENCES CRM_REGRADISTRIBUIDOR (NRREGRADISTRIB)
/
CREATE INDEX CRM00060_FK ON CRM_ACAODISTRIB (NRGRUPORESPADRAO ASC, NRRESPOSTAPADRAO ASC)
/
ALTER TABLE CRM_ACAODISTRIB
    ADD CONSTRAINT FKCRM00060_RESPPAD FOREIGN KEY  (NRGRUPORESPADRAO, NRRESPOSTAPADRAO)
       REFERENCES CRM_RESPOSTAPADRAO (NRGRUPORESPADRAO, NRRESPOSTAPADRAO)