ALTER TABLE TBWORKFLOW_TAREFA
ADD COR VARCHAR(10) DEFAULT '#337ab7';

ALTER TABLE TBWORKFLOW
ADD TAMANHO_MAX_PX NUMERIC(10,2)
DEFAULT 0;

ALTER TABLE TBWORKFLOW
ADD TAMANHO_MAX_PERC NUMERIC(10,2)
DEFAULT 0;

ALTER TABLE TBWORKFLOW_ITEM_TAREFA
ADD USUARIO_CONCLUSAO_ID INTEGER
DEFAULT 0;

CREATE OR ALTER TRIGGER TTBWORKFLOW_ITEM_TAREFA_B05U FOR TBWORKFLOW_ITEM_TAREFA
ACTIVE BEFORE UPDATE POSITION 5
AS
BEGIN
    IF(NEW.STATUS_CONCLUSAO = 1 AND OLD.STATUS_CONCLUSAO = 0)THEN
    BEGIN
       INSERT INTO TBWORKFLOW_ITEM_HISTORICO(
              WORKFLOW_ITEM_TAREFA_ID,
              WORKFLOW_ITEM_ID,
              HISTORICO,
              USUARIO_ID
         )VALUES(
              NEW.ID,
              NEW.WORKFLOW_ITEM_ID,
              'TAREFA '||COALESCE((SELECT FIRST 1 A.TITULO FROM TBWORKFLOW_TAREFA A WHERE A.ID = NEW.WORKFLOW_TAREFA_MODELO_ID),'')||' INICIADA.',
              FN_CURRENT_USER_ID()
         );
    END 

    IF(NEW.STATUS_CONCLUSAO = 2 AND OLD.STATUS_CONCLUSAO = 1)THEN
    BEGIN
       INSERT INTO TBWORKFLOW_ITEM_HISTORICO(
              WORKFLOW_ITEM_TAREFA_ID,
              WORKFLOW_ITEM_ID,
              HISTORICO,
              USUARIO_ID
         )VALUES(
              NEW.ID,
              NEW.WORKFLOW_ITEM_ID,
              'TAREFA '||COALESCE((SELECT FIRST 1 A.TITULO FROM TBWORKFLOW_TAREFA A WHERE A.ID = NEW.WORKFLOW_TAREFA_MODELO_ID),'')||' PAUSADA.',
              FN_CURRENT_USER_ID()
         );
    END

    IF(NEW.STATUS_CONCLUSAO = 1 AND OLD.STATUS_CONCLUSAO = 2)THEN
    BEGIN
       INSERT INTO TBWORKFLOW_ITEM_HISTORICO(
              WORKFLOW_ITEM_TAREFA_ID,
              WORKFLOW_ITEM_ID,
              HISTORICO,
              USUARIO_ID
         )VALUES(
              NEW.ID,
              NEW.WORKFLOW_ITEM_ID,
              'TAREFA '||COALESCE((SELECT FIRST 1 A.TITULO FROM TBWORKFLOW_TAREFA A WHERE A.ID = NEW.WORKFLOW_TAREFA_MODELO_ID),'')||' REINICIADA.',
              FN_CURRENT_USER_ID()
         );
    END

    IF(NEW.STATUS_CONCLUSAO = 3 AND OLD.STATUS_CONCLUSAO = 1)THEN
    BEGIN
       INSERT INTO TBWORKFLOW_ITEM_HISTORICO(
              WORKFLOW_ITEM_TAREFA_ID,
              WORKFLOW_ITEM_ID,
              HISTORICO,
              USUARIO_ID
         )VALUES(
              NEW.ID,
              NEW.WORKFLOW_ITEM_ID,
              'TAREFA '||COALESCE((SELECT FIRST 1 A.TITULO FROM TBWORKFLOW_TAREFA A WHERE A.ID = NEW.WORKFLOW_TAREFA_MODELO_ID),'')||' CONCLUIDA.',
              FN_CURRENT_USER_ID()
         );
    END
END;

CREATE OR ALTER TRIGGER TTBWORKFLOW_ITEM_B02I FOR TBWORKFLOW_ITEM
ACTIVE BEFORE INSERT POSITION 2
AS
declare variable HISTORICO VARCHAR(500);
begin
  IF(NEW.ID > 0)THEN
  BEGIN
    HISTORICO = 'REGISTRO INCLUIDO';
  END
  
  IF (HISTORICO <> '') THEN
  BEGIN
      EXECUTE PROCEDURE SPI_HISTORICO('TBWORKFLOW_ITEM',NEW.ID, HISTORICO);
      
      INSERT INTO TBWORKFLOW_ITEM_HISTORICO(
          WORKFLOW_ITEM_TAREFA_ID,
          WORKFLOW_ITEM_ID,
          HISTORICO,
          USUARIO_ID
       )VALUES(
          0,
          NEW.ID,
          'WOKRFLOW '||COALESCE(NEW.TITULO,'')||' INCLUIDO',
          FN_CURRENT_USER_ID()
      );
  END
end;

CREATE OR ALTER TRIGGER TTBWORKFLOW_ITEM_B01D FOR TBWORKFLOW_ITEM
ACTIVE BEFORE DELETE POSITION 0
AS
begin
   /*if(old.PROGRESSO > 0)then
   begin
      exception "..." 'O WORKFLOW JÁ ESTÁ EM ANDAMENTO, OPERAÇÃO CANCELADA!';
   end  */
    
   if(old.STATUS_CONCLUSAO = '3')then
   begin
      exception "..." 'O WORKFLOW JÁ FOI CONCLUÍDO, OPERAÇÃO CANCELADA!';
   end
end;

CREATE OR ALTER TRIGGER TTBWORKFLOW_ITEM_B02D FOR TBWORKFLOW_ITEM
ACTIVE BEFORE DELETE POSITION 2
AS
declare variable HISTORICO VARCHAR(500);
begin
  IF(OLD.ID > 0)THEN
  BEGIN
    HISTORICO = 'REGISTRO EXCLUIDO';
  END
  
  IF (HISTORICO <> '') THEN
  BEGIN
      EXECUTE PROCEDURE SPI_HISTORICO('TBWORKFLOW_ITEM',OLD.ID, HISTORICO);
      
      INSERT INTO TBWORKFLOW_ITEM_HISTORICO(
          WORKFLOW_ITEM_TAREFA_ID,
          WORKFLOW_ITEM_ID,
          HISTORICO,
          USUARIO_ID
       )VALUES(
          0,
          OLD.ID,
          'WOKRFLOW '||COALESCE(OLD.TITULO, '')||' EXCLUIDO.',
          FN_CURRENT_USER_ID()
      );
  END
end;

UPDATE TBMENU SET TIPO = 0 WHERE CONTROLE = 29011;
UPDATE TBMENU SET TIPO = 0 WHERE CONTROLE = 29013;
UPDATE TBMENU SET TIPO = 0 WHERE CONTROLE = 29012;