ALTER TABLE TBSUPERVISOR
    ADD STATUS INTEGER;

CREATE trigger ttbsupervisor_b01i for tbsupervisor
active before insert position 0
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('TBSUPERVISOR',NEW.id,HISTORICO);
    END
end;

CREATE trigger ttbsupervisor_b01u for tbsupervisor
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.status <> old.status) THEN
    BEGIN
        HISTORICO = HISTORICO||'Status:'||NEW.status||' -> '||old.status;
    END

    IF ( NEW.descricao <> old.descricao) THEN
    BEGIN
        HISTORICO = HISTORICO||', Descricao:'||NEW.descricao||' -> '||old.descricao;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBSUPERVISOR',NEW.id,HISTORICO);
    END
end;

CREATE trigger ttbsupervisor_b01d for tbsupervisor
active before delete position 0
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('TBSUPERVISOR',old.id,HISTORICO);
    END
end;

CREATE trigger ttbrepresentante_b02u for tbrepresentante
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( old.supervisor_id <> new.supervisor_id and coalesce(old.supervisor_id,0) = 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Vinculado:'||new.codigo;
        EXECUTE PROCEDURE SPI_HISTORICO('TBSUPERVISOR',new.supervisor_id,HISTORICO);
    END

    IF ( old.supervisor_id <> new.supervisor_id and coalesce(new.supervisor_id,0) = 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Desvinculado:'||new.codigo;
        EXECUTE PROCEDURE SPI_HISTORICO('TBSUPERVISOR',old.supervisor_id,HISTORICO);
    END
end;

