Files
luos/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Participated.php

359 lines
15 KiB
PHP
Raw Normal View History

<?php
namespace ProcessMaker\BusinessModel\Cases;
2020-12-09 23:21:32 +00:00
use ProcessMaker\Model\Application;
2020-12-16 17:47:42 -04:00
use ProcessMaker\Model\AppNotes;
2020-10-21 16:56:11 -04:00
use ProcessMaker\Model\Delegation;
2020-11-25 18:11:22 -04:00
use ProcessMaker\Model\Task;
2021-01-13 12:41:53 -04:00
use ProcessMaker\Model\User;
2020-10-21 16:56:11 -04:00
class Participated extends AbstractCases
{
2020-11-11 10:38:08 -04:00
// Columns to see in the cases list
public $columnsView = [
// Columns view in the cases list
'APP_DELEGATION.APP_NUMBER', // Case #
2020-12-09 19:04:05 -04:00
'APP_DELEGATION.DEL_TITLE', // Case Title
2020-11-11 10:38:08 -04:00
'PROCESS.PRO_TITLE', // Process Name
2021-07-12 11:38:27 -04:00
'TASK.TAS_TITLE', // Pending Task
2021-01-19 18:05:16 -04:00
'TASK.TAS_ASSIGN_TYPE', // Task assign rule
2021-07-12 11:38:27 -04:00
'APPLICATION.APP_STATUS', // Status
'APPLICATION.APP_CREATE_DATE', // Start Date
'APPLICATION.APP_FINISH_DATE', // Finish Date
'APP_DELEGATION.DEL_TASK_DUE_DATE', // Due Date related to the colors
'APP_DELEGATION.DEL_PREVIOUS', // Previous
'USERS.USR_ID', // Current UserId
2020-11-11 10:38:08 -04:00
// Additional column for other functionalities
2020-11-25 18:11:22 -04:00
'APP_DELEGATION.APP_UID', // Case Uid for Open case
'APP_DELEGATION.DEL_INDEX', // Del Index for Open case
2020-12-07 17:07:26 -04:00
'APP_DELEGATION.PRO_UID', // Process Uid for Case notes
'APP_DELEGATION.TAS_UID', // Task Uid for Case notes
2020-11-11 10:38:08 -04:00
];
/**
* Get the columns related to the cases list
* @return array
*/
public function getColumnsView()
{
return $this->columnsView;
}
2020-11-25 18:11:22 -04:00
/**
* Scope filters
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function filters($query)
{
// Specific case
if ($this->getCaseNumber()) {
$query->case($this->getCaseNumber());
}
2021-07-08 12:55:24 -04:00
// Filter only cases by specific cases like [1,3,5]
if (!empty($this->getCasesNumbers()) && empty($this->getRangeCasesFromTo())) {
$query->specificCases($this->getCasesNumbers());
}
// Filter only cases by range of cases like ['1-5', '10-15']
if (!empty($this->getRangeCasesFromTo()) && empty($this->getCasesNumbers())) {
$query->rangeOfCases($this->getRangeCasesFromTo());
}
// Filter cases mixed by range of cases and specific cases like '1,3-5,8'
if (!empty($this->getCasesNumbers()) && !empty($this->getRangeCasesFromTo())) {
$query->casesOrRangeOfCases($this->getCasesNumbers(), $this->getRangeCasesFromTo());
}
2020-11-25 18:11:22 -04:00
// Specific case title
if (!empty($this->getCaseTitle())) {
2020-12-09 19:04:05 -04:00
$query->title($this->getCaseTitle());
2020-11-25 18:11:22 -04:00
}
// Scope to search for an specific process
if ($this->getProcessId()) {
$query->processId($this->getProcessId());
}
// Specific task
if ($this->getTaskId()) {
$query->task($this->getTaskId());
}
// Specific status
if ($this->getCaseStatus()) {
$query->status($this->getCaseStatus());
}
// Specific start case date from
if (!empty($this->getStartCaseFrom())) {
$query->startDateFrom($this->getStartCaseFrom());
}
// Specific by start case date to
if (!empty($this->getStartCaseTo())) {
$query->startDateTo($this->getStartCaseTo());
}
// Specific finish case date from
if (!empty($this->getFinishCaseFrom())) {
$query->finishCaseFrom($this->getFinishCaseFrom());
}
// Filter by finish case date to
if (!empty($this->getFinishCaseTo())) {
$query->finishCaseTo($this->getFinishCaseTo());
}
// Specific case uid PMFCaseLink
if (!empty($this->getCaseUid())) {
$query->appUid($this->getCaseUid());
}
return $query;
}
2020-10-21 16:56:11 -04:00
/**
* Get the data corresponding to Participated
*
* @return array
*/
public function getData()
{
// Start the query for get the cases related to the user
2020-11-25 18:11:22 -04:00
$query = Delegation::query()->select($this->getColumnsView());
2020-11-11 10:38:08 -04:00
// Join with process
$query->joinProcess();
// Join with task
$query->joinTask();
// Join with users
$query->joinUser();
2020-11-25 18:11:22 -04:00
// Join with application
$query->joinApplication();
2020-10-21 16:56:11 -04:00
// Scope to Participated
$query->participated($this->getUserId());
// Add filter
$filter = $this->getParticipatedStatus();
2020-12-07 17:07:26 -04:00
switch ($filter) {
case 'STARTED':
2021-01-19 14:00:55 -04:00
// Scope that search for the STARTED by user: DRAFT, TO_DO, CANCELED AND COMPLETED
2020-12-07 17:07:26 -04:00
$query->caseStarted();
break;
case 'IN_PROGRESS':
2021-01-19 14:00:55 -04:00
// Only cases in progress: TO_DO without DRAFT
$query->caseTodo();
2020-12-07 17:07:26 -04:00
// Group by AppNumber
$query->groupBy('APP_NUMBER');
break;
case 'COMPLETED':
// Scope that search for the COMPLETED
$query->caseCompleted();
// Scope to set the last thread
$query->lastThread();
break;
2020-10-21 16:56:11 -04:00
}
2020-11-25 18:11:22 -04:00
/** Apply filters */
$this->filters($query);
/** Apply order and pagination */
2020-10-21 16:56:11 -04:00
// The order by clause
$query->orderBy($this->getOrderByColumn(), $this->getOrderDirection());
// The limit by clause
$query->offset($this->getOffset())->limit($this->getLimit());
//Execute the query
2020-11-11 10:38:08 -04:00
$results = $query->get();
// Prepare the result
2020-11-25 18:11:22 -04:00
$results->transform(function ($item, $key) use ($filter) {
2020-11-11 10:38:08 -04:00
// Apply the date format defined in environment
2020-11-25 18:11:22 -04:00
$item['APP_CREATE_DATE_LABEL'] = !empty($item['APP_CREATE_DATE']) ? applyMaskDateEnvironment($item['APP_CREATE_DATE']): null;
$item['APP_FINISH_DATE_LABEL'] = !empty($item['APP_FINISH_DATE']) ? applyMaskDateEnvironment($item['APP_FINISH_DATE']): null;
2020-11-11 10:38:08 -04:00
// Calculate duration
2020-11-25 18:11:22 -04:00
$startDate = (string)$item['APP_CREATE_DATE'];
$endDate = !empty($item['APP_FINISH_DATE']) ? $item['APP_FINISH_DATE'] : date("Y-m-d H:i:s");
2020-11-11 10:38:08 -04:00
$item['DURATION'] = getDiffBetweenDates($startDate, $endDate);
2020-12-16 17:47:42 -04:00
// Get total case notes
$item['CASE_NOTES_COUNT'] = AppNotes::total($item['APP_NUMBER']);
2021-01-19 18:05:16 -04:00
// Define the thread information
$thread = [];
$thread['TAS_TITLE'] = $item['TAS_TITLE'];
$thread['USR_ID'] = $item['USR_ID'];
$thread['DEL_TASK_DUE_DATE'] = $item['DEL_TASK_DUE_DATE'];
2021-01-26 17:37:40 -04:00
$thread['APP_FINISH_DATE'] = $item['APP_FINISH_DATE'];
2021-01-19 18:05:16 -04:00
$thread['TAS_ASSIGN_TYPE'] = $item['TAS_ASSIGN_TYPE'];
$thread['APP_STATUS'] = $item['APP_STATUS'];
2020-12-15 11:37:58 -04:00
// Define data according to the filters
2020-11-25 18:11:22 -04:00
switch ($filter) {
case 'STARTED':
2021-01-19 18:05:16 -04:00
case 'IN_PROGRESS':
2021-02-09 12:14:08 -04:00
switch ($item['APP_STATUS']) {
case 'TO_DO':
// Get the pending task
2021-07-26 13:13:26 -04:00
$taskPending = Delegation::getPendingThreads($item['APP_NUMBER'], false);
2021-08-31 11:04:07 -04:00
$result = [];
2021-02-09 12:14:08 -04:00
foreach ($taskPending as $thread) {
$thread['APP_STATUS'] = $item['APP_STATUS'];
// Get the thread information
2021-08-31 11:04:07 -04:00
$information = $this->threadInformation($thread);
$result['THREAD_TASKS'] = [];
$result['THREAD_TITLES'] = [];
$result['THREAD_TASKS'][] = $information['THREAD_TASK'];
$result['THREAD_TITLES'][] = $information['THREAD_TITLE'];
2021-02-09 12:14:08 -04:00
}
2021-08-31 11:04:07 -04:00
// Return THREAD_TASKS and THREAD_USERS in the same column
$item['PENDING'] = !empty($result['THREAD_TASKS']) ? $result['THREAD_TASKS'] : [];
// Return the THREAD_TITLES
$item['THREAD_TITLES'] = !empty($result['THREAD_TITLES']) ? $result['THREAD_TITLES'] : [];
2021-02-09 12:14:08 -04:00
break;
case 'COMPLETED':
// Get the last thread
$taskPending = Delegation::getLastThread($item['APP_NUMBER']);
// Get the head of array
$thread = head($taskPending);
// Define some values required for define the color status
2021-01-19 18:05:16 -04:00
$thread['APP_STATUS'] = $item['APP_STATUS'];
2021-02-09 12:14:08 -04:00
$thread['APP_FINISH_DATE'] = $item['APP_FINISH_DATE'];
// Get the thread information
2021-08-31 11:04:07 -04:00
$information = $this->threadInformation($thread);
$result = [];
$result[] = $information['THREAD_TASK'];
// Return THREAD_TASKS and THREAD_USERS in the same column
2021-02-09 12:14:08 -04:00
$item['PENDING'] = $result;
2021-08-31 11:04:07 -04:00
// Return the THREAD_TITLES
$result = [];
$result[] = $information['THREAD_TITLE'];
$item['THREAD_TITLES'] = $result;
2021-02-09 12:14:08 -04:00
break;
2021-08-31 11:04:07 -04:00
default: // Other status like DRAFT
// Get the last thread
$taskPending = Delegation::getLastThread($item['APP_NUMBER']);
// Get the head of array
$thread = head($taskPending);
// Define some values required for define the color status
$thread['APP_STATUS'] = $item['APP_STATUS'];
$thread['APP_FINISH_DATE'] = $item['APP_FINISH_DATE'];
// Get the thread information
$information = $this->threadInformation($thread);
$result = [];
$result[] = $information['THREAD_TASK'];
// Return THREAD_TASKS and THREAD_USERS in the same column
2021-02-09 12:14:08 -04:00
$item['PENDING'] = $result;
2021-08-31 11:04:07 -04:00
// Return the THREAD_TITLES
$result = [];
$result[] = $information['THREAD_TITLE'];
$item['THREAD_TITLES'] = $result;
2020-11-25 18:11:22 -04:00
}
break;
case 'COMPLETED':
2021-08-31 11:04:07 -04:00
// Get the last thread
$taskPending = Delegation::getLastThread($item['APP_NUMBER']);
// Get the head of array
$thread = head($taskPending);
// Define some values required for define the color status
$thread['APP_STATUS'] = $item['APP_STATUS'];
$thread['APP_FINISH_DATE'] = $item['APP_FINISH_DATE'];
// Get the thread information
$information = $this->threadInformation($thread);
2020-11-25 18:11:22 -04:00
$result = [];
2021-08-31 11:04:07 -04:00
$result[] = $information['THREAD_TASK'];
// Return THREAD_TASKS and THREAD_USERS in the same column
2020-11-25 18:11:22 -04:00
$item['PENDING'] = $result;
2021-08-31 11:04:07 -04:00
// Return the THREAD_TITLES
$result = [];
$result[] = $information['THREAD_TITLE'];
$item['THREAD_TITLES'] = $result;
2020-11-25 18:11:22 -04:00
break;
}
2021-08-31 11:04:07 -04:00
// Get send by related to the previous index
2021-07-12 11:38:27 -04:00
$previousThread = Delegation::getThreadInfo($item['APP_NUMBER'], $item['DEL_PREVIOUS']);
$userInfo = !empty($previousThread) ? User::getInformation($previousThread['USR_ID']) : [];
$result = [];
$result['del_previous'] = $item['DEL_PREVIOUS'];
$result['user_tooltip'] = $userInfo;
$item['SEND_BY_INFO'] = $result;
2020-11-11 10:38:08 -04:00
return $item;
});
return $results->values()->toArray();
2020-10-21 16:56:11 -04:00
}
2020-11-11 10:38:08 -04:00
2020-10-21 16:56:11 -04:00
/**
2020-12-14 15:24:08 -04:00
* Get the number of rows corresponding has Participation, does not apply filters
2020-10-21 16:56:11 -04:00
*
* @return int
*/
2021-01-19 18:05:16 -04:00
2020-10-21 16:56:11 -04:00
public function getCounter()
{
2020-12-09 23:21:32 +00:00
// Get base query
2020-10-21 16:56:11 -04:00
$query = Delegation::query()->select();
2020-12-09 23:21:32 +00:00
// Join with application
$query->joinApplication();
2020-10-21 16:56:11 -04:00
// Scope that sets the queries for Participated
$query->participated($this->getUserId());
2020-12-09 23:21:32 +00:00
// Get filter
$filter = $this->getParticipatedStatus();
switch ($filter) {
case 'STARTED':
2021-01-19 14:00:55 -04:00
// Scope that search for the STARTED by user: DRAFT, TO_DO, CANCELED AND COMPLETED
2020-12-09 23:21:32 +00:00
$query->caseStarted();
break;
case 'IN_PROGRESS':
// Only distinct APP_NUMBER
$query->distinct();
2021-03-30 10:03:46 -04:00
// Scope for only TO_DO cases
2021-01-19 14:00:55 -04:00
$query->caseTodo();
2020-12-09 23:21:32 +00:00
break;
case 'COMPLETED':
// Scope that search for the COMPLETED
$query->caseCompleted();
// Scope to set the last thread
$query->lastThread();
break;
}
2020-10-21 16:56:11 -04:00
// Return the number of rows
2020-12-09 23:21:32 +00:00
return $query->count(['APP_DELEGATION.APP_NUMBER']);
2020-10-21 16:56:11 -04:00
}
2020-12-14 15:24:08 -04:00
2021-08-19 15:23:25 -04:00
/**
* Count if the user has at least one case in the list
*
* @return bool
*/
public function atLeastOne()
{
// This class does not require this value
return false;
}
2020-12-14 15:24:08 -04:00
/**
* Count how many cases the user has Participation, needs to apply filters
*
* @return int
*/
public function getPagingCounters()
{
// Get base query
$query = Delegation::query()->select();
// Join with application
$query->joinApplication();
// Scope that sets the queries for Participated
$query->participated($this->getUserId());
// Get filter
$filter = $this->getParticipatedStatus();
switch ($filter) {
case 'STARTED':
2021-01-19 14:00:55 -04:00
// Scope that search for the STARTED by user: DRAFT, TO_DO, CANCELED AND COMPLETED
2020-12-14 15:24:08 -04:00
$query->caseStarted();
break;
case 'IN_PROGRESS':
// Only distinct APP_NUMBER
$query->distinct();
2021-01-19 14:00:55 -04:00
// Scope for in progress: TO_DO without DRAFT
$query->caseTodo();
2020-12-14 15:24:08 -04:00
break;
case 'COMPLETED':
// Scope that search for the COMPLETED
$query->caseCompleted();
// Scope to set the last thread
$query->lastThread();
break;
}
// Apply filters
$this->filters($query);
// Return the number of rows
return $query->count(['APP_DELEGATION.APP_NUMBER']);
}
}