diff --git a/public/typo3conf/ext/ep_products/Classes/Controller/AjaxGroupsPriceController.php b/public/typo3conf/ext/ep_products/Classes/Controller/AjaxGroupsPriceController.php index 4f8c54ea..70b2e9f9 100644 --- a/public/typo3conf/ext/ep_products/Classes/Controller/AjaxGroupsPriceController.php +++ b/public/typo3conf/ext/ep_products/Classes/Controller/AjaxGroupsPriceController.php @@ -31,6 +31,7 @@ use EP\EpProducts\Domain\Model\Hotel; use EP\EpProducts\Service\GroupsPriceService; use League\Period\Period; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; +use TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException; class AjaxGroupsPriceController extends ActionController { @@ -46,27 +47,25 @@ class AjaxGroupsPriceController extends ActionController $this->groupsPriceService = $groupsPriceService; } - /** - * @param Hotel $hotel - * @return string - */ - public function configsAction(Hotel $hotel) - { - return json_encode([ - 'configs' => $hotel->getGroupsPriceConfigs()->toArray(), - 'boards' => $hotel->getGroupsPriceBoards()->toArray(), - 'options' => $hotel->getGroupsPriceOptions()->toArray(), - ], JSON_THROW_ON_ERROR, 512); - } - - public function initializePriceAction() + public function initializeAction() { if ($this->request->hasArgument('dateFrom')) { - $dateFrom = new \DateTime($this->request->getArgument('dateFrom')); + try { + $dateFrom = new \DateTime($this->request->getArgument('dateFrom')); + } + catch (\Throwable $e) { + $dateFrom = null; + } $this->request->setArgument('dateFrom', $dateFrom); } + if ($this->request->hasArgument('dateTo')) { - $dateTo = new \DateTime($this->request->getArgument('dateTo')); + try { + $dateTo = new \DateTime($this->request->getArgument('dateTo')); + } + catch (\Throwable $e) { + $dateTo = null; + } $this->request->setArgument('dateTo', $dateTo); } } @@ -79,13 +78,18 @@ class AjaxGroupsPriceController extends ActionController * @return string * @throws \League\Period\Exception */ - public function priceAction(Hotel $hotel, \DateTime $dateFrom, \DateTime $dateTo, $pax) + public function indexAction(Hotel $hotel, \DateTime $dateFrom = null, \DateTime $dateTo = null, $pax = 0) { - $period = new Period($dateFrom, $dateTo); - $price = $this->groupsPriceService->calculateHotelPrice($hotel, $period, $pax); + if (null !== $dateFrom && null !== $dateTo) { + $period = new Period($dateFrom, $dateTo); + $price = $this->groupsPriceService->calculateHotelPrice($hotel, $period, $pax); + } return json_encode([ - 'price' => $price, + 'price' => $price ?? [], + 'configs' => $hotel->getGroupsPriceConfigs()->toArray(), + 'boards' => $hotel->getGroupsPriceBoards()->toArray(), + 'options' => $hotel->getGroupsPriceOptions()->toArray(), ], JSON_THROW_ON_ERROR, 512); } } diff --git a/public/typo3conf/ext/ep_products/Classes/Service/GroupsPriceService.php b/public/typo3conf/ext/ep_products/Classes/Service/GroupsPriceService.php index f44deadf..cc174e83 100644 --- a/public/typo3conf/ext/ep_products/Classes/Service/GroupsPriceService.php +++ b/public/typo3conf/ext/ep_products/Classes/Service/GroupsPriceService.php @@ -9,6 +9,12 @@ use TYPO3\CMS\Core\Utility\GeneralUtility; class GroupsPriceService { + /** + * @param Hotel $hotel + * @param Period $period + * @param int $pax + * @return array + */ public function calculateHotelPrice(Hotel $hotel, Period $period, $pax) { // Get all price configs for provided hotel @@ -17,9 +23,10 @@ class GroupsPriceService // Create periods in configs to simplify following steps $this->patchConfigPeriods($configs); + $nights = 0; $price = 0; - // Iterate over all days in provided period + // Iterate over provided period foreach ($period->getDatePeriod('1 DAY') as $day) { // Iterate over all configs @@ -38,9 +45,27 @@ class GroupsPriceService $price += $config['price_additional_person'] * ($pax - $config['persons_included']); } } + + $nights++; } - return $price; + if ($nights === 2) { + $shortTermCosts = $price * .2; + } + + if ($nights === 3) { + $shortTermCosts = $price * .1; + } + + $electricityCosts = ($nights - 1) * $pax * 1.7; + + return [ + 'pax' => $pax, + 'basePrice' => $price, + 'shortTermCosts' => $shortTermCosts ?? 0.0, + 'electricityCosts' => $electricityCosts, + 'nights' => $nights, + ]; } protected function patchConfigPeriods(array &$configs) diff --git a/public/typo3conf/ext/ep_products/Configuration/TypoScript/setup.typoscript b/public/typo3conf/ext/ep_products/Configuration/TypoScript/setup.typoscript index 5be9ade7..a770a066 100644 --- a/public/typo3conf/ext/ep_products/Configuration/TypoScript/setup.typoscript +++ b/public/typo3conf/ext/ep_products/Configuration/TypoScript/setup.typoscript @@ -134,8 +134,7 @@ tx_epproducts_ajax_json { 1 = list } AjaxGroupsPrice { - 1 = configs - 2 = price + 1 = index } } features.requireCHashArgumentForActionArguments = 0 diff --git a/public/typo3conf/ext/ep_products/ext_localconf.php b/public/typo3conf/ext/ep_products/ext_localconf.php index 83c46f2d..255c1ddf 100644 --- a/public/typo3conf/ext/ep_products/ext_localconf.php +++ b/public/typo3conf/ext/ep_products/ext_localconf.php @@ -345,7 +345,7 @@ $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\EP\EpProducts\T 'AjaxTable' => 'pricetable,pricetableHtml,eventPricetable', 'AjaxCalendar' => 'range,contingents,availableRooms', 'AjaxWatchlist' => 'list', - 'AjaxGroupsPrice' => 'configs,price', + 'AjaxGroupsPrice' => 'index', ], [ 'AjaxSearch' => 'searchresult', @@ -356,7 +356,7 @@ $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\EP\EpProducts\T 'AjaxTable' => 'pricetable,pricetableHtml,eventPricetable', 'AjaxCalendar' => 'range,contingents,availableRooms', 'AjaxWatchlist' => 'list', - 'AjaxGroupsPrice' => 'configs,price', + 'AjaxGroupsPrice' => 'index', ] ); diff --git a/public/typo3conf/ext/ep_theme/Resources/Private/Assets/js/components/GroupsPriceCalculator.vue b/public/typo3conf/ext/ep_theme/Resources/Private/Assets/js/components/GroupsPriceCalculator.vue index ee647cad..1f423707 100644 --- a/public/typo3conf/ext/ep_theme/Resources/Private/Assets/js/components/GroupsPriceCalculator.vue +++ b/public/typo3conf/ext/ep_theme/Resources/Private/Assets/js/components/GroupsPriceCalculator.vue @@ -1,11 +1,12 @@ diff --git a/public/typo3conf/ext/ep_theme/Resources/Private/Assets/js/components/ProductDetail.vue b/public/typo3conf/ext/ep_theme/Resources/Private/Assets/js/components/ProductDetail.vue index dbd0234b..edf8ea47 100644 --- a/public/typo3conf/ext/ep_theme/Resources/Private/Assets/js/components/ProductDetail.vue +++ b/public/typo3conf/ext/ep_theme/Resources/Private/Assets/js/components/ProductDetail.vue @@ -11,7 +11,6 @@ import DaterangeSelect from './DaterangeSelect.vue' import AjaxContent from './AjaxContent.vue' import Calendar from './Calendar.vue' - import GroupsPriceCalculator from './GroupsPriceCalculator.vue'; import FacebookPixel from './FacebookPixel.vue' import WatchlistToggle from './WatchlistToggle.vue' import OsmMap from './OsmMap.vue' @@ -29,7 +28,7 @@ DaterangeSelect, AjaxContent, Calendar, - GroupsPriceCalculator, + GroupsPriceCalculator: () => import('./GroupsPriceCalculator.vue'), FacebookPixel, WatchlistToggle, OsmMap, diff --git a/public/typo3conf/ext/ep_theme/Resources/Private/Templates/Product/Detail.html b/public/typo3conf/ext/ep_theme/Resources/Private/Templates/Product/Detail.html index c2b101a1..efd8fef0 100644 --- a/public/typo3conf/ext/ep_theme/Resources/Private/Templates/Product/Detail.html +++ b/public/typo3conf/ext/ep_theme/Resources/Private/Templates/Product/Detail.html @@ -87,6 +87,13 @@