Skip to content
Snippets Groups Projects
PreProcessActionController.php 79.7 KiB
Newer Older
  • Learn to ignore specific revisions
  •         if (!empty($errors['errors'])) {
                return $response->withStatus($errors['code'])->withJson(['errors' => $errors['errors']]);
            }
    
            $body['resources'] = array_slice($body['resources'], 0, 500);
            if (!ResController::hasRightByResId(['resId' => $body['resources'], 'userId' => $GLOBALS['id']])) {
                return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
            }
    
            $body['resources'] = PreProcessActionController::getNonLockedResources(['resources' => $body['resources'], 'userId' => $GLOBALS['id']]);
    
    
            $signableAttachmentsTypes = [];
            $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
            foreach ($attachmentsTypes as $key => $type) {
                if ($type['sign']) {
                    $signableAttachmentsTypes[] = $key;
                }
            }
    
            $resourcesInformations = [];
            foreach ($body['resources'] as $resId) {
                $resource = ResModel::getById(['resId' => $resId, 'select' => ['alt_identifier']]);
                if (empty($resource['alt_identifier'])) {
                    $resource['alt_identifier'] = _UNDEFINED;
                }
    
                $isSignatory = ListInstanceModel::get(['select' => ['signatory', 'requested_signature'], 'where' => ['res_id = ?', 'difflist_type = ?', 'process_date is null'], 'data' => [$resId, 'VISA_CIRCUIT'], 'orderBy' => ['listinstance_id'], 'limit' => 1]);
                if (empty($isSignatory[0])) {
    
                    $hasCircuit = ListInstanceModel::get(['select' => [1], 'where' => ['res_id = ?', 'difflist_type = ?'], 'data' => [$resId, 'VISA_CIRCUIT']]);
                    if (!empty($hasCircuit)) {
                        $resourcesInformations['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'endedCircuit'];
                    } else {
                        $resourcesInformations['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'noCircuitAvailable'];
                    }
    
                } elseif ($isSignatory[0]['requested_signature'] && !$isSignatory[0]['signatory']) {
                    $resourcesInformations['warning'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'userHasntSigned'];
                } else {
    
                    $attachments = AttachmentModel::get([
                        'select'    => ['status'],
                        'where'     => ['res_id_master = ?', 'attachment_type in (?)', 'in_signature_book = ?', 'status not in (?)'],
                        'data'      => [$resId, $signableAttachmentsTypes, true, ['OBS', 'DEL', 'FRZ']]
                    ]);
    
                    $mailing = false;
                    if (!empty($attachments)) {
                        $statuses = array_column($attachments, 'status');
                        $mailing = in_array('SEND_MASS', $statuses);
                    }
                    $resourcesInformations['success'][] = ['res_id' => $resId, 'alt_identifier' => $resource['alt_identifier'], 'mailing' => $mailing];
    
                }
            }
    
            return $response->withJson(['resourcesInformations' => $resourcesInformations]);
        }
    
    
        public function checkRejectVisa(Request $request, Response $response, array $args)
    
        {
            $body = $request->getParsedBody();
    
            if (!Validator::arrayType()->notEmpty()->validate($body['resources'])) {
                return $response->withStatus(400)->withJson(['errors' => 'Body resources is empty or not an array']);
            }
    
            $errors = ResourceListController::listControl(['groupId' => $args['groupId'], 'userId' => $args['userId'], 'basketId' => $args['basketId'], 'currentUserId' => $GLOBALS['id']]);
            if (!empty($errors['errors'])) {
                return $response->withStatus($errors['code'])->withJson(['errors' => $errors['errors']]);
            }
    
            $body['resources'] = array_slice($body['resources'], 0, 500);
            if (!ResController::hasRightByResId(['resId' => $body['resources'], 'userId' => $GLOBALS['id']])) {
                return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
            }
    
            $body['resources'] = PreProcessActionController::getNonLockedResources(['resources' => $body['resources'], 'userId' => $GLOBALS['id']]);
    
    
            $signableAttachmentsTypes = [];
            $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
            foreach ($attachmentsTypes as $key => $type) {
                if ($type['sign']) {
                    $signableAttachmentsTypes[] = $key;
                }
            }
    
            $resourcesInformation = [];
            foreach ($body['resources'] as $resId) {
                $resource = ResModel::getById(['resId' => $resId, 'select' => ['alt_identifier']]);
                if (empty($resource['alt_identifier'])) {
                    $resource['alt_identifier'] = _UNDEFINED;
                }
    
                $isSignatory = ListInstanceModel::get(['select' => ['signatory', 'requested_signature'], 'where' => ['res_id = ?', 'difflist_type = ?', 'process_date is null'], 'data' => [$resId, 'VISA_CIRCUIT'], 'orderBy' => ['listinstance_id'], 'limit' => 1]);
                if (empty($isSignatory[0])) {
                    $hasCircuit = ListInstanceModel::get(['select' => [1], 'where' => ['res_id = ?', 'difflist_type = ?'], 'data' => [$resId, 'VISA_CIRCUIT']]);
                    if (!empty($hasCircuit)) {
                        $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'endedCircuit'];
                    } else {
                        $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'noCircuitAvailable'];
                    }
                } else {
                    $hasPrevious = ListInstanceModel::get([
                        'select'  => [1],
                        'where'   => ['res_id = ?', 'difflist_type = ?', 'process_date is not null'],
                        'data'    => [$resId, 'VISA_CIRCUIT'],
                        'orderBy' => ['listinstance_id'],
                        'limit'   => 1
                    ]);
                    if (!empty($hasPrevious)) {
                        $resourcesInformation['success'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId];
                    } else {
    
                        $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'circuitNotStarted'];
    
                    }
                }
            }
    
            return $response->withJson(['resourcesInformations' => $resourcesInformation]);
        }
    
    
        public function checkInterruptResetVisa(Request $request, Response $response, array $args)
        {
            $body = $request->getParsedBody();
    
            if (!Validator::arrayType()->notEmpty()->validate($body['resources'])) {
                return $response->withStatus(400)->withJson(['errors' => 'Body resources is empty or not an array']);
            }
    
            $errors = ResourceListController::listControl(['groupId' => $args['groupId'], 'userId' => $args['userId'], 'basketId' => $args['basketId'], 'currentUserId' => $GLOBALS['id']]);
            if (!empty($errors['errors'])) {
                return $response->withStatus($errors['code'])->withJson(['errors' => $errors['errors']]);
            }
    
            $body['resources'] = array_slice($body['resources'], 0, 500);
            if (!ResController::hasRightByResId(['resId' => $body['resources'], 'userId' => $GLOBALS['id']])) {
                return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
            }
    
            $body['resources'] = PreProcessActionController::getNonLockedResources(['resources' => $body['resources'], 'userId' => $GLOBALS['id']]);
    
    
            $signableAttachmentsTypes = [];
            $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
            foreach ($attachmentsTypes as $key => $type) {
                if ($type['sign']) {
                    $signableAttachmentsTypes[] = $key;
                }
            }
    
            $resourcesInformation = [];
            foreach ($body['resources'] as $resId) {
                $resource = ResModel::getById(['resId' => $resId, 'select' => ['alt_identifier']]);
                if (empty($resource['alt_identifier'])) {
                    $resource['alt_identifier'] = _UNDEFINED;
                }
    
                $isSignatory = ListInstanceModel::get(['select' => ['signatory', 'requested_signature'], 'where' => ['res_id = ?', 'difflist_type = ?', 'process_date is null'], 'data' => [$resId, 'VISA_CIRCUIT'], 'orderBy' => ['listinstance_id'], 'limit' => 1]);
                if (empty($isSignatory[0])) {
                    $hasCircuit = ListInstanceModel::get(['select' => [1], 'where' => ['res_id = ?', 'difflist_type = ?'], 'data' => [$resId, 'VISA_CIRCUIT']]);
                    if (!empty($hasCircuit)) {
                        $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'endedCircuit'];
                    } else {
                        $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'noCircuitAvailable'];
                    }
                } else {
                    $resourcesInformation['success'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId];
                }
            }
    
            return $response->withJson(['resourcesInformations' => $resourcesInformation]);
        }
    
    
        public function checkValidateParallelOpinion(Request $request, Response $response, array $args)
    
        {
            $body = $request->getParsedBody();
    
            if (!Validator::arrayType()->notEmpty()->validate($body['resources'])) {
                return $response->withStatus(400)->withJson(['errors' => 'Body resources is empty or not an array']);
            }
    
            $errors = ResourceListController::listControl(['groupId' => $args['groupId'], 'userId' => $args['userId'], 'basketId' => $args['basketId'], 'currentUserId' => $GLOBALS['id']]);
            if (!empty($errors['errors'])) {
                return $response->withStatus($errors['code'])->withJson(['errors' => $errors['errors']]);
            }
    
            $body['resources'] = array_slice($body['resources'], 0, 500);
            if (!ResController::hasRightByResId(['resId' => $body['resources'], 'userId' => $GLOBALS['id']])) {
                return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
            }
    
            $body['resources'] = PreProcessActionController::getNonLockedResources(['resources' => $body['resources'], 'userId' => $GLOBALS['id']]);
    
            $resourcesInformation = [];
    
            $resources = ResModel::get(['select' => ['opinion_limit_date', 'alt_identifier', 'res_id'], 'where' => ['res_id in (?)'], 'data' => [$body['resources']]]);
    
            foreach ($resources as $resource) {
                if (empty($resource['opinion_limit_date'])) {
    
                    $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resource['res_id'], 'reason' => 'noOpinionLimitDate'];
                    continue;
                }
                $opinionLimitDate = new \DateTime($resource['opinion_limit_date']);
                $today = new \DateTime('today');
                if ($opinionLimitDate < $today) {
                    $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resource['res_id'], 'reason' => 'opinionLimitDateOutdated'];
    
    
                $opinionNote = NoteModel::get([
                    'select'    => ['note_text', 'user_id', 'creation_date'],
                    'where'     => ['identifier in (?)', 'note_text like (?)'],
    
                    'data'      => [$resource['res_id'], '['._TO_AVIS.']%'],
    
                    'order_by'  => ['creation_date desc'],
                    'limit'     => 1
                ]);
    
                if (empty($opinionNote)) {
                    $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resource['res_id'], 'reason' => 'noOpinionNote'];
                    continue;
                }
    
    
                $note = str_replace('['._TO_AVIS.'] ', '', $opinionNote[0]['note_text']);
    
                $userInfo = UserModel::getLabelledUserById(['id' => $opinionNote[0]['user_id']]);
    
                $resourcesInformation['success'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resource['res_id'], 'avisUserAsk' => $userInfo, 'note' => $note, 'opinionLimitDate' => $resource['opinion_limit_date']];
    
            return $response->withJson(['resourcesInformations' => $resourcesInformation]);
    
        public function checkGiveParallelOpinion(Request $request, Response $response, array $args)
        {
            $body = $request->getParsedBody();
    
            if (!Validator::arrayType()->notEmpty()->validate($body['resources'])) {
                return $response->withStatus(400)->withJson(['errors' => 'Body resources is empty or not an array']);
            }
    
            $errors = ResourceListController::listControl(['groupId' => $args['groupId'], 'userId' => $args['userId'], 'basketId' => $args['basketId'], 'currentUserId' => $GLOBALS['id']]);
            if (!empty($errors['errors'])) {
                return $response->withStatus($errors['code'])->withJson(['errors' => $errors['errors']]);
            }
    
            $body['resources'] = array_slice($body['resources'], 0, 500);
            if (!ResController::hasRightByResId(['resId' => $body['resources'], 'userId' => $GLOBALS['id']])) {
                return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
            }
    
            $body['resources'] = PreProcessActionController::getNonLockedResources(['resources' => $body['resources'], 'userId' => $GLOBALS['id']]);
    
    
            $currentUser = UserModel::getById(['select' => ['user_id'], 'id' => $GLOBALS['id']]);
    
            $resourcesInformation = [];
            foreach ($body['resources'] as $resId) {
                $resource = ResModel::getById(['resId' => $resId, 'select' => ['alt_identifier', 'opinion_limit_date']]);
    
                if (empty($resource['alt_identifier'])) {
                    $resource['alt_identifier'] = _UNDEFINED;
                }
    
                if (empty($resource['opinion_limit_date'])) {
    
                    $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'noOpinionLimitDate'];
    
                $opinionLimitDate = new \DateTime($resource['opinion_limit_date']);
                $today = new \DateTime('today');
                if ($opinionLimitDate < $today) {
                    $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'opinionLimitDateOutdated'];
    
    
                $opinionNote = NoteModel::get([
    
                    'select'    => ['user_id', 'note_text'],
    
                    'where'     => ['identifier = ?', "note_text like ?"],
                    'data'      => [$resId, '['._TO_AVIS.']%'],
    
                    'order_by'  => ['creation_date desc'],
                    'limit'     => 1
    
                ]);
    
                if (empty($opinionNote)) {
    
                    $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'noOpinionNote'];
                    continue;
    
                }
    
                $isInCircuit = ListInstanceModel::get([
                    'select'  => [1],
                    'where'   => ['res_id = ?', 'difflist_type = ?', 'process_date is null', 'item_id = ?', 'item_mode = ?'],
                    'data'    => [$resId, 'entity_id', $currentUser['user_id'], 'avis']
                ]);
                if (empty($isInCircuit)) {
                    $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'userNotInDiffusionList'];
                } else {
    
                    $userInfo = UserModel::getLabelledUserById(['id' => $opinionNote[0]['user_id']]);
    
                    $resourcesInformation['success'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'avisUserAsk' => $userInfo, 'note' => $opinionNote[0]['note_text'], 'opinionLimitDate' => $resource['opinion_limit_date']];
    
                }
            }
    
            return $response->withJson(['resourcesInformations' => $resourcesInformation]);
        }
    
    
        public function checkContinueOpinionCircuit(Request $request, Response $response, array $args)
        {
            $body = $request->getParsedBody();
    
            if (!Validator::arrayType()->notEmpty()->validate($body['resources'])) {
                return $response->withStatus(400)->withJson(['errors' => 'Body resources is empty or not an array']);
            }
    
            $errors = ResourceListController::listControl(['groupId' => $args['groupId'], 'userId' => $args['userId'], 'basketId' => $args['basketId'], 'currentUserId' => $GLOBALS['id']]);
            if (!empty($errors['errors'])) {
                return $response->withStatus($errors['code'])->withJson(['errors' => $errors['errors']]);
            }
    
            $body['resources'] = array_slice($body['resources'], 0, 500);
            if (!ResController::hasRightByResId(['resId' => $body['resources'], 'userId' => $GLOBALS['id']])) {
                return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
            }
    
            $body['resources'] = PreProcessActionController::getNonLockedResources(['resources' => $body['resources'], 'userId' => $GLOBALS['id']]);
    
    
            $resourcesInformation = [];
            foreach ($body['resources'] as $resId) {
    
                $resource = ResModel::getById(['resId' => $resId, 'select' => ['alt_identifier', 'opinion_limit_date']]);
    
    
                if (empty($resource['alt_identifier'])) {
                    $resource['alt_identifier'] = _UNDEFINED;
                }
    
    
                    $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'noOpinionLimitDate'];
    
                $opinionLimitDate = new \DateTime($resource['opinion_limit_date']);
                $today = new \DateTime('today');
                if ($opinionLimitDate < $today) {
                    $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'opinionLimitDateOutdated'];
    
                    'select'    => ['note_text', 'user_id'],
                    'where'     => ['identifier = ?', "note_text like '[" . _TO_AVIS . "]%'"],
                    'data'      => [$resId]
    
                    $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'noOpinionNote'];
                    continue;
    
                $isInCircuit = ListInstanceModel::get([
    
                    'select'  => [1],
                    'where'   => ['res_id = ?', 'difflist_type = ?', 'process_date is null'],
                    'data'    => [$resId, 'AVIS_CIRCUIT'],
                    'orderBy' => ['listinstance_id'],
                    'limit'   => 1
                ]);
    
                if (empty($isInCircuit[0])) {
    
                    $hasCircuit = ListInstanceModel::get(['select' => [1], 'where' => ['res_id = ?', 'difflist_type = ?'], 'data' => [$resId, 'AVIS_CIRCUIT']]);
                    if (!empty($hasCircuit)) {
                        $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'endedCircuit'];
                    } else {
                        $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'noCircuitAvailable'];
                    }
                } else {
    
                    $userInfo = UserModel::getLabelledUserById(['id' => $opinionNote[0]['user_id']]);
    
                    $resourcesInformation['success'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'avisUserAsk' => $userInfo, 'note' => $opinionNote[0]['note_text'], 'opinionLimitDate' => $resource['opinion_limit_date']];
    
                }
            }
    
            return $response->withJson(['resourcesInformations' => $resourcesInformation]);
        }
    
    
        public function isDestinationChanging(Request $request, Response $response, array $args)
        {
    
            if (!ResController::hasRightByResId(['resId' => [$args['resId']], 'userId' => $GLOBALS['id']])) {
    
                return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
            }
    
            $user = UserModel::getById(['id' => $args['userId'], 'select' => ['user_id']]);
            if (empty($user)) {
                return $response->withStatus(400)->withJson(['errors' => 'User does not exist']);
            }
    
            $changeDestination = true;
    
            $entities = UserEntityModel::get(['select' => ['entity_id'], 'where' => ['user_id = ?'], 'data' => [$user['user_id']]]);
    
            $resource = ResModel::getById(['select' => ['destination'], 'resId' => $args['resId']]);
            foreach ($entities as $entity) {
                if ($entity['entity_id'] == $resource['destination']) {
                    $changeDestination = false;
                }
            }
    
            return $response->withJson(['isDestinationChanging' => $changeDestination]);
        }
    
        public static function checkCloseWithFieldsAction(Request $request, Response $response, array $args)
        {
            $body = $request->getParsedBody();
    
            if (!Validator::arrayType()->notEmpty()->validate($body['resources'])) {
                return $response->withStatus(400)->withJson(['errors' => 'Body resources is empty or not an array']);
            }
    
            $errors = ResourceListController::listControl(['groupId' => $args['groupId'], 'userId' => $args['userId'], 'basketId' => $args['basketId'], 'currentUserId' => $GLOBALS['id']]);
            if (!empty($errors['errors'])) {
                return $response->withStatus($errors['code'])->withJson(['errors' => $errors['errors']]);
            }
    
            $body['resources'] = array_slice($body['resources'], 0, 500);
            if (!ResController::hasRightByResId(['resId' => $body['resources'], 'userId' => $GLOBALS['id']])) {
                return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
            }
            $body['resources'] = PreProcessActionController::getNonLockedResources(['resources' => $body['resources'], 'userId' => $GLOBALS['id']]);
    
    
            $action = ActionModel::getById(['id' => $args['actionId'], 'select' => ['parameters']]);
    
            if (empty($action)) {
                return $response->withStatus(400)->withJson(['errors' => 'Action does not exist']);
            }
    
            $parameters = json_decode($action['parameters'], true);
    
            $actionRequiredFields = $parameters['requiredFields'] ?? [];
    
    
            $canClose = [];
            $emptyFields = [];
    
            foreach ($body['resources'] as $resId) {
                if (!empty($actionRequiredFields)) {
                    $resource = ResModel::getById(['resId' => $resId, 'select' => ['model_id', 'custom_fields', 'alt_identifier']]);
                    $model = $resource['model_id'];
                    $resourceCustomFields = json_decode($resource['custom_fields'], true);
                    $modelFields = IndexingModelFieldModel::get([
                        'select' => ['identifier'],
                        'where'  => ['model_id = ?', "identifier LIKE 'indexingCustomField_%'"],
                        'data'   => [$model]
                    ]);
                    $modelFields = array_column($modelFields, 'identifier');
    
                    $emptyList = [];
    
                    foreach ($actionRequiredFields as $actionRequiredField) {
                        $idCustom = explode("_", $actionRequiredField)[1];
                        if (in_array($actionRequiredField, $modelFields) && empty($resourceCustomFields[$idCustom])) {
                            $emptyList[] = $idCustom;
                        }
                    }
    
                    if (!empty($emptyList)) {
                        $fieldsList = [];
    
                        $customs = CustomFieldModel::get([
                            'select' => ['label'],
                            'where'  => ['id in (?)'],
                            'data'   => [$emptyList]
                        ]);
    
                        foreach ($customs as $custom) {
                            $fieldsList[] = $custom['label'];
                        }
    
                        $emptyFields[] = [
                            'chrono' => $resource['alt_identifier'],
                            'fields' => !empty($fieldsList) ? implode(", ", $fieldsList) : ''
                        ];
                    } else {
                        $canClose[] = $resId;
                    }
                } else {
                    $canClose[] = $resId;
                }
            }
    
    
            return $response->withJson(['errors' => $emptyFields, 'success' => $canClose]);
    
        public function checkReconcile(Request $request, Response $response, array $args)
        {
            $body = $request->getParsedBody();
    
            if (!Validator::arrayType()->notEmpty()->validate($body['resources'])) {
                return $response->withStatus(400)->withJson(['errors' => 'Body resources is empty or not an array']);
            }
    
            $errors = ResourceListController::listControl(['groupId' => $args['groupId'], 'userId' => $args['userId'], 'basketId' => $args['basketId'], 'currentUserId' => $GLOBALS['id']]);
            if (!empty($errors['errors'])) {
                return $response->withStatus($errors['code'])->withJson(['errors' => $errors['errors']]);
            }
    
            $body['resources'] = array_slice($body['resources'], 0, 500);
            if (!ResController::hasRightByResId(['resId' => $body['resources'], 'userId' => $GLOBALS['id']])) {
                return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
            }
            $body['resources'] = PreProcessActionController::getNonLockedResources(['resources' => $body['resources'], 'userId' => $GLOBALS['id']]);
    
            $resourcesInformation = [];
            foreach ($body['resources'] as $resId) {
                $resource = ResModel::getById(['resId' => $resId, 'select' => ['alt_identifier', 'filename']]);
    
                if (empty($resource['alt_identifier'])) {
                    $resource['alt_identifier'] = _UNDEFINED;
                }
    
                if (empty($resource['filename'])) {
                    $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'noFilename'];
                    continue;
                }
    
                $targetResource = ResModel::getById(['select' => ['category_id', 'filename'], 'resId' => $body['data']['resId']]);
                if (empty($targetResource)) {
                    return ['errors' => ['Target resource does not exist']];
                } elseif ($targetResource['category_id'] == 'outgoing' && empty($targetResource['filename'])) {
                    return ['errors' => ['Target resource has no file']];
                }
    
                $resourcesInformation['success'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId];
            }
    
            return $response->withJson(['resourcesInformations' => $resourcesInformation]);
        }
    
    
        private static function getNonLockedResources(array $args)
        {
            ValidatorModel::notEmpty($args, ['resources', 'userId']);
            ValidatorModel::arrayType($args, ['resources']);
            ValidatorModel::intVal($args, ['userId']);
    
            $resources = ResModel::get([
                'select' => ['res_id', 'locker_user_id', 'locker_time'],
                'where'  => ['res_id in (?)'],
                'data'   => [$args['resources']]
            ]);
    
            $resourcesForProcess = [];
            foreach ($resources as $resource) {
                $lock = true;
                if (empty($resource['locker_user_id'] || empty($resource['locker_time']))) {
                    $lock = false;
    
                } elseif ($resource['locker_user_id'] == $args['userId']) {
    
                    $lock = false;
                } elseif (strtotime($resource['locker_time']) < time()) {
                    $lock = false;
                }
                if (!$lock) {
                    $resourcesForProcess[] = $resource['res_id'];
                }
            }
    
    
            return $resourcesForProcess;