public function getRegistrationCodeByTerm(SS_HTTPRequest $request)
 {
     try {
         $term = Convert::raw2sql($request->param('REG_CODE'));
         $summit_id = intval($request->param('SUMMIT_ID'));
         $summit = Summit::get_by_id('Summit', $summit_id);
         if (is_null($summit)) {
             throw new NotFoundEntityException('Summit', sprintf(' id %s', $summit_id));
         }
         $codes = SpeakerSummitRegistrationPromoCode::get()->filter(array('SummitID' => $summit_id, 'OwnerID' => 0, 'SpeakerID' => 0))->where(" Code LIKE '{$term}%' ")->limit(25, 0);
         $data = array();
         foreach ($codes as $code) {
             $data[] = array('code' => trim($code->Code), 'name' => sprintf('%s (%s)', $code->Code, $code->Type));
         }
         return $this->ok($data, false);
     } catch (NotFoundEntityException $ex2) {
         SS_Log::log($ex2->getMessage(), SS_Log::WARN);
         return $this->notFound($ex2->getMessage());
     } catch (Exception $ex) {
         SS_Log::log($ex->getMessage(), SS_Log::ERR);
         return $this->serverError();
     }
 }
 /**
  * @param $promo_code_value
  * @param ISummit $summit
  * @return ISpeakerSummitRegistrationPromoCode
  * @throws EntityValidationException
  * @throws ValidationException
  */
 public function registerSummitPromoCodeByValue($promo_code_value, ISummit $summit)
 {
     // check if we have an assigned one already
     $old_code = SpeakerSummitRegistrationPromoCode::get()->filter(['SummitID' => $summit->getIdentifier(), 'SpeakerID' => $this->ID])->first();
     // we are trying to update the promo code with another one ....
     if ($old_code && $promo_code_value !== $old_code->Code) {
         throw new EntityValidationException(sprintf('speaker has already assigned to another registration code (%s)', $old_code->Code));
     }
     //we already have the same code ...
     if ($old_code) {
         return $old_code;
     }
     // check if the promo code already exists and assigned to another user
     $existent_code = SpeakerSummitRegistrationPromoCode::get()->filter(['Code' => $promo_code_value, 'SummitID' => $summit->getIdentifier(), 'SpeakerID:ExactMatch:not' => 0])->first();
     if ($existent_code) {
         throw new EntityValidationException(sprintf('there is another speaker with that code for this summit (%s)', $promo_code_value));
     }
     // check if promo code exists and its not assigned ...
     $code = SpeakerSummitRegistrationPromoCode::get()->filter(['Code' => $promo_code_value, 'SummitID' => $summit->getIdentifier(), 'SpeakerID' => 0])->first();
     if (!$code) {
         //create it
         $code = SpeakerSummitRegistrationPromoCode::create();
         $code->SummitID = $summit->getIdentifier();
         $code->Code = $promo_code_value;
         $code->write();
     }
     $this->registerSummitPromoCode($code);
     $code->write();
     return $code;
 }
 /**
  * @param int $summit_id
  * @param int $speaker_id
  * @return ISummitRegistrationPromoCode
  */
 public function getBySpeaker($summit_id, $speaker_id)
 {
     $promo_codes = SpeakerSummitRegistrationPromoCode::get()->where("SummitID = {$summit_id} AND SpeakerID = {$speaker_id}");
     return $promo_codes;
 }