CREATE TABLE DEVOLVENDAANTEC
(
	NRDEVOLVENDAANTEC 	NUMBER 			NOT NULL,
	CDFILIAL 			VARCHAR2(4) 	NOT NULL,
	CDCLIENTE 			VARCHAR2(21) 	NOT NULL,
	DTDEVOLUCAO 		DATE 			NOT NULL,
	CDSERVFILI 			VARCHAR2(5) 	NOT NULL,
	QTREFEICOES 		FLOAT 			NOT NULL,
	VRSERVICO 			FLOAT 			NOT NULL,
	IDENTRSAIDOP 		VARCHAR2(1) 	NOT NULL,
	CDTIPOOPER 			VARCHAR2(2) 	NOT NULL,
	NRAPURACFAT 		VARCHAR2(10) 	NOT NULL,
    NRORG 				NUMBER 			DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
	IDATIVO 			VARCHAR2(1) 	DEFAULT 'S' NOT NULL,
		CONSTRAINT CKC_IDATIVO_DEVOLVENDAANTEC 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_DEVOLVENDAANTEC PRIMARY KEY (NRDEVOLVENDAANTEC)
)
/
ALTER TABLE DEVOLVENDAANTEC
    ADD CONSTRAINT FKFAI00639CTRLAPURFAT FOREIGN KEY (CDFILIAL, NRAPURACFAT)
       REFERENCES CTRLAPURFAT (CDFILIAL, NRAPURACFAT)
/
CREATE INDEX FAI00639_FK ON DEVOLVENDAANTEC (CDFILIAL, NRAPURACFAT)
/
ALTER TABLE DEVOLVENDAANTEC
    ADD CONSTRAINT FKFAI00640SERVICO FOREIGN KEY (CDFILIAL, CDSERVFILI)
       REFERENCES SERVICO (CDFILIAL, CDSERVFILI)
/
CREATE INDEX FAI00640_FK ON DEVOLVENDAANTEC (CDFILIAL, CDSERVFILI)
/
ALTER TABLE DEVOLVENDAANTEC
    ADD CONSTRAINT FKFAI00641TIPOOPERAC FOREIGN KEY (IDENTRSAIDOP, CDTIPOOPER)
       REFERENCES TIPOOPERAC (IDENTRSAIDOP, CDTIPOOPER)
/
CREATE INDEX FAI00641_FK ON DEVOLVENDAANTEC (IDENTRSAIDOP, CDTIPOOPER)
/
ALTER TABLE DEVOLVENDAANTEC
    ADD CONSTRAINT FKFAI00642CLIENTE FOREIGN KEY (CDCLIENTE)
       REFERENCES CLIENTE (CDCLIENTE)
/
CREATE INDEX FAI00642_FK ON DEVOLVENDAANTEC (CDCLIENTE)
/
CREATE TABLE TIPOOPERDEVOLFAT
(
	NRTIPOOPERDEVOLFAT 	NUMBER 			NOT NULL,
	IDENTRSAIDOP 		VARCHAR2(1) 	NOT NULL,
	CDTIPOOPER 			VARCHAR2(2) 	NOT NULL,
    NRORG 				NUMBER 			DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
	IDATIVO 			VARCHAR2(1) 	DEFAULT 'S' NOT NULL,
		CONSTRAINT CKC_IDATIVO_TPOPERDEVOLFAT 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_TIPOOPERDEVOLFAT PRIMARY KEY (NRTIPOOPERDEVOLFAT)
)
/
ALTER TABLE TIPOOPERDEVOLFAT
    ADD CONSTRAINT FKFAI00643TIPOOPERAC FOREIGN KEY (IDENTRSAIDOP, CDTIPOOPER)
       REFERENCES TIPOOPERAC (IDENTRSAIDOP, CDTIPOOPER)
/
CREATE INDEX FAI00643_FK ON TIPOOPERDEVOLFAT (IDENTRSAIDOP, CDTIPOOPER)
/
ALTER TABLE CLIENFILIAL ADD CDFORNECED VARCHAR2(21) NULL
/
ALTER TABLE CLIENFILIAL
    ADD CONSTRAINT FKFAI00644FORNECEDOR FOREIGN KEY (CDFORNECED)
       REFERENCES FORNECEDOR (CDFORNECED)
/
CREATE INDEX FKFAI00644FORNECEDOR ON CLIENFILIAL (CDFORNECED)