CREATE OR ALTER TRIGGER TTBREPRESENTANTE_VENDEDOR_B01I FOR TBREPRESENTANTE_VENDEDOR
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.REPRESENTANTE_CODIGO > 0 AND NEW.CODIGO > 0 ) THEN
    BEGIN
        HISTORICO = HISTORICO||'Vendedor(a) incluido(a) - '||NEW.CODIGO||' '||NEW.NOME;
    END

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

CREATE TRIGGER TTBREPRESENTANTE_VENDEDOR_B02I FOR TBREPRESENTANTE_VENDEDOR
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.CODIGO IS NULL) THEN
    NEW.CODIGO = GEN_ID(GTBREPRESENTANTE_VENDEDOR,1);
END;

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

    HISTORICO = '';

    IF (OLD.NOME <> NEW.NOME) THEN
    BEGIN
        HISTORICO = HISTORICO||' Vendedor Alterado: '||OLD.CODIGO||' Nome '||OLD.NOME||' ->'||NEW.NOME;
    END

    IF (OLD.COMISSAO <> NEW.COMISSAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' Vendedor Alterado: '||OLD.CODIGO||' %Com. '||CAST(OLD.COMISSAO AS NUMERIC(15,2))||' ->'||CAST(NEW.COMISSAO AS NUMERIC(15,2));
    END

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

END;

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

    HISTORICO = '';

    IF (OLD.CODIGO > 0) THEN
    BEGIN
         HISTORICO = HISTORICO||'Vendedor(a) Excluido(a) - '||OLD.CODIGO||' '||OLD.NOME;
    END

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

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

    HISTORICO = '';

    IF ( NEW.ORDEM > 0 AND NEW.ID > 0 ) THEN
    BEGIN
        HISTORICO = HISTORICO||'Reg.Chk.list incluido';
    END

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

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

    HISTORICO = '';

    IF (OLD.ORDEM <> NEW.ORDEM) THEN
    BEGIN
        HISTORICO = HISTORICO||' Chk.List Alterado: '||LPAD(OLD.ID,4,'0')||', Ord. '||OLD.ORDEM||' ->'||NEW.ORDEM;
    END

    IF (OLD.DESCRICAO <> NEW.DESCRICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' Chk.List Alterado: '||LPAD(OLD.ID,4,'0')||', Desc. '||OLD.DESCRICAO||' ->'||NEW.DESCRICAO;
    END

    IF (OLD.RESPONSAVEL <> NEW.RESPONSAVEL) THEN
    BEGIN
        HISTORICO = HISTORICO||' Chk.List Alterado: '||LPAD(OLD.ID,4,'0')||', Resp. '||OLD.RESPONSAVEL||' ->'||NEW.RESPONSAVEL;
    END

    IF (OLD.DIAS <> NEW.DIAS) THEN
    BEGIN
        HISTORICO = HISTORICO||' Chk.List Alterado: '||LPAD(OLD.ID,4,'0')||', Dias: '||OLD.DIAS||' ->'||NEW.DIAS;
    END

    IF (OLD.BAIXAR_VIA_NF <> NEW.BAIXAR_VIA_NF) THEN
    BEGIN
        HISTORICO = HISTORICO||' Chk.List Alterado: '||LPAD(OLD.ID,4,'0')||', Bx.NF: '||OLD.BAIXAR_VIA_NF||' ->'||NEW.BAIXAR_VIA_NF;
    END

    IF (OLD.DATA_BASE <> NEW.DATA_BASE) THEN
    BEGIN
        HISTORICO = HISTORICO||' Chk.List Alterado: '||LPAD(OLD.ID,4,'0')||', Dt.Base: '||OLD.DATA_BASE||' ->'||NEW.DATA_BASE;
    END

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

END;

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

    HISTORICO = '';

    IF (OLD.ID > 0) THEN
    BEGIN
         HISTORICO = HISTORICO||'Chk.List Excluido. ';
    END

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

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

    HISTORICO = '';

    IF (OLD.NOME <> NEW.NOME) THEN
    BEGIN
        HISTORICO = HISTORICO||' Ref. Com/Banc alterada '||lpad(OLD.ID,5,'0')||', NOME: '||OLD.NOME||' ->'||NEW.NOME;
    END

    IF (OLD.TELEFONE <> NEW.TELEFONE) THEN
    BEGIN
        HISTORICO = HISTORICO||' Ref. Com/Banc alterada '||lpad(OLD.ID,5,'0')||', FONE: '||OLD.TELEFONE||' ->'||NEW.TELEFONE;
    END

    IF (OLD.CONTATO <> NEW.CONTATO) THEN
    BEGIN
        HISTORICO = HISTORICO||' Ref. Com/Banc alterada '||lpad(OLD.ID,5,'0')||', CONTATO: '||OLD.CONTATO||' ->'||NEW.CONTATO;
    END

    IF (OLD.OBSERVACAO <> NEW.OBSERVACAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' Ref. Com/Banc alterada '||lpad(OLD.ID,5,'0')||', Obs.: '||OLD.OBSERVACAO||' ->'||NEW.OBSERVACAO;
    END

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

END;

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

    HISTORICO = '';

    IF (OLD.BANCO <> NEW.BANCO) THEN
    BEGIN
        HISTORICO = HISTORICO||' Ref. Com/Banc alterada '||lpad(OLD.ID,5,'0')||', Nome: '||OLD.BANCO||' ->'||NEW.BANCO;
    END

    IF (OLD.TELEFONE <> NEW.TELEFONE) THEN
    BEGIN
        HISTORICO = HISTORICO||' Ref. Com/Banc alterada '||lpad(OLD.ID,5,'0')||', Fone: '||OLD.TELEFONE||' ->'||NEW.TELEFONE;
    END

    IF (OLD.GERENTE <> NEW.GERENTE) THEN
    BEGIN
        HISTORICO = HISTORICO||' Ref. Com/Banc alterada '||lpad(OLD.ID,5,'0')||', Contato: '||OLD.GERENTE||' ->'||NEW.GERENTE;
    END

    IF (OLD.AGENCIA <> NEW.AGENCIA) THEN
    BEGIN
        HISTORICO = HISTORICO||' Ref. Com/Banc alterada '||lpad(OLD.ID,5,'0')||', Agencia: '||OLD.AGENCIA||' ->'||NEW.AGENCIA;
    END

    IF (OLD.CONTA <> NEW.CONTA) THEN
    BEGIN
        HISTORICO = HISTORICO||' Ref. Com/Banc alterada '||lpad(OLD.ID,5,'0')||', Conta: '||OLD.CONTA||' ->'||NEW.CONTA;
    END

    IF (OLD.OBSERVACAO <> NEW.OBSERVACAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' Ref. Com/Banc alterada '||lpad(OLD.ID,5,'0')||', Obs.: '||OLD.OBSERVACAO||' ->'||NEW.OBSERVACAO;
    END

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

END;



