From 0ac81276537876fc6f3d9c80e471326c05fe6fad Mon Sep 17 00:00:00 2001 From: adametz Date: Tue, 14 Aug 2018 15:14:56 +0000 Subject: [PATCH] #1423 #1441 - Ansicht der Kalender, Kalender neu auslesen und darstellen git-svn-id: http://78.47.251.156/svn/dev/sterntours-3@3441 f459cee4-fb09-11de-96c3-f9c5f16c3c76 --- trunk/.idea/dataSources.xml | 19 ++ trunk/.idea/vcs.xml | 6 + .../cms/calendarLodgingProgram.html.twig | 57 ++---- .../pages/cms/fewoTravelProgram.html.twig | 48 ++++- .../views/default/pages/fewoBooking.html.twig | 5 +- .../AppBundle/Controller/CmsController.php | 4 +- .../Controller/FewoBookingController.php | 2 +- trunk/src/AppBundle/Entity/FewoLodging.php | 29 +++ .../Service/LodgingCalendarService.php | 187 +++++++++++++++--- trunk/src/AppBundle/Util/CalendarDayState.php | 81 ++++++++ 10 files changed, 365 insertions(+), 73 deletions(-) create mode 100644 trunk/.idea/dataSources.xml create mode 100644 trunk/.idea/vcs.xml diff --git a/trunk/.idea/dataSources.xml b/trunk/.idea/dataSources.xml new file mode 100644 index 00000000..445b1689 --- /dev/null +++ b/trunk/.idea/dataSources.xml @@ -0,0 +1,19 @@ + + + + + mysql + true + com.mysql.jdbc.Driver + jdbc:mysql://localhost:3306/sterntours + + + + + + + + + + + \ No newline at end of file diff --git a/trunk/.idea/vcs.xml b/trunk/.idea/vcs.xml new file mode 100644 index 00000000..6c0b8635 --- /dev/null +++ b/trunk/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/trunk/app/Resources/views/default/pages/cms/calendarLodgingProgram.html.twig b/trunk/app/Resources/views/default/pages/cms/calendarLodgingProgram.html.twig index 914fccf7..28fb41da 100644 --- a/trunk/app/Resources/views/default/pages/cms/calendarLodgingProgram.html.twig +++ b/trunk/app/Resources/views/default/pages/cms/calendarLodgingProgram.html.twig @@ -25,46 +25,23 @@ {# @var dayState \AppBundle\Util\CalendarDayState #} {% set dayState = calendar_month['data'][(weekIndex * 7) + dayIndex] %} - {% set is_past_date = dayState.date is not empty and date(dayState.date) < date('now') %} - - {# Set cell class #} - - {% if dayState.isReserved and (not dayState.isReservationBegin and not dayState.isReservationEnd) %} - {% set cell_class = 'calendar-day-disabled' %} - {% elseif dayState.day == 0 %} - {% set cell_class = 'calendar-day-non' %} - {% elseif is_past_date %} - {% set cell_class = 'calendar-day-past' %} - {% elseif not dayState.isInSeason %} - {% set cell_class = 'calendar-day-non' %} - {% elseif dayState.isReservationBegin %} - {% set cell_class = 'calendar-day-reservation-begin' %} - {% elseif dayState.isReservationEnd %} - {% set cell_class = 'calendar-day-reservation-end' %} - {% else %} - {% set cell_class = '' %} - {% endif %} - - - {% if dayState.isBookable and not is_past_date %} - - {{ dayState.day }} - - {% elseif dayState.day == 0 %} -   - {% elseif cell_class == '' %} - {{ dayState.day }} - {% else %} - {% if is_past_date %} - {{ dayState.day }} - {% else %} - {{ dayState.day }} - {% endif %} - {% endif %} - + + {% if dayState.isBookable %} + + {{ dayState.day }} + + {% elseif dayState.day == 0 %} +   + {% else %} + {% if dayState.isPastDate %} + {{ dayState.day }} + {% else %} + {{ dayState.day }} + {% endif %} + {% endif %} + {% endfor %} {% endfor %} diff --git a/trunk/app/Resources/views/default/pages/cms/fewoTravelProgram.html.twig b/trunk/app/Resources/views/default/pages/cms/fewoTravelProgram.html.twig index 390201c8..91cfab50 100644 --- a/trunk/app/Resources/views/default/pages/cms/fewoTravelProgram.html.twig +++ b/trunk/app/Resources/views/default/pages/cms/fewoTravelProgram.html.twig @@ -61,7 +61,7 @@
@@ -86,7 +86,7 @@ ********* BESCHREIBUNG ********* #} -
+

Allgemeines

@@ -158,7 +158,7 @@ ********* TERMINE UND PREISE ********* #} -
+

Saisons

@@ -214,7 +214,7 @@

Buchung

-

Bitte klicken Sie einen Anreisetermin, um zur Buchungsmaske zu gelagen.

+

Bitte klicken Sie einen Anreisetermin (grĂ¼ne Zahl), um zur Buchungsmaske zu gelagen.

{% include 'default/pages/cms/calendarLodgingProgram.html.twig' %} diff --git a/trunk/app/Resources/views/default/pages/fewoBooking.html.twig b/trunk/app/Resources/views/default/pages/fewoBooking.html.twig index 6910b64d..8fd95227 100644 --- a/trunk/app/Resources/views/default/pages/fewoBooking.html.twig +++ b/trunk/app/Resources/views/default/pages/fewoBooking.html.twig @@ -37,7 +37,7 @@ -{# +
@@ -265,10 +265,11 @@ {{ form_rest(form) }} +
-#} +
diff --git a/trunk/src/AppBundle/Controller/CmsController.php b/trunk/src/AppBundle/Controller/CmsController.php index 0a663507..352010dc 100644 --- a/trunk/src/AppBundle/Controller/CmsController.php +++ b/trunk/src/AppBundle/Controller/CmsController.php @@ -144,8 +144,8 @@ class CmsController extends Controller $paddedCalendar = $calendarService->getCalendarWithPadding($calendarService->getMinCalendarEntriesByLodging($lodging)); if (count($lodging->getPrices()->toArray()) != 0) { - $calendar = $calendarService->createCalendarAndFillDayStates($lodging); - $calendar = $calendarService->mergeWithPaddedCalendar($calendar, $paddedCalendar); + $calendar = $calendarService->calendarAndFillDayStates($paddedCalendar, $lodging); + // $calendar = $calendarService->mergeWithPaddedCalendar($calendar, $paddedCalendar); } else { $calendar = $paddedCalendar; } diff --git a/trunk/src/AppBundle/Controller/FewoBookingController.php b/trunk/src/AppBundle/Controller/FewoBookingController.php index b727fb6b..916225f5 100644 --- a/trunk/src/AppBundle/Controller/FewoBookingController.php +++ b/trunk/src/AppBundle/Controller/FewoBookingController.php @@ -67,7 +67,6 @@ class FewoBookingController extends Controller $fromDate = $calendarService->convertDate($fromDate); $fromDateTime = new \DateTime($fromDate); $toDate = ''; - $maxPersons = $lodging->getMaximumPersons(); @@ -80,6 +79,7 @@ class FewoBookingController extends Controller $fewoBookingRequest->setLodging($lodging); $fewoBookingRequest->setPrice($price); + $allowedToDateStrs = []; if (!$lodging->getPrices()->isEmpty()) { diff --git a/trunk/src/AppBundle/Entity/FewoLodging.php b/trunk/src/AppBundle/Entity/FewoLodging.php index b05340cb..a5afadfd 100644 --- a/trunk/src/AppBundle/Entity/FewoLodging.php +++ b/trunk/src/AppBundle/Entity/FewoLodging.php @@ -522,6 +522,22 @@ class FewoLodging return $this->prices; } + /** + * Get prices + * + * @return \Doctrine\Common\Collections\Collection + */ + public function getPricesFilter($dateFrom, $dateTo) + { + + return $this->getPrices()->filter(function(FewoPrice $price) use ($dateFrom, $dateTo) { + return ($price->getSeason()->getFromDate() >= $dateFrom && $price->getSeason()->getFromDate() <= $dateTo) || + ($price->getSeason()->getToDate() >= $dateFrom && $price->getSeason()->getToDate() <= $dateTo); + }); + } + + + /** * Add image * @@ -590,6 +606,19 @@ class FewoLodging return $this->reservations; } + /** + * Get reservationsFilter + * + * @return \Doctrine\Common\Collections\Collection + */ + public function getReservationsFilter($dateFrom, $dateTo) + { + return $this->getReservations()->filter(function(FewoReservation $reservation) use ($dateFrom, $dateTo) { + return ($reservation->getFromDate() >= $dateFrom && $reservation->getFromDate() <= $dateTo) || + ($reservation->getToDate() >= $dateFrom && $reservation->getToDate() <= $dateTo); + }); + } + /** * Set page * diff --git a/trunk/src/AppBundle/Service/LodgingCalendarService.php b/trunk/src/AppBundle/Service/LodgingCalendarService.php index d48c1323..83efea23 100644 --- a/trunk/src/AppBundle/Service/LodgingCalendarService.php +++ b/trunk/src/AppBundle/Service/LodgingCalendarService.php @@ -25,7 +25,7 @@ class LodgingCalendarService { return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31); } - + //$months = number of Month to the last season public function getCalendarWithPadding($months) { $calendar = null; @@ -37,6 +37,7 @@ class LodgingCalendarService $yearTurned = false; $yearsTurned = 0; + $now = new \DateTime(); for($m = $currentMonth; $m < ($currentMonth + $months); $m++) { @@ -47,9 +48,7 @@ class LodgingCalendarService $actualMonth = 12; $yearTurned = true; } - $actualYear = $currentYear + $yearsTurned; - if($yearTurned) { $yearsTurned = $yearsTurned + 1; @@ -68,7 +67,7 @@ class LodgingCalendarService { $actualStartingWeekDay = 7; } - + //first empty days from the last month for($fwd = 0; $fwd < $actualStartingWeekDay - 1; $fwd++) { $day = new CalendarDayState(); @@ -78,25 +77,35 @@ class LodgingCalendarService $day->setIsReserved(false); $day->setIsReservationBegin(false); $day->setIsReservationEnd(false); + $day->setIsEmpty(true); + $day->setIsPastDate(false); $data[] = $day; } - + //days of month for($d = 1; $d <= $numberDays; $d++) { $day = new CalendarDayState(); $day->setDay($d); + $datetime = new \DateTime(); + $datetime->setTimestamp(mktime(0,0,0, $actualMonth, $d, $actualYear)); + $day->setDate($datetime); $day->setIsBookable(false); $day->setIsInSeason(false); $day->setIsReserved(false); $day->setIsReservationBegin(false); $day->setIsReservationEnd(false); - + $day->setIsEmpty(false); + if($datetime < $now){ + $day->setIsPastDate(true); + }else{ + $day->setIsPastDate(false); + } $data[] = $day; } $startIndex = count($data); - + //last empty days from the next month for($lwd = $startIndex; $lwd < 42; $lwd++) { $day = new CalendarDayState(); @@ -106,11 +115,12 @@ class LodgingCalendarService $day->setIsReserved(false); $day->setIsReservationBegin(false); $day->setIsReservationEnd(false); + $day->setIsEmpty(true); + $day->setIsPastDate(false); $data[] = $day; } - - $calendar[] = [ + $calendar[$actualYear."-".$actualMonth] = [ 'numberDays' => $numberDays, 'monthNumber' => $actualMonth, 'monthName' => utf8_encode(strftime("%B", mktime(0, 0, 0, $actualMonth, 1, $actualYear))), @@ -161,7 +171,6 @@ class LodgingCalendarService $numberDays = $this->daysInMonth($actualMonth, $actualYear); - /** @var CalendarDayState[] $data */ $data = null; // alle Wochentage @@ -257,6 +266,7 @@ class LodgingCalendarService } + /** * @param $calendar * @param \DateTime $fromDate @@ -296,7 +306,6 @@ class LodgingCalendarService // anfang und ende innerhalb eines monats if($startCalendarIndex == $endCalendarIndex) { - /** @var CalendarDayState[] $data */ $data = $calendar[$startCalendarIndex]['data']; $calendar[$startCalendarIndex]['marked'] = 1; @@ -348,7 +357,6 @@ class LodgingCalendarService { // erster monat - /** @var CalendarDayState[] $data */ $data = $calendar[$startCalendarIndex]['data']; $calendar[$startCalendarIndex]['marked'] = 1; @@ -388,7 +396,6 @@ class LodgingCalendarService // letzter monat - /** @var CalendarDayState[] $data */ $data = $calendar[$endCalendarIndex]['data']; $calendar[$endCalendarIndex]['marked'] = 1; @@ -431,7 +438,6 @@ class LodgingCalendarService // alle monate dazwischen for($i = $startCalendarIndex + 1; $i < $endCalendarIndex; $i++) { - /** @var CalendarDayState[] $data */ $data = $calendar[$i]['data']; $calendar[$i]['marked'] = 1; @@ -463,15 +469,14 @@ class LodgingCalendarService return $calendar; } + // in der Regel: calendar = pricesCalendar; extensionCalendar = reservationsCalendar - private function mergeCalendars($calendar, $extensionCalendar) + private function mergeCalendars($calendar, $extensionCalendar) { for($i = 0; $i < count($calendar); $i++) { - /** @var CalendarDayState[] $calendarData */ $calendarData = $calendar[$i]['data']; - /** @var CalendarDayState[] $extensionCalendarData */ $extensionCalendarData = $extensionCalendar[$i]['data']; $endIndex = count($calendarData); @@ -492,7 +497,6 @@ class LodgingCalendarService $calendarData[$j]->setIsReservationEnd(false); } - /** @var FewoPrice $price */ $calendarData[$j]->setIsInSeason(true); $calendarData[$j]->setPrice($price); } @@ -576,13 +580,11 @@ class LodgingCalendarService */ private function filterReservableDays(FewoLodging $lodging, $calendar) { - /** @var CalendarDayState[] $potentiallyReservableDays */ $potentiallyReservableDays = []; for ($currMonthIndex = 0; $currMonthIndex < count($calendar); $currMonthIndex++) { $currMonth = $calendar[$currMonthIndex]; - /** @var CalendarDayState[] $data */ $data = $currMonth['data']; for ($currDayIndex = 0; $currDayIndex < count($data); $currDayIndex++) @@ -649,6 +651,148 @@ class LodgingCalendarService return $calendar; } + private function findCalendarDayKey($calendarMonth, $dayNumber){ + + foreach ($calendarMonth['data'] as $key => $data) { + if($data->getDay() == $dayNumber){ + return $key; + } + } + + + } + public function calendarAndFillDayStates($paddedCalendar, FewoLodging $lodging){ + + $startMonth = array_values($paddedCalendar)[0]['monthNumber']; + $startYear = array_values($paddedCalendar)[0]['year']; + $endMonth = array_values($paddedCalendar)[count($paddedCalendar)-1]['monthNumber']; + $endYear = array_values($paddedCalendar)[count($paddedCalendar)-1]['year']; + $endDay = array_values($paddedCalendar)[count($paddedCalendar)-1]['numberDays']; + + $startDate = new \DateTime(date( "Y-m-d", mktime(0,0,0, $startMonth, 1, $startYear))); + $endDate = new \DateTime(date( "Y-m-d", mktime(0,0,0, $endMonth, $endDay, $endYear))); + + + //reservations in the Calender Time + $reservations = $lodging->getReservationsFilter($startDate, $endDate); + + + foreach($reservations as $reservation) + { + $setFromIntervall = clone $reservation->getFromDate(); + $fromDate = $reservation->getFromDate(); + $toDate = $reservation->getToDate(); + + // $diff = $fromDate->diff($toDate); + $interval = \DateInterval::createFromDateString('1 day'); + $period = new \DatePeriod($setFromIntervall->modify('+1 day'), $interval, $toDate); + + //first + if(isset($paddedCalendar[$fromDate->format("Y-n")])){ + $key = $this->findCalendarDayKey($paddedCalendar[$fromDate->format("Y-n")], $fromDate->format("d")); + $paddedCalendar[$fromDate->format("Y-n")]['data'][$key]->setIsReservationBegin(true); + } + //last + if(isset($paddedCalendar[$toDate->format("Y-n")])) { + $key = $this->findCalendarDayKey($paddedCalendar[$toDate->format("Y-n")], $toDate->format("d")); + $paddedCalendar[$toDate->format("Y-n")]['data'][$key]->setIsReservationEnd(true); + } + //days + foreach ($period as $dt) { + if(isset($paddedCalendar[$dt->format("Y-n")])) { + $key = $this->findCalendarDayKey($paddedCalendar[$dt->format("Y-n")], $dt->format("d")); + $paddedCalendar[$dt->format("Y-n")]['data'][$key]->setIsReserved(true); + } + } + } + + $prices = $lodging->getPricesFilter($startDate, $endDate); + + foreach ($prices as $price) + { + + /** @var FewoSeason $season */ + $season = $price->getSeason(); + $setFromIntervall = clone $season->getFromDate(); + //$fromDate = $season->getFromDate(); + $setToIntervall = clone $season->getToDate(); + //$toDate = $season->getToDate(); + + $interval = \DateInterval::createFromDateString('1 day'); + $period = new \DatePeriod($setFromIntervall, $interval, $setToIntervall->modify('+1 day')); + + //days + foreach ($period as $dt) { + if(isset($paddedCalendar[$dt->format("Y-n")])) { + $key = $this->findCalendarDayKey($paddedCalendar[$dt->format("Y-n")], $dt->format("d")); + $currDay = $paddedCalendar[$dt->format("Y-n")]['data'][$key]; + $currDay->setPrice($price); + + + if(!$currDay->getIsReserved() && $season->getOnlyWeekday() != null && $season->getOnlyWeekday() == $currDay->getDate()->format('w')){ + $currDay->setIsBookable(true); + } + + if(!$currDay->getIsReserved() && $season->getOnlyWeekday() == null){ + $currDay->setIsBookable(true); + } + if($currDay->getIsReservationBegin() == true && $currDay->getIsReservationEnd() == true){ + $currDay->setIsBookable(false); + } + //check for the min sty days. + if ($currDay->getIsReservationBegin() == true) + { + //days + $minDays = $season->getMinimumStay(); + $setMinStayFrom = clone $currDay->getDate(); + $setMinStayTo = clone $currDay->getDate(); + + //$minStayInterval = \DateInterval::createFromDateString('-1 day'); + // $minStayPeriod = new \DatePeriod($setMinStayTo->modify('+1 day'), $minStayInterval, $setMinStayFrom->modify('-'.($minDays-1).' days')); + $setMinStayTo->modify('-'.($minDays).' days'); + $counter = 0; + $last = $minDays; + + for($mSdt = $setMinStayFrom; $mSdt >= $setMinStayTo; $mSdt->modify('-1 day')){ + + if(isset($paddedCalendar[$mSdt->format("Y-n")])) { + $key = $this->findCalendarDayKey($paddedCalendar[$mSdt->format("Y-n")], $mSdt->format("d")); + $checkDay = $paddedCalendar[$mSdt->format("Y-n")]['data'][$key]; + + + if($minDays != $checkDay->getPrice()->getSeason()->getMinimumStay()){ + + if($minDays > $checkDay->getPrice()->getSeason()->getMinimumStay()){ + $last = $counter+$checkDay->getPrice()->getSeason()->getMinimumStay(); + + + $setMinStayTo->modify('-'.($last).' days'); + } + if($minDays < $checkDay->getPrice()->getSeason()->getMinimumStay()){ + $last = $counter+$checkDay->getPrice()->getSeason()->getMinimumStay(); + $setMinStayTo->modify('-'.($last).' days'); + + } + $minDays = $checkDay->getPrice()->getSeason()->getMinimumStay(); + } + $counter++; + + if($counter <= $last) { + $checkDay->setIsBookable(false); + } + } + } + + + } + } + } + //$season is end + } + return $paddedCalendar; + + + } /** * @param FewoLodging $lodging * @param $calendar @@ -662,11 +806,11 @@ class LodgingCalendarService $reservationsCalendar = $this->getCalendar($lodging); $reservations = $lodging->getReservations(); + $prices = $lodging->getPrices(); foreach ($prices as $price) { - /** @var FewoSeason $season */ $season = $price->getSeason(); $pricesCalendar = $this->markCalendarDays($pricesCalendar, $season->getFromDate(), $season->getToDate(), false, $price); @@ -680,7 +824,6 @@ class LodgingCalendarService $reservationsCalendar = $this->markCalendarDays($reservationsCalendar, $fromDate, $toDate, true, null, $reservation); //TODO fast fertig } - $mergedPricesAndReservationsCalendar = $this->mergeCalendars($pricesCalendar, $reservationsCalendar); $actuallyReservableDaysCalendar = $this->filterReservableDays($lodging, $mergedPricesAndReservationsCalendar); //$resultCalendar = $this->mergeCalendars($actuallyReservableDaysCalendar, $reservationsCalendar, true); diff --git a/trunk/src/AppBundle/Util/CalendarDayState.php b/trunk/src/AppBundle/Util/CalendarDayState.php index e6aa1351..a7c987a2 100644 --- a/trunk/src/AppBundle/Util/CalendarDayState.php +++ b/trunk/src/AppBundle/Util/CalendarDayState.php @@ -56,6 +56,16 @@ class CalendarDayState */ private $date = null; + /** + * @var bool + */ + private $isEmpty; //empty day in the month + + /** + * @var bool + */ + private $isPastDate; + /** * @return int */ @@ -200,4 +210,75 @@ class CalendarDayState $this->date = $date; } + /** + * @return bool + */ + public function getIsEmpty(): bool + { + return $this->isEmpty; + } + + /** + * @param bool $isEmpty + */ + public function setIsEmpty(bool $isEmpty) + { + $this->isEmpty = $isEmpty; + } + + /** + * @return bool + */ + public function getIsPastDate(): bool + { + return $this->isPastDate; + } + + /** + * @param bool $isPastDate + */ + public function setIsPastDate(bool $isPastDate) + { + $this->isPastDate = $isPastDate; + } + + + public function getCssClass(){ + $ret = ""; + if($this->getIsPastDate()){ + $ret .= 'calendar-light '; + } + + if($this->getDay() == 0){ + $ret .= 'calendar-day-non '; + } + + if($this->getIsReserved()){ + $ret .= 'calendar-day-disabled '; + return $ret; + } + + if($this->getIsReservationBegin() && $this->getIsReservationEnd()){ + $ret .= 'calendar-day-disabled-half '; + return $ret; + } + + if($this->getIsReservationBegin()){ + $ret .= 'calendar-day-reservation-begin '; + return $ret; + } + + if($this->getIsReservationEnd()){ + $ret .= 'calendar-day-reservation-end '; + return $ret; + } + + + + return $ret; + + + } + + }