ALTER TABLE SOLICADIANTAMENTO DROP CONSTRAINT PK_CDTIPODESPESAS
/
ALTER TABLE SOLICADIANTAMENTO ADD NRSOLICADIANTA NUMBER NULL
/
CREATE SEQUENCE SEQ_SOLICADIANTAMENTO START WITH 1 MINVALUE 1 INCREMENT BY 1
/
BEGIN
   FOR C_LOOP IN (SELECT * FROM SOLICADIANTAMENTO WHERE NRSOLICADIANTA IS NULL)  LOOP
       UPDATE SOLICADIANTAMENTO
          SET NRSOLICADIANTA = SEQ_SOLICADIANTAMENTO.NEXTVAL
        WHERE CDTIPODESPESAS = C_LOOP.CDTIPODESPESAS;
        COMMIT;
   END LOOP;                
END;
/
ALTER TABLE SOLICADIANTAMENTO MODIFY NRSOLICADIANTA NOT NULL 
/
ALTER TABLE SOLICADIANTAMENTO ADD CONSTRAINT PK_SOLICADIANTAMENTO PRIMARY KEY (NRSOLICADIANTA)
/
ALTER TABLE SOLICADIANTAMENTO ADD CDFILIALNFPAG VARCHAR2(04) NULL
/
ALTER TABLE SOLICADIANTAMENTO ADD NRLANCTONFPAG VARCHAR2(20) NULL
/
ALTER TABLE SOLICADIANTAMENTO MODIFY CDCENTCUST NULL 
/
ALTER TABLE SOLICADIANTAMENTO ADD CONSTRAINT FKFIN00409NOTFISCL_SOLCDINTMNT FOREIGN KEY (CDFILIALNFPAG,NRLANCTONFPAG)
    REFERENCES NOTAFISCAL (CDFILIAL, NRLANCTONF)
/

CREATE INDEX FIN00409_FK ON SOLICADIANTAMENTO (CDFILIALNFPAG ASC,NRLANCTONFPAG ASC)
/
ALTER TABLE SOLICADIANTAMENTO ADD IDSTATUS VARCHAR2(1) NULL
/
ALTER TABLE SOLICADIANTAMENTO ADD DSRECUSADIANTA VARCHAR2(200) NULL
/
ALTER TABLE SOLICADIANTAMENTO ADD CDEMPRESA VARCHAR2(2) NULL
/
ALTER TABLE SOLICADIANTAMENTO MODIFY CDTIPODESPESAS NULL
/
ALTER TABLE SOLICADIANTAMENTO MODIFY VRLIMITE NULL
/

CREATE TABLE DESPADIANTA
( 
    NRDESPADIANTA     INTEGER         NOT NULL,
    NRSOLICADIANTA    INTEGER         NOT NULL, 
    CDTIPODESP        NUMBER          NOT NULL,
    DTDESPESA         DATE            NOT NULL,
    DSDESPESA         VARCHAR2(200)   NOT NULL,
    VRDESPESA         FLOAT           NULL,
    CDCENTCUST        VARCHAR2(04)    NULL,
    CDFILIALNFPAG     VARCHAR2(04)    NULL,
    NRLANCTONFPAG     VARCHAR2(20)    NULL,
    NRORG             NUMBER 	      DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') 		NOT NULL,
    IDATIVO           VARCHAR2(1)     DEFAULT 'S'					NOT NULL,
    DTINCLUSAO        DATE            DEFAULT SYSDATE,
    DTULTATU          DATE            DEFAULT SYSDATE,
    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 CKC_IDATIVO_DESPADIANTA CHECK (IDATIVO IN ('S','N')),
	  CONSTRAINT PK_DESPADIANTA PRIMARY KEY (NRDESPADIANTA),
	  CONSTRAINT FKFIN00410SOLICDNTMNT_DSPDIANT FOREIGN KEY (NRSOLICADIANTA)
		REFERENCES SOLICADIANTAMENTO (NRSOLICADIANTA),
	  CONSTRAINT FKFIN00411TIPDESPESAS_DSPDIANT FOREIGN KEY (CDTIPODESP)
		REFERENCES TIPODESPESAS(CDTIPODESP),
	  CONSTRAINT FKFIN00412NOTAFISCAL_DSPDIANT FOREIGN KEY (CDFILIALNFPAG, NRLANCTONFPAG)
		REFERENCES NOTAFISCAL (CDFILIAL, NRLANCTONF),
	  CONSTRAINT FKFIN00413CENTCUST_DESPADIANTA FOREIGN KEY (CDCENTCUST)
		REFERENCES CENTCUST (CDCENTCUST)
)
/
CREATE INDEX FIN00410_FK ON DESPADIANTA (NRSOLICADIANTA ASC)
/
CREATE INDEX FIN00411_FK ON DESPADIANTA (CDTIPODESP ASC)
/
CREATE INDEX FIN00412_FK ON DESPADIANTA (CDFILIALNFPAG ASC, NRLANCTONFPAG ASC)
/
CREATE INDEX FIN00413_FK ON DESPADIANTA (CDCENTCUST ASC)
/
CREATE SEQUENCE SEQ_DESPADIANTA START WITH 1 MINVALUE 1 INCREMENT BY 1
/
