public function __invoke(array $reservations, array $cellLinkParams, Square $square)
 {
     $view = $this->getView();
     $reservationsCount = count($reservations);
     if ($reservationsCount > 1) {
         return $view->calendarCellLink('Occupied', $view->url('square', [], $cellLinkParams), 'cc-single');
     } else {
         $reservation = current($reservations);
         $booking = $reservation->needExtra('booking');
         if ($square->getMeta('public_names', 'false') == 'true') {
             $cellLabel = $booking->needExtra('user')->need('alias');
         } else {
             $cellLabel = null;
         }
         $cellGroup = ' cc-group-' . $booking->need('bid');
         switch ($booking->need('status')) {
             case 'single':
                 if (!$cellLabel) {
                     $cellLabel = 'Occupied';
                 }
                 return $view->calendarCellLink($cellLabel, $view->url('square', [], $cellLinkParams), 'cc-single' . $cellGroup);
             case 'subscription':
                 if (!$cellLabel) {
                     $cellLabel = 'Subscription';
                 }
                 return $view->calendarCellLink($cellLabel, $view->url('square', [], $cellLinkParams), 'cc-multiple' . $cellGroup);
         }
     }
 }
Example #2
0
 public function __invoke(array $eventsForCol, Square $square)
 {
     $eventsForCell = array();
     foreach ($eventsForCol as $eid => $event) {
         $sid = $event->get('sid');
         if ($sid == $square->need('sid') || is_null($sid)) {
             if ($event->need('status') == 'enabled') {
                 $eventsForCell[$eid] = $event;
             }
         }
     }
     return $eventsForCell;
 }
 public function __invoke(array $reservationsForCol, Square $square)
 {
     $reservationsForCell = array();
     foreach ($reservationsForCol as $rid => $reservation) {
         $booking = $reservation->needExtra('booking');
         if ($booking->need('sid') == $square->need('sid')) {
             if ($booking->need('status') != 'cancelled') {
                 if ($booking->need('visibility') == 'public') {
                     $reservationsForCell[$rid] = $reservation;
                 }
             }
         }
     }
     return $reservationsForCell;
 }
Example #4
0
 public function createSingle(User $user, Square $square, $quantity, DateTime $dateTimeStart, DateTime $dateTimeEnd, array $bills = array())
 {
     if (!$this->connection->inTransaction()) {
         $this->connection->beginTransaction();
         $transaction = true;
     } else {
         $transaction = false;
     }
     try {
         $booking = new Booking(array('uid' => $user->need('uid'), 'sid' => $square->need('sid'), 'status' => 'single', 'status_billing' => 'pending', 'visibility' => 'public', 'quantity' => $quantity));
         $this->bookingManager->save($booking);
         $reservations = $this->reservationManager->createInRange($booking, $dateTimeStart, $dateTimeEnd);
         $booking->setExtra('reservations', $reservations);
         $pricing = $this->squarePricingManager->getFinalPricingInRange($dateTimeStart, $dateTimeEnd, $square, $quantity);
         if ($pricing) {
             $squareType = $this->optionManager->need('subject.square.type');
             $squareName = $this->t($square->need('name'));
             /** @var $dateRangeHelper DateRange  */
             $dateRangeHelper = $this->viewHelperManager->get('DateRange');
             $description = sprintf('%s %s, %s', $squareType, $squareName, $dateRangeHelper($dateTimeStart, $dateTimeEnd));
             $bookingBill = new Bill(array('description' => $description, 'quantity' => $quantity, 'time' => $pricing['seconds'], 'price' => $pricing['price'], 'rate' => $pricing['rate'], 'gross' => $pricing['gross']));
             array_unshift($bills, $bookingBill);
         }
         if ($bills) {
             $extraBills = array();
             foreach ($bills as $bill) {
                 if (!$bill instanceof Bill) {
                     throw new RuntimeException('Invalid bills array passed');
                 }
                 $bill->set('bid', $booking->need('bid'));
                 $this->billManager->save($bill);
                 $extraBills[$bill->need('bid')] = $bill;
             }
             $booking->setExtra('bills', $extraBills);
         }
         if ($transaction) {
             $this->connection->commit();
         }
         $this->getEventManager()->trigger('create.single', $booking);
         return $booking;
     } catch (Exception $e) {
         if ($transaction) {
             $this->connection->rollback();
         }
         throw $e;
     }
 }
Example #5
0
 public function __invoke(Square $square, $quantity, $wrap = 'p')
 {
     $squareCapacity = $square->need('capacity');
     $squareCapacityInfo = $square->getMeta('info.capacity');
     if (!$squareCapacityInfo) {
         if ($squareCapacity == 1) {
             $squareCapacityInfo = 'false';
         } else {
             $squareCapacityInfo = 'true';
         }
     }
     if ($squareCapacityInfo == 'true') {
         if ($quantity > 0) {
             return sprintf('<%s><span class="yellow">' . $this->getView()->t('%s/%s already occupied') . '</span></%s>', $wrap, $quantity, $squareCapacity, $wrap);
         }
     }
     return null;
 }
Example #6
0
 public function __invoke(array $reservations, array $cellLinkParams, Square $square)
 {
     $view = $this->getView();
     $reservationsCount = count($reservations);
     if ($reservationsCount == 0) {
         $labelFree = $square->getMeta('label.free', 'Free');
         return $view->calendarCellLink($labelFree, $view->url('backend/booking/edit', [], $cellLinkParams), 'cc-free');
     } else {
         if ($reservationsCount == 1) {
             $reservation = current($reservations);
             $booking = $reservation->needExtra('booking');
             $cellLabel = $booking->needExtra('user')->need('alias');
             $cellGroup = ' cc-group-' . $booking->need('bid');
             return $view->calendarCellLink($cellLabel, $view->url('backend/booking/edit', [], $cellLinkParams), 'cc-free cc-free-partially' . $cellGroup);
         } else {
             $labelFree = $square->getMeta('label.free', 'Still free');
             return $view->calendarCellLink($labelFree, $view->url('backend/booking/edit', [], $cellLinkParams), 'cc-free cc-free-partially');
         }
     }
 }
Example #7
0
 public function __invoke($user, $userBooking, array $reservations, array $cellLinkParams, Square $square)
 {
     $view = $this->getView();
     $labelFree = $square->getMeta('label.free', 'Free');
     if ($user && $user->can('calendar.see-data, calendar.create-single-bookings, calendar.create-subscription-bookings')) {
         return $view->calendarCellRenderFreeForPrivileged($reservations, $cellLinkParams, $square);
     } else {
         if ($user) {
             if ($userBooking) {
                 $cellLabel = $view->t('Your Booking');
                 $cellGroup = ' cc-group-' . $userBooking->need('bid');
                 return $view->calendarCellLink($cellLabel, $view->url('square', [], $cellLinkParams), 'cc-own' . $cellGroup);
             } else {
                 return $view->calendarCellLink($labelFree, $view->url('square', [], $cellLinkParams), 'cc-free');
             }
         } else {
             return $view->calendarCellLink($labelFree, $view->url('square', [], $cellLinkParams), 'cc-free');
         }
     }
 }
Example #8
0
 public function __invoke(Square $square, array $bookings)
 {
     $quantityAvailable = $square->need('capacity');
     foreach ($bookings as $booking) {
         $quantityAvailable -= $booking->need('quantity');
     }
     $view = $this->getView();
     $html = '';
     $html .= '<label for="sb-quantity" style="margin-right: 8px;">';
     $html .= sprintf($view->t('How many %s?'), $this->optionManager->need('subject.square.unit.plural'));
     $html .= '</label>';
     $html .= '<select id="sb-quantity" style="min-width: 64px;">';
     for ($i = 1; $i <= $quantityAvailable; $i++) {
         $html .= sprintf('<option value="%1$s">%1$s</option>', $i);
     }
     $html .= '</select>';
     $quantityOccupied = $square->need('capacity') - $quantityAvailable;
     $capacityInfo = $view->squareCapacityInfo($square, $quantityOccupied, 'span');
     if ($capacityInfo) {
         $html .= '<span style="margin-left: 8px;">' . $capacityInfo . '</span>';
     }
     return $html;
 }
Example #9
0
 protected function renderDateBlockTimeChoice(Square $square, $id, DateTime $timeNow)
 {
     $view = $this->getView();
     $html = '';
     $timeBlockBookable = $square->need('time_block_bookable');
     $timeStartParts = explode(':', $square->need('time_start'));
     $timeStart = $timeStartParts[0] * 3600 + $timeStartParts[1] * 60;
     $timeEndParts = explode(':', $square->need('time_end'));
     $timeEnd = $timeEndParts[0] * 3600 + $timeEndParts[1] * 60;
     if ($timeEnd == 0) {
         $timeEnd = 86400;
     }
     $html .= '<select id="' . $id . '" style="margin-left: 8px;">';
     for ($walkingTime = $timeStart; $walkingTime < $timeEnd; $walkingTime += $timeBlockBookable) {
         $walkingTimeFormat = gmdate('H:i', $walkingTime);
         if ($walkingTimeFormat == $timeNow->format('H:i')) {
             $attr = 'selected="selected"';
         } else {
             $attr = null;
         }
         $html .= '<option value="' . $walkingTimeFormat . '" ' . $attr . '>' . $view->timeFormat($walkingTime, true, 'UTC') . '</option>';
     }
     $html .= '</select>';
     return $html;
 }
Example #10
0
 public function editAction()
 {
     $this->authorize('admin.config');
     $serviceManager = $this->getServiceLocator();
     $squareManager = $serviceManager->get('Square\\Manager\\SquareManager');
     $formElementManager = $serviceManager->get('FormElementManager');
     $sid = $this->params()->fromRoute('sid');
     if ($sid) {
         $square = $squareManager->get($sid);
     } else {
         $square = null;
     }
     $editForm = $formElementManager->get('Backend\\Form\\ConfigSquare\\EditForm');
     if ($this->getRequest()->isPost()) {
         $editForm->setData($this->params()->fromPost());
         if ($editForm->isValid()) {
             $editData = $editForm->getData();
             if (!$square) {
                 $square = new Square();
             }
             $square->set('name', $editData['cf-name']);
             $square->set('status', $editData['cf-status']);
             $square->setMeta('readonly.message', $editData['cf-readonly-message']);
             $square->set('priority', $editData['cf-priority']);
             $square->set('capacity', $editData['cf-capacity']);
             $square->set('capacity_heterogenic', $editData['cf-capacity-heterogenic']);
             $square->set('time_start', $editData['cf-time-start']);
             $square->set('time_end', $editData['cf-time-end']);
             $square->set('time_block', max($editData['cf-time-block'], 10) * 60);
             $square->set('time_block_bookable', max($editData['cf-time-block-bookable'], 10) * 60);
             $square->set('time_block_bookable_max', max($editData['cf-time-block-bookable-max'], 10) * 60);
             $square->set('range_book', max($editData['cf-range-book'], 10) * 60 * 60 * 24);
             $square->set('range_cancel', max($editData['cf-range-cancel'], 10) * 60 * 60);
             $squareManager->save($square);
             $this->flashMessenger()->addSuccessMessage('Square has been saved');
             return $this->redirect()->toRoute('backend/config/square');
         }
     } else {
         if ($square) {
             $editForm->setData(array('cf-name' => $square->get('name'), 'cf-status' => $square->get('status'), 'cf-readonly-message' => $square->getMeta('readonly.message'), 'cf-priority' => $square->get('priority'), 'cf-capacity' => $square->get('capacity'), 'cf-capacity-heterogenic' => $square->get('capacity_heterogenic'), 'cf-time-start' => substr($square->get('time_start'), 0, 5), 'cf-time-end' => substr($square->get('time_end'), 0, 5), 'cf-time-block' => round($square->get('time_block') / 60), 'cf-time-block-bookable' => round($square->get('time_block_bookable') / 60), 'cf-time-block-bookable-max' => round($square->get('time_block_bookable_max') / 60), 'cf-range-book' => round($square->get('range_book') / 60 / 60 / 24), 'cf-range-cancel' => round($square->get('range_cancel') / 60 / 60)));
         } else {
             $editForm->setData(array('cf-status' => 'enabled', 'cf-priority' => 1, 'cf-capacity' => 1, 'cf-capacity-heterogenic' => false, 'cf-time-start' => '08:00', 'cf-time-end' => '23:00', 'cf-time-block' => 60, 'cf-time-block-bookable' => 30, 'cf-time-block-bookable-max' => 180, 'cf-range-book' => 56, 'cf-range-cancel' => 24));
         }
     }
     return array('square' => $square, 'editForm' => $editForm);
 }
Example #11
0
 /**
  * Gets all square products for the passed square.
  *
  * @param Square $square
  * @return array
  */
 public function getBySquare(Square $square)
 {
     $where = new Where();
     $where = $where->nest();
     $where->isNull('sid')->or->equalTo('sid', $square->need('sid'));
     $where = $where->unnest();
     $where = $where->and->nest();
     $where->isNull('locale')->or->equalTo('locale', $this->locale);
     $where = $where->unnest();
     return $this->getBy($where, 'priority ASC');
 }
Example #12
0
 /**
  * Deletes one square and all respective meta properties (through database foreign keys).
  *
  * @param int|Square $square
  * @return int
  * @throws InvalidArgumentException
  */
 public function delete($square)
 {
     if ($square instanceof Square) {
         $sid = $square->need('sid');
     } else {
         $sid = $square;
     }
     if (!(is_numeric($sid) && $sid > 0)) {
         throw new InvalidArgumentException('Square id must be numeric');
     }
     $square = $this->get($sid);
     $deletion = $this->squareTable->delete(array('sid' => $sid));
     $this->getEventManager()->trigger('delete', $square);
     return $deletion;
 }
Example #13
0
 public function __invoke(Square $square)
 {
     $view = $this->getView();
     $html = '';
     $html .= '<tr>';
     $html .= sprintf('<td class="priority-col">%s</td>', $square->get('priority'));
     $html .= sprintf('<td><span class="gray">%s</span> %s</td>', $view->option('subject.square.type'), $square->get('name'));
     $html .= sprintf('<td>%s</td>', $view->t($square->getStatus()));
     $html .= sprintf('<td>%s</td>', $view->timeRange($square->need('time_start'), $square->need('time_end'), '%s to %s'));
     $html .= sprintf('<td>%s</td>', $view->prettyTime($square->need('time_block')));
     $html .= sprintf('<td>%s</td>', $view->prettyTime($square->need('time_block_bookable')));
     $html .= sprintf('<td>%s</td>', $view->prettyTime($square->need('time_block_bookable_max')));
     $html .= '<td class="actions-col no-print">' . '<a href="' . $view->url('backend/config/square/edit', ['sid' => $square->need('sid')]) . '" class="unlined gray symbolic symbolic-edit">' . $view->t('Edit') . '</a> &nbsp; ' . '<a href="' . $view->url('backend/config/square/delete', ['sid' => $square->need('sid')]) . '" class="unlined gray symbolic symbolic-cross">' . $view->t('Delete') . '</a></td>';
     $html .= '</tr>';
     return $html;
 }
Example #14
0
 /**
  * Determines the final calculated pricing for the passed square and datetime interval.
  *
  * @param DateTime $dateTimeStart
  * @param DateTime $dateTimeEnd
  * @param Square $square
  * @param int $quantity
  * @return array
  * @throws InvalidArgumentException
  * @throws RuntimeException
  */
 public function getFinalPricingInRange(DateTime $dateTimeStart, DateTime $dateTimeEnd, Square $square, $quantity)
 {
     if ($dateTimeStart > $dateTimeEnd) {
         throw new InvalidArgumentException('The passed date range is invalid');
     }
     $finalPricingInRange = array();
     $days = $dateTimeEnd->format('z') - $dateTimeStart->format('z');
     $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');
         }
         $finalPricing = $this->getFinalPricing($walkingDate, $walkingTimeStart, $walkingTimeEnd, $square, $quantity);
         if ($finalPricing) {
             if (!isset($finalPricingInRange['price'])) {
                 $finalPricingInRange['price'] = $finalPricing['price'];
             } else {
                 $finalPricingInRange['price'] += $finalPricing['price'];
             }
             if (!isset($finalPricingInRange['rate'])) {
                 $finalPricingInRange['rate'] = $finalPricing['rate'];
             } else {
                 if ($finalPricingInRange['rate'] != $finalPricing['rate']) {
                     throw new RuntimeException('Pricing rates must be consistent');
                 }
             }
             if (!isset($finalPricingInRange['gross'])) {
                 $finalPricingInRange['gross'] = $finalPricing['gross'];
             } else {
                 if ($finalPricingInRange['gross'] != $finalPricing['gross']) {
                     throw new RuntimeException('Pricing gross must be consistent');
                 }
             }
             if (!isset($finalPricingInRange['seconds'])) {
                 $finalPricingInRange['seconds'] = $finalPricing['seconds'];
             } else {
                 $finalPricingInRange['seconds'] += $finalPricing['seconds'];
             }
             if (!isset($finalPricingInRange['per_quantity'])) {
                 $finalPricingInRange['per_quantity'] = $finalPricing['per_quantity'];
             } else {
                 if ($finalPricingInRange['per_quantity'] != $finalPricing['per_quantity']) {
                     throw new RuntimeException('Pricing per quantity must be consistent');
                 }
             }
         }
         $walkingDate->modify('+1 day');
         $walkingDateIndex++;
     }
     return $finalPricingInRange;
 }