From 3c49756d8dfebd942a48f99944c0607d4ae83f64 Mon Sep 17 00:00:00 2001 From: Paula Quispe Date: Wed, 30 May 2018 09:19:46 -0400 Subject: [PATCH] HOR-4454 --- .../engine/classes/model/AppDelegation.php | 46 ++++++++++++ .../src/ProcessMaker/BusinessModel/Cases.php | 71 ++++++++++--------- .../src/ProcessMaker/Services/Api/Light.php | 4 +- 3 files changed, 84 insertions(+), 37 deletions(-) diff --git a/workflow/engine/classes/model/AppDelegation.php b/workflow/engine/classes/model/AppDelegation.php index 25b3c708b..d47790a7e 100644 --- a/workflow/engine/classes/model/AppDelegation.php +++ b/workflow/engine/classes/model/AppDelegation.php @@ -999,5 +999,51 @@ class AppDelegation extends BaseAppDelegation return $proId; } + /** + * Get the last index by a specific status + * + * @param integer $appNumber + * @param string $status + * + * @return integer + */ + public static function getLastIndexByStatus($appNumber, $status = 'OPEN') + { + $delIndex = 0; + $criteria = new Criteria(); + $criteria->add(AppDelegationPeer::APP_NUMBER, $appNumber); + $criteria->add(AppDelegationPeer::DEL_THREAD_STATUS, $status); + $criteria->addDescendingOrderByColumn(AppDelegationPeer::DEL_INDEX); + $dataset = AppDelegationPeer::doSelectOne($criteria); + if (!is_null($dataset)) { + $delIndex = $dataset->getDelIndex(); + } + return $delIndex; + } + + /** + * Get the last index assigned to the user by a specific status + * + * @param integer $appNumber + * @param integer $usrId + * @param string $status + * + * @return integer + */ + public static function getLastIndexByUserAndStatus($appNumber, $usrId, $status = 'OPEN') + { + $delIndex = 0; + $criteria = new Criteria(); + $criteria->add(AppDelegationPeer::APP_NUMBER, $appNumber); + $criteria->add(AppDelegationPeer::USR_ID, $usrId); + $criteria->add(AppDelegationPeer::DEL_THREAD_STATUS, $status); + $criteria->addDescendingOrderByColumn(AppDelegationPeer::DEL_INDEX); + $dataset = AppDelegationPeer::doSelectOne($criteria); + if (!is_null($dataset)) { + $delIndex = $dataset->getDelIndex(); + } + + return $delIndex; + } } diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php b/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php index 6ca874fe6..de12c5e5b 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php @@ -865,51 +865,52 @@ class Cases * Put cancel case * * @access public - * @param string $app_uid, Uid for case - * @param string $usr_uid, Uid for user - * @param bool|string $del_index + * @param string $appUid, Uid for case + * @param string $usrUid, Uid for user + * @param bool|string $delIndex * * @return void * @throws Exception */ - public function putCancelCase($app_uid, $usr_uid, $del_index = false) + public function putCancelCase($appUid, $usrUid, $delIndex = false) { - Validator::isString($app_uid, '$app_uid'); - Validator::isString($usr_uid, '$usr_uid'); - - Validator::appUid($app_uid, '$app_uid'); - Validator::usrUid($usr_uid, '$usr_uid'); - - if ($del_index === false) { - $del_index = AppDelegation::getCurrentIndex($app_uid); - } - Validator::isInteger($del_index, '$del_index'); + Validator::isString($appUid, '$app_uid'); + Validator::appUid($appUid, '$app_uid'); + Validator::isString($usrUid, '$usr_uid'); + Validator::usrUid($usrUid, '$usr_uid'); $case = new ClassesCases(); - $fields = $case->loadCase($app_uid); - if ($fields['APP_STATUS'] == 'CANCELLED') { - throw (new Exception(G::LoadTranslation("ID_CASE_ALREADY_CANCELED", array($app_uid)))); + $fields = $case->loadCase($appUid); + $supervisor = new BmProcessSupervisor(); + $isSupervisor = $supervisor->isUserProcessSupervisor($fields['PRO_UID'], $usrUid); + + if ($delIndex === false) { + $u = new ModelUsers(); + $usrId = $u->load($usrUid)['USR_ID']; + + if ($isSupervisor) { + //Get the last index open + $delIndex = AppDelegation::getLastIndexByStatus($fields['APP_NUMBER']); + } else { + //Get the last index open related to the user + $delIndex = AppDelegation::getLastIndexByUserAndStatus($fields['APP_NUMBER'], $usrId); + } + + //We will to validate when the case is TO_DO and the user does not have a index OPEN + //The scenarios with COMPLETED, CANCELLED and DRAFT is considered in the WsBase::cancelCase + if ($fields['APP_STATUS'] === 'TO_DO' && $delIndex === 0) { + throw (new Exception(G::LoadTranslation("ID_CASE_USER_INVALID_CANCEL_CASE", [$usrUid]))); + } } + Validator::isInteger($delIndex, '$del_index'); - $processUser = new ProcessUser(); - $arrayProcess = $processUser->getProUidSupervisor($usr_uid); - - $criteria = new Criteria("workflow"); - - $criteria->addSelectColumn(AppDelegationPeer::APP_UID); - $criteria->add(AppDelegationPeer::APP_UID, $app_uid, Criteria::EQUAL); - $criteria->add(AppDelegationPeer::DEL_INDEX, $del_index, Criteria::EQUAL); - $criteria->add( - $criteria->getNewCriterion(AppDelegationPeer::USR_UID, $usr_uid, Criteria::EQUAL)->addOr( - $criteria->getNewCriterion(AppDelegationPeer::PRO_UID, $arrayProcess, Criteria::IN)) - ); - $rsCriteria = AppDelegationPeer::doSelectRS($criteria); - - if (!$rsCriteria->next()) { - throw (new Exception(G::LoadTranslation("ID_CASE_USER_INVALID_CANCEL_CASE", array($usr_uid)))); + /** Cancel case */ + $ws = new WsBase(); + $result = $ws->cancelCase($appUid, $delIndex, $usrUid); + $result = (object)$result; + if ($result->status_code !== 0) { + throw new Exception($result->message); } - - $case->cancelCase($app_uid, $del_index, $usr_uid); } /** diff --git a/workflow/engine/src/ProcessMaker/Services/Api/Light.php b/workflow/engine/src/ProcessMaker/Services/Api/Light.php index f958feeae..81074bb13 100644 --- a/workflow/engine/src/ProcessMaker/Services/Api/Light.php +++ b/workflow/engine/src/ProcessMaker/Services/Api/Light.php @@ -1762,13 +1762,13 @@ class Light extends Api * * @url POST /cases/:app_uid/cancel * - * @param string $cas_uid {@min 1}{@max 32} + * @param string $app_uid {@min 1}{@max 32} * * @return array * @throws RestException * * @access protected - * @class AccessControl {@permission PM_CASES} + * @class AccessControl {@permission PM_CANCELCASE} */ public function doPutCancelCase($app_uid) {