2019-04-25 13:47:21 -07:00
|
|
|
<?php
|
|
|
|
|
|
2019-04-25 13:54:39 -07:00
|
|
|
namespace ProcessMaker\Model;
|
2019-04-25 13:47:21 -07:00
|
|
|
|
2020-10-02 12:53:58 -04:00
|
|
|
use DateTime;
|
2019-04-25 15:32:23 -07:00
|
|
|
use G;
|
2019-04-25 17:28:15 -07:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2019-04-25 18:02:32 -07:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2020-05-07 19:14:11 +00:00
|
|
|
use ProcessMaker\Core\System;
|
2019-04-25 13:47:21 -07:00
|
|
|
|
|
|
|
|
class Delegation extends Model
|
|
|
|
|
{
|
2019-06-10 15:04:14 -04:00
|
|
|
// Class constants
|
|
|
|
|
const PRIORITIES_MAP = [1 => 'VL', 2 => 'L', 3 => 'N', 4 => 'H', 5 => 'VH'];
|
|
|
|
|
|
2019-04-25 13:47:21 -07:00
|
|
|
protected $table = "APP_DELEGATION";
|
|
|
|
|
|
2019-04-25 14:15:41 -07:00
|
|
|
// We don't have our standard timestamp columns
|
2019-04-25 15:32:23 -07:00
|
|
|
public $timestamps = false;
|
2019-04-25 14:15:41 -07:00
|
|
|
|
2019-12-24 15:14:24 -04:00
|
|
|
// Static properties to preserve values
|
|
|
|
|
public static $usrUid = '';
|
|
|
|
|
public static $groups = [];
|
|
|
|
|
|
2019-04-25 13:47:21 -07:00
|
|
|
/**
|
|
|
|
|
* Returns the application this delegation belongs to
|
|
|
|
|
*/
|
|
|
|
|
public function application()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Application::class, 'APP_UID', 'APP_UID');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the user this delegation belongs to
|
|
|
|
|
*/
|
|
|
|
|
public function user()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class, 'USR_ID', 'USR_ID');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the process task this belongs to
|
|
|
|
|
*/
|
|
|
|
|
public function task()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Task::class, 'TAS_ID', 'TAS_ID');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the process this delegation belongs to
|
|
|
|
|
*/
|
|
|
|
|
public function process()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Process::class, 'PRO_ID', 'PRO_ID');
|
|
|
|
|
}
|
2019-04-25 15:32:23 -07:00
|
|
|
|
2019-06-10 15:04:14 -04:00
|
|
|
/**
|
|
|
|
|
* Scope a query to only include open threads
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
|
|
|
|
public function scopeThreadOpen($query)
|
|
|
|
|
{
|
|
|
|
|
return $query->where('DEL_THREAD_STATUS', '=', 'OPEN');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scope a query to only include a specific index
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @param int $index
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
|
|
|
|
public function scopeIndex($query, $index)
|
|
|
|
|
{
|
|
|
|
|
return $query->where('DEL_INDEX', '=', $index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scope a query to only include a specific delegate date
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @param string $from
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
|
|
|
|
public function scopeDelegateDateFrom($query, $from)
|
|
|
|
|
{
|
|
|
|
|
return $query->where('DEL_DELEGATE_DATE', '>=', $from);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scope a query to only include a specific delegate date
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @param string $to
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
|
|
|
|
public function scopeDelegateDateTo($query, $to)
|
|
|
|
|
{
|
|
|
|
|
return $query->where('DEL_DELEGATE_DATE', '<=', $to);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scope a query to get only the date on time
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
|
|
|
|
public function scopeOnTime($query)
|
|
|
|
|
{
|
|
|
|
|
return $query->whereRaw('TIMEDIFF(DEL_RISK_DATE, NOW()) > 0');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scope a query to get only the date at risk
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
|
|
|
|
public function scopeAtRisk($query)
|
|
|
|
|
{
|
|
|
|
|
return $query->whereRaw('TIMEDIFF(DEL_RISK_DATE, NOW()) < 0 AND TIMEDIFF(DEL_TASK_DUE_DATE, NOW()) > 0');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scope a query to get only the date overdue
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
|
|
|
|
public function scopeOverdue($query)
|
|
|
|
|
{
|
|
|
|
|
return $query->whereRaw('TIMEDIFF(DEL_TASK_DUE_DATE, NOW()) < 0');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scope a query to only include a specific case
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @param int $appNumber
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
|
|
|
|
public function scopeCase($query, $appNumber)
|
|
|
|
|
{
|
|
|
|
|
return $query->where('APP_DELEGATION.APP_NUMBER', '=', $appNumber);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scope a query to only include specific cases
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @param array $cases
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
|
|
|
|
public function scopeSpecificCases($query, array $cases)
|
|
|
|
|
{
|
|
|
|
|
return $query->whereIn('APP_DELEGATION.APP_NUMBER', $cases);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 13:23:31 -04:00
|
|
|
/**
|
|
|
|
|
* Scope a query to get the delegations from a case by APP_UID
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @param string $appUid
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
|
|
|
|
public function scopeAppUid($query, $appUid)
|
|
|
|
|
{
|
2019-06-10 15:04:14 -04:00
|
|
|
return $query->where('APP_DELEGATION.APP_UID', '=', $appUid);
|
2019-05-20 13:23:31 -04:00
|
|
|
}
|
|
|
|
|
|
2019-05-13 16:44:40 -04:00
|
|
|
/**
|
2019-06-10 15:04:14 -04:00
|
|
|
* Scope a query to only include specific cases by APP_UID
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @param array $cases
|
2019-05-13 16:44:40 -04:00
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
2019-06-10 15:04:14 -04:00
|
|
|
public function scopeSpecificCasesByUid($query, array $cases)
|
2019-05-13 16:44:40 -04:00
|
|
|
{
|
2019-06-10 15:04:14 -04:00
|
|
|
return $query->whereIn('APP_DELEGATION.APP_UID', $cases);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scope a query to only include specific user
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @param int $user
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
|
|
|
|
public function scopeUserId($query, $user)
|
|
|
|
|
{
|
|
|
|
|
return $query->where('APP_DELEGATION.USR_ID', '=', $user);
|
2019-05-13 16:44:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scope a query to only include threads without user
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
2019-06-10 15:04:14 -04:00
|
|
|
public function scopeWithoutUserId($query)
|
2019-05-13 16:44:40 -04:00
|
|
|
{
|
2019-06-10 15:04:14 -04:00
|
|
|
return $query->where('APP_DELEGATION.USR_ID', '=', 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scope a query to only include specific process
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @param int $process
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
|
|
|
|
public function scopeProcessId($query, $process)
|
|
|
|
|
{
|
|
|
|
|
return $query->where('APP_DELEGATION.PRO_ID', $process);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scope a query to only include a specific task
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @param int $task
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
|
|
|
|
public function scopeTask($query, $task)
|
|
|
|
|
{
|
|
|
|
|
return $query->where('APP_DELEGATION.TAS_ID', '=', $task);
|
2019-05-13 16:44:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scope a query to only include specific tasks
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @param array $tasks
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
2019-06-10 15:04:14 -04:00
|
|
|
public function scopeSpecificTasks($query, array $tasks)
|
2019-05-13 16:44:40 -04:00
|
|
|
{
|
|
|
|
|
return $query->whereIn('APP_DELEGATION.TAS_ID', $tasks);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-06-10 15:04:14 -04:00
|
|
|
* Scope a join with task and include a specific task assign type:
|
|
|
|
|
* BALANCED|MANUAL|EVALUATE|REPORT_TO|SELF_SERVICE|STATIC_MI|CANCEL_MI|MULTIPLE_INSTANCE|MULTIPLE_INSTANCE_VALUE_BASED
|
2019-05-13 16:44:40 -04:00
|
|
|
*
|
2019-06-10 15:04:14 -04:00
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @param string $taskType
|
2019-05-13 16:44:40 -04:00
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
2019-06-10 15:04:14 -04:00
|
|
|
public function scopeTaskAssignType($query, $taskType = 'SELF_SERVICE')
|
2019-05-13 16:44:40 -04:00
|
|
|
{
|
2019-06-10 15:04:14 -04:00
|
|
|
$query->join('TASK', function ($join) use ($taskType) {
|
|
|
|
|
$join->on('APP_DELEGATION.TAS_ID', '=', 'TASK.TAS_ID')
|
|
|
|
|
->where('TASK.TAS_ASSIGN_TYPE', '=', $taskType);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return $query;
|
2019-05-13 16:44:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-06-11 07:51:19 -04:00
|
|
|
* Scope a join with task and exclude a specific task assign type:
|
2019-06-10 15:04:14 -04:00
|
|
|
* NORMAL|ADHOC|SUBPROCESS|HIDDEN|GATEWAYTOGATEWAY|WEBENTRYEVENT|END-MESSAGE-EVENT|START-MESSAGE-EVENT|
|
|
|
|
|
* INTERMEDIATE-THROW-MESSAGE-EVENT|INTERMEDIATE-CATCH-MESSAGE-EVENT|SCRIPT-TASK|START-TIMER-EVENT|
|
|
|
|
|
* INTERMEDIATE-CATCH-TIMER-EVENT|END-EMAIL-EVENT|INTERMEDIATE-THROW-EMAIL-EVENT|SERVICE-TASK
|
2019-05-13 16:44:40 -04:00
|
|
|
*
|
2019-06-10 15:04:14 -04:00
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @param array $taskTypes
|
2019-05-13 16:44:40 -04:00
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
2019-06-11 07:51:19 -04:00
|
|
|
public function scopeExcludeTaskTypes($query, array $taskTypes)
|
2019-05-13 16:44:40 -04:00
|
|
|
{
|
2019-06-10 15:04:14 -04:00
|
|
|
$query->join('TASK', function ($join) use ($taskTypes) {
|
|
|
|
|
$join->on('APP_DELEGATION.TAS_ID', '=', 'TASK.TAS_ID')
|
2019-06-11 07:51:19 -04:00
|
|
|
->whereNotIn('TASK.TAS_TYPE', $taskTypes);
|
2019-06-10 15:04:14 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return $query;
|
2019-05-13 16:44:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-06-10 15:04:14 -04:00
|
|
|
* Scope a join with APPLICATION with specific app status
|
2019-05-13 16:44:40 -04:00
|
|
|
*
|
2019-06-10 15:04:14 -04:00
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @param integer $statusId
|
2019-05-13 16:44:40 -04:00
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
2019-06-10 15:04:14 -04:00
|
|
|
public function scopeAppStatusId($query, $statusId = 2)
|
2019-05-13 16:44:40 -04:00
|
|
|
{
|
2019-06-10 15:04:14 -04:00
|
|
|
$query->join('APPLICATION', function ($join) use ($statusId) {
|
|
|
|
|
$join->on('APP_DELEGATION.APP_NUMBER', '=', 'APPLICATION.APP_NUMBER')
|
|
|
|
|
->where('APPLICATION.APP_STATUS_ID', $statusId);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scope a join with APPLICATION with specific app status
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @param string $category
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
|
|
|
|
public function scopeCategoryProcess($query, $category = '')
|
|
|
|
|
{
|
|
|
|
|
$query->join('PROCESS', function ($join) use ($category) {
|
|
|
|
|
$join->on('APP_DELEGATION.PRO_ID', '=', 'PROCESS.PRO_ID');
|
|
|
|
|
if ($category) {
|
|
|
|
|
$join->where('PROCESS.PRO_CATEGORY', $category);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-11 07:51:19 -04:00
|
|
|
/**
|
|
|
|
|
* Scope the Inbox cases
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @param int $userId
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
|
|
|
|
public function scopeInbox($query, $userId)
|
|
|
|
|
{
|
|
|
|
|
// This scope is for the join with the APP_DELEGATION table
|
|
|
|
|
$query->appStatusId(2);
|
|
|
|
|
|
|
|
|
|
// Scope for the restriction of the task that must not be searched for
|
|
|
|
|
$query->excludeTaskTypes(Task::DUMMY_TASKS);
|
|
|
|
|
|
|
|
|
|
// Scope that establish that the DEL_THREAD_STATUS must be OPEN
|
|
|
|
|
$query->threadOpen();
|
|
|
|
|
|
|
|
|
|
// Scope that return the results for an specific user
|
|
|
|
|
$query->userId($userId);
|
|
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-10 15:04:14 -04:00
|
|
|
/**
|
|
|
|
|
* Scope a self service cases
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @param string $user
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
|
|
|
|
public function scopeSelfService($query, $user)
|
|
|
|
|
{
|
|
|
|
|
// Add Join with task filtering only the type self-service
|
|
|
|
|
$query->taskAssignType('SELF_SERVICE');
|
|
|
|
|
// Filtering the open threads and without users
|
|
|
|
|
$query->threadOpen()->withoutUserId();
|
|
|
|
|
// Filtering the cases unassigned that the user can view
|
|
|
|
|
$this->casesUnassigned($query, $user);
|
|
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get specific cases unassigned that the user can view
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @param string $usrUid
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
|
|
|
|
public function casesUnassigned(&$query, $usrUid)
|
|
|
|
|
{
|
|
|
|
|
//Get the task self services related to the user
|
|
|
|
|
$taskSelfService = TaskUser::getSelfServicePerUser($usrUid);
|
|
|
|
|
//Get the task self services value based related to the user
|
|
|
|
|
$selfServiceValueBased = AppAssignSelfServiceValue::getSelfServiceCasesByEvaluatePerUser($usrUid);
|
|
|
|
|
|
|
|
|
|
//Get the cases unassigned
|
|
|
|
|
if (!empty($selfServiceValueBased)) {
|
|
|
|
|
$query->where(function ($query) use ($selfServiceValueBased, $taskSelfService) {
|
|
|
|
|
//Get the cases related to the task self service
|
|
|
|
|
$query->specificTasks($taskSelfService);
|
|
|
|
|
foreach ($selfServiceValueBased as $case) {
|
|
|
|
|
//Get the cases related to the task self service value based
|
|
|
|
|
$query->orWhere(function ($query) use ($case) {
|
|
|
|
|
$query->case($case['APP_NUMBER'])->index($case['DEL_INDEX'])->task($case['TAS_ID']);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
//Get the cases related to the task self service
|
|
|
|
|
$query->specificTasks($taskSelfService);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $query;
|
2019-05-13 16:44:40 -04:00
|
|
|
}
|
|
|
|
|
|
2019-04-25 15:32:23 -07:00
|
|
|
/**
|
|
|
|
|
* Searches for delegations which match certain criteria
|
|
|
|
|
*
|
|
|
|
|
* The query is related to advanced search with different filters
|
|
|
|
|
* We can search by process, status of case, category of process, users, delegate date from and to
|
|
|
|
|
*
|
2019-05-01 13:49:05 -07:00
|
|
|
* @param integer $userId The USR_ID to search for (Note, this is no longer the USR_UID)
|
2019-04-25 15:32:23 -07:00
|
|
|
* @param integer $start for the pagination
|
|
|
|
|
* @param integer $limit for the pagination
|
|
|
|
|
* @param string $search
|
|
|
|
|
* @param integer $process the pro_id
|
|
|
|
|
* @param integer $status of the case
|
|
|
|
|
* @param string $dir if the order is DESC or ASC
|
|
|
|
|
* @param string $sort name of column by sort, can be:
|
|
|
|
|
* [APP_NUMBER, APP_TITLE, APP_PRO_TITLE, APP_TAS_TITLE, APP_CURRENT_USER, APP_UPDATE_DATE, DEL_DELEGATE_DATE, DEL_TASK_DUE_DATE, APP_STATUS_LABEL]
|
|
|
|
|
* @param string $category uid for the process
|
|
|
|
|
* @param date $dateFrom
|
|
|
|
|
* @param date $dateTo
|
|
|
|
|
* @param string $filterBy name of column for a specific search, can be: [APP_NUMBER, APP_TITLE, TAS_TITLE]
|
|
|
|
|
* @return array $result result of the query
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public static function search(
|
2019-05-01 13:49:05 -07:00
|
|
|
$userId = null,
|
|
|
|
|
// Default pagination values
|
|
|
|
|
$start = 0,
|
|
|
|
|
$limit = 25,
|
2019-04-25 15:32:23 -07:00
|
|
|
$search = null,
|
|
|
|
|
$process = null,
|
|
|
|
|
$status = null,
|
|
|
|
|
$dir = null,
|
|
|
|
|
$sort = null,
|
|
|
|
|
$category = null,
|
|
|
|
|
$dateFrom = null,
|
|
|
|
|
$dateTo = null,
|
|
|
|
|
$filterBy = 'APP_TITLE'
|
2019-04-25 17:28:15 -07:00
|
|
|
) {
|
2019-04-25 16:10:12 -07:00
|
|
|
$search = trim($search);
|
|
|
|
|
|
2019-04-25 18:02:32 -07:00
|
|
|
// Start the query builder, selecting our base attributes
|
|
|
|
|
$selectColumns = [
|
|
|
|
|
'APPLICATION.APP_NUMBER',
|
|
|
|
|
'APPLICATION.APP_UID',
|
|
|
|
|
'APPLICATION.APP_STATUS',
|
|
|
|
|
'APPLICATION.APP_STATUS AS APP_STATUS_LABEL',
|
|
|
|
|
'APPLICATION.PRO_UID',
|
|
|
|
|
'APPLICATION.APP_CREATE_DATE',
|
|
|
|
|
'APPLICATION.APP_FINISH_DATE',
|
|
|
|
|
'APPLICATION.APP_UPDATE_DATE',
|
|
|
|
|
'APPLICATION.APP_TITLE',
|
|
|
|
|
'APP_DELEGATION.USR_UID',
|
|
|
|
|
'APP_DELEGATION.TAS_UID',
|
|
|
|
|
'APP_DELEGATION.USR_ID',
|
|
|
|
|
'APP_DELEGATION.PRO_ID',
|
|
|
|
|
'APP_DELEGATION.DEL_INDEX',
|
|
|
|
|
'APP_DELEGATION.DEL_LAST_INDEX',
|
|
|
|
|
'APP_DELEGATION.DEL_DELEGATE_DATE',
|
|
|
|
|
'APP_DELEGATION.DEL_INIT_DATE',
|
|
|
|
|
'APP_DELEGATION.DEL_FINISH_DATE',
|
|
|
|
|
'APP_DELEGATION.DEL_TASK_DUE_DATE',
|
|
|
|
|
'APP_DELEGATION.DEL_RISK_DATE',
|
|
|
|
|
'APP_DELEGATION.DEL_THREAD_STATUS',
|
|
|
|
|
'APP_DELEGATION.DEL_PRIORITY',
|
|
|
|
|
'APP_DELEGATION.DEL_DURATION',
|
|
|
|
|
'APP_DELEGATION.DEL_QUEUE_DURATION',
|
|
|
|
|
'APP_DELEGATION.DEL_STARTED',
|
|
|
|
|
'APP_DELEGATION.DEL_DELAY_DURATION',
|
|
|
|
|
'APP_DELEGATION.DEL_FINISHED',
|
|
|
|
|
'APP_DELEGATION.DEL_DELAYED',
|
|
|
|
|
'APP_DELEGATION.DEL_DELAY_DURATION',
|
|
|
|
|
'TASK.TAS_TITLE AS APP_TAS_TITLE',
|
|
|
|
|
'TASK.TAS_TYPE AS APP_TAS_TYPE',
|
|
|
|
|
];
|
|
|
|
|
$query = DB::table('APP_DELEGATION')->select(DB::raw(implode(',', $selectColumns)));
|
2019-04-25 15:32:23 -07:00
|
|
|
|
2019-04-25 15:58:26 -07:00
|
|
|
// Add join for task, filtering for task title if needed
|
|
|
|
|
// It doesn't make sense for us to search for any delegations that match tasks that are events or web entry
|
2019-04-25 17:28:15 -07:00
|
|
|
$query->join('TASK', function ($join) use ($filterBy, $search) {
|
2019-04-25 15:58:26 -07:00
|
|
|
$join->on('APP_DELEGATION.TAS_ID', '=', 'TASK.TAS_ID')
|
|
|
|
|
->whereNotIn('TASK.TAS_TYPE', [
|
|
|
|
|
'WEBENTRYEVENT',
|
|
|
|
|
'END-MESSAGE-EVENT',
|
|
|
|
|
'START-MESSAGE-EVENT',
|
2019-04-25 17:28:15 -07:00
|
|
|
'INTERMEDIATE-THROW',
|
2019-04-25 15:58:26 -07:00
|
|
|
]);
|
2019-04-25 17:28:15 -07:00
|
|
|
if ($filterBy == 'TAS_TITLE' && $search) {
|
2019-04-25 15:58:26 -07:00
|
|
|
$join->where('TASK.TAS_TITLE', 'LIKE', "%${search}%");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-25 17:28:15 -07:00
|
|
|
// Add join for application, taking care of status and filtering if necessary
|
|
|
|
|
$query->join('APPLICATION', function ($join) use ($filterBy, $search, $status, $query) {
|
2019-04-26 08:45:38 -07:00
|
|
|
$join->on('APP_DELEGATION.APP_NUMBER', '=', 'APPLICATION.APP_NUMBER');
|
2019-04-25 17:28:15 -07:00
|
|
|
if ($filterBy == 'APP_TITLE' && $search) {
|
2020-05-07 19:14:11 +00:00
|
|
|
$config = System::getSystemConfiguration();
|
|
|
|
|
if ((int)$config['disable_advanced_search_case_title_fulltext'] === 0) {
|
|
|
|
|
// Cleaning "fulltext" operators in order to avoid unexpected results
|
|
|
|
|
$search = str_replace(['-', '+', '<', '>', '(', ')', '~', '*', '"'],
|
|
|
|
|
['', '', '', '', '', '', '', '', ''], $search);
|
2019-07-26 10:01:39 -04:00
|
|
|
|
2020-05-07 19:14:11 +00:00
|
|
|
// Build the "fulltext" expression
|
|
|
|
|
$search = '+"' . preg_replace('/\s+/', '" +"', addslashes($search)) . '"';
|
2019-07-26 10:01:39 -04:00
|
|
|
|
2020-05-07 19:14:11 +00:00
|
|
|
// Searching using "fulltext" index
|
|
|
|
|
$join->whereRaw("MATCH(APPLICATION.APP_TITLE) AGAINST('{$search}' IN BOOLEAN MODE)");
|
|
|
|
|
} else {
|
|
|
|
|
// Searching using "like" operator
|
|
|
|
|
$join->where('APPLICATION.APP_TITLE', 'LIKE', "%${search}%");
|
|
|
|
|
}
|
2019-04-25 17:28:15 -07:00
|
|
|
}
|
|
|
|
|
// Based on the below, we can further limit the join so that we have a smaller data set based on join criteria
|
|
|
|
|
switch ($status) {
|
|
|
|
|
case 1: //DRAFT
|
|
|
|
|
$join->where('APPLICATION.APP_STATUS_ID', 1);
|
|
|
|
|
break;
|
|
|
|
|
case 2: //TO_DO
|
|
|
|
|
$join->where('APPLICATION.APP_STATUS_ID', 2);
|
|
|
|
|
break;
|
|
|
|
|
case 3: //COMPLETED
|
|
|
|
|
$join->where('APPLICATION.APP_STATUS_ID', 3);
|
|
|
|
|
break;
|
|
|
|
|
case 4: //CANCELLED
|
|
|
|
|
$join->where('APPLICATION.APP_STATUS_ID', 4);
|
|
|
|
|
break;
|
|
|
|
|
case "PAUSED":
|
|
|
|
|
$join->where('APPLICATION.APP_STATUS', 'TO_DO');
|
|
|
|
|
break;
|
|
|
|
|
default: //All status
|
|
|
|
|
// Don't do anything here, we'll need to do the more advanced where below
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-04-25 15:58:26 -07:00
|
|
|
|
2019-04-25 16:50:52 -07:00
|
|
|
// Add join for process, but only for certain scenarios such as category or process
|
2019-05-22 10:27:00 -04:00
|
|
|
if ($category || $process || $sort == 'APP_PRO_TITLE') {
|
2019-04-25 17:28:15 -07:00
|
|
|
$query->join('PROCESS', function ($join) use ($category) {
|
2019-04-25 16:50:52 -07:00
|
|
|
$join->on('APP_DELEGATION.PRO_ID', '=', 'PROCESS.PRO_ID');
|
2019-04-25 17:28:15 -07:00
|
|
|
if ($category) {
|
2019-04-25 16:50:52 -07:00
|
|
|
$join->where('PROCESS.PRO_CATEGORY', $category);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add join for user, but only for certain scenarios as sorting
|
2019-04-25 17:28:15 -07:00
|
|
|
if ($sort == 'APP_CURRENT_USER') {
|
2019-05-01 13:49:05 -07:00
|
|
|
$query->join('USERS', function ($join) use ($userId) {
|
2019-04-25 16:50:52 -07:00
|
|
|
$join->on('APP_DELEGATION.USR_ID', '=', 'USERS.USR_ID');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Search for specified user
|
2019-05-01 13:49:05 -07:00
|
|
|
if ($userId) {
|
|
|
|
|
$query->where('APP_DELEGATION.USR_ID', $userId);
|
2019-04-25 16:50:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Search for specified process
|
2019-04-25 17:28:15 -07:00
|
|
|
if ($process) {
|
2019-04-25 16:50:52 -07:00
|
|
|
$query->where('APP_DELEGATION.PRO_ID', $process);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Search for an app/case number
|
2019-04-25 17:28:15 -07:00
|
|
|
if ($filterBy == 'APP_NUMBER' && $search) {
|
2019-07-12 15:46:00 -04:00
|
|
|
$query->where('APP_DELEGATION.APP_NUMBER', '=', $search);
|
2019-04-25 16:10:12 -07:00
|
|
|
}
|
|
|
|
|
|
2019-04-25 16:50:52 -07:00
|
|
|
// Date range filter
|
|
|
|
|
if (!empty($dateFrom)) {
|
|
|
|
|
$query->where('APP_DELEGATION.DEL_DELEGATE_DATE', '>=', $dateFrom);
|
|
|
|
|
}
|
|
|
|
|
if (!empty($dateTo)) {
|
|
|
|
|
$dateTo = $dateTo . " 23:59:59";
|
|
|
|
|
// This is inclusive
|
|
|
|
|
$query->where('APP_DELEGATION.DEL_DELEGATE_DATE', '<=', $dateTo);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 17:28:15 -07:00
|
|
|
// Status Filter
|
|
|
|
|
// This is tricky, the below behavior is combined with the application join behavior above
|
|
|
|
|
switch ($status) {
|
|
|
|
|
case 1: //DRAFT
|
|
|
|
|
$query->where('APP_DELEGATION.DEL_THREAD_STATUS', 'OPEN');
|
|
|
|
|
break;
|
|
|
|
|
case 2: //TO_DO
|
|
|
|
|
$query->where('APP_DELEGATION.DEL_THREAD_STATUS', 'OPEN');
|
|
|
|
|
break;
|
|
|
|
|
case 3: //COMPLETED
|
|
|
|
|
$query->where('APP_DELEGATION.DEL_LAST_INDEX', 1);
|
|
|
|
|
break;
|
|
|
|
|
case 4: //CANCELLED
|
|
|
|
|
$query->where('APP_DELEGATION.DEL_LAST_INDEX', 1);
|
|
|
|
|
break;
|
|
|
|
|
case "PAUSED":
|
|
|
|
|
// Do nothing, as the app status check for TO_DO is performed in the join above
|
|
|
|
|
break;
|
|
|
|
|
default: //All statuses.
|
|
|
|
|
$query->where(function ($query) {
|
|
|
|
|
// Check to see if thread status is open
|
|
|
|
|
$query->where('APP_DELEGATION.DEL_THREAD_STATUS', 'OPEN')
|
|
|
|
|
->orWhere(function ($query) {
|
|
|
|
|
// Or, we make sure if the thread is closed, and it's the last delegation, and if the app is completed or cancelled
|
|
|
|
|
$query->where('APP_DELEGATION.DEL_THREAD_STATUS', 'CLOSED')
|
|
|
|
|
->where('APP_DELEGATION.DEL_LAST_INDEX', 1)
|
|
|
|
|
->whereIn('APPLICATION.APP_STATUS_ID', [3, 4]);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-04-25 16:50:52 -07:00
|
|
|
|
2019-04-25 16:10:12 -07:00
|
|
|
// Add any sort if needed
|
2019-06-11 07:51:19 -04:00
|
|
|
if ($sort) {
|
2019-05-01 13:49:05 -07:00
|
|
|
switch ($sort) {
|
|
|
|
|
case 'APP_NUMBER':
|
|
|
|
|
$query->orderBy('APP_DELEGATION.APP_NUMBER', $dir);
|
|
|
|
|
break;
|
|
|
|
|
case 'APP_PRO_TITLE':
|
|
|
|
|
// We can do this because we joined the process table if sorting by it
|
|
|
|
|
$query->orderBy('PROCESS.PRO_TITLE', $dir);
|
|
|
|
|
break;
|
|
|
|
|
case 'APP_TAS_TITLE':
|
|
|
|
|
$query->orderBy('TASK.TAS_TITLE', $dir);
|
|
|
|
|
break;
|
|
|
|
|
case 'APP_CURRENT_USER':
|
|
|
|
|
// We can do this because we joined the user table if sorting by it
|
|
|
|
|
$query->orderBy('USERS.USR_LASTNAME', $dir);
|
|
|
|
|
$query->orderBy('USERS.USR_FIRSTNAME', $dir);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
$query->orderBy($sort, $dir);
|
|
|
|
|
}
|
2019-04-25 16:10:12 -07:00
|
|
|
}
|
|
|
|
|
|
2019-04-25 15:32:23 -07:00
|
|
|
// Add pagination to the query
|
|
|
|
|
$query = $query->offset($start)
|
|
|
|
|
->limit($limit);
|
|
|
|
|
|
|
|
|
|
// Fetch results and transform to a laravel collection
|
2019-04-25 18:02:32 -07:00
|
|
|
$results = $query->get();
|
2019-04-25 15:32:23 -07:00
|
|
|
|
|
|
|
|
// Transform with additional data
|
2019-04-25 17:28:15 -07:00
|
|
|
$priorities = ['1' => 'VL', '2' => 'L', '3' => 'N', '4' => 'H', '5' => 'VH'];
|
|
|
|
|
$results->transform(function ($item, $key) use ($priorities) {
|
2019-04-25 18:02:32 -07:00
|
|
|
// Convert to an array as our results must be an array
|
|
|
|
|
$item = json_decode(json_encode($item), true);
|
2019-05-02 14:06:55 -07:00
|
|
|
// If it's assigned, fetch the user
|
2019-06-11 07:51:19 -04:00
|
|
|
if ($item['USR_ID']) {
|
2019-05-02 14:06:55 -07:00
|
|
|
$user = User::where('USR_ID', $item['USR_ID'])->first();
|
2019-06-11 07:51:19 -04:00
|
|
|
} else {
|
2019-05-02 14:06:55 -07:00
|
|
|
$user = null;
|
2019-04-25 15:32:23 -07:00
|
|
|
}
|
2019-05-02 14:06:55 -07:00
|
|
|
$process = Process::where('PRO_ID', $item['PRO_ID'])->first();
|
2019-04-25 17:28:15 -07:00
|
|
|
|
2019-04-25 15:32:23 -07:00
|
|
|
// Rewrite priority string
|
2019-04-25 17:28:15 -07:00
|
|
|
if ($item['DEL_PRIORITY']) {
|
2019-05-07 12:32:34 -04:00
|
|
|
$item['DEL_PRIORITY'] = G::LoadTranslation("ID_PRIORITY_{$priorities[$item['DEL_PRIORITY']]}");
|
2019-04-25 15:32:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Merge in desired application data
|
2019-04-25 17:28:15 -07:00
|
|
|
if ($item['APP_STATUS']) {
|
2019-05-07 12:32:34 -04:00
|
|
|
$item['APP_STATUS_LABEL'] = G::LoadTranslation("ID_${item['APP_STATUS']}");
|
2019-04-25 15:32:23 -07:00
|
|
|
} else {
|
2019-05-07 12:32:34 -04:00
|
|
|
$item['APP_STATUS_LABEL'] = $item['APP_STATUS'];
|
2019-04-25 15:32:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Merge in desired process data
|
2019-05-02 14:06:55 -07:00
|
|
|
// Handle situation where the process might not be in the system anymore
|
|
|
|
|
$item['APP_PRO_TITLE'] = $process ? $process->PRO_TITLE : '';
|
2019-04-25 15:32:23 -07:00
|
|
|
|
|
|
|
|
// Merge in desired user data
|
2019-05-02 14:06:55 -07:00
|
|
|
$item['USR_LASTNAME'] = $user ? $user->USR_LASTNAME : '';
|
|
|
|
|
$item['USR_FIRSTNAME'] = $user ? $user->USR_FIRSTNAME : '';
|
|
|
|
|
$item['USR_USERNAME'] = $user ? $user->USR_USERNAME : '';
|
2019-04-25 15:32:23 -07:00
|
|
|
|
|
|
|
|
//@todo: this section needs to use 'User Name Display Format', currently in the extJs is defined this
|
2019-04-25 17:28:15 -07:00
|
|
|
$item["APP_CURRENT_USER"] = $item["USR_LASTNAME"] . ' ' . $item["USR_FIRSTNAME"];
|
2019-04-25 15:32:23 -07:00
|
|
|
|
|
|
|
|
$item["APPDELCR_APP_TAS_TITLE"] = '';
|
|
|
|
|
|
|
|
|
|
$item["USRCR_USR_UID"] = $item["USR_UID"];
|
|
|
|
|
$item["USRCR_USR_FIRSTNAME"] = $item["USR_FIRSTNAME"];
|
|
|
|
|
$item["USRCR_USR_LASTNAME"] = $item["USR_LASTNAME"];
|
|
|
|
|
$item["USRCR_USR_USERNAME"] = $item["USR_USERNAME"];
|
|
|
|
|
$item["APP_OVERDUE_PERCENTAGE"] = '';
|
|
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Remove any empty erroenous data
|
|
|
|
|
$results = $results->filter();
|
|
|
|
|
|
|
|
|
|
// Bundle into response array
|
|
|
|
|
$response = [
|
|
|
|
|
// Fake totalCount to show pagination
|
|
|
|
|
'totalCount' => $start + $limit + 1,
|
2019-04-25 16:10:12 -07:00
|
|
|
'sql' => $query->toSql(),
|
2019-04-25 16:50:52 -07:00
|
|
|
'bindings' => $query->getBindings(),
|
2019-05-02 10:53:43 -04:00
|
|
|
'data' => $results->values()->toArray(),
|
2019-04-25 15:32:23 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 13:23:31 -04:00
|
|
|
/**
|
|
|
|
|
* Get participation information for a case
|
|
|
|
|
*
|
|
|
|
|
* @param string $appUid
|
|
|
|
|
* @return array
|
|
|
|
|
*
|
2019-06-10 15:04:14 -04:00
|
|
|
* @see \ProcessMaker\BusinessModel\Cases:getStatusInfo()
|
2019-05-20 13:23:31 -04:00
|
|
|
*/
|
|
|
|
|
public static function getParticipatedInfo($appUid)
|
|
|
|
|
{
|
|
|
|
|
// Build the query
|
|
|
|
|
$query = Delegation::query()->select([
|
|
|
|
|
'APP_UID',
|
|
|
|
|
'DEL_INDEX',
|
|
|
|
|
'PRO_UID'
|
|
|
|
|
]);
|
|
|
|
|
$query->appUid($appUid);
|
|
|
|
|
$query->orderBy('DEL_INDEX', 'ASC');
|
|
|
|
|
|
|
|
|
|
// Fetch results
|
|
|
|
|
$results = $query->get();
|
|
|
|
|
|
|
|
|
|
// Initialize the array to return
|
|
|
|
|
$arrayData = [];
|
|
|
|
|
|
|
|
|
|
// If the collection have at least one item, build the main array to return
|
|
|
|
|
if ($results->count() > 0) {
|
|
|
|
|
// Get the first item
|
|
|
|
|
$first = $results->first();
|
|
|
|
|
|
|
|
|
|
// Build the main array to return
|
|
|
|
|
$arrayData = [
|
2019-06-11 07:51:19 -04:00
|
|
|
'APP_STATUS' => 'PARTICIPATED',
|
|
|
|
|
// Value hardcoded because we need to return the same structure previously sent
|
|
|
|
|
'DEL_INDEX' => [],
|
|
|
|
|
// Initialize this item like an array
|
2019-05-20 13:23:31 -04:00
|
|
|
'PRO_UID' => $first->PRO_UID
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Populate the DEL_INDEX key with the values of the items collected
|
|
|
|
|
$results->each(function ($item) use (&$arrayData) {
|
|
|
|
|
$arrayData['DEL_INDEX'][] = $item->DEL_INDEX;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $arrayData;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-13 16:44:40 -04:00
|
|
|
/**
|
2020-01-31 16:10:56 -04:00
|
|
|
* Get the self-services query by user
|
2019-05-13 16:44:40 -04:00
|
|
|
*
|
|
|
|
|
* @param string $usrUid
|
2020-01-31 16:10:56 -04:00
|
|
|
* @param bool $count
|
2019-05-13 16:44:40 -04:00
|
|
|
*
|
2020-01-31 16:10:56 -04:00
|
|
|
* @return \Illuminate\Database\Query\Builder | string
|
2019-05-13 16:44:40 -04:00
|
|
|
*/
|
2020-01-31 16:10:56 -04:00
|
|
|
public static function getSelfServiceQuery($usrUid, $count = false)
|
2019-05-13 16:44:40 -04:00
|
|
|
{
|
2019-12-24 15:14:24 -04:00
|
|
|
// Set the 'usrUid' property to preserve
|
|
|
|
|
Delegation::$usrUid = $usrUid;
|
|
|
|
|
|
|
|
|
|
// Get and build the groups parameter related to the user
|
|
|
|
|
$groups = GroupUser::getGroups($usrUid);
|
|
|
|
|
$groups = array_map(function ($value) {
|
|
|
|
|
return "'" . $value['GRP_ID'] . "'";
|
|
|
|
|
}, $groups);
|
|
|
|
|
|
|
|
|
|
// Add dummy value to avoid syntax error in complex join
|
|
|
|
|
$groups[] = "'-1'";
|
|
|
|
|
|
|
|
|
|
// Set the 'groups' property to preserve
|
|
|
|
|
Delegation::$groups = $groups;
|
|
|
|
|
|
|
|
|
|
// Start the first query
|
2020-03-23 09:26:34 -04:00
|
|
|
$query1 = Delegation::query()->select(['APP_NUMBER', 'DEL_INDEX']);
|
2019-12-24 15:14:24 -04:00
|
|
|
|
|
|
|
|
// Add the join clause
|
|
|
|
|
$query1->join('TASK', function ($join) {
|
|
|
|
|
// Build partial plain query for a complex Join, because Eloquent doesn't support this type of Join
|
|
|
|
|
$complexJoin = "
|
|
|
|
|
((`APP_DELEGATION`.`APP_NUMBER`, `APP_DELEGATION`.`DEL_INDEX`, `APP_DELEGATION`.`TAS_ID`) IN (
|
|
|
|
|
SELECT
|
|
|
|
|
`APP_ASSIGN_SELF_SERVICE_VALUE`.`APP_NUMBER`,
|
|
|
|
|
`APP_ASSIGN_SELF_SERVICE_VALUE`.`DEL_INDEX`,
|
|
|
|
|
`APP_ASSIGN_SELF_SERVICE_VALUE`.`TAS_ID`
|
|
|
|
|
FROM
|
|
|
|
|
`APP_ASSIGN_SELF_SERVICE_VALUE`
|
|
|
|
|
INNER JOIN `APP_ASSIGN_SELF_SERVICE_VALUE_GROUP` ON
|
|
|
|
|
`APP_ASSIGN_SELF_SERVICE_VALUE`.`ID` = `APP_ASSIGN_SELF_SERVICE_VALUE_GROUP`.`ID`
|
|
|
|
|
WHERE (
|
|
|
|
|
`APP_ASSIGN_SELF_SERVICE_VALUE_GROUP`.`GRP_UID` = '%s' OR (
|
|
|
|
|
`APP_ASSIGN_SELF_SERVICE_VALUE_GROUP`.`ASSIGNEE_ID` IN (%s) AND
|
|
|
|
|
`APP_ASSIGN_SELF_SERVICE_VALUE_GROUP`.`ASSIGNEE_TYPE` = '2')
|
|
|
|
|
)
|
|
|
|
|
))";
|
|
|
|
|
$groups = implode(',', Delegation::$groups);
|
|
|
|
|
$complexJoin = sprintf($complexJoin, Delegation::$usrUid, $groups);
|
|
|
|
|
|
|
|
|
|
// Add joins
|
|
|
|
|
$join->on('APP_DELEGATION.TAS_ID', '=', 'TASK.TAS_ID');
|
|
|
|
|
$join->on('TASK.TAS_ASSIGN_TYPE', '=', DB::raw("'SELF_SERVICE'"));
|
|
|
|
|
$join->on('APP_DELEGATION.DEL_THREAD_STATUS', '=', DB::raw("'OPEN'"));
|
|
|
|
|
$join->on('APP_DELEGATION.USR_ID', '=', DB::raw("'0'"))->
|
|
|
|
|
whereRaw($complexJoin);
|
2019-05-13 16:44:40 -04:00
|
|
|
});
|
2019-12-24 15:14:24 -04:00
|
|
|
|
2020-01-31 16:10:56 -04:00
|
|
|
// Clean static properties
|
|
|
|
|
Delegation::$usrUid = '';
|
|
|
|
|
Delegation::$groups = [];
|
|
|
|
|
|
2019-12-24 15:14:24 -04:00
|
|
|
// Get self services tasks related to the user
|
|
|
|
|
$selfServiceTasks = TaskUser::getSelfServicePerUser($usrUid);
|
|
|
|
|
|
|
|
|
|
if (!empty($selfServiceTasks)) {
|
2020-03-10 09:14:17 -04:00
|
|
|
// Start the second query
|
2020-03-23 09:26:34 -04:00
|
|
|
$query2 = Delegation::query()->select(['APP_NUMBER', 'DEL_INDEX']);
|
2019-12-24 15:14:24 -04:00
|
|
|
$query2->tasksIn($selfServiceTasks);
|
2020-03-10 09:14:17 -04:00
|
|
|
$query2->isThreadOpen();
|
2019-12-24 15:14:24 -04:00
|
|
|
$query2->noUserInThread();
|
|
|
|
|
|
|
|
|
|
// Build the complex query that uses "UNION DISTINCT" clause
|
2020-01-31 16:10:56 -04:00
|
|
|
$query = sprintf('select ' . ($count ? 'count(*) as aggregate' : 'APP_NUMBER') .
|
|
|
|
|
' from ((%s) union distinct (%s)) self_service_cases', toSqlWithBindings($query1), toSqlWithBindings($query2));
|
2019-12-24 15:14:24 -04:00
|
|
|
|
2020-01-31 16:10:56 -04:00
|
|
|
return $query;
|
2019-05-13 16:44:40 -04:00
|
|
|
} else {
|
2020-01-31 16:10:56 -04:00
|
|
|
return $query1;
|
2019-05-13 16:44:40 -04:00
|
|
|
}
|
2020-01-31 16:10:56 -04:00
|
|
|
}
|
2019-05-13 16:44:40 -04:00
|
|
|
|
2020-01-31 16:10:56 -04:00
|
|
|
/**
|
|
|
|
|
* Get the self-services cases by user
|
|
|
|
|
*
|
|
|
|
|
* @param string $usrUid
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function getSelfService($usrUid)
|
|
|
|
|
{
|
|
|
|
|
// Initializing the variable to return
|
|
|
|
|
$data = [];
|
|
|
|
|
|
|
|
|
|
// Get the query
|
|
|
|
|
$query = self::getSelfServiceQuery($usrUid);
|
|
|
|
|
|
|
|
|
|
// Get data
|
|
|
|
|
if (!is_string($query)) {
|
|
|
|
|
$items = $query->get();
|
|
|
|
|
$items->each(function ($item) use (&$data) {
|
|
|
|
|
$data[] = get_object_vars($item);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
$items = DB::select($query);
|
|
|
|
|
foreach ($items as $item) {
|
|
|
|
|
$data[] = get_object_vars($item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return data
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Count the self-services cases by user
|
|
|
|
|
*
|
|
|
|
|
* @param string $usrUid
|
|
|
|
|
*
|
|
|
|
|
* @return integer
|
|
|
|
|
*/
|
|
|
|
|
public static function countSelfService($usrUid)
|
|
|
|
|
{
|
|
|
|
|
// Get the query
|
|
|
|
|
$query = self::getSelfServiceQuery($usrUid, true);
|
|
|
|
|
|
|
|
|
|
// Get count value
|
|
|
|
|
if (!is_string($query)) {
|
|
|
|
|
$count = $query->count();
|
|
|
|
|
} else {
|
|
|
|
|
$result = DB::selectOne($query);
|
|
|
|
|
$count = $result->aggregate;
|
|
|
|
|
}
|
|
|
|
|
// Return data
|
2019-12-24 15:14:24 -04:00
|
|
|
return $count;
|
2019-05-13 16:44:40 -04:00
|
|
|
}
|
2019-07-25 16:12:48 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This function get the current user related to the specific case and index
|
|
|
|
|
*
|
|
|
|
|
* @param integer $appNumber, Case number
|
|
|
|
|
* @param integer $index, Index to review
|
|
|
|
|
* @param string $status, The status of the thread
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function getCurrentUser($appNumber, $index, $status = 'OPEN')
|
|
|
|
|
{
|
|
|
|
|
$query = Delegation::query()->select('USR_UID');
|
|
|
|
|
$query->where('APP_NUMBER', $appNumber);
|
|
|
|
|
$query->where('DEL_INDEX', $index);
|
|
|
|
|
$query->where('DEL_THREAD_STATUS', $status);
|
|
|
|
|
$query->first();
|
|
|
|
|
$results = $query->get();
|
|
|
|
|
|
|
|
|
|
$userUid = '';
|
|
|
|
|
$results->each(function ($item, $key) use (&$userUid) {
|
|
|
|
|
$userUid = $item->USR_UID;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return $userUid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the open thread related to the task
|
|
|
|
|
*
|
|
|
|
|
* @param integer $appNumber, Case number
|
|
|
|
|
* @param string $tasUid, The task uid
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function getOpenThreads($appNumber, $tasUid)
|
|
|
|
|
{
|
|
|
|
|
$query = Delegation::query()->select();
|
|
|
|
|
$query->where('DEL_THREAD_STATUS', 'OPEN');
|
|
|
|
|
$query->where('DEL_FINISH_DATE', null);
|
|
|
|
|
$query->where('APP_NUMBER', $appNumber);
|
|
|
|
|
$query->where('TAS_UID', $tasUid);
|
|
|
|
|
$results = $query->get();
|
|
|
|
|
|
|
|
|
|
$arrayOpenThreads = [];
|
|
|
|
|
$results->each(function ($item, $key) use (&$arrayOpenThreads) {
|
|
|
|
|
$arrayOpenThreads = $item->toArray();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return $arrayOpenThreads;
|
|
|
|
|
}
|
2019-09-03 12:39:37 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return if the user has participation in the case
|
|
|
|
|
*
|
|
|
|
|
* @param string $appUid, Case key
|
|
|
|
|
* @param string $userUid, User key
|
|
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public static function participation($appUid, $userUid)
|
|
|
|
|
{
|
|
|
|
|
$query = Delegation::query()->select();
|
|
|
|
|
$query->where('APP_UID', $appUid);
|
|
|
|
|
$query->where('USR_UID', $userUid);
|
|
|
|
|
$query->limit(1);
|
|
|
|
|
|
|
|
|
|
return ($query->count() > 0);
|
|
|
|
|
}
|
2020-10-02 12:53:58 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the thread related to the specific task-index
|
|
|
|
|
*
|
|
|
|
|
* @param string $appUid
|
|
|
|
|
* @param string $delIndex
|
|
|
|
|
* @param string $tasUid
|
|
|
|
|
* @param string $taskType
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function getDatesFromThread(string $appUid, string $delIndex, string $tasUid, string $taskType)
|
|
|
|
|
{
|
|
|
|
|
$query = Delegation::query()->select([
|
|
|
|
|
'DEL_INIT_DATE',
|
|
|
|
|
'DEL_DELEGATE_DATE',
|
|
|
|
|
'DEL_FINISH_DATE',
|
|
|
|
|
'DEL_RISK_DATE',
|
|
|
|
|
'DEL_TASK_DUE_DATE'
|
|
|
|
|
]);
|
|
|
|
|
$query->where('APP_UID', $appUid);
|
|
|
|
|
$query->where('DEL_INDEX', $delIndex);
|
|
|
|
|
$query->where('TAS_UID', $tasUid);
|
|
|
|
|
$results = $query->get();
|
|
|
|
|
|
|
|
|
|
$thread = [];
|
|
|
|
|
$results->each(function ($item, $key) use (&$thread, $taskType) {
|
|
|
|
|
$thread = $item->toArray();
|
|
|
|
|
if (in_array($taskType, Task::$typesRunAutomatically)) {
|
|
|
|
|
$startDate = $thread['DEL_DELEGATE_DATE'];
|
|
|
|
|
} else {
|
|
|
|
|
$startDate = $thread['DEL_INIT_DATE'];
|
|
|
|
|
}
|
|
|
|
|
$endDate = $thread['DEL_FINISH_DATE'];
|
|
|
|
|
// Calculate the task-thread duration
|
|
|
|
|
if (!empty($startDate) && !empty($endDate)) {
|
|
|
|
|
$initDate = new DateTime($startDate);
|
|
|
|
|
$finishDate = new DateTime($endDate);
|
|
|
|
|
$diff = $initDate->diff($finishDate);
|
|
|
|
|
$format = ' %a ' . G::LoadTranslation('ID_DAY_DAYS');
|
|
|
|
|
$format .= ' %H '. G::LoadTranslation('ID_HOUR_ABBREVIATE');
|
|
|
|
|
$format .= ' %I '. G::LoadTranslation('ID_MINUTE_ABBREVIATE');
|
|
|
|
|
$format .= ' %S '. G::LoadTranslation('ID_SECOND_ABBREVIATE');
|
|
|
|
|
$thread['DEL_THREAD_DURATION'] = $diff->format($format);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return $thread;
|
|
|
|
|
}
|
2019-06-10 15:04:14 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper to get the priority code from a given value
|
|
|
|
|
*
|
|
|
|
|
* @param int $priorityValue
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static function getPriorityCode($priorityValue)
|
|
|
|
|
{
|
|
|
|
|
if (!empty(self::PRIORITIES_MAP[$priorityValue])) {
|
|
|
|
|
$priorityCode = self::PRIORITIES_MAP[$priorityValue];
|
|
|
|
|
} else {
|
|
|
|
|
throw new Exception("Priority value {$priorityValue} is not valid.");
|
|
|
|
|
}
|
|
|
|
|
return $priorityCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper to get the priority label from a given value
|
|
|
|
|
*
|
|
|
|
|
* @param int $priorityValue
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function getPriorityLabel($priorityValue)
|
|
|
|
|
{
|
|
|
|
|
$priorityCode = self::getPriorityCode($priorityValue);
|
|
|
|
|
|
|
|
|
|
return G::LoadTranslation("ID_PRIORITY_{$priorityCode}");
|
|
|
|
|
}
|
2019-04-25 13:47:21 -07:00
|
|
|
}
|