PMCORE-495

This commit is contained in:
Julio Cesar Laura Avendaño
2019-12-24 15:14:24 -04:00
parent b350d4797f
commit 35d617cd2d
2 changed files with 104 additions and 27 deletions

View File

@@ -13,6 +13,10 @@ class Delegation extends Model
// We don't have our standard timestamp columns // We don't have our standard timestamp columns
public $timestamps = false; public $timestamps = false;
// Static properties to preserve values
public static $usrUid = '';
public static $groups = [];
/** /**
* Returns the application this delegation belongs to * Returns the application this delegation belongs to
*/ */
@@ -478,39 +482,81 @@ class Delegation extends Model
*/ */
public static function countSelfService($usrUid) public static function countSelfService($usrUid)
{ {
//Get the task self services related to the user // Set the 'usrUid' property to preserve
$taskSelfService = TaskUser::getSelfServicePerUser($usrUid); Delegation::$usrUid = $usrUid;
//Get the task self services value based related to the user
$selfServiceValueBased = AppAssignSelfServiceValue::getSelfServiceCasesByEvaluatePerUser($usrUid);
//Start the query for get the cases related to the user // Get and build the groups parameter related to the user
$query = Delegation::query()->select('APP_NUMBER'); $groups = GroupUser::getGroups($usrUid);
//Add Join with task filtering only the type self-service $groups = array_map(function ($value) {
$query->join('TASK', function ($join) { return "'" . $value['GRP_ID'] . "'";
$join->on('APP_DELEGATION.TAS_ID', '=', 'TASK.TAS_ID') }, $groups);
->where('TASK.TAS_ASSIGN_TYPE', '=', 'SELF_SERVICE');
// 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
$query1 = Delegation::query()->select('APP_NUMBER');
// 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);
}); });
//Filtering the open threads and without users
$query->isThreadOpen()->noUserInThread();
//Get the cases unassigned // Get self services tasks related to the user
if (!empty($selfServiceValueBased)) { $selfServiceTasks = TaskUser::getSelfServicePerUser($usrUid);
$query->where(function ($query) use ($selfServiceValueBased, $taskSelfService) {
//Get the cases related to the task self service if (!empty($selfServiceTasks)) {
$query->tasksIn($taskSelfService); // Start the first query
foreach ($selfServiceValueBased as $case) { $query2 = Delegation::query()->select('APP_NUMBER');
//Get the cases related to the task self service value based $query2->tasksIn($selfServiceTasks);
$query->orWhere(function ($query) use ($case) { $query2->noUserInThread();
$query->case($case['APP_NUMBER'])->index($case['DEL_INDEX'])->task($case['TAS_ID']);
}); // 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));
// Execute the query
$result = DB::selectOne($unionQuery);
$count = $result->aggregate;
} else { } else {
//Get the cases related to the task self service // Execute the query
$query->tasksIn($taskSelfService); $count = $query1->count();
} }
return $query->count(); // Clean static properties
Delegation::$usrUid = '';
Delegation::$groups = [];
// Return value
return $count;
} }
/** /**

View File

@@ -545,3 +545,34 @@ function updateUserLastLogin($userLog, $keyLastLogin = 'LOG_INIT_DATE')
} }
} }
/**
* Return raw query with the bindings replaced
*
* @param \Illuminate\Database\Eloquent\Builder $queryObject
* @return string
*/
function toSqlWithBindings(Illuminate\Database\Eloquent\Builder $queryObject) {
// Get some values from the object
$bindings = $queryObject->getBindings();
$originalQuery = $queryObject->toSql();
// If not exist bindings, return the original query
if (empty($bindings)) {
return $originalQuery;
}
// Initializing another variables
$queryParts = explode('?', $originalQuery);
$pdo = $queryObject->getConnection()->getPdo();
$query = '';
// Walking the parts of the query replacing the bindings
foreach ($queryParts as $index => $part) {
if ($index < count($queryParts) - 1) {
$query .= $part . $pdo->quote($bindings[$index]);
}
}
// Return query
return $query;
}