Example #1
0
 public function bookCourt()
 {
     $isAllowed = Util\Auth::isAuthorized($this->signedUser, 'member');
     $json = [];
     $json['result'] = \Rebond\Core\ResultType::ERROR;
     if (!$isAllowed) {
         $json['message'] = Util\Lang::lang('accessNonAuthorized');
         return json_encode($json);
     }
     $courtId = Util\Converter::toInt('courtId', 'post');
     $dateTime = Util\Converter::toString('dateTime', 'post');
     $bookingType = Util\Converter::toInt('bookingType', 'post');
     $playerId = Util\Converter::toInt('playerId', 'post');
     $pay = Util\Converter::toInt('pay', 'post');
     $guest = Util\Converter::toString('guest', 'post');
     if (!isset($courtId, $dateTime, $bookingType, $playerId, $pay, $guest) || !array_key_exists($bookingType, \Own\Bus\BookingType::toArray())) {
         $json['message'] = Util\Lang::lang('error.invalid.parameters');
         return json_encode($json);
     }
     $dateTime = new Util\DateTime($dateTime);
     if ($dateTime < new \DateTime()) {
         $json['message'] = Util\Lang::lang('error.invalid.time');
         return json_encode($json);
     }
     switch ($bookingType) {
         case \Own\Bus\BookingType::PARTNER:
             $guest = '';
             break;
         case \Own\Bus\BookingType::GUEST:
             $pay = \Own\Bus\Pay::PLAYER_1;
             $playerId = 0;
             break;
         case \Own\Bus\BookingType::CAROUSSEL:
             $pay = \Own\Bus\Pay::BOTH;
             $playerId = 0;
             $guest = '';
             break;
         default:
     }
     // check player
     $player = \Own\Bus\Player\Data::loadById($playerId);
     if (!isset($player) || $player->getIdItem()->getStatus() != 1) {
         $json['message'] = Util\Lang::lang('playerNotFound');
         return json_encode($json);
     }
     if ($pay == \Own\Bus\Pay::BOTH && $this->player->getCredits() < 1 || $pay == \Own\Bus\Pay::PLAYER_1 && $this->player->getCredits() < 2) {
         $json['message'] = Util\Lang::lang('notEnoughCredits');
         return json_encode($json);
     }
     // check credits
     if ($pay == \Own\Bus\Pay::BOTH && $player->getCredits() < 1 || $pay == \Own\Bus\Pay::PLAYER_2 && $player->getCredits() < 2) {
         $json['message'] = Util\Lang::lang('partnerNotEnoughCredits');
         return json_encode($json);
     }
     // check membership
     $memberships = \Own\Bus\Player\Data::hasMembership($courtId, [$this->player->getId(), $playerId]);
     if ($memberships != 2 || $memberships != 1 && $bookingType != \Own\Bus\BookingType::PARTNER) {
         $json['message'] = Util\Lang::lang('noMembership');
         return json_encode($json);
     }
     // check court time
     $options = [];
     $options['where'][] = ['id = ?', $courtId];
     $court = \Own\Bus\Court\Data::load($options);
     if (!isset($court)) {
         $json['message'] = Util\Lang::lang('error.invalid.parameters');
         return json_encode($json);
     }
     $rule = \Own\Bus\Rule\Data::loadById(1, true);
     $timeLength = $rule->getTimeLength();
     $askedTime = (int) $dateTime->format('H') * 60 + (int) $dateTime->format('i');
     $possibleTime = (int) $court->getStartTime()->format('H') * 60 + (int) $court->getStartTime()->format('i');
     $limitTime = (int) $court->getEndTime()->format('H') * 60 + (int) $court->getEndTime()->format('i');
     if ($askedTime < $possibleTime || $askedTime > $limitTime || $askedTime % $timeLength != 0) {
         $json['message'] = Util\Lang::lang('error.invalid.time');
         return json_encode($json);
     }
     // check for court already booked
     $options = [];
     $options['where'][] = ['court_id = ?', $courtId];
     $options['where'][] = ['booking_date = ?', $dateTime->format('Y-m-d H:i:00')];
     $booking = Book\Data::load($options);
     if (isset($booking)) {
         $json['message'] = Util\Lang::lang('alreadyBookedTime');
         return json_encode($json);
     }
     // check for same hour
     $options = [];
     $options['where'][] = ['player1_id = ? OR player2_id = ?', $this->player->getId(), $this->player->getId()];
     $options['where'][] = ['booking_date BETWEEN ? AND ?', $dateTime->format('Y-m-d H:00:00'), $dateTime->format('Y-m-d H:59:00')];
     $booking = Book\Data::load($options);
     if (isset($booking)) {
         $json['message'] = Util\Lang::lang('alreadyBookedTime');
         return json_encode($json);
     }
     // check for max booking
     $maxBooking = $this->player->getMaxBooking();
     if ($maxBooking > 0) {
         $options = [];
         $options['where'][] = ['player1_id = ? OR player2_id = ?', $this->player->getId(), $this->player->getId()];
         $options['where'][] = 'booking_date > NOW()';
         $futureBookings = Book\Data::count($options);
         if ($futureBookings >= $maxBooking) {
             $json['message'] = Util\Lang::lang('maxBookingReachedByYou');
             return json_encode($json);
         }
     }
     $options = [];
     $options['where'][] = ['player1_id = ? OR player2_id = ?', $playerId, $playerId];
     $options['where'][] = 'booking_date > NOW()';
     $futureBookings = Book\Data::count($options);
     if ($futureBookings >= $player->getMaxBooking()) {
         $json['message'] = Util\Lang::lang('maxBookingReached', [$player->getIdItem()->getFullName()]);
         return json_encode($json);
     }
     // add booking
     if ($pay == \Own\Bus\Pay::BOTH) {
         $this->player->addCredits(-1);
         $this->player->save();
         $player->addCredits(-1);
         $player->save();
     } else {
         if ($pay == \Own\Bus\Pay::PLAYER_1) {
             $this->player->addCredits(-2);
             $this->player->save();
         } else {
             if ($pay == \Own\Bus\Pay::PLAYER_2) {
                 $player->addCredits(-2);
                 $player->save();
             }
         }
     }
     $book = new Book\Model();
     $book->setType($bookingType);
     $book->setPlayer1Id($this->player->getId());
     $book->setPlayer2Id($playerId);
     $book->setCourtId($courtId);
     $book->setBookingDate($dateTime);
     $book->setPay($pay);
     $book->setGuest($guest);
     $book->save();
     $json['result'] = \Rebond\Core\ResultType::SUCCESS;
     $json['message'] = Util\Lang::lang('courtBooked');
     return json_encode($json);
 }