Пример #1
0
 protected static function getMonthAvailability($room_id_list, $activityId, $person, $timestamp)
 {
     $now = time();
     // Get the month limits
     $monthStart = ReservationPeer::getMonthStart($timestamp);
     $monthStop = ReservationPeer::getMonthStop($timestamp);
     $tst = $monthStart;
     // We get all the reservations for the current room list in the month containing the given timestamp.
     $c = ReservationPeer::getMonthCriteria($timestamp);
     $c->addJoin(ReservationPeer::ROOMPROFILE_ID, RoomprofilePeer::ID);
     $c->addAnd(RoomprofilePeer::ROOM_ID, $room_id_list, Criteria::IN);
     $c->addAscendingOrderByColumn(ReservationPeer::DATE);
     $reservations = ReservationPeer::doSelect($c);
     // We get all the day periods for the current room list.
     $c = new Criteria();
     $c->addAnd(DayperiodPeer::ROOM_ID, $room_id_list, Criteria::IN);
     $c->addAscendingOrderByColumn(DayperiodPeer::DAY_OF_WEEK);
     $c->addAscendingOrderByColumn(DayperiodPeer::START);
     $c->addGroupByColumn(DayperiodPeer::ROOM_ID);
     $c->addGroupByColumn(DayperiodPeer::DAY_OF_WEEK);
     $dayPeriods = DayperiodPeer::doSelect($c);
     // We get all the close periods for the current room list.
     $c = CloseperiodPeer::getMonthCriteria($timestamp);
     $c->addAnd(CloseperiodPeer::ROOM_ID, $room_id_list, Criteria::IN);
     $c->addAscendingOrderByColumn(CloseperiodPeer::START);
     $c->addAscendingOrderByColumn(CloseperiodPeer::STOP);
     $closePeriods = CloseperiodPeer::doSelect($c);
     // Days off
     $dayOff = array();
     for ($i = 0; $i < 7; ++$i) {
         $dayOff[$i] = 0;
     }
     foreach ($dayPeriods as $dayPeriod) {
         ++$dayOff[$dayPeriod->getDayOfWeek()];
     }
     // Opening duration
     $roomsDurations = array();
     for ($dayOfWeek = 0; $dayOfWeek < 7; ++$dayOfWeek) {
         $roomsDurations[$dayOfWeek] = array();
         foreach ($room_id_list as $room_id) {
             $roomsDurations[$dayOfWeek][$room_id] = RoomPeer::getOpeningDuration($room_id, $dayOfWeek);
         }
     }
     $roomsDuration = array();
     // Result
     $result = array();
     for ($i = 1; $tst < $monthStop; ++$i) {
         $roomsId = $room_id_list;
         $dayOfWeek = date('N', $tst) - 1;
         $roomsDuration = $roomsDurations[$dayOfWeek];
         if ($dayOff[$dayOfWeek] == 0) {
             $value = RoomPeer::COMPLETE;
             $roomsId = array();
         } else {
             $value = RoomPeer::FREE;
             foreach ($closePeriods as $closePeriod) {
                 if ($closePeriod->matchFullDayTimestamp($tst)) {
                     $roomsDuration[$closePeriod->getRoomId()] = 0;
                 } elseif ($closePeriod->matchDayTimestamp($tst)) {
                     $roomsDuration[$closePeriod->getRoomId()] -= $closePeriod->getDayCloseDuration($tst);
                     $value = RoomPeer::OCCUPIED;
                 }
             }
             if (strtotime(date('Y-m-d', $now)) > $tst) {
                 $value = RoomPeer::PAST;
             } else {
                 $cnt = 0;
                 foreach ($room_id_list as $roomId) {
                     $maximumTimestamp = $person->getMaximumDate($activityId, $roomId);
                     if ($maximumTimestamp <= $tst || !$person->hasSubscription($activityId, $roomId, $tst)) {
                         ++$cnt;
                         unset($roomsId[array_search($roomId, $roomsId)]);
                     }
                 }
                 if ($cnt == count($room_id_list)) {
                     $value = RoomPeer::TOOFAR;
                 } else {
                     if ($dayOff[$dayOfWeek] < count($room_id_list)) {
                         $value = RoomPeer::OCCUPIED;
                     }
                     foreach ($reservations as $reservation) {
                         if (!$reservation->isOld()) {
                             if ($reservation->matchDayTimestamp($tst)) {
                                 $roomsDuration[$reservation->getRoomprofile()->getRoomId()] -= $reservation->getDuration($tst);
                                 $value = RoomPeer::OCCUPIED;
                             } elseif (strtotime($reservation->getDate() . ' - 1 day') > $tst) {
                                 break;
                             }
                         }
                     }
                     $cnt = 0;
                     foreach ($room_id_list as $room_id) {
                         if ($roomsDuration[$room_id] < 60) {
                             ++$cnt;
                             unset($roomsId[array_search($room_id, $roomsId)]);
                         }
                     }
                     if ($cnt < count($room_id_list)) {
                         if ($cnt > 0) {
                             $value = RoomPeer::OCCUPIED;
                         }
                     } else {
                         $value = RoomPeer::COMPLETE;
                         $roomsId = array();
                     }
                 }
             }
         }
         $result[$i] = array();
         $result[$i]['value'] = $value;
         $result[$i]['timestamp'] = $tst;
         $result[$i]['rooms'] = $roomsId;
         $tst = strtotime(date('Y-m-d', $monthStart) . ' + ' . $i . ' day');
     }
     return $result;
 }