From 58104d8499a8fc052af6ca6a3829adc325ea3d12 Mon Sep 17 00:00:00 2001 From: Shreesh Arora Date: Mon, 11 Mar 2024 12:17:23 +0530 Subject: [PATCH] Fixed Multiple Advance Price Rule can be created with same conditions --- .../classes/HotelRoomTypeFeaturePricing.php | 20 +-- .../admin/AdminHotelFeaturePricesSettings.php | 115 ++++++++++++------ 2 files changed, 91 insertions(+), 44 deletions(-) diff --git a/modules/hotelreservationsystem/classes/HotelRoomTypeFeaturePricing.php b/modules/hotelreservationsystem/classes/HotelRoomTypeFeaturePricing.php index 76b77a65c..1505a6dcd 100644 --- a/modules/hotelreservationsystem/classes/HotelRoomTypeFeaturePricing.php +++ b/modules/hotelreservationsystem/classes/HotelRoomTypeFeaturePricing.php @@ -189,13 +189,13 @@ class HotelRoomTypeFeaturePricing extends ObjectModel if ($type == 'specific_date') { return Db::getInstance()->getRow( 'SELECT * FROM `'._DB_PREFIX_.'htl_room_type_feature_pricing` - WHERE `id_product`='.(int) $id_product.' + WHERE `id_product`='.(int) $id_product.' AND `active`=1 AND `date_selection_type` = '.(int) self::DATE_SELECTION_TYPE_SPECIFIC.' AND `date_from` = \''.pSQL($date_from).'\' AND `id_feature_price`!='.(int) $id_feature_price ); } elseif ($type == 'special_day') { - $featurePrice = Db::getInstance()->getRow( + $featurePrices = Db::getInstance()->executeS( 'SELECT * FROM `'._DB_PREFIX_.'htl_room_type_feature_pricing` WHERE `id_product`='.(int) $id_product.' AND `is_special_days_exists`=1 AND `active`=1 @@ -203,19 +203,21 @@ class HotelRoomTypeFeaturePricing extends ObjectModel AND `date_to` > \''.pSQL($date_from).'\' AND `id_feature_price`!='.(int) $id_feature_price ); - if ($featurePrice) { - $specialDays = json_decode($featurePrice['special_days']); - $currentSpecialDays = json_decode($current_Special_days); - $commonValues = array_intersect($specialDays, $currentSpecialDays); - if ($commonValues) { - return $featurePrice; + if ($featurePrices) { + foreach ($featurePrices as $featurePrice) { + $specialDays = json_decode($featurePrice['special_days']); + $currentSpecialDays = json_decode($current_Special_days); + $commonValues = array_intersect($specialDays, $currentSpecialDays); + if ($commonValues) { + return $featurePrice; + } } } return false; } elseif ($type == 'date_range') { return Db::getInstance()->getRow( 'SELECT * FROM `'._DB_PREFIX_.'htl_room_type_feature_pricing` - WHERE `id_product`='.(int) $id_product.' + WHERE `id_product`='.(int) $id_product.' AND `active`=1 AND `date_selection_type` = '.(int) self::DATE_SELECTION_TYPE_RANGE.' AND `is_special_days_exists`=0 AND `date_from` <= \''.pSQL($date_to).'\' diff --git a/modules/hotelreservationsystem/controllers/admin/AdminHotelFeaturePricesSettings.php b/modules/hotelreservationsystem/controllers/admin/AdminHotelFeaturePricesSettings.php index 763fce92d..015fb395e 100644 --- a/modules/hotelreservationsystem/controllers/admin/AdminHotelFeaturePricesSettings.php +++ b/modules/hotelreservationsystem/controllers/admin/AdminHotelFeaturePricesSettings.php @@ -173,6 +173,31 @@ class AdminHotelFeaturePricesSettingsController extends ModuleAdminController return $this->context->smarty->fetch(_PS_MODULE_DIR_.$tpl_path); } + public function processStatus() + { + $objFeaturePricing = $this->loadObject(); + if (!$objFeaturePricing->active) { + if ($this->validateExistingFeaturePrice( + $objFeaturePricing->date_selection_type, + $objFeaturePricing->id_product, + $objFeaturePricing->date_from, + $objFeaturePricing->date_to, + $objFeaturePricing->id, + $objFeaturePricing->is_special_days_exists, + $objFeaturePricing->special_days + + + )) { + $this->errors[] = $this->l('An advanced price rule already exists in which some dates are common with this + plan. Please select a different date range.'); + return ; + } + } + + return parent::processStatus(); + + } + public function initToolbar() { parent::initToolbar(); @@ -253,6 +278,46 @@ class AdminHotelFeaturePricesSettingsController extends ModuleAdminController } } + public function validateExistingFeaturePrice( + $dateSelectionType, + $roomTypeId, + $dateFrom, + $dateTo, + $idFeaturePrice, + $isSpecialDaysExists = false, + $jsonSpecialDays = "false" + ) { + $objFeaturePricing = new HotelRoomTypeFeaturePricing(); + if ($dateSelectionType == HotelRoomTypeFeaturePricing::DATE_SELECTION_TYPE_SPECIFIC) { + return $objFeaturePricing->checkRoomTypeFeaturePriceExistance( + $roomTypeId, + $dateFrom, + $dateTo, + 'specific_date', + false, + $idFeaturePrice + ); + } elseif (isset($isSpecialDaysExists) && $isSpecialDaysExists && $jsonSpecialDays != "false") { + return $objFeaturePricing->checkRoomTypeFeaturePriceExistance( + $roomTypeId, + $dateFrom, + $dateTo, + 'special_day', + $jsonSpecialDays, + $idFeaturePrice + ); + } else { + return $objFeaturePricing->checkRoomTypeFeaturePriceExistance( + $roomTypeId, + $dateFrom, + $dateTo, + 'date_range', + false, + $idFeaturePrice + ); + } + } + public function processSave() { $idFeaturePrice = Tools::getValue('id_feature_price'); @@ -281,40 +346,26 @@ class AdminHotelFeaturePricesSettingsController extends ModuleAdminController $languages = Language::getLanguages(false); $objDefaultLang = new language($defaultLangId); - $isPlanTypeExists = 0; + if ($dateSelectionType == HotelRoomTypeFeaturePricing::DATE_SELECTION_TYPE_SPECIFIC) { - $isPlanTypeExists = $objFeaturePricing->checkRoomTypeFeaturePriceExistance( - $roomTypeId, - $specificDate, - date('Y-m-d', strtotime("+1 day", strtotime($specificDate))), - 'specific_date', - false, - $idFeaturePrice - ); - } elseif (isset($isSpecialDaysExists) && $isSpecialDaysExists == 'on') { - if ($jsonSpecialDays != "false") { - $isPlanTypeExists = $objFeaturePricing->checkRoomTypeFeaturePriceExistance( - $roomTypeId, - $dateFrom, - $dateTo, - 'special_day', - $jsonSpecialDays, - $idFeaturePrice - ); - } else { - $this->errors[] = $this->l('Please select at least one day for the special day selection.'); - } + $dateFrom = $specificDate; + $dateTo = date('Y-m-d', strtotime("+1 day", strtotime($specificDate))); + } + + $isPlanTypeExists = 0; + if (isset($isSpecialDaysExists) && $isSpecialDaysExists == 'on' && $jsonSpecialDays == "false") { + $this->errors[] = $this->l('Please select at least one day for the special day selection.'); } else { - $isPlanTypeExists = $objFeaturePricing->checkRoomTypeFeaturePriceExistance( + $isPlanTypeExists = $this->validateExistingFeaturePrice( + $dateSelectionType, $roomTypeId, $dateFrom, $dateTo, - 'date_range', - false, - $idFeaturePrice + $idFeaturePrice, + $isSpecialDaysExists, + $jsonSpecialDays ); } - if ($isPlanTypeExists) { $this->errors[] = $this->l('An advanced price rule already exists in which some dates are common with this plan. Please select a different date range.'); @@ -410,14 +461,8 @@ class AdminHotelFeaturePricesSettingsController extends ModuleAdminController } } $objFeaturePricing->date_selection_type = $dateSelectionType; - - if ($dateSelectionType == HotelRoomTypeFeaturePricing::DATE_SELECTION_TYPE_RANGE) { - $objFeaturePricing->date_from = $dateFrom; - $objFeaturePricing->date_to = $dateTo; - } else { - $objFeaturePricing->date_from = $specificDate; - $objFeaturePricing->date_to = date('Y-m-d', strtotime($specificDate) + 86400); - } + $objFeaturePricing->date_from = $dateFrom; + $objFeaturePricing->date_to = $dateTo; $objFeaturePricing->impact_way = $priceImpactWay; $objFeaturePricing->is_special_days_exists = $isSpecialDaysExists; $objFeaturePricing->special_days = $jsonSpecialDays;