Example #1
0
 public function ajaxToggleAvailabilityAction()
 {
     /**
      * @var Request $request
      * @var Response $response
      * @var \DDD\Service\Apartment\Inventory $inventoryService
      */
     $request = $this->getRequest();
     $output = ['bo' => ['status' => 'error']];
     try {
         $date = $request->getPost('date', null);
         $action = $request->getPost('action', null);
         $reasonMessage = $request->getPost('message', null);
         if ($request->isPost() && $request->isXmlHttpRequest()) {
             $inventoryService = $this->getServiceLocator()->get('service_apartment_inventory');
             if (strtotime($date) !== false && in_array($action, ['open', 'close'])) {
                 $apartmentId = $this->apartmentId;
                 $inventoryDao = new InventoryDao($this->getServiceLocator());
                 $availability = $action == 'open' ? 1 : 0;
                 $preInvData = $inventoryDao->getApartmentAvailabilityByDate($apartmentId, date('Y-m-d', strtotime($date)));
                 $preAvailability = null;
                 if ($preInvData) {
                     $preAvailability = $preInvData->getAvailability();
                 }
                 if (!$availability && !$reasonMessage) {
                     throw new \Exception(TextConstants::AVAILABILITY_CLOSE_MSG);
                 }
                 $responseUpdate = $inventoryService->updateAvailabilityFromCalendar($apartmentId, $date, $availability);
                 $auth = $this->getServiceLocator()->get('library_backoffice_auth');
                 if ($responseUpdate['status'] == 'success') {
                     if (!$availability) {
                         $reasonMessage = !empty($reasonMessage) ? '<br><i>Reason:</i> "' . $reasonMessage . '"' : '';
                         /* @var $actionLogger \Library\ActionLogger\Logger */
                         $actionLogger = $this->getServiceLocator()->get('ActionLogger');
                         $actionLogger->save(ActionLogger::MODULE_APARTMENT_CALENDAR, $apartmentId, ActionLogger::ACTION_APARTMENT_CALENDAR_AVAILABILITY, 'Availability <b>closed</b> for ' . $date . $reasonMessage);
                     }
                     $output['bo']['status'] = 'success';
                     $output['bo']['msg'] = $responseUpdate['msg'];
                 } else {
                     throw new \Exception($responseUpdate['msg']);
                     //'Cannot update price.'
                 }
                 $dateExploded = explode(' ', $date);
                 /* @var $apartmentInventoryService \DDD\Service\Apartment\Inventory */
                 $apartmentInventoryService = $this->getServiceLocator()->get('service_apartment_inventory');
                 $accommodationsDao = $this->getServiceLocator()->get('dao_accommodation_accommodations');
                 $bookingOnDate = $apartmentInventoryService->checkApartmentAvailabilityByDate($apartmentId, $dateExploded[0], Booking::BOOKING_STATUS_BOOKED);
                 $userId = $auth->getIdentity()->id;
                 $accInfo = $accommodationsDao->fetchOne(["id" => $apartmentId]);
                 $sellingStatus = [ApartmentService::APARTMENT_STATUS_LIVE_AND_SELLING, ApartmentService::APARTMENT_STATUS_SELLING_NOT_SEARCHABLE];
                 if (in_array($accInfo->getStatus(), $sellingStatus) && $preAvailability != $availability && $availability == 0 && !$bookingOnDate) {
                     $notifService = $this->getServiceLocator()->get('service_notifications');
                     $userManagerDao = $this->getServiceLocator()->get('dao_user_user_manager');
                     $userGroupDao = $this->getServiceLocator()->get('dao_user_user_groups');
                     $accName = $accInfo->getName();
                     $userInfo = $userManagerDao->getUserById($userId);
                     $roledUsers = $userGroupDao->getUsersByGroupId(Roles::ROLE_APARTMENT_AVAILABILITY_MONITOR);
                     $recipient = [];
                     foreach ($roledUsers as $roledUser) {
                         $recipient[] = $roledUser->getUserId();
                     }
                     $now = date('Y-m-d');
                     $sender = NotificationService::$availabilityMonitoring;
                     $calendarDate = date('Y/m', strtotime($date));
                     $closeDate = date('Y-m-d', strtotime($date));
                     if ($availability) {
                         $notifMsg = TextConstants::OPEN_APARTMENT_CALENDAR;
                     } else {
                         $notifMsg = TextConstants::CLOSE_APARTMENT_CALENDAR . ' ' . $reasonMessage;
                     }
                     // notification
                     if (!$auth->hasRole(Roles::ROLE_NO_TRACK_AVAILABILITY_CHANGES)) {
                         $message = sprintf($notifMsg, $userId, $userInfo->getFirstname() . ' ' . $userInfo->getLastname(), $apartmentId, $calendarDate, $accName, $closeDate);
                         $url = '/apartment/' . $apartmentId . '/calendar/' . $calendarDate;
                         $notificationData = ['recipient' => $recipient, 'sender' => $sender, 'sender_id' => $userId, 'message' => $message, 'url' => $url, 'show_date' => $now];
                         $notifService->createNotification($notificationData);
                     }
                 }
             } else {
                 $output['bo']['msg'] = 'Bad parameters.';
             }
         } else {
             $output['bo']['msg'] = 'Bad request.';
         }
     } catch (\Exception $ex) {
         $output['bo']['msg'] = $ex->getMessage();
     }
     return new JsonModel($output);
 }