CREATE trigger ttbpdv_representante_b02u for tbpdv_representante
active before insert position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' REPRESENTANTE INCLUIDO: '||NEW.REPRESENTANTE_ID||' - '||(SELECT FIRST 1 R.NOMEFANTASIA FROM TBEMPRESA R WHERE R.CODIGO = NEW.REPRESENTANTE_ID);
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBPDV',NEW.PDV_ID, HISTORICO);
    END
END;


CREATE trigger ttbpdv_representante_b01d for tbpdv_representante
active before delete position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' REPRESENTANTE EXCLUIDO: '||OLD.REPRESENTANTE_ID||' - '||(SELECT FIRST 1 R.NOMEFANTASIA FROM TBEMPRESA R WHERE R.CODIGO = OLD.REPRESENTANTE_ID);
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBPDV',OLD.PDV_ID, HISTORICO);
    END
END;
