SET TERM ^ ;

CREATE trigger ttbcheque_pdv_b09i for tbcheque_pdv
active before insert position 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'REG. INCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCHEQUE_PDV', NEW.ID, HISTORICO);
    END
END
^

SET TERM ; ^



SET TERM ^ ;

CREATE trigger ttbcheque_pdv_b09d for tbcheque_pdv
active before delete position 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF (OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'REG. EXCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCHEQUE_PDV', OLD.ID, HISTORICO);
    END
END
^

SET TERM ; ^


SET TERM ^ ;

CREATE trigger ttbcheque_pdv_b09u for tbcheque_pdv
active before update position 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF (OLD.PAGAMENTO_CONDICAO_ID IS DISTINCT FROM NEW.PAGAMENTO_CONDICAO_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' COND.PAG:'||OLD.PAGAMENTO_CONDICAO_ID||'->'||NEW.PAGAMENTO_CONDICAO_ID;
    END

    IF (OLD.BANCO_ID IS DISTINCT FROM NEW.BANCO_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' BANCO:'||OLD.BANCO_ID||'->'||NEW.BANCO_ID;
    END

    IF (OLD.AGENCIA IS DISTINCT FROM NEW.AGENCIA) THEN
    BEGIN
        HISTORICO = HISTORICO||' AGENCIA:'||OLD.AGENCIA||'->'||NEW.AGENCIA;
    END

    IF (OLD.CONTA_DV IS DISTINCT FROM NEW.CONTA_DV) THEN
    BEGIN
        HISTORICO = HISTORICO||' CONTA:'||OLD.CONTA_DV||'->'||NEW.CONTA_DV;
    END

    IF (OLD.NUMERO_CHEQUE IS DISTINCT FROM NEW.NUMERO_CHEQUE) THEN
    BEGIN
        HISTORICO = HISTORICO||' NUM.CHEQUE:'||OLD.NUMERO_CHEQUE||'->'||NEW.NUMERO_CHEQUE;
    END

    IF (OLD.NOME_TITULAR IS DISTINCT FROM NEW.NOME_TITULAR) THEN
    BEGIN
        HISTORICO = HISTORICO||' NOME TITULAR:'||OLD.NOME_TITULAR||'->'||NEW.NOME_TITULAR;
    END

    IF (OLD.DATA_EMISSAO IS DISTINCT FROM NEW.DATA_EMISSAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' DT.EMISSAO:'||OLD.DATA_EMISSAO||'->'||NEW.DATA_EMISSAO;
    END

    IF (OLD.VALOR IS DISTINCT FROM NEW.VALOR) THEN
    BEGIN
        HISTORICO = HISTORICO||' VALOR:'||OLD.VALOR||'->'||NEW.VALOR;
    END

    IF (OLD.STATUS IS DISTINCT FROM NEW.STATUS) THEN
    BEGIN
        HISTORICO = HISTORICO||' STTS:'||OLD.STATUS||'->'||NEW.STATUS;
    END

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

SET TERM ; ^

