CREATE TABLE CARGOVENDAS
(
    CDCARGO        	VARCHAR2(3) 	NOT NULL,
    NMCARGO        	VARCHAR2(255) 	NOT NULL,
    NRORG 	   	NUMBER 		DEFAULT 	SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    IDATIVO	   	VARCHAR2(1)	DEFAULT 'S' 	NOT NULL,
	CONSTRAINT CKC_IDATIVO_CARGOVENDAS CHECK (IDATIVO IN ('S', 'N')),          
    DTINCLUSAO      	DATE           	NULL,
    DTULTATU        	DATE           	NULL,
    NRORGINCLUSAO   	NUMBER         	DEFAULT 1	NULL,
    CDOPERINCLUSAO  	VARCHAR2(12)   	DEFAULT '1'	NULL,
    NRORGULTATU     	NUMBER         	DEFAULT 1	NULL,
    CDOPERULTATU    	VARCHAR2(12)   	DEFAULT '1'	NULL,    
	CONSTRAINT PK_CARGOVENDAS PRIMARY KEY (CDCARGO)
)
/

CREATE TABLE CARGOVENDASOPERADOR
(
    CDCARGO        	VARCHAR2(3)  	NOT NULL,
    CDOPERADOR     	VARCHAR2(12)	NOT NULL,
    NRORG 	       	NUMBER 		DEFAULT 	SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    IDATIVO		VARCHAR2(1)	DEFAULT 'S' 	NOT NULL,
	CONSTRAINT CKC_IDATIVO_CARGOVENDASOPER CHECK (IDATIVO IN ('S', 'N')),          
    DTINCLUSAO      	DATE           	NULL,
    DTULTATU        	DATE           	NULL,
    NRORGINCLUSAO   	NUMBER         	DEFAULT 	NULL,
    CDOPERINCLUSAO  	VARCHAR2(12)   	DEFAULT '1'	NULL,
    NRORGULTATU     	NUMBER         	DEFAULT 	NULL,
    CDOPERULTATU    	VARCHAR2(12)   	DEFAULT '1'	NULL,    
	CONSTRAINT PK_CARGOVEN PRIMARY KEY (CDCARGO, CDOPERADOR)
)
/

ALTER TABLE CARGOVENDASOPERADOR 
	 ADD CONSTRAINT FKVND00735CARGOVEN FOREIGN KEY (CDCARGO)
		REFERENCES CARGOVENDAS(CDCARGO)
/

ALTER TABLE CARGOVENDASOPERADOR 
	 ADD CONSTRAINT FKVND00736CARGOVEN FOREIGN KEY (CDOPERADOR)
		REFERENCES OPERADOR(CDOPERADOR)
/

CREATE TABLE CARGOVENDASFILIAL
(
    CDCARGO        	VARCHAR2(3) 	NOT NULL,
    CDFILIAL       	VARCHAR2(4) 	NOT NULL,
    CDOPERADOR     	VARCHAR2(12) 	NOT NULL,
    NRORG 	   	NUMBER 		DEFAULT 	SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    IDATIVO	   	VARCHAR2(1)	DEFAULT 'S' 	NOT NULL,
		CONSTRAINT CKC_IDATIVO_CARGOVENDASFILI CHECK (IDATIVO IN ('S', 'N')),          
    DTINCLUSAO      	DATE           	NULL,
    DTULTATU        	DATE           	NULL,
    NRORGINCLUSAO   	NUMBER         	DEFAULT 	NULL,
    CDOPERINCLUSAO  	VARCHAR2(12)   	DEFAULT '1'	NULL,
    NRORGULTATU     	NUMBER         	DEFAULT 	NULL,
    CDOPERULTATU    	VARCHAR2(12)   	DEFAULT '1'	NULL,    
		CONSTRAINT PK_CARGOVENDASFILIAL PRIMARY KEY (CDCARGO, CDOPERADOR, CDFILIAL)
)
/

ALTER TABLE CARGOVENDASFILIAL
	 ADD CONSTRAINT FKVND00737CARGOVENDASFILIAL FOREIGN KEY (CDCARGO, CDOPERADOR)
		REFERENCES CARGOVENDASOPERADOR (CDCARGO, CDOPERADOR)
/

ALTER TABLE CARGOVENDASFILIAL
	 ADD CONSTRAINT FKVND00738CARGOVENDASFILIAL FOREIGN KEY (CDFILIAL)
		REFERENCES FILIAL(CDFILIAL)
/

CREATE TABLE TIPOLOJA
(
	CDTIPOLOJA     		VARCHAR2(20),
    	NMTIPOLOJA     		VARCHAR2(40),
	NRORG 	       		NUMBER 		DEFAULT 	SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
	IDATIVO	       		VARCHAR2(1)	DEFAULT 'S' 	NOT NULL,
		CONSTRAINT CKC_IDATIVO_TIPOLOJA CHECK (IDATIVO IN ('S', 'N')),          
	DTINCLUSAO      	DATE           	NULL,
	DTULTATU        	DATE           	NULL,
	NRORGINCLUSAO   	NUMBER         	DEFAULT 	NULL,
	CDOPERINCLUSAO  	VARCHAR2(12)   	DEFAULT '1'	NULL,
	NRORGULTATU     	NUMBER         	DEFAULT 	NULL,
	CDOPERULTATU    	VARCHAR2(12)   	DEFAULT '1'	NULL,    
		CONSTRAINT PK_TIPOLOJA PRIMARY KEY (CDTIPOLOJA)
)
/

CREATE TABLE FILILOJATIPO
(
	CDTIPOLOJA     		VARCHAR2(20),
    	CDLOJA         		VARCHAR2(10),
    	CDFILIAL       		VARCHAR2(4),
	NRORG 	       		NUMBER 		DEFAULT 	SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
	IDATIVO			VARCHAR2(1)	DEFAULT 'S' 	NOT NULL,
		CONSTRAINT CKC_IDATIVO_FILILOJATIPO CHECK (IDATIVO IN ('S', 'N')),          
	DTINCLUSAO      	DATE           	NULL,
	DTULTATU        	DATE           	NULL,
	NRORGINCLUSAO   	NUMBER         	DEFAULT 		NULL,
	CDOPERINCLUSAO  	VARCHAR2(12)   	DEFAULT '1'		NULL,
	NRORGULTATU     	NUMBER         	DEFAULT 		NULL,
	CDOPERULTATU    	VARCHAR2(12)   	DEFAULT '1'		NULL,    
		CONSTRAINT PK_FILILOJATIPO PRIMARY KEY (CDFILIAL, CDLOJA)
)
/
	
ALTER TABLE FILILOJATIPO
	 ADD CONSTRAINT FKVND00739FILILOJATIPO FOREIGN KEY (CDFILIAL, CDLOJA)
		REFERENCES LOJA(CDFILIAL, CDLOJA)
/

ALTER TABLE FILILOJATIPO
	 ADD CONSTRAINT FKVND00740FILILOJATIPO FOREIGN KEY (CDTIPOLOJA)
		REFERENCES TIPOLOJA(CDTIPOLOJA)
/