Respect contingents of linked products

This commit is contained in:
Björn Fromme
2021-08-23 15:58:30 +02:00
parent 18394fa298
commit 6a3c4d92ea
3 changed files with 39 additions and 22 deletions
@@ -66,8 +66,7 @@ class ContingentCommand extends Command
*/ */
public function execute(InputInterface $input, OutputInterface $output) public function execute(InputInterface $input, OutputInterface $output)
{ {
$path = GeneralUtility::getFileAbsFileName('fileadmin/xmlexportzimmer'); $count = $this->contingentImportService->import();
$count = $this->contingentImportService->import($path);
$output->writeln(sprintf('%d rows imported.', $count )); $output->writeln(sprintf('%d rows imported.', $count ));
return 0; return 0;
@@ -41,7 +41,8 @@ class ContingentImportService implements SingletonInterface, LoggerAwareInterfac
use LoggerAwareTrait; use LoggerAwareTrait;
use DbConnectionTrait; use DbConnectionTrait;
const ROOMCODE_STATUS = 'BelKal'; public const XML_PATH = 'fileadmin/xmlexportzimmer';
public const ROOMCODE_STATUS = 'BelKal';
/** /**
* @var string[] * @var string[]
@@ -83,15 +84,14 @@ class ContingentImportService implements SingletonInterface, LoggerAwareInterfac
} }
/** /**
* @param string $path
* @return int * @return int
* @throws \Doctrine\DBAL\DBALException * @throws \Doctrine\DBAL\DBALException
*/ */
public function import($path) public function import()
{ {
$this->createTempTable(); $this->createTempTable();
$this->logger->info('starting product import'); $this->logger->info('starting product import');
$contingentCount = $this->parseXmlFilesInFolder($path); $contingentCount = $this->parseXmlFilesInFolder();
$this->clearTable(); $this->clearTable();
$this->copyTable(); $this->copyTable();
$this->clearTempTable(); $this->clearTempTable();
@@ -109,35 +109,47 @@ class ContingentImportService implements SingletonInterface, LoggerAwareInterfac
} }
/** /**
* @param string $path
* @return int * @return int
*/ */
public function parseXmlFilesInFolder($path) public function parseXmlFilesInFolder()
{ {
$dateCount = 0; $dateCount = 0;
$path = GeneralUtility::getFileAbsFileName(self::XML_PATH);
$folder = opendir($path); $folder = opendir($path);
while (($fileName = readdir($folder)) !== false) { while (($fileName = readdir($folder)) !== false) {
if (strpos($fileName, 'HotelZimmer') === 0) { if (strpos($fileName, 'HotelZimmer') === 0) {
$filePath = $path . '/' . $fileName; $dateCount += $this->parseContingentXmlFile($path, $fileName);
$dateCount += $this->parseContingentXmlFile($filePath);
} }
} }
return $dateCount; return $dateCount;
} }
/** /**
* @param string $file * @param string $fileName
* @return int * @return int
*/ */
public function parseContingentXmlFile($file) public function parseContingentXmlFile($path, $fileName)
{
$filePath = sprintf('%s/%s', $path , $fileName);
if (null === $xmlData = $this->loadXmlFile($filePath)) {
return 0;
}
return $this->parseContingent($xmlData);
}
public function loadXmlFile($file): ?\SimpleXMLElement
{ {
libxml_use_internal_errors (true); libxml_use_internal_errors (true);
/** @var \SimpleXMLElement $xmlData */ /** @var \SimpleXMLElement $xmlData */
if (!$xmlData = simplexml_load_string(file_get_contents($file))) { if (!$xmlData = simplexml_load_string(file_get_contents($file))) {
libxml_clear_errors(); libxml_clear_errors();
return 0; return null;
} }
return $this->parseContingent($xmlData);
return $xmlData;
} }
/** /**
@@ -192,14 +204,21 @@ class ContingentImportService implements SingletonInterface, LoggerAwareInterfac
return 0; return 0;
} }
$hotelData['busProId'] = (string) $xmlContingent['idbuspro'];
$hotelData['code'] = (string) $xmlContingent['code'];
$roomTypes = $this->parseRoomTypes($xmlContingent->unterbringungen);
if ($xmlContingent->idbuspro_kontingent_aus) {
$linkedProductId = (int)$xmlContingent->idbuspro_kontingent_aus;
$path = GeneralUtility::getFileAbsFileName(self::XML_PATH);
$xmlContingent = $this->loadXmlFile(sprintf('%s/HotelZimmer_%d.xml', $path, $linkedProductId));
}
if (empty($xmlContingent->kapazitaeten)) { if (empty($xmlContingent->kapazitaeten)) {
$this->logger->warning('hotel has no contingents', ['code' => $hotelCode]); $this->logger->warning('hotel has no contingents', ['code' => $hotelCode]);
return 0; return 0;
} }
$hotelData['busProId'] = (string) $xmlContingent['idbuspro'];
$hotelData['code'] = $hotelCode;
$roomTypes = $this->parseRoomTypes($xmlContingent->unterbringungen);
return $this->parseRooms($xmlContingent->kapazitaeten, $hotelData, $roomTypes); return $this->parseRooms($xmlContingent->kapazitaeten, $hotelData, $roomTypes);
} }
@@ -41,8 +41,7 @@ class ImportContingentsTask extends AbstractTask
/** @var \EP\EpProducts\Service\ContingentImportService $importService */ /** @var \EP\EpProducts\Service\ContingentImportService $importService */
$importService = $objectManager->get(ContingentImportService::class); $importService = $objectManager->get(ContingentImportService::class);
if ($importService->isImportRequired()) { if ($importService->isImportRequired()) {
$path = GeneralUtility::getFileAbsFileName('fileadmin/xmlexportzimmer'); $importService->import();
$importService->import($path);
} }
return true; return true;