CREATE TABLE TBPDV_DEVOLUCAO_TROCA (
    ID INTEGER,
    CLIENTE_ID INTEGER,
    DATAHORA TIMESTAMP DEFAULT 'NOW',
    TIPO_ED VARCHAR(1),
    DOCUMENTO_IR INTEGER,
    TIPO_DOCUMENTO VARCHAR(1),
    VALOR_SALDO NUMERIC(15,8));
	
ALTER TABLE TBPDV_DEVOLUCAO_TROCA ALTER ID SET NOT NULL;

ALTER TABLE TBPDV_DEVOLUCAO_TROCA
ADD CONSTRAINT PK_TBPDV_DEVOLUCAO_TROCA
PRIMARY KEY (ID);

	
CREATE SEQUENCE GTBPDV_DEVOLUCAO_TROCA;

CREATE TRIGGER TTBPDV_DEVOLUCAO_TROCA_B0I FOR TBPDV_DEVOLUCAO_TROCA
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBPDV_DEVOLUCAO_TROCA,1);
END;

CREATE TABLE TBPDV_DEVOLUCAO_TROCA_ITEM (
    ID INTEGER NOT NULL,
    ITEM_ID INTEGER,
    QUANTIDADE NUMERIC(15,8),
    VALOR_UNITARIO NUMERIC(15,8),
    VALOR_DESCONTO NUMERIC(15,8),
    VALOR_TOTAL NUMERIC(15,8));
	
	
ALTER TABLE TBPDV_DEVOLUCAO_TROCA_ITEM
ADD CONSTRAINT PK_TBPDV_DEVOLUCAO_TROCA_ITEM
PRIMARY KEY (ID);

CREATE SEQUENCE GTBPDV_DEVOLUCAO_TROCA_ITEM;

CREATE TRIGGER TTBPDV_DEVOLUCAO_TROCA_ITEM_B0I FOR TBPDV_DEVOLUCAO_TROCA_ITEM
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBPDV_DEVOLUCAO_TROCA_ITEM,1);
END;	