From ebb9214586c9c1725fd71bfd02760500773633f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Fromme?= Date: Wed, 18 Aug 2021 19:02:38 +0200 Subject: [PATCH] Refactor import service to consider linked contingents --- ...ndController.php => ContingentCommand.php} | 9 +- .../Service/ContingentImportService.php | 100 +++++++++--------- .../ep_products/Configuration/Commands.php | 4 + 3 files changed, 61 insertions(+), 52 deletions(-) rename public/typo3conf/ext/ep_products/Classes/Command/{ContingentCommandController.php => ContingentCommand.php} (85%) diff --git a/public/typo3conf/ext/ep_products/Classes/Command/ContingentCommandController.php b/public/typo3conf/ext/ep_products/Classes/Command/ContingentCommand.php similarity index 85% rename from public/typo3conf/ext/ep_products/Classes/Command/ContingentCommandController.php rename to public/typo3conf/ext/ep_products/Classes/Command/ContingentCommand.php index 6333064d..dd14d2ca 100644 --- a/public/typo3conf/ext/ep_products/Classes/Command/ContingentCommandController.php +++ b/public/typo3conf/ext/ep_products/Classes/Command/ContingentCommand.php @@ -32,8 +32,9 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Extbase\Object\ObjectManager; -class ContingentCommandController extends Command +class ContingentCommand extends Command { /** @@ -49,9 +50,11 @@ class ContingentCommandController extends Command /** * @param ContingentImportService $contingentImportService */ - public function __construct(ContingentImportService $contingentImportService) + public function __construct() { - $this->contingentImportService = $contingentImportService; + /** @var ObjectManager $objectManager */ + $objectManager = GeneralUtility::makeInstance(ObjectManager::class); + $this->contingentImportService = $objectManager->get(ContingentImportService::class); parent::__construct(); } diff --git a/public/typo3conf/ext/ep_products/Classes/Service/ContingentImportService.php b/public/typo3conf/ext/ep_products/Classes/Service/ContingentImportService.php index 6f16691d..6dc2b363 100644 --- a/public/typo3conf/ext/ep_products/Classes/Service/ContingentImportService.php +++ b/public/typo3conf/ext/ep_products/Classes/Service/ContingentImportService.php @@ -212,8 +212,11 @@ class ContingentImportService implements SingletonInterface, LoggerAwareInterfac $types = []; foreach ($xmlTypes->unterbringung as $xmlType) { + $idBusPro = (string) $xmlType['idbuspro']; $code = (string) $xmlType['code']; - $types[(string) $xmlType['idbuspro']] = [ + $types[] = [ + 'idBusPro' => $idBusPro, + 'link' => isset($xmlType['idbuspro_kontingent_aus']) ? (string) $xmlType['idbuspro_kontingent_aus'] : null, 'pax' => (int) $xmlType['pax_max'], 'code' => $code, 'label' => (string) $xmlType['zimmerbezeichnung'], @@ -233,63 +236,62 @@ class ContingentImportService implements SingletonInterface, LoggerAwareInterfac protected function parseRooms(\SimpleXMLElement $xmlDates, array $hotelData, array $roomTypes) { $dateCount = 0; - foreach ($xmlDates->kapazitaet as $xmlDate) - { + foreach ($xmlDates->kapazitaet as $xmlDate) { $date = \DateTime::createFromFormat('d.m.Y', (string) $xmlDate['termin']); - $status = Contingent::STATUS_OK; - foreach ($xmlDate->zimmerliste->children() as $xmlRoom) - { - $roomTypeBusProId = (string) $xmlRoom['idbuspro']; - if (!array_key_exists($roomTypeBusProId, $roomTypes)) { - continue; - } - $roomType = $roomTypes[$roomTypeBusProId]; + foreach ($roomTypes as $roomType) { $roomCode = $roomType['code']; if (in_array($roomCode, $this->ignoredRoomTypes, true)) { continue; } - $availablePax = 0; - $capacity = 0; - if ($roomType['ctrl'] === true) { - if ((string) $xmlRoom['status'] === 'A') { - $status = Contingent::STATUS_ONREQUEST; - } - if ((string) $xmlRoom['status'] === 'S') { - $status = Contingent::STATUS_BLOCKED; - } - } else { - $capacity = (int) $xmlRoom['kontingent'] * (int) $roomType['pax']; - $availableRooms = (int) $xmlRoom['frei'] > 0 ? (int) $xmlRoom['frei'] : 0; - $availablePax = $availableRooms * $roomType['pax']; - } - $minPrice = (int) $xmlRoom['abpreis']; - $minNights = (int) $xmlRoom['abpreis_naechte']; - $additionalNightMinPrice = (int) $xmlRoom['abpreis_verlaengerung']; - $additionalNightMinNights = (int) $xmlRoom['abpreis_verlaengerung_naechte']; - $roomLabel = $roomType['label']; - $this->db->insert( - 'tx_epproducts_domain_model_contingent_temp', - [ - 'pid' => $this->pid, - 'hotel' => $hotelData['uid'], - 'hotel_code' => $hotelData['code'], - 'hotel_bus_pro_id' => $hotelData['busProId'], - 'room_code' => $roomCode, - 'room_label' => $roomLabel, - 'pax' => $capacity, - 'available' => $availablePax, - 'status' => $status, - 'date' => $date->format('Y-m-d'), - 'min_price' => $minPrice, - 'min_nights' => $minNights, - 'additional_night_min_price' => $additionalNightMinPrice, - 'additional_night_min_nights' => $additionalNightMinNights, - ] - ); + $roomTypeBusProId = $roomType['link'] ?? $roomType['idBusPro']; + $xpath = sprintf('zimmerliste/zimmer[@idbuspro="%d"]', $roomTypeBusProId); + $xmlRoom = $xmlDate->xpath($xpath)[0]; + $this->calculateContingent($xmlRoom, $roomType, $date, $hotelData); } $dateCount++; } + return $dateCount; } + protected function calculateContingent(\SimpleXMLElement $xmlRoom, array $roomType, \DateTime $date, array $hotelData): void + { + $availablePax = 0; + $capacity = 0; + $status = Contingent::STATUS_OK; + $isControlRoom = $roomType['ctrl'] === true; + if ($isControlRoom && (string) $xmlRoom['status'] === 'A') { + $status = Contingent::STATUS_ONREQUEST; + } elseif ($isControlRoom && (string) $xmlRoom['status'] === 'S') { + $status = Contingent::STATUS_BLOCKED; + } else { + $capacity = (int) $xmlRoom['kontingent'] * (int) $roomType['pax']; + $availableRooms = (int) $xmlRoom['frei'] > 0 ? (int) $xmlRoom['frei'] : 0; + $availablePax = $availableRooms * $roomType['pax']; + } + $minPrice = (int) $xmlRoom['abpreis']; + $minNights = (int) $xmlRoom['abpreis_naechte']; + $additionalNightMinPrice = (int) $xmlRoom['abpreis_verlaengerung']; + $additionalNightMinNights = (int) $xmlRoom['abpreis_verlaengerung_naechte']; + $roomLabel = $roomType['label']; + $this->db->insert( + 'tx_epproducts_domain_model_contingent_temp', + [ + 'pid' => $this->pid, + 'hotel' => $hotelData['uid'], + 'hotel_code' => $hotelData['code'], + 'hotel_bus_pro_id' => $hotelData['busProId'], + 'room_code' => $roomType['code'], + 'room_label' => $roomLabel, + 'pax' => $capacity, + 'available' => $availablePax, + 'status' => $status, + 'date' => $date->format('Y-m-d'), + 'min_price' => $minPrice, + 'min_nights' => $minNights, + 'additional_night_min_price' => $additionalNightMinPrice, + 'additional_night_min_nights' => $additionalNightMinNights, + ] + ); + } } diff --git a/public/typo3conf/ext/ep_products/Configuration/Commands.php b/public/typo3conf/ext/ep_products/Configuration/Commands.php index f6430e22..96c19841 100644 --- a/public/typo3conf/ext/ep_products/Configuration/Commands.php +++ b/public/typo3conf/ext/ep_products/Configuration/Commands.php @@ -8,4 +8,8 @@ return [ 'ep:groups-swiss' => [ 'class' => \EP\EpProducts\Command\GroupsSwissCommand::class, ], + 'ep:contingent-import' => [ + 'class' => \EP\EpProducts\Command\ContingentCommand::class, + 'scheduleable' => false, + ], ];