CREATE GENERATOR GTBCLIENTE_CONDOMINIO;

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



SET TERM ^ ;



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



/* Trigger: TTBCLIENTE_CONDOMINIO_B01D */
CREATE OR ALTER TRIGGER TTBCLIENTE_CONDOMINIO_B01D FOR TBCLIENTE_CONDOMINIO
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    
    HISTORICO = '';

    IF (OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'LOTE: '||FN_LPAD(OLD.LOTE, 3, '0')||' QUADRA: '||FN_LPAD(OLD.QUADRA, 3, '0')||' EXCLUIDO';
    END

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


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


/* Trigger: TTBCLIENTE_CONDOMINIO_B01U */
CREATE OR ALTER TRIGGER TTBCLIENTE_CONDOMINIO_B01U FOR TBCLIENTE_CONDOMINIO
ACTIVE BEFORE UPDATE POSITION 0
AS
declare variable HISTORICO varchar(5000);
BEGIN
    
    HISTORICO = '';

    IF (OLD.QUADRA <> NEW.QUADRA) THEN
    BEGIN
        HISTORICO = HISTORICO||' QUADRA:'||FN_LPAD(OLD.QUADRA, 3, '0')||'->'||FN_LPAD(NEW.QUADRA, 3, '0');
    END

    IF (OLD.LOTE <> NEW.LOTE) THEN
    BEGIN
        HISTORICO = HISTORICO||' LOTE:'||FN_LPAD(OLD.LOTE, 3, '0')||'->'||FN_LPAD(NEW.LOTE, 3, '0');
    END

    IF (OLD.AREA <> NEW.AREA) THEN
    BEGIN
        HISTORICO = HISTORICO||' AREA:'||OLD.AREA||'M²->'||NEW.AREA||'M²';
    END

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

    IF (OLD.OBSERVACAO <> NEW.OBSERVACAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' OBS:'||OLD.OBSERVACAO||'->'||NEW.OBSERVACAO;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'LOTE: '||FN_LPAD(NEW.LOTE, 3, '0')||' QUADRA: '||FN_LPAD(NEW.QUADRA, 3, '0')||' ALTERADO:'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBEMPRESA', OLD.CLIENTE_ID, FN_ELIPSE(HISTORICO, 500));
    END
END
^


/* Trigger: TTBCLIENTE_CONDOMINIO_B02I */
CREATE OR ALTER TRIGGER TTBCLIENTE_CONDOMINIO_B02I FOR TBCLIENTE_CONDOMINIO
ACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    
    HISTORICO = '';

    IF (NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'LOTE: '||FN_LPAD(NEW.LOTE, 3, '0')||' QUADRA: '||FN_LPAD(NEW.QUADRA, 3, '0')||' INCLUIDO';
    END

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

SET TERM ; ^