179 lines
6.5 KiB
PHP
179 lines
6.5 KiB
PHP
<?php
|
|
/*************************************************************************************/
|
|
/* Copyright (c) Franck Allimant, CQFDev */
|
|
/* email : thelia@cqfdev.fr */
|
|
/* web : http://www.cqfdev.fr */
|
|
/* */
|
|
/* For the full copyright and license information, please view the LICENSE */
|
|
/* file that was distributed with this source code. */
|
|
/*************************************************************************************/
|
|
|
|
/**
|
|
* Created by Franck Allimant, CQFDev <franck@cqfdev.fr>
|
|
* Date: 13/06/2019 16:01
|
|
*/
|
|
namespace Beds24\Loop;
|
|
|
|
use Beds24\Beds24;
|
|
use Beds24\Beds24\Beds24Request;
|
|
use Beds24\Beds24\SearchParameters;
|
|
use Beds24\Model\Base\Beds24ProductInfoQuery;
|
|
use Thelia\Core\Template\Element\ArraySearchLoopInterface;
|
|
use Thelia\Core\Template\Element\BaseLoop;
|
|
use Thelia\Core\Template\Element\LoopResult;
|
|
use Thelia\Core\Template\Element\LoopResultRow;
|
|
use Thelia\Core\Template\Loop\Argument\Argument;
|
|
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
|
|
|
|
|
|
/**
|
|
* @method string getRoomId()
|
|
* @method int getAdults()
|
|
* @method int getChildren()
|
|
* @method bool getIgnoreAvailability()
|
|
* @method bool getUseSavedSearchCriteria()
|
|
* @method string getStartDate()
|
|
* @method string getEndDate()
|
|
* @method int getProductId()
|
|
*/
|
|
class RoomAvailability extends BaseLoop implements ArraySearchLoopInterface
|
|
{
|
|
/**
|
|
* @return \Thelia\Core\Template\Loop\Argument\ArgumentCollection
|
|
*/
|
|
protected function getArgDefinitions()
|
|
{
|
|
return new ArgumentCollection(
|
|
Argument::createAnyTypeArgument('start_date'),
|
|
Argument::createAnyTypeArgument('end_date'),
|
|
Argument::createIntTypeArgument('adults', 2),
|
|
Argument::createIntTypeArgument('children', 0),
|
|
Argument::createBooleanTypeArgument('ignore_availability', false),
|
|
Argument::createIntTypeArgument('room_id'),
|
|
Argument::createIntTypeArgument('product_id'),
|
|
Argument::createBooleanTypeArgument('use_saved_search_criteria', false)
|
|
);
|
|
}
|
|
|
|
|
|
/**
|
|
* @return array
|
|
* @throws \Exception
|
|
*/
|
|
public function buildArray()
|
|
{
|
|
if ($this->getUseSavedSearchCriteria()) {
|
|
$searchData = new SearchParameters($this->requestStack->getCurrentRequest());
|
|
|
|
$adults = $searchData->getAdultCount();
|
|
$children = $searchData->getChildCount();
|
|
$dateDebut = $searchData->getStartDate();
|
|
$dateFin = $searchData->getEndDate();
|
|
} else {
|
|
$adults = $this->getAdults();
|
|
$children = $this->getChildren();
|
|
|
|
if (false === $dateDebut = \DateTime::createFromFormat('Y-m-d', $this->getStartDate())) {
|
|
throw new \InvalidArgumentException("Date de début incorrecte : ".$this->getStartDate());
|
|
}
|
|
|
|
if (false === $dateFin = \DateTime::createFromFormat('Y-m-d', $this->getEndDate())) {
|
|
throw new \InvalidArgumentException("Date de fin incorrecte : ".$this->getEndDate());
|
|
}
|
|
}
|
|
|
|
$roomId = $this->getRoomId();
|
|
|
|
// Si un ID produit est fourni, trouver le Room ID associée (on écrase celui fourni !)
|
|
if (null !== $productId = $this->getProductId()) {
|
|
if (null === $roomInfo = Beds24ProductInfoQuery::create()->findOneByProductId($productId)) {
|
|
return [];
|
|
} else {
|
|
$roomId = $roomInfo->getRoomId();
|
|
}
|
|
}
|
|
|
|
$api = new Beds24Request();
|
|
|
|
$ignoreAvailability =$this->getIgnoreAvailability();
|
|
|
|
$responseArray = $api->getAvailabilities(
|
|
$dateDebut,
|
|
$dateFin,
|
|
$roomId,
|
|
$adults,
|
|
$children,
|
|
$ignoreAvailability
|
|
);
|
|
|
|
if ($api->hasError($responseArray)) {
|
|
return [];
|
|
}
|
|
|
|
$resultItemBase = [
|
|
'checkIn' => $responseArray['checkIn'],
|
|
'lastNight' => $responseArray['lastNight'],
|
|
'checkOut' => $responseArray['checkOut'],
|
|
'numAdult' => isset($responseArray['numAdult']) ? $responseArray['numAdult'] : 0,
|
|
'numChildren' => isset($responseArray['numChild']) ? $responseArray['numChild'] : 0
|
|
];
|
|
|
|
$result = [];
|
|
|
|
foreach ($responseArray as $item) {
|
|
if (is_array($item) && isset($item['roomId'])) {
|
|
if (intval($item['price']) > 0 || $ignoreAvailability) {
|
|
// Find the related product
|
|
if (null !== $productInfo = Beds24ProductInfoQuery::create()->findOneByRoomId($item['roomId'])) {
|
|
$resultItem = $resultItemBase;
|
|
|
|
$resultItem['productId'] = $productInfo->getProductId();
|
|
|
|
foreach ($item as $var => $value) {
|
|
$resultItem[$var] = $value;
|
|
}
|
|
|
|
$result[] = $resultItem;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// print_r($result);
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function parseResults(LoopResult $loopResult)
|
|
{
|
|
foreach ($loopResult->getResultDataCollection() as $item) {
|
|
$loopResultRow = new LoopResultRow($item);
|
|
|
|
$checkIn = \DateTime::createFromFormat("Ymd", $item['checkIn']);
|
|
$checkOut = \DateTime::createFromFormat("Ymd", $item['checkOut']);
|
|
$remise = Beds24::getRemiseDuree($checkIn, $checkOut, $nbJours);
|
|
$prixRemise = round($item['price'] * (1 - $remise), 2);
|
|
|
|
$loopResultRow
|
|
->set('ROOM_ID', $item['roomId'])
|
|
->set('PRODUCT_ID', $item['productId'])
|
|
->set('PROP_ID', $item['propId'])
|
|
->set('ROOMS_AVAIL', $item['roomsavail'])
|
|
->set('PRICE', $item['price'])
|
|
->set('PRICE_REMISE', $prixRemise)
|
|
->set('REMISE', $remise * 100)
|
|
->set('NB_JOURS', $nbJours)
|
|
->set('CHECK_IN', $checkIn)
|
|
->set('CHECK_OUT', $checkOut)
|
|
->set('LAST_NIGHT', \DateTime::createFromFormat("Ymd", $item['lastNight']))
|
|
->set('NUM_ADULTS', $item['numAdult'])
|
|
->set('NUM_CHILDREN', $item['numChildren'])
|
|
;
|
|
|
|
$loopResult->addRow($loopResultRow);
|
|
}
|
|
|
|
return $loopResult;
|
|
}
|
|
}
|