From 4436fd016c3f3da874d0a303cb73be8fdefb38ba Mon Sep 17 00:00:00 2001 From: "vishalsingh.qlo325" Date: Fri, 14 Feb 2025 13:19:20 +0530 Subject: [PATCH] Optmized the functions and removed non rquired object initialization to reduce stress --- classes/Cart.php | 1 - classes/Product.php | 37 ++++++++++++++----- .../classes/HotelBranchInformation.php | 14 +++++-- .../classes/HotelCartBookingData.php | 2 +- .../classes/HotelRoomType.php | 20 +++++++--- .../classes/HotelRoomTypeFeaturePricing.php | 6 +-- .../classes/RoomTypeServiceProduct.php | 1 - .../RoomTypeServiceProductCartDetail.php | 5 +-- 8 files changed, 57 insertions(+), 29 deletions(-) diff --git a/classes/Cart.php b/classes/Cart.php index 5255c580c..9fc023781 100644 --- a/classes/Cart.php +++ b/classes/Cart.php @@ -2296,7 +2296,6 @@ class CartCore extends ObjectModel $orderPackage = array(); $serviceProducts = array(); $objRoomType = new HotelRoomType(); - $objHtlCartBookingData = new HotelCartBookingData(); $objRoomTypeServiceProductCartDetail = new RoomTypeServiceProductCartDetail(); $objHotelServiceProductCartDetail = new HotelServiceProductCartDetail(); foreach ($final_package_list as $id_address => $packages) { diff --git a/classes/Product.php b/classes/Product.php index f62901fe9..e5a74106d 100644 --- a/classes/Product.php +++ b/classes/Product.php @@ -931,12 +931,21 @@ class ProductCore extends ObjectModel ), 'id_product = '.(int)$id_product); } - public static function getProductPriceCalculation($id_product) { - return Db::getInstance()->getValue( - 'SELECT product_shop.`price_calculation_method` - FROM `'._DB_PREFIX_.'product` p '.Shop::addSqlAssociation('product', 'p').' - WHERE p.`id_product` = '.(int)$id_product - ); + public static function getProductPriceCalculation($id_product) + { + $cache_key = 'Product::getProductPriceCalculation'.(int)$id_product; + if (!Cache::isStored($cache_key)) { + $res = Db::getInstance()->getValue( + 'SELECT product_shop.`price_calculation_method` + FROM `'._DB_PREFIX_.'product` p '.Shop::addSqlAssociation('product', 'p').' + WHERE p.`id_product` = '.(int)$id_product + ); + Cache::store($cache_key, $res); + } else { + $res = Cache::retrieve($cache_key); + } + + return $res; } /** @@ -975,10 +984,18 @@ class ProductCore extends ObjectModel public static function isBookingProduct($id_product) { - return Db::getInstance()->getValue(' - SELECT `booking_product` FROM `'._DB_PREFIX_.'product` p - WHERE p.`id_product` = '.(int)$id_product - ); + $cache_key = 'Product::isBookingProduct'.(int)$id_product; + if (!Cache::isStored($cache_key)) { + $res = Db::getInstance()->getValue(' + SELECT `booking_product` FROM `'._DB_PREFIX_.'product` p + WHERE p.`id_product` = '.(int)$id_product + ); + Cache::store($cache_key, $res); + } else { + $res = Cache::retrieve($cache_key); + } + + return $res; } public function delete() diff --git a/modules/hotelreservationsystem/classes/HotelBranchInformation.php b/modules/hotelreservationsystem/classes/HotelBranchInformation.php index c2779a01e..b7c85eac4 100644 --- a/modules/hotelreservationsystem/classes/HotelBranchInformation.php +++ b/modules/hotelreservationsystem/classes/HotelBranchInformation.php @@ -527,9 +527,17 @@ class HotelBranchInformation extends ObjectModel */ public static function getHotelIdByIdCategory($id_category) { - return Db::getInstance()->getValue( - 'SELECT `id` FROM `'._DB_PREFIX_.'htl_branch_info` WHERE id_category = '.(int)$id_category - ); + $cache_key = 'HotelBranchInformation::getHotelIdByIdCategory'.(int)$id_category; + if (!Cache::isStored($cache_key)) { + $res = Db::getInstance()->getValue( + 'SELECT `id` FROM `'._DB_PREFIX_.'htl_branch_info` WHERE id_category = '.(int)$id_category + ); + Cache::store($cache_key, $res); + } else { + $res = Cache::retrieve($cache_key); + } + + return $res; } /** diff --git a/modules/hotelreservationsystem/classes/HotelCartBookingData.php b/modules/hotelreservationsystem/classes/HotelCartBookingData.php index 0cd9c678d..c2d9bf333 100644 --- a/modules/hotelreservationsystem/classes/HotelCartBookingData.php +++ b/modules/hotelreservationsystem/classes/HotelCartBookingData.php @@ -1350,7 +1350,7 @@ class HotelCartBookingData extends ObjectModel * @param [int] $id_group [id_group for which price is need (if available for the passed group)] * @return [array|false] [returns array containg info of the feature plan if foung otherwise returns false] */ - public function getProductFeaturePricePlanByDateByPriority( + public static function getProductFeaturePricePlanByDateByPriority( $id_product, $date, $id_group, diff --git a/modules/hotelreservationsystem/classes/HotelRoomType.php b/modules/hotelreservationsystem/classes/HotelRoomType.php index a523d4bc4..03112f59b 100644 --- a/modules/hotelreservationsystem/classes/HotelRoomType.php +++ b/modules/hotelreservationsystem/classes/HotelRoomType.php @@ -248,12 +248,20 @@ class HotelRoomType extends ObjectModel */ public static function getHotelIdAddressByIdProduct($id_product) { - return Db::getInstance()->getValue( - 'SELECT `id_address` from `'._DB_PREFIX_.'address` a - INNER JOIN `'._DB_PREFIX_.'htl_room_type` hrt - ON (hrt.`id_hotel` = a.`id_hotel`) - WHERE hrt.`id_product` = '.(int)$id_product.' AND a.`deleted` = 0 - '); + $cache_key = 'HotelRoomType::getHotelIdAddressByIdProduct'.(int)$id_product; + if (!Cache::isStored($cache_key)) { + $res = Db::getInstance()->getValue( + 'SELECT `id_address` from `'._DB_PREFIX_.'address` a + INNER JOIN `'._DB_PREFIX_.'htl_room_type` hrt + ON (hrt.`id_hotel` = a.`id_hotel`) + WHERE hrt.`id_product` = '.(int)$id_product.' AND a.`deleted` = 0 + '); + Cache::store($cache_key, $res); + } else { + $res = Cache::retrieve($cache_key); + } + + return $res; } /** diff --git a/modules/hotelreservationsystem/classes/HotelRoomTypeFeaturePricing.php b/modules/hotelreservationsystem/classes/HotelRoomTypeFeaturePricing.php index fd1ef9d93..82621b434 100644 --- a/modules/hotelreservationsystem/classes/HotelRoomTypeFeaturePricing.php +++ b/modules/hotelreservationsystem/classes/HotelRoomTypeFeaturePricing.php @@ -671,9 +671,8 @@ class HotelRoomTypeFeaturePricing extends ObjectModel $context = Context::getContext(); $id_currency = Validate::isLoadedObject($context->currency) ? (int)$context->currency->id : (int)Configuration::get('PS_CURRENCY_DEFAULT'); - $hotelCartBookingData = new HotelCartBookingData(); for($currentDate = date('Y-m-d', strtotime($date_from)); $currentDate < date('Y-m-d', strtotime($date_to)); $currentDate = date('Y-m-d', strtotime('+1 day', strtotime($currentDate)))) { - if ($use_reduc && ($featurePrice = $hotelCartBookingData->getProductFeaturePricePlanByDateByPriority( + if ($use_reduc && ($featurePrice = HotelCartBookingData::getProductFeaturePricePlanByDateByPriority( $id_product, $currentDate, $id_group, @@ -826,8 +825,7 @@ class HotelRoomTypeFeaturePricing extends ObjectModel $totalDurationPriceTI = $totalDurationPrice['total_price_tax_incl']; $totalDurationPriceTE = $totalDurationPrice['total_price_tax_excl']; - $objHotelBookingDetail = new HotelBookingDetail(); - $numDaysInDuration = $objHotelBookingDetail->getNumberOfDays($dateFrom, $dateTo); + $numDaysInDuration = HotelHelper::getNumberOfDays($dateFrom, $dateTo); if ($use_tax) { $pricePerDay = $totalDurationPriceTI/$numDaysInDuration; } else { diff --git a/modules/hotelreservationsystem/classes/RoomTypeServiceProduct.php b/modules/hotelreservationsystem/classes/RoomTypeServiceProduct.php index 7c80b17e1..033222b22 100644 --- a/modules/hotelreservationsystem/classes/RoomTypeServiceProduct.php +++ b/modules/hotelreservationsystem/classes/RoomTypeServiceProduct.php @@ -184,7 +184,6 @@ class RoomTypeServiceProduct extends ObjectModel true, $subCategory )) { - $objHotelRoomType = new HotelRoomType(); $serviceProducts = Product::getProductsProperties($idLang, $serviceProducts); $objRoomTypeServiceProductPrice = new RoomTypeServiceProductPrice(); foreach($serviceProducts as &$serviceProduct) { diff --git a/modules/hotelreservationsystem/classes/RoomTypeServiceProductCartDetail.php b/modules/hotelreservationsystem/classes/RoomTypeServiceProductCartDetail.php index fec2fed2c..c7f894b18 100644 --- a/modules/hotelreservationsystem/classes/RoomTypeServiceProductCartDetail.php +++ b/modules/hotelreservationsystem/classes/RoomTypeServiceProductCartDetail.php @@ -267,10 +267,9 @@ class RoomTypeServiceProductCartDetail extends ObjectModel if ($getTotalPrice) { $totalPrice = 0; } - $objRoomTypeServiceProductPrice = new RoomTypeServiceProductPrice(); - $objHotelRoomType = new HotelRoomType(); - $selectedServiceProducts = array(); + $objRoomTypeServiceProductPrice = new RoomTypeServiceProductPrice(); + $selectedServiceProducts = array(); if ($serviceProducts = Db::getInstance()->executeS($sql)) { foreach ($serviceProducts as $product) { $qty = $product['quantity'] ? (int)$product['quantity'] : 1;