-- ======================================================================
-- Table: PERFACESSCONTROL 
-- ======================================================================
CREATE TABLE PERFACESSCONTROL
(
   NRPERFACESSCONT 	NUMBER		NOT NULL,
   NRPERFILACESSO 	NUMBER 		NOT NULL,
   NRORG                       	NUMBER DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
   IDATIVO                    	VARCHAR2(1)     	DEFAULT 'S' NOT NULL
	CONSTRAINT CKC_IDATIVO_PERFACESSCONT CHECK (IDATIVO IN ('S','N')),
   DTINCLUSAO             	DATE                   	NULL,
   DTULTATU                  	DATE                  	NULL,
   NRORGINCLUSAO     	NUMBER 		DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg')  NULL,
   CDOPERINCLUSAO    	VARCHAR2(12)  	DEFAULT '1' NULL,
   NRORGULTATU          	NUMBER 		DEFAULT  SYS_CONTEXT('nrorg_ctx','nrorg') NULL,
   CDOPERULTATU        	VARCHAR2(12) 	DEFAULT '1',
	CONSTRAINT PK_PERFACESSCON PRIMARY KEY (NRPERFILACESSO, NRPERFACESSCONT)
)
/

CREATE INDEX VND00753_FK ON PERFACESSCONTROL (NRPERFACESSCONT ASC)
/

ALTER TABLE PERFACESSCONTROL ADD
	CONSTRAINT FKVND00753PERFILACESSO  FOREIGN KEY (NRPERFACESSCONT) 
		REFERENCES PERFILACESSO (NRPERFILACESSO) ON DELETE CASCADE
/

CREATE INDEX VND00754_FK ON PERFACESSCONTROL (NRPERFILACESSO ASC)
/

ALTER TABLE PERFACESSCONTROL ADD
	CONSTRAINT FKVND00754PERFILACESSO FOREIGN KEY (NRPERFILACESSO)
		REFERENCES PERFILACESSO (NRPERFILACESSO)
/

-- ======================================================================
-- Table: PERFACESSFILI
-- ======================================================================
CREATE TABLE PERFACESSFILI
(
   CDOPERADOR 		VARCHAR2(12) 	DEFAULT '1' NULL,
   CDFILIAL 		VARCHAR2(4), 
   NRPERFILACESSO 	NUMBER 		NOT NULL,
   NRPERFACESSPARC 	NUMBER 		NOT NULL,
   DTVALIDADE 		DATE,
   NRORG                       	NUMBER DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
   IDATIVO                    	VARCHAR2(1)     	DEFAULT 'S' NOT NULL
	CONSTRAINT CKC_IDATIVO_PERFACESSFILI CHECK (IDATIVO IN ('S','N')),
   DTINCLUSAO             	DATE                   	NULL,
   DTULTATU                  	DATE                  	NULL,
   NRORGINCLUSAO     	NUMBER 		DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg')  NULL,
   CDOPERINCLUSAO    	VARCHAR2(12)  	DEFAULT '1' NULL,
   NRORGULTATU          	NUMBER 		DEFAULT  SYS_CONTEXT('nrorg_ctx','nrorg') NULL,
   CDOPERULTATU        	VARCHAR2(12) 	DEFAULT '1',
	CONSTRAINT PK_PERFACESSFILI PRIMARY KEY (CDFILIAL, CDOPERADOR, NRPERFILACESSO)
)
/

CREATE INDEX VND00755_FK ON PERFACESSFILI (CDFILIAL ASC)
/

ALTER TABLE PERFACESSFILI ADD
	CONSTRAINT FKVND00755FILIAL FOREIGN KEY (CDFILIAL)
		REFERENCES FILIAL (CDFILIAL)
/

CREATE INDEX VND00756_FK ON PERFACESSFILI (CDOPERADOR ASC)
/

ALTER TABLE PERFACESSFILI ADD
	CONSTRAINT FKVND00756OPERADOR FOREIGN KEY (CDOPERADOR) 
		REFERENCES OPERADOR (CDOPERADOR)
/

CREATE INDEX VND00757_FK ON PERFACESSFILI (NRPERFILACESSO ASC)
/

ALTER TABLE PERFACESSFILI ADD
	CONSTRAINT FKVND00757PERFILACESSO FOREIGN KEY (NRPERFILACESSO) 
		REFERENCES PERFILACESSO (NRPERFILACESSO)
/

CREATE INDEX VND00758_FK ON PERFACESSFILI (CDOPERADOR ASC, CDFILIAL ASC)
/

ALTER TABLE PERFACESSFILI ADD
	CONSTRAINT FKVND00758FILIOPER FOREIGN KEY (CDOPERADOR, CDFILIAL) 
		REFERENCES FILIOPER (CDOPERADOR, CDFILIAL) ON DELETE CASCADE
/

-- ======================================================================
-- Table: PARAMGERAL 
-- ======================================================================
ALTER TABLE PARAMGERAL ADD IDUTLPERFILFILIAL VARCHAR2(1) DEFAULT 'N' NOT NULL
/

ALTER TABLE PARAMGERAL ADD CONSTRAINT CKC_IDUTLPERFILFILIAL_PARAMG CHECK (IDUTLPERFILFILIAL IN ('S','N'))
/

-- ======================================================================
-- Table: OPERADOR 
-- ======================================================================
ALTER TABLE OPERADOR ADD CDPERFILSELECIONADO NUMBER
/

CREATE INDEX VND00759_FK ON OPERADOR (CDPERFILSELECIONADO ASC)
/

ALTER TABLE OPERADOR ADD 
	CONSTRAINT FKVND00759PERFILACESSO FOREIGN KEY (CDPERFILSELECIONADO) 
		REFERENCES PERFILACESSO (NRPERFILACESSO)