/******************************************************************************/
/****                                Tables                                ****/
/******************************************************************************/


CREATE GENERATOR GTBMOTIVO_ENCERRAMENTO_PED;

CREATE TABLE TBMOTIVO_ENCERRAMENTO_PED (
    ID         INTEGER NOT NULL,
    DESCRICAO  VARCHAR(100),
    STATUS     INTEGER
);




/******************************************************************************/
/****                             Primary keys                             ****/
/******************************************************************************/

ALTER TABLE TBMOTIVO_ENCERRAMENTO_PED ADD CONSTRAINT PK_TBMOTIVO_ENCERRAMENTO_PED PRIMARY KEY (ID);


/******************************************************************************/
/****                               Indices                                ****/
/******************************************************************************/

CREATE INDEX ITBMOTIVO_ENCERRAMENTO_PED_01 ON TBMOTIVO_ENCERRAMENTO_PED (STATUS);


/******************************************************************************/
/****                               Triggers                               ****/
/******************************************************************************/



SET TERM ^ ;



/******************************************************************************/
/****                         Triggers for tables                          ****/
/******************************************************************************/



/* Trigger: TTBMOTIVO_ENCERRAMENTO_PED_B01D */
CREATE OR ALTER TRIGGER TTBMOTIVO_ENCERRAMENTO_PED_B01D FOR TBMOTIVO_ENCERRAMENTO_PED
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' REGISTRO EXCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMOTIVO_ENCERRAMENTO_PED', OLD.ID, HISTORICO);
    END
END
^


/* Trigger: TTBMOTIVO_ENCERRAMENTO_PED_B01I */
CREATE OR ALTER TRIGGER TTBMOTIVO_ENCERRAMENTO_PED_B01I FOR TBMOTIVO_ENCERRAMENTO_PED
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
    IF (NEW.ID IS NULL) THEN
        NEW.ID = GEN_ID(GTBMOTIVO_ENCERRAMENTO_PED, 1);
END
^


/* Trigger: TTBMOTIVO_ENCERRAMENTO_PED_B01U */
CREATE OR ALTER TRIGGER TTBMOTIVO_ENCERRAMENTO_PED_B01U FOR TBMOTIVO_ENCERRAMENTO_PED
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(5000);
BEGIN

    HISTORICO = '';

    IF (OLD.DESCRICAO IS DISTINCT FROM NEW.DESCRICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' DESC:'||OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END

    IF (OLD.STATUS IS DISTINCT FROM NEW.STATUS) THEN
    BEGIN
        HISTORICO = HISTORICO||' STATUS:'||OLD.STATUS||'->'||NEW.STATUS;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'REGISTRO ALTERADO:'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBMOTIVO_ENCERRAMENTO_PED', OLD.ID, FN_ELIPSE(HISTORICO, 500));
    END
END
^


/* Trigger: TTBMOTIVO_ENCERRAMENTO_PED_B02I */
CREATE OR ALTER TRIGGER TTBMOTIVO_ENCERRAMENTO_PED_B02I FOR TBMOTIVO_ENCERRAMENTO_PED
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' REGISTRO INCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMOTIVO_ENCERRAMENTO_PED', NEW.ID, HISTORICO);
    END
END
^

SET TERM ; ^

/******************************************************************************/
/******************************************************************************/
/******************************************************************************/

INSERT INTO TBMENU (CONTROLE, GRUPO, SUBGRUPO, TIPO, URL, DESCRICAO, SUB_GRUPO, DESCRICAO_RESUMIDA)
    VALUES (12600, 'VEN', 'VEN', '1', '', 'CADASTRO DE MOTIVOS PARA ENCERRAMENTO DOS PEDIDOS', '2', 'CAD. DE MOTIVOS PARA ENC. DOS PEDIDOS');
