CREATE TRIGGER TTBCLIENTE_VENDEDOR_B01I FOR TBCLIENTE_VENDEDOR
ACTIVE BEFORE INSERT POSITION 0
as
begin
  if (new.ID is null) then
    new.ID = gen_id(GTBCLIENTE_VENDEDOR,1);
end;

CREATE TRIGGER TTBCLIENTE_VENDEDOR_B02I FOR TBCLIENTE_VENDEDOR
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.CLIENTE_ID > 0 AND NEW.ID > 0 ) THEN
    BEGIN
        HISTORICO = HISTORICO||'Vendedor(a) incluido(a)';
    END

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

CREATE OR ALTER TRIGGER TTBCLIENTE_VENDEDOR_B01U FOR TBCLIENTE_VENDEDOR
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.NOME <> NEW.NOME) THEN
    BEGIN
        HISTORICO = HISTORICO||' Vendedor: '||lpad(OLD.ID,4,'0')||', Nome Alterado.';
    END

    IF (OLD.DATA_NASCIMENTO <> NEW.DATA_NASCIMENTO) THEN
    BEGIN
        HISTORICO = HISTORICO||', Vendedor: '||lpad(OLD.ID,4,'0')||' Dt.Nasc. '||FORMATDATE(OLD.DATA_NASCIMENTO)||' ->'||FORMATDATE(NEW.DATA_NASCIMENTO);
    END

    IF (OLD.EMAIL <> NEW.EMAIL) THEN
    BEGIN
        HISTORICO = HISTORICO||', Vendedor: '||lpad(OLD.ID,4,'0')||' Email Alterado.';
    END

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

END;


CREATE TRIGGER TTBCLIENTE_VENDEDOR_B01D FOR TBCLIENTE_VENDEDOR
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Vendedor(a) excluido(a)';
    END

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

CREATE TRIGGER TTBCLIENTE_SOCIOS_B01I FOR TBCLIENTE_SOCIOS
ACTIVE BEFORE INSERT POSITION 0
as
begin
  if (new.ID is null) then
    new.ID = gen_id(GTBCLIENTE_SOCIOS,1);
end;

CREATE TRIGGER TTBCLIENTE_SOCIOS_B02I FOR TBCLIENTE_SOCIOS
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.CLIENTE_ID > 0 AND NEW.ID > 0 ) THEN
    BEGIN
        HISTORICO = HISTORICO||'Sócio incluido';
    END

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

CREATE TRIGGER TTBCLIENTE_SOCIOS_B01U FOR TBCLIENTE_SOCIOS
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.NOME <> NEW.NOME) THEN
    BEGIN
        HISTORICO = HISTORICO||' Sócio: '||lpad(OLD.ID,6,'0')||', Nome Alterado.';
    END

    IF (OLD.CPF <> NEW.CPF) THEN
    BEGIN
        HISTORICO = HISTORICO||', Sócio: '||lpad(OLD.ID,6,'0')||' CNPJ/CPF: '||(Case When Char_length(OLD.CPF)=11 Then Substring(OLD.CPF from 1 for 3)||'.'||Substring(OLD.CPF from 4 for 3)||'.'||Substring(OLD.CPF from 7 for 3)||'-'||Substring(OLD.CPF from 10 for 2) else
                    (Case When Char_length(OLD.CPF)=14 Then Substring(OLD.CPF from 1 for 2)||'.'||Substring(OLD.CPF from 3 for 3)||'.'||Substring(OLD.CPF from 6 for 3)||'/'||Substring(OLD.CPF from 9 for 4)||'-'||Substring(OLD.CPF from 13 for 2) else OLD.CPF end) end)||' ->'||(Case When Char_length(NEW.CPF)=11 Then Substring(NEW.CPF from 1 for 3)||'.'||Substring(NEW.CPF from 4 for 3)||'.'||Substring(NEW.CPF from 7 for 3)||'-'||Substring(NEW.CPF from 10 for 2) else
                    (Case When Char_length(NEW.CPF)=14 Then Substring(NEW.CPF from 1 for 2)||'.'||Substring(NEW.CPF from 3 for 3)||'.'||Substring(NEW.CPF from 6 for 3)||'/'||Substring(NEW.CPF from 9 for 4)||'-'||Substring(NEW.CPF from 13 for 2) else NEW.CPF end) end);
    END

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

END;

CREATE TRIGGER TTBCLIENTE_SOCIOS_B01D FOR TBCLIENTE_SOCIOS
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Sócio excluido';
    END

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





