CREATE trigger ttbprontuario_atendimento_b02i for tbprontuario_atendimento
active before insert position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

   HISTORICO = ' ATENDIMENTO INCLUIDO: '||NEW.ID||', MEDICO:'||NEW.MEDICO_ID||' - '||(SELECT FIRST 1 M.NOME FROM TBMEDICO M WHERE M.ID = NEW.MEDICO_ID);

   IF (HISTORICO <> '') THEN
       BEGIN

       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBPRONTUARIO', NEW.PRONTUARIO_ID,
      (SELECT FIRST 1 MON$USER FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
      (SELECT FIRST 1 MON$REMOTE_ADDRESS FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
       :HISTORICO);
       END
END;


CREATE trigger ttbprontuario_atendimento_b01d for tbprontuario_atendimento
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

   HISTORICO = ' ATENDIMENTO EXCLUIDO: '||OLD.ID||', MEDICO:'||OLD.MEDICO_ID||' - '||(SELECT FIRST 1 M.NOME FROM TBMEDICO M WHERE M.ID = OLD.MEDICO_ID);

   IF (HISTORICO <> '') THEN
       BEGIN

       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBPRONTUARIO', OLD.PRONTUARIO_ID,
      (SELECT FIRST 1 MON$USER FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
      (SELECT FIRST 1 MON$REMOTE_ADDRESS FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
       :HISTORICO);
       END
END;


CREATE trigger ttbpront_atendimento_b01d for tbprontuario_atendimento
active before delete position 1
AS
BEGIN
   IF(OLD.ID > 0) THEN
   BEGIN
      DELETE FROM TBPRONTUARIO_DIAGNOSTICO WHERE ATENDIMENTO_ID = OLD.ID;
      DELETE FROM TBPRONTUARIO_PRESCRICAO WHERE ATENDIMENTO_ID = OLD.ID;
   END
END;



CREATE trigger ttbprontuario_atendimento_b01u for tbprontuario_atendimento
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

   HISTORICO = '';

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

   IF (OLD.QUEIXA_PRINCIPAL <> NEW.QUEIXA_PRINCIPAL) THEN
       BEGIN
       HISTORICO = HISTORICO||' QUEIXA:'||OLD.QUEIXA_PRINCIPAL||'->'||NEW.QUEIXA_PRINCIPAL;
       END

   IF (OLD.HISTORIA_DOENCA_ATUAL <> NEW.HISTORIA_DOENCA_ATUAL) THEN
       BEGIN
       HISTORICO = HISTORICO||' HIST.DOENCA ATUAL ALTERADA:';
       END

   IF (OLD.OBSERVACAO <> NEW.OBSERVACAO) THEN
       BEGIN
       HISTORICO = HISTORICO||' OBS:'||OLD.OBSERVACAO||'->'||NEW.OBSERVACAO;
       END

   IF (HISTORICO <> '') THEN
       BEGIN

       HISTORICO = 'ATENDIMENTO: '||FN_LPAD(NEW.ID, 4, '0')||' ALTERADO: '||HISTORICO;

       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBPRONTUARIO', NEW.PRONTUARIO_ID,
      (SELECT FIRST 1 MON$USER FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
      (SELECT FIRST 1 MON$REMOTE_ADDRESS FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
       :HISTORICO);
       END
END;
