DB-Skript:

CREATE TABLE fewo_lodging (id INT AUTO_INCREMENT NOT NULL, type_id INT DEFAULT NULL, name VARCHAR(255) NOT NULL, description LONGTEXT NOT NULL, equipment LONGTEXT NOT NULL, adress1 VARCHAR(255) NOT NULL, adress2 VARCHAR(255) DEFAULT NULL, zip_code VARCHAR(255) NOT NULL, city VARCHAR(255) NOT NULL, maximum_persons INT NOT NULL, deposit DOUBLE PRECISION NOT NULL, only_weekday INT NOT NULL, calendar_visible TINYINT(1) NOT NULL, INDEX IDX_9629C357C54C8C93 (type_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE fewo_lodging_image (id INT AUTO_INCREMENT NOT NULL, lodging_id INT DEFAULT NULL, full_file_name VARCHAR(255) NOT NULL, file_name VARCHAR(255) NOT NULL, description VARCHAR(255) DEFAULT NULL, INDEX IDX_D49F667187335AF1 (lodging_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE fewo_lodging_type (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE fewo_price (id INT AUTO_INCREMENT NOT NULL, lodging_id INT DEFAULT NULL, season_id INT DEFAULT NULL, per_night DOUBLE PRECISION NOT NULL, flat_price DOUBLE PRECISION NOT NULL, INDEX IDX_3DE13C987335AF1 (lodging_id), INDEX IDX_3DE13C94EC001D1 (season_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE fewo_reservation (id INT AUTO_INCREMENT NOT NULL, lodging_id INT DEFAULT NULL, from_date DATE NOT NULL, to_date DATE NOT NULL, status INT NOT NULL, type INT NOT NULL, INDEX IDX_36537F7487335AF1 (lodging_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE fewo_season (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, from_date DATE NOT NULL, to_date DATE NOT NULL, minimum_stay INT NOT NULL, description LONGTEXT DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
ALTER TABLE fewo_lodging ADD CONSTRAINT FK_9629C357C54C8C93 FOREIGN KEY (type_id) REFERENCES fewo_lodging_type (id);
ALTER TABLE fewo_lodging_image ADD CONSTRAINT FK_D49F667187335AF1 FOREIGN KEY (lodging_id) REFERENCES fewo_lodging (id) ON DELETE SET NULL;
ALTER TABLE fewo_price ADD CONSTRAINT FK_3DE13C987335AF1 FOREIGN KEY (lodging_id) REFERENCES fewo_lodging (id);
ALTER TABLE fewo_price ADD CONSTRAINT FK_3DE13C94EC001D1 FOREIGN KEY (season_id) REFERENCES fewo_season (id) ON DELETE SET NULL;
ALTER TABLE fewo_reservation ADD CONSTRAINT FK_36537F7487335AF1 FOREIGN KEY (lodging_id) REFERENCES fewo_lodging (id) ON DELETE SET NULL;
ALTER TABLE page ADD fewo_lodging INT DEFAULT NULL;
ALTER TABLE page ADD CONSTRAINT FK_140AB6209629C357 FOREIGN KEY (fewo_lodging) REFERENCES fewo_lodging (id) ON DELETE SET NULL;
CREATE UNIQUE INDEX UNIQ_140AB6209629C357 ON page (fewo_lodging);

INSERT INTO `fewo_lodging_type` (`name`) VALUES ('Apartment');
INSERT INTO `fewo_lodging_type` (`name`) VALUES ('Bauernhof');
INSERT INTO `fewo_lodging_type` (`name`) VALUES ('Bungalow');
INSERT INTO `fewo_lodging_type` (`name`) VALUES ('Campingplatz');
INSERT INTO `fewo_lodging_type` (`name`) VALUES ('Chalet');
INSERT INTO `fewo_lodging_type` (`name`) VALUES ('Ferienanlage');
INSERT INTO `fewo_lodging_type` (`name`) VALUES ('Ferienhaus');
INSERT INTO `fewo_lodging_type` (`name`) VALUES ('Ferienwohnung');
INSERT INTO `fewo_lodging_type` (`name`) VALUES ('Finca');
INSERT INTO `fewo_lodging_type` (`name`) VALUES ('Hotel');
INSERT INTO `fewo_lodging_type` (`name`) VALUES ('Hütte');
INSERT INTO `fewo_lodging_type` (`name`) VALUES ('Pension');
INSERT INTO `fewo_lodging_type` (`name`) VALUES ('Schloss');
INSERT INTO `fewo_lodging_type` (`name`) VALUES ('Villa');

git-svn-id: http://78.47.251.156/svn/dev/sterntours-3@3348 f459cee4-fb09-11de-96c3-f9c5f16c3c76
This commit is contained in:
valentin.wacker 2017-10-25 12:25:37 +00:00
parent 2ebd38d3d7
commit ab026b752f
57 changed files with 6507 additions and 25 deletions

View file

@ -0,0 +1,620 @@
<?php
namespace AppBundle\Util;
use AppBundle\Entity\FewoLodging;
use AppBundle\Entity\FewoPrice;
use AppBundle\Entity\FewoReservation;
use AppBundle\Entity\FewoSeason;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManager;
class LodgingCalendarUtil
{
private $em;
public function __construct(EntityManager $entityManager)
{
$this->em = $entityManager;
}
private function daysInMonth($month, $year)
{
return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
}
public function getCalendarWithPadding()
{
$calendar = null;
$currentMonth = date('n');
$currentYear = date('Y');
$weekDays = ['So','Mo','Di','Mi','Do','Fr','Sa'];
setlocale(LC_TIME, "german");
for($m = $currentMonth; $m < ($currentMonth + 12); $m++)
{
if($m < 13)
{
$actualMonth = $m;
$actualCurrentYear = $currentYear;
}
else
{
$actualMonth = $m % 12;
$actualCurrentYear = $currentYear + 1;
}
$date = getdate(mktime(0,0,0, $actualMonth, 1, $actualCurrentYear));
$weekDay = $weekDays[$date['wday']];
$numberDays = $this->daysInMonth($actualMonth, $actualCurrentYear);
$data = null;
$actualStartingWeekDay = $date['wday'];
if($actualStartingWeekDay == 0)
{
$actualStartingWeekDay = 7;
}
for($fwd = 0; $fwd < $actualStartingWeekDay - 1; $fwd++)
{
$data[] = 0;
}
for($d = 1; $d <= $numberDays; $d++)
{
$data[] = $d;
}
$startIndex = count($data);
for($lwd = $startIndex; $lwd < 42; $lwd++)
{
$data[] = 0;
}
$calendar[] = [
'numberDays' => $numberDays,
'monthNumber' => $actualMonth,
'monthName' => utf8_encode(strftime("%B", mktime(0, 0, 0, $actualMonth, 1, $actualCurrentYear))),
'year' => $actualCurrentYear,
'startWeekDay' => $weekDay,
'data' => $data,
'marked' => 0
];
}
return $calendar;
}
public function getCalendar()
{
$calendar = null;
$currentMonth = date('n');
$currentYear = date('Y');
$weekDays = ['So','Mo','Di','Mi','Do','Fr','Sa'];
setlocale(LC_TIME, "german");
for($m = $currentMonth; $m < ($currentMonth + 12); $m++)
{
if($m < 13)
{
$actualMonth = $m;
$actualCurrentYear = $currentYear;
}
else
{
$actualMonth = $m % 12;
$actualCurrentYear = $currentYear + 1;
}
$date = getdate(mktime(0,0,0, $actualMonth, 1, $actualCurrentYear));
$weekDay = $weekDays[$date['wday']];
$numberDays = $this->daysInMonth($actualMonth, $actualCurrentYear);
$data = null;
// alle Wochentage
for($d = 1; $d <= $numberDays; $d++)
{
$data[] = $d;
}
$calendar[] = [
'numberDays' => $numberDays,
'monthNumber' => $actualMonth,
'monthName' => utf8_encode(strftime("%B", mktime(0, 0, 0, $actualMonth, 1, $actualCurrentYear))),
'year' => $actualCurrentYear,
'startWeekDay' => $weekDay,
'data' => $data,
'marked' => 0
];
}
return $calendar;
}
/**
* @param FewoLodging $lodging
* @return array|null
*/
public function getReservations(FewoLodging $lodging)
{
$reservations = $lodging->getReservations();
$ret = null;
for($i = 0; $i < count($reservations); $i++)
{
$fromDate = $reservations[$i]->getFromDate();
$toDate = $reservations[$i]->getToDate();
$ret[] = [
'startDay' => $fromDate->format('j'),
'startMonth' => $fromDate->format('n'),
'startYear' => $fromDate->format('Y'),
'endDay' => $toDate->format('j'),
'endMonth' => $toDate->format('n'),
'endYear' => $toDate->format('Y')
];
}
return $ret;
}
private function getMonthIndex($calendar, $month)
{
$result = 0;
for($i = 0; $i < count($calendar); $i++)
{
if($calendar[$i]['monthNumber'] == $month)
{
$result = $i;
break;
}
}
return $result;
}
/**
* @param $calendar
* @param \DateTime $fromDate
* @param \DateTime $toDate
* @param $appendix
* @return mixed
*/
private function markCalendarDays($calendar, \DateTime $fromDate, \DateTime $toDate, $appendix, $reservationMode = false)
{
$startDay = $fromDate->format('j');
$startMonth = $fromDate->format('n');
$startYear = $fromDate->format('Y');
$endDay = $toDate->format('j');
$endMonth = $toDate->format('n');
$endYear = $toDate->format('Y');
$today = new \DateTime();
$todayDay = $today->format('j');
$todayMonth = $today->format('n');
$todayYear = $today->format('Y');
if($startMonth < $todayMonth && $startYear == $todayYear)
{
$startDay = 1;
$startMonth = $today->format('n');
$startYear = $today->format('Y');
}
$startCalendarIndex = $this->getMonthIndex($calendar, $startMonth);
$endCalendarIndex = $this->getMonthIndex($calendar, $endMonth);
if($startCalendarIndex == $endCalendarIndex)
{
$data = $calendar[$startCalendarIndex]['data'];
$calendar[$startCalendarIndex]['marked'] = 1;
$startIndex = $startDay - 1;
$endIndex = $endDay - 1;
$firstDay = explode(',', $data[$startIndex]);
if(count($firstDay) > 1 && $reservationMode == false)
$startIndex = $startIndex + 1;
for($i = $startIndex; $i <= $endIndex; $i++)
{
if($i == $startIndex && $reservationMode)
{
$data[$i] = $data[$i].$appendix.",from";
} elseif($i == $endIndex && $reservationMode) {
$data[$i] = $data[$i].$appendix.",to";
} else {
$data[$i] = $data[$i].$appendix;
}
}
$calendar[$startCalendarIndex]['data'] = $data;
}
else
{
$data = $calendar[$startCalendarIndex]['data'];
$calendar[$startCalendarIndex]['marked'] = 1;
$startIndex = $startDay - 1;
$firstDay = explode(',', $data[$startIndex]);
if(count($firstDay) > 1 && $reservationMode == false)
$startIndex = $startIndex + 1;
$endIndex = count($data) - 1;
for($i = $startIndex; $i <= $endIndex; $i++)
{
if($i == $startIndex && $reservationMode)
{
$data[$i] = $data[$i].$appendix.",from";
} else {
$data[$i] = $data[$i].$appendix;
}
}
$calendar[$startCalendarIndex]['data'] = $data;
$data = $calendar[$endCalendarIndex]['data'];
$calendar[$endCalendarIndex]['marked'] = 1;
$startIndex = 0;
$endIndex = $endDay - 1;
for($i = $startIndex; $i <= $endIndex; $i++)
{
if($i == $endIndex && $reservationMode)
{
$data[$i] = $data[$i].$appendix.",to";
} else {
$data[$i] = $data[$i].$appendix;
}
}
$calendar[$endCalendarIndex]['data'] = $data;
for($i = $startCalendarIndex + 1; $i < $endCalendarIndex; $i++)
{
$data = $calendar[$i]['data'];
$calendar[$i]['marked'] = 1;
$startIndex = 0;
$endIndex = count($data) - 1;
for($j = $startIndex; $j <= $endIndex; $j++)
{
$data[$j] = $data[$j].$appendix;
}
$calendar[$i]['data'] = $data;
}
}
return $calendar;
}
private function mergeCalendars($calendar, $extensionCalendar, $withFromTo = false)
{
for($i = 0; $i < 12; $i++)
{
if($calendar[$i]['marked'] == 1 && $extensionCalendar[$i]['marked'] == 1)
{
$calendarData = $calendar[$i]['data'];
$extensionCalendarData = $extensionCalendar[$i]['data'];
$endIndex = count($calendarData);
for($j = 0; $j < $endIndex; $j++)
{
if(strlen($extensionCalendarData[$j]) > 2)
{
if($withFromTo)
{
$calDay = explode(',', $calendarData[$j]);
$extCalDay = explode(',', $extensionCalendarData[$j]);
if(count ($calDay) > 1 && $calDay[1] == "reservable" && count($extCalDay) > 2 && $extCalDay[2] == 'to')
{
$calendarData[$j] = $extensionCalendarData[$j].",reservable".','.$calDay[2];
}
else
$calendarData[$j] = $extensionCalendarData[$j];
}
else
{
$calendarData[$j] = $extensionCalendarData[$j];
}
}
}
$calendar[$i]['data'] = $calendarData;
}
}
return $calendar;
}
public function mergeWithPaddedCalendar($calendar, $paddedCalendar)
{
for($i = 0; $i < count($paddedCalendar); $i++)
{
$calendarDataCurrMonth = $calendar[$i]['data'];
$paddedCalendarDataCurrMonth = $paddedCalendar[$i]['data'];
$startIndex = array_search(1, $paddedCalendarDataCurrMonth);
for($j = 0; $j < count($calendarDataCurrMonth); $j++, $startIndex++)
{
$paddedCalendarDataCurrMonth[$startIndex] = $calendarDataCurrMonth[$j];
}
$paddedCalendar[$i]['data'] = $paddedCalendarDataCurrMonth;
}
return $paddedCalendar;
}
private function isNextDay($current, $next)
{
$result = false;
$calculatedNextDate = date('d.m.Y', strtotime($current." + 1 day"));
if($calculatedNextDate == $next)
{
$result = true;
}
return $result;
}
private function getMonthFromCalendar($calendar, $monthNumber)
{
$result = [];
for($i = 0; $i < count($calendar); $i++)
{
if($calendar[$i]['monthNumber'] == $monthNumber)
{
$result = $calendar[$i];
}
}
return $result;
}
/**
* @param FewoLodging $lodging
* @param $calendar
* @return array|null
*/
private function filterReservableDays(FewoLodging $lodging, $calendar)
{
$priceRepo = $this->em->getRepository('AppBundle:FewoPrice');
$today = new \DateTime();
$seasons = $lodging->getSeasons();
$prices = $lodging->getPrices();
$cleanCalendar = $this->getCalendar();
$potentiallyReservableDays = [];
$actuallyReservableDays = [];
$lastReservablePriceId = 0;
$priceId = 0;
for($currMonthIndex = 0; $currMonthIndex < count($calendar); $currMonthIndex++)
{
$currMonth = $calendar[$currMonthIndex];
$data = $currMonth['data'];
for($currDayIndex = 0; $currDayIndex < count($data); $currDayIndex++)
{
$currDay = explode(',', $data[$currDayIndex]);
if(count($currDay) > 1)
{
if($currDay[1] == "reservable")
{
$priceId = $currDay[2];
if($lastReservablePriceId == 0)
$lastReservablePriceId = $priceId;
if($lastReservablePriceId != $priceId)
{
$price = $priceRepo->find($lastReservablePriceId);
/** @var FewoSeason $season */
$season = $price->getSeason();
$actuallyReservableDays = $this->processPotentiallyReservableDays($potentiallyReservableDays, $season->getMinimumStay());
$this->processRest($actuallyReservableDays, $lastReservablePriceId, $cleanCalendar);
$lastReservablePriceId = $priceId;
$potentiallyReservableDays = [];
$actuallyReservableDays = [];
}
$currentDayDate = date("d.m.Y", strtotime($currMonth['year']."-".$currMonth['monthNumber']."-".$currDay[0]));
$potentiallyReservableDays[] = $currentDayDate;
}
elseif(count($currDay) > 2 && $currDay[2] == "from")
{
$currentDayDate = date("d.m.Y", strtotime($currMonth['year']."-".$currMonth['monthNumber']."-".$currDay[0]));
$potentiallyReservableDays[] = $currentDayDate;
}
}
else
{
if($lastReservablePriceId == $priceId)
{
$price = $priceRepo->find($lastReservablePriceId);
/** @var FewoSeason $season */
$season = $price->getSeason();
$actuallyReservableDays = $this->processPotentiallyReservableDays($potentiallyReservableDays, $season->getMinimumStay());
$this->processRest($actuallyReservableDays, $lastReservablePriceId, $cleanCalendar);
$lastReservablePriceId = $priceId;
$potentiallyReservableDays = [];
$actuallyReservableDays = [];
}
}
}
}
return $cleanCalendar;
}
private function processPotentiallyReservableDays($potentiallyReservableDays, $minimumStay)
{
$coherentDays = [];
$actuallyReservableDays = [];
//$coherentDays[] = $potentiallyReservableDays[0];
for($i = 1; $i < count($potentiallyReservableDays); $i++)
{
if($this->isNextDay($potentiallyReservableDays[$i - 1], $potentiallyReservableDays[$i]))
{
if(count($coherentDays) == 0)
{
$coherentDays[] = $potentiallyReservableDays[$i - 1];
}
$coherentDays[] = $potentiallyReservableDays[$i];
}
else
{
if(count($coherentDays) < $minimumStay)
{
$coherentDays = [];
$coherentDays[] = $potentiallyReservableDays[$i];
}
else
{
$coherentDaysCount = count($coherentDays);
$offset = $coherentDaysCount - ($minimumStay - 1);
$coherentDays = array_splice($coherentDays, 0, $offset);
for($j = 0; $j < count($coherentDays); $j++)
{
$actuallyReservableDays[] = $coherentDays[$j];
}
$coherentDays = [];
}
}
}
if(count($coherentDays) > 0)
{
$coherentDaysCount = count($coherentDays);
$offset = $coherentDaysCount - ($minimumStay - 1);
$coherentDays = array_splice($coherentDays, 0, $offset);
for($j = 0; $j < count($coherentDays); $j++)
{
$actuallyReservableDays[] = $coherentDays[$j];
}
}
return $actuallyReservableDays;
}
private function processRest($actuallyReservableDays, $priceId, &$cleanCalendar)
{
for($currMonthIndex = 0; $currMonthIndex < count($cleanCalendar); $currMonthIndex++)
{
$currMonth = $cleanCalendar[$currMonthIndex];
$data = $currMonth['data'];
$startIndex = array_search(1, $data);
$endIndex = $startIndex + ($currMonth['numberDays']);
for($currDayIndex = $startIndex; $currDayIndex < $endIndex; $currDayIndex++)
{
$currDay = explode(',', $data[$currDayIndex]);
$currentDate = date("d.m.Y", strtotime($currMonth['year']."-".$currMonth['monthNumber']."-".$currDay[0]));
if(in_array($currentDate, $actuallyReservableDays))
{
$data[$currDayIndex] = $data[$currDayIndex].",reservable,".$priceId;
$currMonth['marked'] = 1;
}
}
$currMonth['data'] = $data;
$cleanCalendar[$currMonthIndex] = $currMonth;
}
}
/**
* @param FewoLodging $lodging
* @param $calendar
* @return mixed
*/
public function markCalendarDaysWithReservations(FewoLodging $lodging, $calendar)
{
$today = new \DateTime();
$pricesCalendar = $calendar;
$reservationsCalendar = $calendar;
$reservations = $lodging->getReservations();
$prices = $lodging->getPrices();
$lodgingId = $lodging->getId();
foreach($prices as $price)
{
/** @var FewoSeason $season */
$season = $price->getSeason();
$appendix = ',reservable,'.$price->getId();
$fromDate = $season->getFromDate();
$toDate = $season->getToDate();
if($toDate < $today)
{
}
else
{
$pricesCalendar = $this->markCalendarDays($pricesCalendar, $fromDate, $toDate, $appendix, false);
}
}
foreach($reservations as $reservation)
{
$reservationId = $reservation->getId();
$appendix = ','.$reservationId;
$fromDate = $reservation->getFromDate();
$toDate = $reservation->getToDate();
$reservationsCalendar = $this->markCalendarDays($reservationsCalendar, $fromDate, $toDate, $appendix, true);
}
$mergedPricesAndReservationsCalendar = $this->mergeCalendars($pricesCalendar, $reservationsCalendar, false);
$actuallyReservableDaysCalendar = $this->filterReservableDays($lodging, $mergedPricesAndReservationsCalendar);
$resultCalendar = $this->mergeCalendars($actuallyReservableDaysCalendar, $reservationsCalendar, true);
return $resultCalendar;
}
public function convertDate($date)
{
$result = substr($date, 0, 2).'.'.substr($date, 2, 2).'.'.substr($date, 4, 4);
return $result;
}
}