WIP Implement price calculation and frontend

This commit is contained in:
Björn Fromme
2020-01-13 09:47:29 +01:00
parent 70f21a28b9
commit 85eaace11f
7 changed files with 89 additions and 32 deletions
@@ -31,6 +31,7 @@ use EP\EpProducts\Domain\Model\Hotel;
use EP\EpProducts\Service\GroupsPriceService; use EP\EpProducts\Service\GroupsPriceService;
use League\Period\Period; use League\Period\Period;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException;
class AjaxGroupsPriceController extends ActionController class AjaxGroupsPriceController extends ActionController
{ {
@@ -46,27 +47,25 @@ class AjaxGroupsPriceController extends ActionController
$this->groupsPriceService = $groupsPriceService; $this->groupsPriceService = $groupsPriceService;
} }
/** public function initializeAction()
* @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()
{ {
if ($this->request->hasArgument('dateFrom')) { 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); $this->request->setArgument('dateFrom', $dateFrom);
} }
if ($this->request->hasArgument('dateTo')) { 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); $this->request->setArgument('dateTo', $dateTo);
} }
} }
@@ -79,13 +78,18 @@ class AjaxGroupsPriceController extends ActionController
* @return string * @return string
* @throws \League\Period\Exception * @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); if (null !== $dateFrom && null !== $dateTo) {
$price = $this->groupsPriceService->calculateHotelPrice($hotel, $period, $pax); $period = new Period($dateFrom, $dateTo);
$price = $this->groupsPriceService->calculateHotelPrice($hotel, $period, $pax);
}
return json_encode([ return json_encode([
'price' => $price, 'price' => $price ?? [],
'configs' => $hotel->getGroupsPriceConfigs()->toArray(),
'boards' => $hotel->getGroupsPriceBoards()->toArray(),
'options' => $hotel->getGroupsPriceOptions()->toArray(),
], JSON_THROW_ON_ERROR, 512); ], JSON_THROW_ON_ERROR, 512);
} }
} }
@@ -9,6 +9,12 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
class GroupsPriceService class GroupsPriceService
{ {
/**
* @param Hotel $hotel
* @param Period $period
* @param int $pax
* @return array
*/
public function calculateHotelPrice(Hotel $hotel, Period $period, $pax) public function calculateHotelPrice(Hotel $hotel, Period $period, $pax)
{ {
// Get all price configs for provided hotel // Get all price configs for provided hotel
@@ -17,9 +23,10 @@ class GroupsPriceService
// Create periods in configs to simplify following steps // Create periods in configs to simplify following steps
$this->patchConfigPeriods($configs); $this->patchConfigPeriods($configs);
$nights = 0;
$price = 0; $price = 0;
// Iterate over all days in provided period // Iterate over provided period
foreach ($period->getDatePeriod('1 DAY') as $day) { foreach ($period->getDatePeriod('1 DAY') as $day) {
// Iterate over all configs // Iterate over all configs
@@ -38,9 +45,27 @@ class GroupsPriceService
$price += $config['price_additional_person'] * ($pax - $config['persons_included']); $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) protected function patchConfigPeriods(array &$configs)
@@ -134,8 +134,7 @@ tx_epproducts_ajax_json {
1 = list 1 = list
} }
AjaxGroupsPrice { AjaxGroupsPrice {
1 = configs 1 = index
2 = price
} }
} }
features.requireCHashArgumentForActionArguments = 0 features.requireCHashArgumentForActionArguments = 0
@@ -345,7 +345,7 @@ $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\EP\EpProducts\T
'AjaxTable' => 'pricetable,pricetableHtml,eventPricetable', 'AjaxTable' => 'pricetable,pricetableHtml,eventPricetable',
'AjaxCalendar' => 'range,contingents,availableRooms', 'AjaxCalendar' => 'range,contingents,availableRooms',
'AjaxWatchlist' => 'list', 'AjaxWatchlist' => 'list',
'AjaxGroupsPrice' => 'configs,price', 'AjaxGroupsPrice' => 'index',
], ],
[ [
'AjaxSearch' => 'searchresult', 'AjaxSearch' => 'searchresult',
@@ -356,7 +356,7 @@ $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\EP\EpProducts\T
'AjaxTable' => 'pricetable,pricetableHtml,eventPricetable', 'AjaxTable' => 'pricetable,pricetableHtml,eventPricetable',
'AjaxCalendar' => 'range,contingents,availableRooms', 'AjaxCalendar' => 'range,contingents,availableRooms',
'AjaxWatchlist' => 'list', 'AjaxWatchlist' => 'list',
'AjaxGroupsPrice' => 'configs,price', 'AjaxGroupsPrice' => 'index',
] ]
); );
@@ -1,11 +1,12 @@
<template> <template>
<div> <div>
Foo!
</div> </div>
</template> </template>
<script> <script>
import axios from 'axios'; import axios from 'axios';
import $ from 'jquery';
export default { export default {
props: { props: {
@@ -17,11 +18,36 @@
data () { data () {
return { return {
processing: false, processing: false,
price: {},
configs: [],
boards: [],
options: []
} }
}, },
methods: { methods: {
load () {
this.processing = true;
axios({
url: this.endpoint,
method: 'POST',
data: $.param({
'tx_epproducts_ajax[pax]': 60,
'tx_epproducts_ajax[dateFrom]': '2020-04-28',
'tx_epproducts_ajax[dateTo]': '2020-04-31'
})
}).then(response => {
this.configs = response.data.configs;
this.boards = response.data.boards;
this.options = response.data.options;
this.price = response.data.price;
}).catch(error => {
}).finally(() => {
this.processing = false;
});
}
}, },
mounted () { mounted () {
this.load();
} }
} }
</script> </script>
@@ -11,7 +11,6 @@
import DaterangeSelect from './DaterangeSelect.vue' import DaterangeSelect from './DaterangeSelect.vue'
import AjaxContent from './AjaxContent.vue' import AjaxContent from './AjaxContent.vue'
import Calendar from './Calendar.vue' import Calendar from './Calendar.vue'
import GroupsPriceCalculator from './GroupsPriceCalculator.vue';
import FacebookPixel from './FacebookPixel.vue' import FacebookPixel from './FacebookPixel.vue'
import WatchlistToggle from './WatchlistToggle.vue' import WatchlistToggle from './WatchlistToggle.vue'
import OsmMap from './OsmMap.vue' import OsmMap from './OsmMap.vue'
@@ -29,7 +28,7 @@
DaterangeSelect, DaterangeSelect,
AjaxContent, AjaxContent,
Calendar, Calendar,
GroupsPriceCalculator, GroupsPriceCalculator: () => import('./GroupsPriceCalculator.vue'),
FacebookPixel, FacebookPixel,
WatchlistToggle, WatchlistToggle,
OsmMap, OsmMap,
@@ -87,6 +87,13 @@
</div> </div>
</div> </div>
<div class="hidden-xs"> <div class="hidden-xs">
<f:comment>
<f:if condition="{product.calendarHotel}">
<groups-price-calculator
endpoint="{ep:uri.ajax(action: 'index', controller: 'AjaxGroupsPrice', extensionName: 'epproducts', pageUid: settings.defaultAjaxUid, arguments: '{hotel: hotel}')}"
></groups-price-calculator>
</f:if>
</f:comment>
<f:render section="Calendar" arguments="{_all}"/> <f:render section="Calendar" arguments="{_all}"/>
<f:render partial="Facts" arguments="{facts: product.facts}"/> <f:render partial="Facts" arguments="{facts: product.facts}"/>
<f:if condition="{product.video}"> <f:if condition="{product.video}">
@@ -248,9 +255,6 @@
<f:section name="Calendar"> <f:section name="Calendar">
<f:if condition="{product.calendarHotel}"> <f:if condition="{product.calendarHotel}">
<groups-price-calculator
endpoint="{ep:uri.ajax(action: 'prices', controller: 'AjaxGroupsPrice', extensionName: 'epproducts', pageUid: settings.defaultAjaxUid, arguments: '{hotel: hotel}')}"
></groups-price-calculator>
<div class="ep-sidebar ep-facts"> <div class="ep-sidebar ep-facts">
<p><strong>Belegungskalender</strong></p> <p><strong>Belegungskalender</strong></p>
<calendar <calendar