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

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' VENDEDOR INCLUIDO: '||NEW.VENDEDOR_ID||' - '||(SELECT FIRST 1 R.NOME FROM TBREPRESENTANTE_VENDEDOR R WHERE R.CODIGO = NEW.VENDEDOR_ID);
    END

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



CREATE trigger ttbpdv_vendedor_b01d for tbpdv_vendedor
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' VENDEDOR EXCLUIDO: '||OLD.VENDEDOR_ID||' - '||(SELECT FIRST 1 R.NOME FROM TBREPRESENTANTE_VENDEDOR R WHERE R.CODIGO = OLD.VENDEDOR_ID);
    END

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