Using your own PriceCalculation Service

You may need to use your own particular rules for your price calculations. The booking extension allows you to use your own price calculation service. The following example shows how you would do this.

In our example we want to rent out a room that costs € 134 from Sunday to Friday night and € 154 from Friday to Sunday night. If a booking is from Wednesday to Sunday the lower price should be used. This calculation is below in Price Calculation: Weekend Overnights.

Preparation and registration of the service

Your service must implement the abstract class AbstractPriceCalculationService. You can store this service in your own extension and it should look like this:

namespace CodingMs\Bookings\Service;
use CodingMs\Bookings\Domain\Model\BookingObject;
class PriceCalculationExampleService extends AbstractPriceCalculationService
{

    /**
     * @param BookingObject $bookingObject
     * @param array $data
     * @return array
     */
    public function calculate($bookingObject, $data): array
    {
        $calculation = $this->prepareData($bookingObject, $data);

        // ...

        return $calculation;
    }

}

The service must then be registered in the system. We do this with a TCA override. Create a file with the name tx_bookings_domain_model_bookingobject.php in the Configuration/TCA/Overrides/ directory of the extension. You have to put the file in your extension/theme of course. It is important that this is loaded and contains the following content:

<?php
if (!defined('TYPO3_MODE')) {
    die ('Access denied.');
}

$GLOBALS['TCA']['tx_bookings_domain_model_bookingobject']['columns']['record_type']['config']['items'][] =
    ['Price Calculation: Weekend Overnights', '\CodingMs\Bookings\Service\PriceCalculationWeekendOvernightsService'];

// Display Fields
$extKey = 'bookings';
$table = 'tx_bookings_domain_model_bookingobject';
$lll = 'LLL:EXT:' . $extKey . '/Resources/Private/Language/locallang_db.xlf:' . $table;
$GLOBALS['TCA']['tx_bookings_domain_model_bookingobject']['types']['\CodingMs\Bookings\Service\PriceCalculationWeekendOvernightsService'] = [
    'showitem' => 'title,
        subtitle,
        --palette--;' . $lll . '.palette_min_max_persons;min_max_persons_children_selection,
        --palette--;' . $lll . '.palette_min_max_days;min_max_days,
        description,

    --div--;' . $lll . '.tab_relations,
        tags,
        categories,
        images,

    --div--;' . $lll . '.tab_bookings,
        booking_persons_required,
        bookings,

    --div--;' . $lll . '.tab_prices,
        record_type,
        record_type_information,
        currency,
        price_per_overnight,
        price_per_overnight_per_additional_person,
        additional_prices,
        payment_notices,

    --div--;' . $lll . '.tab_map,
        --palette--;' . $lll . '.palette_map_latitude_longitude;map_latitude_longitude,
        map_zoom,

    --div--;' . $lll . '.tab_language,
        sys_language_uid,
        l10n_parent,
        l10n_diffsource,

    --div--;' . $lll . '.tab_access,
        hidden,
        starttime,
        endtime',
];

Here you can add your calculation service to the selection. The namespace and the class must of course be changed to your name. After clearing the system cache, the new selection should be available.

In the showitem area you can specify which fields are displayed from your service.

Select this in a booking object so that you can test it.

Price table output

Data for the AJAX service will be calculated via your price calculation service and then transfers the result to the stored fluid template, which then renders the HTML that is returned. It uses the fluid partial EXT:bookings/Resources/Private/Partials/Bookings/PriceCalculation.html. You can overwrite this as normal with the help of TypoScript.

This gives you added flexibility for your price calculations.

Error handling

-> Just throw an exception!