public function giveSimpleCode(DateTime $vksStart, DateTime $vksEnd)
 {
     //pull data from request
     $settings = new Settings_controller();
     $params = $settings->getSimpleVksCodeParams();
     $codeRange = range(floatval($params->code['start']), floatval($params->code['end']));
     $codeGiven = false;
     foreach ($codeRange as $code) {
         $foundedCodes = ConnectionCode::where('value', (string) $code)->with(['vks' => function ($query) use($vksStart, $vksEnd) {
             $query->where('is_simple', 1)->where('start_date_time', '<=', $vksEnd->modify("+" . Settings_controller::getOther('pause_gap') . " minutes"))->where('end_date_time', '>=', $vksStart->modify("-" . Settings_controller::getOther('pause_gap') . " minutes"))->where('status', VKS_STATUS_APPROVED);
         }])->get();
         $isFree = true;
         foreach ($foundedCodes as $codeObj) {
             if ($codeObj->vks) {
                 $isFree = false;
             }
         }
         if ($isFree) {
             $codeGiven = $code;
             break;
         }
     }
     return $codeGiven;
 }
 function process($vksId)
 {
     Token::checkToken();
     $connCtrl = new ConnectionCode_controller();
     try {
         $vks = Vks::findOrFail($vksId);
     } catch (Exception $e) {
         $this->error('404');
     }
     if (!self::isVksCanBeApproved($vks)) {
         $this->error('500', "Эта ВКС не может быть согласована, т.к. не имеет соответвующего статуса");
     }
     $request = $this->request->request;
     //validate
     $this->validator->validate(['Комментарий для пользователя' => [$request->get('comment_for_user'), 'max(255)'], 'Статус ВКС' => [$request->get('status'), 'required|between(1,2)']]);
     //if no passes
     if (!$this->validator->passes()) {
         $this->backWithData($this->validator->errors()->all());
     }
     /*
      * if declined
      */
     if ($request->get('status') == VKS::APPROVE_STATUS_DECLINED) {
         $backMessage = "<p>Вкс #{$vks->id}, отказ в проведении</p>";
         $vks->comment_for_user = $request->get('comment_for_user');
         $vks->status = $request->get('status');
         $vks->approved_by = App::$instance->user->id;
         $vks->save();
         //delete related VKS in CA
         if ($vks->other_tb_required && $vks->link_ca_vks_type == 1 && isset($vks->link_ca_vks_id)) {
             $caVks = CAVksNoSupport::find($vks->link_ca_vks_id);
             if ($caVks->status == VKS_STATUS_TRANSPORT_FOR_TB) {
                 App::$instance->log->logWrite(LOG_OTHER_EVENTS, "Транспортная ВКС " . $caVks->id . " удалена");
                 $backMessage .= "<p>Транспортная ВКС " . $caVks->id . " удалена</p>";
                 $caVks->delete();
             }
         }
         $vks = Vks::full()->find($vks->id);
         $this->humanize($vks);
         App::$instance->callService('vks_report_builder')->sendDeclineMail($vks, false);
         //revoke all outlook requests requests
         OutlookCalendarRequest_controller::changeRequestTypeAndPutToResend($vks->id, OutlookCalendarRequest::REQUEST_TYPE_DELETE);
         if (count($vks->tech_support_requests)) {
             foreach ($vks->tech_support_requests as $request) {
                 if ($request->status == TechSupportRequest::STATUS_WAIT_VKS_DECISION) {
                     $request->status = TechSupportRequest::STATUS_VKS_REFUSED;
                     $request->save();
                 }
             }
         }
         App::$instance->MQ->setMessage($backMessage);
         ST::redirect(ST::route("Vks/notApproved"));
     }
     if (!$request->has('no-codes') && !count($request->get('code'))) {
         $this->error('500', 'bad params combined');
     }
     /*
      * if no codes
      *
      */
     if ($request->has('no-codes')) {
         $backMessage = "<p>Вкс #{$vks->id} согласована без кода подключения</p>";
         //delete old
         $oldCodes = ConnectionCode::where('vks_id', $vks->id)->get();
         if (count($oldCodes)) {
             foreach ($oldCodes as $oldCode) {
                 $oldCode->delete();
             }
         }
         $vks->comment_for_user = $request->get('comment_for_user');
         $vks->status = $request->get('status');
         $vks->flag = $request->get('flag') ? 1 : 0;
         //set admin id
         $vks->approved_by = App::$instance->user->id;
         $vks->save();
         //vks ca process
         $transportVksMessage = App::$instance->callService('vks_ca_negotiator')->transportVksProcessor($this->request, $vks);
         if ($transportVksMessage->getStatusCode() != Response::HTTP_OK) {
             $this->backWithData($transportVksMessage->getContent());
         }
         $vks = Vks::full()->find($vks->id);
         $this->humanize($vks);
         App::$instance->callService('vks_report_builder')->sendReportMail($vks, false);
         App::$instance->MQ->setMessage($backMessage . $transportVksMessage->getContent());
         ST::redirect(ST::route("Vks/notApproved"));
     }
     /*
      * standart process
      */
     //validate codes
     if (!count($request->get('code'))) {
         $this->error('500', 'no code was passed, error!');
     }
     foreach ($request->get('code') as $code) {
         $this->validator->validate(['Префикс' => [$code['prefix'], 'required|max(160)'], 'Постфикс' => [$code['postfix'], 'required|max(4)'], 'Подсказка' => [$code['tip'], 'max(255)']]);
         //if no passes
         if (!$this->validator->passes()) {
             $this->backWithData($this->validator->errors()->all());
         }
     }
     Capsule::beginTransaction();
     $oldCodes = ConnectionCode::where('vks_id', $vks->id)->get();
     if (count($oldCodes)) {
         foreach ($oldCodes as $oldCode) {
             $oldCode->delete();
         }
     }
     foreach ($request->get('code') as $code) {
         $compiledCode = $code['prefix'] . $code['postfix'];
         if ($request->has('no-check')) {
             $checkVks = false;
         } else {
             $checkVks = $connCtrl->isCodeInUse($code['postfix'], $vks->start_date_time, $vks->end_date_time);
         }
         if ($checkVks) {
             App::$instance->MQ->setMessage('Ошибка: Код ' . $code['postfix'] . ' уже используется в ' . ST::linkToVksPage($checkVks->id), 'danger');
             ST::redirect("back");
         } else {
             $newCodes[] = ConnectionCode::create(['vks_id' => $vks->id, 'value' => $compiledCode, 'tip' => $code['tip']]);
         }
     }
     $vks->comment_for_user = $request->get('comment_for_user');
     $vks->status = $request->get('status');
     $vks->flag = $request->get('flag') ? 1 : 0;
     //set admin id
     $vks->approved_by = App::$instance->user->id;
     $vks->save();
     //vks ca process
     $transportVksMessage = App::$instance->callService('vks_ca_negotiator')->transportVksProcessor($this->request, $vks);
     if ($transportVksMessage->getStatusCode() != Response::HTTP_OK) {
         $this->backWithData($transportVksMessage->getContent());
     }
     Capsule::commit();
     //now send mails
     //send mail to user, if it not anon user
     if ($vks->status == VKS_STATUS_APPROVED) {
         $vks = Vks::full()->find($vks->id);
         $this->humanize($vks);
         App::$instance->callService('vks_report_builder')->sendReportMail($vks, false);
         //revoke all outlook requests requests
         OutlookCalendarRequest_controller::changeRequestTypeAndPutToResend($vks->id, OutlookCalendarRequest::REQUEST_TYPE_UPDATE);
     }
     if ($vks->status == VKS_STATUS_APPROVED) {
         App::$instance->MQ->setMessage("<p>ВКС {$vks->id} согласована </p>" . $transportVksMessage->getContent());
         if (count($vks->tech_support_requests)) {
             foreach ($vks->tech_support_requests as $request) {
                 if ($request->status == TechSupportRequest::STATUS_WAIT_VKS_DECISION) {
                     $request->status = TechSupportRequest::STATUS_READY_FOR_SEND;
                     $request->save();
                 }
             }
         }
     } else {
         App::$instance->MQ->setMessage("Отказ по ВКС {$vks->id}");
         if (count($vks->tech_support_requests)) {
             foreach ($vks->tech_support_requests as $request) {
                 if ($request->status == TechSupportRequest::STATUS_WAIT_VKS_DECISION) {
                     $request->status = TechSupportRequest::STATUS_VKS_REFUSED;
                     $request->save();
                 }
             }
         }
     }
     ST::redirect(ST::route("Vks/notApproved"));
 }