ALTER TABLE TBMEDICO_CLINICA_ATENDIMENTO
    ADD CONSULTORIO_ID INTEGER DEFAULT 0 NOT NULL;

CREATE INDEX ITBMEDICO_CLINICA_ATENDIMEN_10
ON TBMEDICO_CLINICA_ATENDIMENTO (CONSULTORIO_ID);

SET TERM ^ ;

CREATE OR ALTER trigger ttbmedico_clinica_aten_b09u for tbmedico_clinica_atendimento
active before update position 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN   
    
    HISTORICO = '';

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

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

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

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

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

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

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

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

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

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

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

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

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

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

SET TERM ; ^

SET TERM ^ ;

CREATE trigger ttbsessao_paciente_det_b04u for tbsessao_paciente_detalhe
active before update position 4
AS
BEGIN
    IF( OLD.INICIO IS DISTINCT FROM NEW.INICIO OR
        OLD.FIM IS DISTINCT FROM NEW.FIM OR
        OLD.PRODUTO_ID IS DISTINCT FROM NEW.PRODUTO_ID OR
        OLD.PROFISSIONAL_ID IS DISTINCT FROM NEW.PROFISSIONAL_ID OR
        OLD.CLINICA_ID IS DISTINCT FROM NEW.CLINICA_ID OR
        OLD.PACIENTE_ID IS DISTINCT FROM NEW.PACIENTE_ID OR
        OLD.CONSULTORIO_ID IS DISTINCT FROM NEW.CONSULTORIO_ID OR
        OLD.DATA_AGENDAMENTO IS DISTINCT FROM NEW.DATA_AGENDAMENTO) THEN
    BEGIN
         IF(NEW.STATUS_CONFIRMACAO = 1) THEN
         BEGIN
             EXCEPTION "..."'NAO E POSSIVEL ALTERAR UMA CONSULTA JA CONFIRMADA. OPERACAO CANCELADA!';
         END
    END
END
^

SET TERM ; ^



