Name

sn_hr_core.hr_CaseChildTodoUtils

Description

No description available

Script

var hr_CaseChildTodoUtils = Class.create();

hr_CaseChildTodoUtils._hrCaseTables = sn_hr_core.hr.TABLE_CASE_EXTENSIONS;
hr_CaseChildTodoUtils._hrTables = hr_CaseChildTodoUtils._hrCaseTables.slice();
hr_CaseChildTodoUtils._hrTables.push(hr.TABLE_TASK);
hr_CaseChildTodoUtils._approvalTables = hr_CaseChildTodoUtils._hrTables.slice();
hr_CaseChildTodoUtils._approvalTables.push('sc_request');
hr_CaseChildTodoUtils.MY_TODOS = 'MY_TODOS';
hr_CaseChildTodoUtils.OTHER_TODOS = 'OTHER_TODOS';
hr_CaseChildTodoUtils.ALL_TODOS = 'ALL_TODOS';

hr_CaseChildTodoUtils.prototype = {
  initialize: function(grSysId, isOpenedFor) {
      var gr = new GlideRecord(hr.TABLE_CASE);
      gr.get(grSysId);
      this._gr = gr;
      this._user = isOpenedFor ? this._gr.opened_for : gs.getUserID();
      this._isAuthorizedSubjectPerson = this._gr ? hr_Case.userHasSubjectPersonAccess(this._gr) : false;
      this._awaitingAcceptanceState = hr.STATE_AWAITING_ACCEPTANCE;
      this._draftState = hr.STATE_DRAFT;
      this.maxLevels = hr.MAX_CHILD_TODO_LEVELS;
      this.defaultLevels = hr.DEFAULT_CHILD_TODO_LEVELS;
  },
  getLevelforChildTodos: function() {
      if (this._gr && (this._isAuthorizedSubjectPerson || (this._gr.opened_for == this._user)))
          return this.maxLevels;
      return this.defaultLevels;
  },
  getCaseChildTodosAggregate: function(level) {
      var taskTodos = this.getCaseChildTaskTodosFiltered(hr_CaseChildTodoUtils.MY_TODOS, level, false, true);
      var overdueTodos = this.getCaseChildTaskTodosFiltered(hr_CaseChildTodoUtils.ALL_TODOS, level, true, true);
      var approvalTodos = this.getCaseChildApprovalTodos(level, false, true);
      var approvalOverdueTodos = this.getCaseChildApprovalTodos(level, true, true);
      var count = {
          'todo_count': taskTodos.length + approvalTodos.length,
          'overdue_todo_count': overdueTodos.length + approvalOverdueTodos.length
      };
      return count;
  },
  getCaseChildTaskTodosFiltered: function(todoFilter, level, isOverdue, isAggregate) {
      var levelsToDisplay = level || this.getLevelforChildTodos();
      var todoList = [];
      for (var currentLevel = 1; currentLevel <= levelsToDisplay; currentLevel++) {
          var taskTables = this.getTaskTables(currentLevel);
          for (var j = 0; j < taskTables.length; j++)
              this._addChildTasks(taskTables[j], todoFilter, currentLevel, todoList, isOverdue, isAggregate);
      }
      ///Add case to my todos if its in awaiting acceptance state	

      var isCaseAcceptance = (this._gr.opened_for == this._user) && (this._gr.state == this._awaitingAcceptanceState) && gs.nil(this._gr.universal_request);

      if (isCaseAcceptance && (todoFilter == hr_CaseChildTodoUtils.MY_TODOS || todoFilter == hr_CaseChildTodoUtils.ALL_TODOS)) {
          if (!isOverdue)
              todoList.push(this._gr.getUniqueValue());
          else if ((isOverdue && this.isOverdue(this._gr)))
              todoList.push(this._gr.getUniqueValue());
      }

      return todoList;
  },
  getCaseChildTaskTodos: function(mine, level) {
      if (mine)
          return this.getCaseChildTaskTodosFiltered(hr_CaseChildTodoUtils.MY_TODOS, level);
      else
          return this.getCaseChildTaskTodosFiltered(hr_CaseChildTodoUtils.OTHER_TODOS, level);
  },
  getCaseChildApprovalTodos: function(level, isOverdue, isAggregate) {
      var levelsToDisplay = level || this.getLevelforChildTodos();
      var todoList = [];
      for (var currentLevel = 1; currentLevel <= levelsToDisplay; currentLevel++)
          this._addChildApprovals(currentLevel, todoList, isOverdue, isAggregate);
      return todoList;
  },
  _addChildTasks: function(className, todoFilter, level, todoList, isOverdue, isAggregate) {
      var grTaskGr = this.getChildTasksGr(className, level, true);
      if (isOverdue)
          grTaskGr.addQuery('due_date', '<', 'javascript:gs.beginningOfToday()');

      var isHRCase = hr_CaseChildTodoUtils._hrCaseTables.indexOf(className) >= 0;

      if (todoFilter == hr_CaseChildTodoUtils.ALL_TODOS && isHRCase) {
          grTaskGr.addQuery('state', '=', this._awaitingAcceptanceState);
          grTaskGr.addQuery('opened_for', this._user);
      }
      grTaskGr.query();
      while (grTaskGr.next()) {
          if (this.filterTodo(grTaskGr, todoFilter) && (isAggregate || grTaskGr.canRead()))
              todoList.push(grTaskGr.getUniqueValue());
      }
  },
  getChildTasksGr: function(className, level, skipQuery) {
      var grTask = new GlideRecord(className);
      this._addParentQueryStringForLevel(grTask, level);
      if (hr_CaseChildTodoUtils._hrTables.indexOf(className) >= 0)
          grTask.addQuery('state', '!=', this._draftState);
      grTask.addActiveQuery(); ///filter inactive todos
      if (!skipQuery)
          grTask.query();
      return grTask;
  },
  _getApprovalGR: function(level) {
      var grApproval = new GlideRecord('sysapproval_approver');
      //Approval To-dos only appear for the logged-in user
      grApproval.addQuery('approver', this._user);
      this._addParentQueryStringForLevel(grApproval, level, true);
      grApproval.addQuery('sysapproval.sys_class_name', 'IN', hr_CaseChildTodoUtils._approvalTables);
      grApproval.addQuery('state', '=', 'requested'); //show only open todos
      return grApproval;

  },
  _addChildApprovals: function(level, todoList, isOverdue, isAggregate) {
      var grApproval = this._getApprovalGR(level);
      if (isOverdue)
          grApproval.addQuery('due_date', '<', 'javascript:gs.beginningOfToday()');
      grApproval.query();
      while (grApproval.next()) {
          if (isAggregate || grApproval.canRead())
              todoList.push(grApproval.getUniqueValue());
      }
  },
  filterTodo: function(gr, taskFilter) {
      if (taskFilter == hr_CaseChildTodoUtils.ALL_TODOS)
          return true;
      var isHRTask = gr.sys_class_name == 'sn_hr_core_task';
      var isDocTask = gr.sys_class_name == 'sn_doc_task';
      var isCaseAcceptance = hr_CaseChildTodoUtils._hrCaseTables.indexOf(String(gr.sys_class_name)) >= 0 && gr.state == this._awaitingAcceptanceState && gs.nil(gr.universal_request);
      var answer = false;
      // HR case in awaiting acceptance state should be part of Other Todos
      if (isCaseAcceptance && taskFilter == hr_CaseChildTodoUtils.MY_TODOS)
          answer = (gr.opened_for == this._user);
      else if (isHRTask || isDocTask)
          answer = new hr_Delegation().isAssignedOrDelegated(gr, this._user);
      return taskFilter == hr_CaseChildTodoUtils.MY_TODOS ? answer : !answer;
  },
  getUserAssignment: function(gr, mine) {
      if (mine)
          return this.filterTodo(gr, hr_CaseChildTodoUtils.MY_TODOS);
      else
          return this.filterTodo(gr, hr_CaseChildTodoUtils.OTHER_TODOS);

  },
  isOverdue: function(gr) {
      var dueDays;
      var hrTask = new sn_hr_core.hr_Task();
      if (gr.isValidField('due_date')) {
          dueDays = hrTask.getDueDays(gr.getDisplayValue('due_date'));
          return dueDays < 0;
      }
      return false;
  },
  getTaskTables: function(level) {
      var taskTables = [];
      var aggregate = new GlideAggregate('task');
      this._addParentQueryStringForLevel(aggregate, level);
      aggregate.groupBy('sys_class_name');
      aggregate.query();

      while (aggregate.next())
          taskTables.push(String(aggregate.sys_class_name));

      return taskTables;
  },
  _addParentQueryStringForLevel: function(gr, level, isApproval) {
      var parentId = String(this._gr.sys_id);
      var parentString = isApproval ? 'sysapproval' : 'parent';
      for (var i = 1; i < level; i++) {
          parentString = parentString + '.parent';
      }
      gr.addQuery(parentString, parentId);
  },

  /* Get open todos count & overdue todos count for subject person
  	@param level - Number - (optional) level of child todos upto which todos count will be calculated. 
  	@return Number - Count of child todos & overdue todos
  */
  getCaseChildTodosCountForSubjectPerson: function(level) {
      var taskTodos = this._getCaseChildTaskTodosCountFiltered(hr_CaseChildTodoUtils.MY_TODOS, level, false);
      var overdueTodos = this._getCaseChildTaskTodosCountFiltered(hr_CaseChildTodoUtils.MY_TODOS, level, true);
      var count = {
          'todo_count': taskTodos,
          'overdue_todo_count': overdueTodos
      };
      return count;
  },

  _getCaseChildTaskTodosCountFiltered: function(todoFilter, level, isOverdue) {
      var levelsToDisplay = level || this.getLevelforChildTodos();
      var todoCount = 0;
      for (var currentLevel = 1; currentLevel <= levelsToDisplay; currentLevel++) {
          var taskTables = this.getTaskTables(currentLevel);
          for (var j = 0; j < taskTables.length; j++)
              todoCount += this._getChildTasksCount(taskTables[j], todoFilter, currentLevel, isOverdue);
      }

      return todoCount;
  },

  _getChildTasksCount: function(className, todoFilter, level, isOverdue) {
      var childTasks = this._getChildTasksGrAggregate(className, level, isOverdue);
      var delegatedTasks = this._getDelegatedTasksCount(className, level, isOverdue);

      return childTasks + delegatedTasks;
  },

  _getDelegatedTasksCount: function(className, level, isOverdue) {
      var grTaskGrDelegated = this.getChildTasksGr(className, level, true);

      if (isOverdue) {
          grTaskGrDelegated.addQuery('due_date', '<', 'javascript:gs.beginningOfToday()');
      }

      var delegatedTasks = [];
      var isHRTask = className == 'sn_hr_core_task';
      var isDocTask = className == 'sn_doc_task';
      if (isHRTask || isDocTask)
          delegatedTasks = new sn_hr_core.hr_Delegation().getDelegatedTasks({
              encoded_query: grTaskGrDelegated.getEncodedQuery()
          }, this._user, false);

      return delegatedTasks.length;
  },

  _getChildTasksGrAggregate: function(className, level, isOverdue) {
      var todoCount = 0;

      if(hr_CaseChildTodoUtils._hrTables.indexOf(className) >= 0 || className == 'sn_doc_task') {
          var grTask = new GlideAggregate(className);
          this._addParentQueryStringForLevel(grTask, level);
          grTask.addQuery('active', 'true'); //filter inactive todos

          if (isOverdue)
              grTask.addQuery('due_date', '<', 'javascript:gs.beginningOfToday()');

          if (hr_CaseChildTodoUtils._hrTables.indexOf(className) >= 0) {
              grTask.addQuery('state', '!=', this._draftState);
              grTask.addQuery('assigned_to', this._user);
          }

          if (hr_CaseChildTodoUtils._hrCaseTables.indexOf(className) >= 0) {
              grTask.addQuery('state', this._awaitingAcceptanceState);
              grTask.addNullQuery('universal_request');
          }

          grTask.addAggregate('COUNT');
          grTask.query();
          if (grTask.next())
              todoCount = parseInt(grTask.getAggregate('COUNT'));
      }

      return todoCount;
  },

  type: 'hr_CaseChildTodoUtils'
};

Sys ID

ac12ece953e033000ad0ddeeff7b1296

Offical Documentation

Official Docs: