CREATE TABLE AGRUPPEDENTREGA
(
    CDFILIAL            VARCHAR2(4)            NOT NULL,
    NRAGRUPPEDENT       VARCHAR2(10)           NOT NULL,
    DSAGRUPPEDENT       VARCHAR2(200)          NULL    ,
    DTENTREGAAGRUP      DATE                   NOT NULL, 
    IDSITAGRUPPEDENT    VARCHAR2(01)           NULL    ,   
    IDTPIJURTRAN        VARCHAR2(01)           NULL    ,
    NRINSJURTRAN        VARCHAR2(20)           NULL    ,
    CDVIATURA           VARCHAR2(4)            NULL    ,
    CDMOTORISTA         VARCHAR2(4)            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_AGRUPPEDENTREGA CHECK (IDATIVO IN ('S','N')),
		CONSTRAINT PK_AGRUPPEDENTREGA PRIMARY KEY (CDFILIAL, NRAGRUPPEDENT)
)
/

ALTER TABLE AGRUPPEDENTREGA
    ADD CONSTRAINT FKPED00463TRANSPOR FOREIGN KEY  (IDTPIJURTRAN, NRINSJURTRAN)
       REFERENCES TRANSPORT (IDTPIJURTRAN, NRINSJURTRAN)
/

CREATE INDEX PED00463_FK ON AGRUPPEDENTREGA(IDTPIJURTRAN ASC, NRINSJURTRAN ASC)
/
ALTER TABLE AGRUPPEDENTREGA
    ADD CONSTRAINT FKPED00464VIATURA FOREIGN KEY  (CDVIATURA)
       REFERENCES VIATURA (CDVIATURA)
/

CREATE INDEX PED00464_FK ON AGRUPPEDENTREGA(CDVIATURA ASC)
/

ALTER TABLE AGRUPPEDENTREGA
    ADD CONSTRAINT FKPED00465MOTORISTA FOREIGN KEY  (CDMOTORISTA)
       REFERENCES MOTORISTA (CDMOTORISTA)
/

CREATE INDEX PED00465_FK ON AGRUPPEDENTREGA(CDMOTORISTA ASC)
/

CREATE TABLE ITAGRUPPEDENTREGA
(
    CDFILIAL            VARCHAR2(4)         NOT NULL,
    NRAGRUPPEDENT       VARCHAR2(10)        NOT NULL,
    NRSEQPED            VARCHAR2(10)        NOT NULL, 
    CDFILIALPED         VARCHAR2(04)        NULL,
    NRPEDIDO            VARCHAR2(10)        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_ITGRPEDENTREG CHECK (IDATIVO IN ('S','N')),	
		CONSTRAINT PK_ITAGRUPPEDENTREGA PRIMARY KEY (CDFILIAL, NRAGRUPPEDENT, NRSEQPED)
)
/

ALTER TABLE ITAGRUPPEDENTREGA
    ADD CONSTRAINT FKPED00466ITAGRUPPED FOREIGN KEY  (CDFILIAL, NRAGRUPPEDENT)
       REFERENCES AGRUPPEDENTREGA (CDFILIAL, NRAGRUPPEDENT)
/

CREATE INDEX PED00466_FK ON ITAGRUPPEDENTREGA(CDFILIAL ASC, NRAGRUPPEDENT ASC)
/

ALTER TABLE ITAGRUPPEDENTREGA
    ADD CONSTRAINT FKPED00467ITAGRUPPED FOREIGN KEY  (CDFILIALPED, NRPEDIDO)
       REFERENCES PEDIDO (CDFILIAL, NRPEDIDO)
/

CREATE INDEX PED00467_FK ON ITAGRUPPEDENTREGA(CDFILIALPED ASC, NRPEDIDO ASC)
/