Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated: Optimized dataForFrontSearch function #1194

Merged
merged 8 commits into from
Sep 9, 2024
175 changes: 64 additions & 111 deletions modules/hotelreservationsystem/classes/HotelBookingDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,6 @@ public function getBookingDataParams(&$params)
$params['occupancy'] = array();
}

if (!isset($params['ratting'])) {
$params['ratting'] = -1;
}
if (!isset($params['amenities'])) {
$params['amenities'] = 0;
}
Expand All @@ -239,6 +236,10 @@ public function getBookingDataParams(&$params)
$params['only_search_data'] = 0;
}

if (!isset($params['full_detail'])) {
$params['full_detail'] = 0;
}

Hook::exec('actionBookingDataParamsModifier', array('params' => &$params));

return $params;
Expand Down Expand Up @@ -399,7 +400,7 @@ public function getBookingData($params)
}

// Now we will formate the data after geting search result according to search type
$roomTypesDetail = $objRoomType->getRoomTypeDetailByRoomTypeIds($allowedIdRoomTypes);
$roomTypesDetail = $objRoomType->getRoomTypeDetailByRoomTypeIds($allowedIdRoomTypes, true, $full_detail);
if ($roomTypesDetail) {
// Formate data for response
$finalSearchResponse = array(
Expand All @@ -414,15 +415,12 @@ public function getBookingData($params)
$finalSearchResponse['stats']['total_rooms'] += $roomTypeDetail['numberOfRooms'];
$idProduct = $roomTypeDetail['id_product'];

$roomTypeSearchData = array(
'name' => (new Product((int) $idProduct, false, $this->context->cookie->id_lang))->name,
'id_product' => $idProduct,
'adults' => $roomTypeDetail['adults'],
'children' => $roomTypeDetail['children'],
'max_adults' => $roomTypeDetail['max_adults'],
'max_children' => $roomTypeDetail['max_children'],
'max_guests' => $roomTypeDetail['max_guests'],
'data' => array(),
$roomTypeSearchData = array_merge(
$roomTypeDetail,
array(
'id_product' => $idProduct,
'data' => array(),
)
);

if ($search_unavai) {
Expand Down Expand Up @@ -1195,6 +1193,7 @@ protected function getSearchPartiallyAvailRooms($params)
}
}
unset($datetimeObj);
unset($roomDetail);

// If all dates are covered
if (!$datesToCover) {
Expand Down Expand Up @@ -1634,7 +1633,6 @@ public function updateBookingOrderStatusByOrderId(
* @param [int] $only_search_data [used for product page and category page for block cart]
* @param [int] $adults []
* @param [int] $children []
* @param [] $ratting [description]
* @param [] $amenities [description]
* @param [] $price [description]
* @param [int] $id_cart [Id of the cart]
Expand All @@ -1645,10 +1643,6 @@ public function updateBookingOrderStatusByOrderId(
*/
public function dataForFrontSearch($bookingParams)
{
// if (Module::isInstalled('productcomments')) {
// require_once _PS_MODULE_DIR_.'productcomments/ProductComment.php';
// }

$this->context = Context::getContext();

if (!isset($bookingParams['search_available'])) {
Expand All @@ -1664,6 +1658,7 @@ public function dataForFrontSearch($bookingParams)
$bookingParams['search_unavai'] = 0;
}

$bookingParams['full_detail'] = 1;
$bookingData = $this->getBookingData($bookingParams);

extract($this->getBookingDataParams($bookingParams));
Expand All @@ -1673,104 +1668,62 @@ public function dataForFrontSearch($bookingParams)
$objRoomType = new HotelRoomType();

foreach ($bookingData['rm_data'] as $key => $value) {
if (empty($value['data']['available'])) {
unset($bookingData['rm_data'][$key]);
} else {
$prod_ratting = 0;
// if (Module::isInstalled('productcomments')) {
// $prod_ratting = ProductComment::getAverageGrade($value['id_product'])['grade'];
// }
// if (empty($prod_ratting)) {
// $prod_ratting = 0;
// }

// if ($prod_ratting < $ratting && $ratting != -1) {
// unset($bookingData['rm_data'][$key]);
// } else
// {
$product = new Product($value['id_product'], false, $this->context->language->id);

$product_feature = $product->getFrontFeaturesStatic($this->context->language->id, $value['id_product']);

$prod_amen = array();
if (!empty($amenities) && $amenities) {
$prod_amen = $amenities;
foreach ($product_feature as $a_key => $a_val) {
if (($pa_key = array_search($a_val['id_feature'], $prod_amen)) !== false) {
unset($prod_amen[$pa_key]);
if (empty($prod_amen)) {
break;
}
$product_feature = Product::getFrontFeaturesStatic($this->context->language->id, $value['id_product']);
$prod_amen = array();
if (!empty($amenities) && $amenities) {
$prod_amen = $amenities;
foreach ($product_feature as $a_key => $a_val) {
if (($pa_key = array_search($a_val['id_feature'], $prod_amen)) !== false) {
unset($prod_amen[$pa_key]);
if (empty($prod_amen)) {
break;
}
}
if (!empty($prod_amen)) {
unset($bookingData['rm_data'][$key]);
}
}
if (!empty($prod_amen)) {
unset($bookingData['rm_data'][$key]);
continue;
}
}
$productFeaturePrice = HotelRoomTypeFeaturePricing::getRoomTypeFeaturePricesPerDay($value['id_product'], $date_from, $date_to, self::useTax());
if (!empty($price) && ($price['from'] > $productFeaturePrice || $price['to'] < $productFeaturePrice)) {
unset($bookingData['rm_data'][$key]);
continue;
}
if (count($value['data']['available'])) {
$prod_price = Product::getPriceStatic($value['id_product'], self::useTax());
$productPriceWithoutReduction = HotelRoomTypeFeaturePricing::getRoomTypeFeaturePricesPerDay($value['id_product'], $date_from, $date_to, self::useTax(), 0, 0, 0, 0, 1, 0);
$cover_image_arr = Product::getCover($value['id_product']);
if (!empty($cover_image_arr)) {
$cover_img = $this->context->link->getImageLink($value['link_rewrite'], $value['id_product'].'-'.$cover_image_arr['id_image'], 'home_default');
} else {
$cover_img = $this->context->link->getImageLink($value['link_rewrite'], $this->context->language->iso_code.'-default', 'home_default');
}
$bookingData['rm_data'][$key]['image'] = $cover_img;
$bookingData['rm_data'][$key]['feature'] = $product_feature;
$bookingData['rm_data'][$key]['price'] = $prod_price;
$bookingData['rm_data'][$key]['feature_price'] = $productFeaturePrice;
$bookingData['rm_data'][$key]['price_without_reduction'] = $productPriceWithoutReduction;
$bookingData['rm_data'][$key]['feature_price_diff'] = $productPriceWithoutReduction - $productFeaturePrice;
$bookingData['rm_data'][$key]['room_left'] = count($bookingData['rm_data'][$key]['data']['available']);

// create URL with the parameters from URL
$urlData = array ('date_from' => $date_from, 'date_to' => $date_to);
if (!isset($occupancy)) {
$occupancy = Tools::getValue('occupancy');
}
if ($occupancy) {
$urlData['occupancy'] = $occupancy;
}
if ($location = Tools::getValue('location')) {
$urlData['location'] = $location;
}

if (empty($prod_amen)) {
$prod_price = Product::getPriceStatic($value['id_product'], self::useTax());
$productPriceWithoutReduction = $product->getPriceWithoutReduct(!self::useTax());
$productFeaturePrice = HotelRoomTypeFeaturePricing::getRoomTypeFeaturePricesPerDay($value['id_product'], $date_from, $date_to, self::useTax());
$productFeaturePriceWithoutAutoAdd = HotelRoomTypeFeaturePricing::getRoomTypeFeaturePricesPerDay($value['id_product'], $date_from, $date_to, self::useTax(), 0, 0, 0, 0, 0);

if (empty($price) || ($price['from'] <= $prod_price && $price['to'] >= $prod_price)) {
$cover_image_arr = $product->getCover($value['id_product']);

if (!empty($cover_image_arr)) {
$cover_img = $this->context->link->getImageLink($product->link_rewrite, $product->id.'-'.$cover_image_arr['id_image'], 'home_default');
} else {
$cover_img = $this->context->link->getImageLink($product->link_rewrite, $this->context->language->iso_code.'-default', 'home_default');
}

$room_left = count($bookingData['rm_data'][$key]['data']['available']);

$rm_dtl = $objRoomType->getRoomTypeInfoByIdProduct($value['id_product']);

$bookingData['rm_data'][$key]['name'] = $product->name;
$bookingData['rm_data'][$key]['image'] = $cover_img;
$bookingData['rm_data'][$key]['description'] = $product->description_short;
$bookingData['rm_data'][$key]['feature'] = $product_feature;
$bookingData['rm_data'][$key]['price'] = $prod_price;
$bookingData['rm_data'][$key]['feature_price'] = $productFeaturePrice;
$bookingData['rm_data'][$key]['feature_price_withoout_auto_add'] = $productFeaturePriceWithoutAutoAdd;
$bookingData['rm_data'][$key]['price_without_reduction'] = $productPriceWithoutReduction;
$bookingData['rm_data'][$key]['price_without_reduction_with_auto_add'] = $productPriceWithoutReduction + ($productFeaturePrice - $productFeaturePriceWithoutAutoAdd);
$bookingData['rm_data'][$key]['feature_price_diff'] = $bookingData['rm_data'][$key]['price_without_reduction_with_auto_add'] - $productFeaturePrice;

// if ($room_left <= (int)Configuration::get('WK_ROOM_LEFT_WARNING_NUMBER'))
$bookingData['rm_data'][$key]['room_left'] = $room_left;

$bookingData['rm_data'][$key]['adults'] = $rm_dtl['adults'];
$bookingData['rm_data'][$key]['children'] = $rm_dtl['children'];

$bookingData['rm_data'][$key]['ratting'] = $prod_ratting;
// if (Module::isInstalled('productcomments')) {
// $bookingData['rm_data'][$key]['num_review'] = ProductComment::getCommentNumber($value['id_product']);
// }

// create URL with the parameters from URL
$urlData = array ('date_from' => $date_from, 'date_to' => $date_to);
if (!isset($occupancy)) {
$occupancy = Tools::getValue('occupancy');
}
if ($occupancy) {
$urlData['occupancy'] = $occupancy;
}
if ($location = Tools::getValue('location')) {
$urlData['location'] = $location;
}

if (Configuration::get('PS_REWRITING_SETTINGS')) {
$bookingData['rm_data'][$key]['product_link'] = $this->context->link->getProductLink($product).'?'.http_build_query($urlData);
} else {
$bookingData['rm_data'][$key]['product_link'] = $this->context->link->getProductLink($product).'&'.http_build_query($urlData);
}
} else {
unset($bookingData['rm_data'][$key]);
}
if (Configuration::get('PS_REWRITING_SETTINGS')) {
$bookingData['rm_data'][$key]['product_link'] = $this->context->link->getProductLink($value['id_product']).'?'.http_build_query($urlData);
} else {
$bookingData['rm_data'][$key]['product_link'] = $this->context->link->getProductLink($value['id_product']).'&'.http_build_query($urlData);
}
// }
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions modules/hotelreservationsystem/classes/HotelRoomType.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,18 @@ public function getRoomTypeByHotelId($hotel_id, $id_lang, $active = 2)
/**
* @param [int] $roomTypesList: string of idRoomTypes seperated by ","
*/
public function getRoomTypeDetailByRoomTypeIds($roomTypesList, $position = true)
public function getRoomTypeDetailByRoomTypeIds($roomTypesList, $position = true, $fullDetail = false, $idLang = false)
{
$sql = 'SELECT COUNT(hri.`id`) AS `numberOfRooms`, hrt.`id_product`, `adults`, `children`, `max_adults`, `max_children`, `max_guests`'.
($position ? ', cp.`position`' : '').'
if (!$idLang) {
$idLang = Context::getContext()->language->id;
}

$sql = 'SELECT pl.`name`, COUNT(hri.`id`) AS `numberOfRooms`, hrt.`id_product`, `adults`, `children`, `max_adults`, `max_children`, `max_guests`
'.($position ? ', cp.`position`' : '').'
'.($fullDetail ? ', pl.`link_rewrite`, pl.`description_short`' : '').'
FROM `'._DB_PREFIX_.'htl_room_type` AS `hrt`
INNER JOIN `'._DB_PREFIX_.'htl_room_information` AS `hri` ON (hri.`id_product` = hrt.`id_product`)';
$sql .= ' INNER JOIN `'._DB_PREFIX_.'product_lang` pl ON (hrt.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$idLang.')';

if ($position) {
$sql .= ' INNER JOIN `'._DB_PREFIX_.'htl_branch_info` hbi ON (hbi.`id` = hrt.`id_hotel`)
Expand Down
4 changes: 2 additions & 2 deletions modules/wkhotelfilterblock/views/js/wkhotelfilterblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ $(document).ready(function()
values: [min_price, max_price],
slide: function(event, ui)
{
$("#filter_price_from").html(ui.values[0]);
$("#filter_price_to").html(ui.values[1]);
$("#filter_price_from").html(formatCurrency(ui.values[0], currencyFormat, currencySign, currencyBlank));
$("#filter_price_to").html(formatCurrency(ui.values[1], currencyFormat, currencySign, currencyBlank));
},
change: function(event, ui)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
</div>
<div class="col-sm-12 lf_sub_cont">
<div class="row margin-lr-0 price_filter_subcont">
<span class="pull-left">{$currency->prefix} <span id="filter_price_from">{$min_price}</span> {$currency->suffix}</span>
<span class="pull-right">{$currency->prefix} <span id="filter_price_to">{$max_price}</span> {$currency->suffix}</span>
<span class="pull-left"><span id="filter_price_from">{displayPrice price=$min_price}</span></span>
<span class="pull-right"><span id="filter_price_to">{displayPrice price=$max_price}</span></span>
</div>
<div id="filter_price_silder"></div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion modules/wkhotelfilterblock/wkhotelfilterblock.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function hookDisplayLeftColumn()
$prod_price = array();
if ($room_types) {
foreach ($room_types as $key => $value) {
$prod_price[] = Product::getPriceStatic($value['id_product'], HotelBookingDetail::useTax());
$prod_price[] = HotelRoomTypeFeaturePricing::getRoomTypeFeaturePricesPerDay($value['id_product'], $date_from, $date_to, HotelBookingDetail::useTax());
}
}

Expand Down
Loading