PMCORE-1122

This commit is contained in:
Julio Cesar Laura Avendaño
2020-01-31 16:10:56 -04:00
parent aa6b0edb3b
commit 1551677a5f

View File

@@ -474,13 +474,14 @@ class Delegation extends Model
}
/**
* Count the self-services cases by user
* Get the self-services query by user
*
* @param string $usrUid
* @param bool $count
*
* @return integer
* @return \Illuminate\Database\Query\Builder | string
*/
public static function countSelfService($usrUid)
public static function getSelfServiceQuery($usrUid, $count = false)
{
// Set the 'usrUid' property to preserve
Delegation::$usrUid = $usrUid;
@@ -530,6 +531,10 @@ class Delegation extends Model
whereRaw($complexJoin);
});
// Clean static properties
Delegation::$usrUid = '';
Delegation::$groups = [];
// Get self services tasks related to the user
$selfServiceTasks = TaskUser::getSelfServicePerUser($usrUid);
@@ -540,22 +545,68 @@ class Delegation extends Model
$query2->noUserInThread();
// Build the complex query that uses "UNION DISTINCT" clause
$unionQuery = sprintf('select count(*) as aggregate from ((%s) union distinct (%s)) self_service_cases',
toSqlWithBindings($query1), toSqlWithBindings($query2));
$query = sprintf('select ' . ($count ? 'count(*) as aggregate' : 'APP_NUMBER') .
' from ((%s) union distinct (%s)) self_service_cases', toSqlWithBindings($query1), toSqlWithBindings($query2));
// Execute the query
$result = DB::selectOne($unionQuery);
$count = $result->aggregate;
return $query;
} else {
// Execute the query
$count = $query1->count();
return $query1;
}
}
// Clean static properties
Delegation::$usrUid = '';
Delegation::$groups = [];
/**
* Get the self-services cases by user
*
* @param string $usrUid
*
* @return array
*/
public static function getSelfService($usrUid)
{
// Initializing the variable to return
$data = [];
// Return value
// 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
return $count;
}