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 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);
}
}
@@ -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)
@@ -134,8 +134,7 @@ tx_epproducts_ajax_json {
1 = list
}
AjaxGroupsPrice {
1 = configs
2 = price
1 = index
}
}
features.requireCHashArgumentForActionArguments = 0
@@ -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',
]
);
@@ -1,11 +1,12 @@
<template>
<div>
Foo!
</div>
</template>
<script>
import axios from 'axios';
import $ from 'jquery';
export default {
props: {
@@ -17,11 +18,36 @@
data () {
return {
processing: false,
price: {},
configs: [],
boards: [],
options: []
}
},
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 () {
this.load();
}
}
</script>
@@ -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,
@@ -87,6 +87,13 @@
</div>
</div>
<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 partial="Facts" arguments="{facts: product.facts}"/>
<f:if condition="{product.video}">
@@ -248,9 +255,6 @@
<f:section name="Calendar">
<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">
<p><strong>Belegungskalender</strong></p>
<calendar