コード例 #1
0
ファイル: SquareValidator.php プロジェクト: Bertie1708/ep3-bs
 /**
  * Checks if the current user is allowed to cancel this booking.
  *
  * @param Booking $booking
  * @return boolean
  * @throws RuntimeException
  */
 public function isCancellable(Booking $booking)
 {
     if ($this->user && $this->user->can('calendar.cancel-single-bookings')) {
         if ($booking->need('status') == 'single') {
             return true;
         }
     }
     if ($this->user && $this->user->can('calendar.cancel-subscription-bookings')) {
         if ($booking->need('status') == 'subscription') {
             return true;
         }
     }
     if (!($this->user && $this->user->need('uid') == $booking->need('uid'))) {
         throw new RuntimeException('You have no permission to cancel this booking');
     }
     if ($booking->need('status') == 'subscription') {
         throw new RuntimeException('You have no permission to cancel this subscription');
     }
     $square = $this->squareManager->get($booking->need('sid'));
     $squareCancelRange = $square->get('range_cancel');
     if (!$squareCancelRange) {
         return false;
     }
     $reservations = $this->reservationManager->getBy(array('bid' => $booking->need('bid')), 'date ASC, time_start ASC');
     $reservation = current($reservations);
     if (!$reservation) {
         return true;
     }
     $reservationStartDate = new DateTime($reservation->need('date') . ' ' . $reservation->need('time_start'));
     $reservationCancelDate = new DateTime();
     $reservationCancelDate->modify('+' . $squareCancelRange . ' sec');
     if ($reservationStartDate > $reservationCancelDate) {
         return true;
     } else {
         return false;
     }
 }
コード例 #2
0
ファイル: BookingService.php プロジェクト: Mendim/ep3-bs
 public function cancelSingle(Booking $booking)
 {
     $booking->set('status', 'cancelled');
     $this->bookingManager->save($booking);
     $this->getEventManager()->trigger('cancel.single', $booking);
     return $booking;
 }
コード例 #3
0
ファイル: ReservationManager.php プロジェクト: Mendim/ep3-bs
 /**
  * Creates an array of new reservations for the specified datetime interval.
  *
  * @param Booking $booking
  * @param string|DateTime $dateTimeStart
  * @param string|DateTime $dateTimeEnd
  * @return array
  * @throws InvalidArgumentException
  */
 public function createInRange(Booking $booking, $dateTimeStart, $dateTimeEnd)
 {
     $connection = $this->reservationTable->getAdapter()->getDriver()->getConnection();
     if (!$connection->inTransaction()) {
         $connection->beginTransaction();
         $transaction = true;
     } else {
         $transaction = false;
     }
     try {
         if (is_string($dateTimeStart)) {
             $dateTimeStart = new DateTime($dateTimeStart);
         }
         if (!$dateTimeStart instanceof DateTime) {
             throw new InvalidArgumentException('Invalid start datetime passed');
         }
         if (is_string($dateTimeEnd)) {
             $dateTimeEnd = new DateTime($dateTimeEnd);
         }
         if (!$dateTimeEnd instanceof DateTime) {
             throw new InvalidArgumentException('Invalid end datetime passed');
         }
         if ($dateTimeStart > $dateTimeEnd) {
             throw new InvalidArgumentException('Invalid datetime range passed');
         }
         $days = $dateTimeEnd->format('z') - $dateTimeStart->format('z');
         if ($days > 186) {
             throw new InvalidArgumentException('Maximum date range exceeded');
         }
         $square = $this->squareManager->get($booking->need('sid'));
         $reservations = array();
         $walkingDate = clone $dateTimeStart;
         $walkingDate->setTime(0, 0, 0);
         $walkingDateLimit = clone $dateTimeEnd;
         $walkingDateLimit->setTime(0, 0, 0);
         $walkingDateIndex = 0;
         while ($walkingDate <= $walkingDateLimit) {
             if ($walkingDateIndex == 0) {
                 $walkingTimeStart = $dateTimeStart->format('H:i');
             } else {
                 $walkingTimeStart = $square->need('time_start');
             }
             if ($walkingDateIndex == $days) {
                 $walkingTimeEnd = $dateTimeEnd->format('H:i');
             } else {
                 $walkingTimeEnd = $square->need('time_end');
             }
             $reservation = $this->create($booking, $walkingDate, $walkingTimeStart, $walkingTimeEnd);
             $reservations[$reservation->need('rid')] = $reservation;
             $walkingDate->modify('+1 day');
             $walkingDateIndex++;
         }
         if ($transaction) {
             $connection->commit();
         }
         $this->getEventManager()->trigger('createRange', $reservations);
         return $reservations;
     } catch (Exception $e) {
         if ($transaction) {
             $connection->rollback();
         }
         throw $e;
     }
 }
コード例 #4
0
ファイル: BookingManager.php プロジェクト: Mendim/ep3-bs
 /**
  * Deletes one booking, all respective meta properties and all respective bills (through database foreign keys).
  *
  * @param int|Booking $booking
  * @return int
  * @throws InvalidArgumentException
  */
 public function delete($booking)
 {
     if ($booking instanceof Booking) {
         $bid = $booking->need('bid');
     } else {
         $bid = $booking;
     }
     if (!(is_numeric($bid) && $bid > 0)) {
         throw new InvalidArgumentException('Booking id must be numeric');
     }
     $booking = $this->get($bid);
     $deletion = $this->bookingTable->delete(array('bid' => $bid));
     $this->getEventManager()->trigger('delete', $booking);
     return $deletion;
 }