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

207 lines
7.5 KiB
PHP
Raw Normal View History

2020-11-05 17:50:41 -04:00
<?php
namespace ProcessMaker\BusinessModel\Cases;
2020-11-11 10:38:08 -04:00
use G;
2020-11-25 18:11:22 -04:00
use ProcessMaker\Model\Application;
2020-12-16 17:47:42 -04:00
use ProcessMaker\Model\AppNotes;
2020-11-05 17:50:41 -04:00
use ProcessMaker\Model\Delegation;
2021-03-30 10:03:46 -04:00
use ProcessMaker\Model\Process;
2020-12-15 11:37:58 -04:00
use ProcessMaker\Model\Task;
use ProcessMaker\Model\User;
2020-11-05 17:50:41 -04:00
class Search 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
2021-03-30 10:03:46 -04:00
'APPLICATION.APP_NUMBER', // Case #
'APPLICATION.APP_TITLE AS DEL_TITLE', // Case Title
2020-11-11 10:38:08 -04:00
'PROCESS.PRO_TITLE', // Process
2020-11-25 18:11:22 -04:00
'APPLICATION.APP_STATUS', // Status
2020-12-15 11:37:58 -04:00
'APPLICATION.APP_CREATE_DATE', // Case create date
'APPLICATION.APP_FINISH_DATE', // Case finish date
2020-11-11 10:38:08 -04:00
// Additional column for other functionalities
2021-03-30 10:03:46 -04:00
'APPLICATION.APP_UID', // Case Uid for Open case
'APPLICATION.PRO_UID', // Process 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-05 17:50:41 -04:00
/**
2020-11-25 18:11:22 -04:00
* Scope filters
2020-11-05 17:50:41 -04:00
*
2020-11-25 18:11:22 -04:00
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
2020-11-05 17:50:41 -04:00
*/
2020-11-25 18:11:22 -04:00
public function filters($query)
2020-11-05 17:50:41 -04:00
{
2020-11-25 18:11:22 -04:00
// Filter case by case number
if ($this->getCaseNumber()) {
2020-11-11 10:38:08 -04:00
$query->case($this->getCaseNumber());
}
2020-11-25 18:11:22 -04:00
// Filter cases by specific cases like [1,3,5]
if (!empty($this->getCasesNumbers())) {
$query->specificCases($this->getCasesNumbers());
2020-11-05 17:50:41 -04:00
}
2020-11-25 18:11:22 -04:00
// Filter cases by range of cases like ['1-5', '10-15']
if (!empty($this->getRangeCasesFromTo())) {
$query->rangeOfCases($this->getRangeCasesFromTo());
}
// Specific case title
if (!empty($this->getCaseTitle())) {
2021-03-30 10:03:46 -04:00
// Join with delegation
$query->joinDelegation();
// Add the filter
// $query->title($this->getCaseTitle());
2020-11-05 17:50:41 -04:00
}
// Filter by process
2020-11-25 18:11:22 -04:00
if ($this->getProcessId()) {
2021-03-30 10:03:46 -04:00
$result = Process::query()->select(['PRO_UID'])
->where('PRO_ID', '=', $this->getProcessId())->get()->toArray();
$result = head($result);
$query->proUid($result['PRO_UID']);
2020-11-05 17:50:41 -04:00
}
// Filter by user
2020-11-25 18:11:22 -04:00
if ($this->getUserId()) {
2021-03-30 10:03:46 -04:00
// Join with delegation
$query->joinDelegation();
// Add the filter
2020-11-05 17:50:41 -04:00
$query->userId($this->getUserId());
2021-03-30 10:03:46 -04:00
// Get only the open threads related to the user
$query->where('APP_DELEGATION.DEL_THREAD_STATUS', '=', 'OPEN');
2020-11-05 17:50:41 -04:00
}
// Filter by task
2020-11-25 18:11:22 -04:00
if ($this->getTaskId()) {
2021-03-30 10:03:46 -04:00
// Join with delegation
$query->joinDelegation();
// Add the filter
2020-11-05 17:50:41 -04:00
$query->task($this->getTaskId());
2021-03-30 10:03:46 -04:00
// Get only the open threads related to the task
$query->where('APP_DELEGATION.DEL_THREAD_STATUS', '=', 'OPEN');
2020-11-05 17:50:41 -04:00
}
2020-12-15 11:37:58 -04:00
// Specific start case date from
if (!empty($this->getStartCaseFrom())) {
$query->startDateFrom($this->getStartCaseFrom());
2020-11-25 18:11:22 -04:00
}
2020-12-15 11:37:58 -04:00
// Specific by start case date to
if (!empty($this->getStartCaseTo())) {
$query->startDateTo($this->getStartCaseTo());
2020-11-25 18:11:22 -04:00
}
2020-12-15 11:37:58 -04:00
// Specific finish case date from
if (!empty($this->getFinishCaseFrom())) {
$query->finishCaseFrom($this->getFinishCaseFrom());
2020-11-25 18:11:22 -04:00
}
2020-12-15 11:37:58 -04:00
// Filter by finish case date to
if (!empty($this->getFinishCaseTo())) {
$query->finishCaseTo($this->getFinishCaseTo());
2020-11-25 18:11:22 -04:00
}
// Filter related to the case status like ['DRAFT', 'TO_DO']
if (!empty($this->getCaseStatuses())) {
2021-03-30 10:03:46 -04:00
$query->statusIds($this->getCaseStatuses());
2020-11-25 18:11:22 -04:00
}
return $query;
}
/**
* Get the data corresponding to advanced search
*
* @return array
*/
public function getData()
{
2021-03-30 10:03:46 -04:00
$query = Application::query()->select($this->getColumnsView());
2020-11-25 18:11:22 -04:00
// Join with process
$query->joinProcess();
/** Apply filters */
$this->filters($query);
2020-12-22 15:21:08 -04:00
/** Exclude the web entries does not submitted */
$query->positiveCases($query);
2020-11-25 18:11:22 -04:00
/** Apply order and pagination */
2020-11-05 17:50:41 -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
$results = $query->get();
2020-11-11 10:38:08 -04:00
// Prepare the result
$results->transform(function ($item, $key) {
// Apply the date format defined in environment
2020-12-15 11:37:58 -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;
// Calculate duration
$startDate = (string)$item['APP_CREATE_DATE'];
$endDate = !empty($item['APP_FINISH_DATE']) ? $item['APP_FINISH_DATE'] : date("Y-m-d H:i:s");
2021-01-27 09:55:52 -04:00
$dateToCompare = !empty($item['APP_FINISH_DATE']) ? $item['APP_FINISH_DATE'] : 'now';
2020-12-15 11:37:58 -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']);
2020-12-15 11:37:58 -04:00
// Get the detail related to the open thread
2021-03-30 10:03:46 -04:00
$taskPending = [];
$status = $item['APP_STATUS'];
switch ($status) {
case 'DRAFT':
case 'TO_DO':
$taskPending = Delegation::getPendingThreads($item['APP_NUMBER']);
break;
case 'COMPLETED':
case 'CANCELLED':
$taskPending = Delegation::getLastThread($item['APP_NUMBER']);
break;
2020-12-15 11:37:58 -04:00
}
2021-03-30 10:03:46 -04:00
$i = 0;
$result = [];
foreach ($taskPending as $thread) {
$thread['APP_STATUS'] = $item['APP_STATUS'];
$information = $this->threadInformation($thread, true, true);
$result['THREAD_TASKS'][$i] = $information['THREAD_TASK'];
$result['THREAD_USERS'][$i] = $information['THREAD_USER'];
$result['THREAD_TITLES'][$i] = $information['THREAD_TITLE'];
$i++;
// Del Index for Open case
$item['DEL_INDEX'] = $information['THREAD_TITLE']['del_index'];
// Task Uid for Case notes
$item['TAS_UID'] = $information['THREAD_TASK']['tas_uid'];
}
$item['THREAD_TASKS'] = !empty($result['THREAD_TASKS']) ? $result['THREAD_TASKS'] : [];
$item['THREAD_USERS'] = !empty($result['THREAD_USERS']) ? $result['THREAD_USERS'] : [];
$item['THREAD_TITLES'] = !empty($result['THREAD_TITLES']) ? $result['THREAD_TITLES'] : [];
2020-11-11 10:38:08 -04:00
return $item;
});
2020-11-05 17:50:41 -04:00
return $results->values()->toArray();
}
/**
2020-12-14 15:24:08 -04:00
* Count how many cases the user has in the advanced search, does not apply filters
2020-11-05 17:50:41 -04:00
*
* @return int
*/
public function getCounter()
{
2020-12-14 15:24:08 -04:00
// The search does not have a counters
return 0;
}
2020-11-05 17:50:41 -04:00
2020-12-14 15:24:08 -04:00
/**
* Get the number of rows corresponding to the advanced search, needs to apply filters
*
* @return int
*/
public function getPagingCounters()
{
// The search always will enable the pagination
return 0;
2020-11-05 17:50:41 -04:00
}
}