From 6e8374049597b64a07bae34a89bfcd91a6f69e50 Mon Sep 17 00:00:00 2001 From: "vishalsingh.qlo325" Date: Mon, 19 May 2025 11:07:50 +0530 Subject: [PATCH] Added Preference for the address type selection for standalone service products --- .../helpers/options/options.tpl | 83 +++++++++++ classes/Product.php | 52 +++++-- .../admin/AdminOrderPreferencesController.php | 133 +++++++++++++++++- install/data/xml/configuration.xml | 3 + js/admin.js | 8 ++ 5 files changed, 270 insertions(+), 9 deletions(-) diff --git a/admin/themes/default/template/controllers/order_preferences/helpers/options/options.tpl b/admin/themes/default/template/controllers/order_preferences/helpers/options/options.tpl index eba33ee11..45eb6a349 100644 --- a/admin/themes/default/template/controllers/order_preferences/helpers/options/options.tpl +++ b/admin/themes/default/template/controllers/order_preferences/helpers/options/options.tpl @@ -24,9 +24,92 @@ *} {extends file="helpers/options/options.tpl"} +{block name="input"} + {if $field['type'] == 'html' && $category == 'standard_product' && $key == 'PS_STANDARD_PRODUCT_ORDER_ADDRESS'} +
+
{l s='Please set all the required fields for the custom address below'}
+
+
+ {if isset($countries) && $countries} +
+ +
+ +
+
+
+ +
+ +
+
+ {/if} +
+ +
+ +
+
+
+ +
+ +
+
+
+ {else} + {$smarty.block.parent} + {/if} +{/block} + +{block name="footer"} + {$smarty.block.parent} + {if $category == 'standard_product'} + + {/if} +{/block} + + {block name="after"} {/block} diff --git a/classes/Product.php b/classes/Product.php index 34f8b3b59..7a095d405 100644 --- a/classes/Product.php +++ b/classes/Product.php @@ -562,6 +562,9 @@ class ProductCore extends ObjectModel const PRICE_CALCULATION_METHOD_PER_BOOKING = 1; const PRICE_CALCULATION_METHOD_PER_DAY = 2; + const STANDARD_PRODUCT_ADDRESS_PREFERENCE_CUSTOMER = 1; + const STANDARD_PRODUCT_ADDRESS_PREFERENCE_HOTEL = 2; + const STANDARD_PRODUCT_ADDRESS_PREFERENCE_CUSTOM = 3; public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null) { @@ -584,7 +587,11 @@ class ProductCore extends ObjectModel // Keep base price $this->base_price = $this->price; - $this->price = Product::getPriceStatic((int)$this->id, false, null, 6, null, false, true, 1, false, null, null, null, $this->specificPrice); + if ($this->booking_product) { + $this->price = Product::getPriceStatic((int)$this->id, false, null, 6, null, false, true, 1, false, null, null, null, $this->specificPrice); + } else { + $this->price = Product::getServiceProductPrice((int)$this->id); + } $this->unit_price = ($this->unit_price_ratio != 0 ? $this->price / $this->unit_price_ratio : 0); if ($this->id) { $this->tags = Tag::getProductTags((int)$this->id); @@ -934,15 +941,25 @@ class ProductCore extends ObjectModel ), 'id_product = '.(int)$id_product); } - public static function getSellingPreferenceType($id_product) { - return Db::getInstance()->getValue( - 'SELECT p.`selling_preference_type` - FROM `'._DB_PREFIX_.'product` p '.Shop::addSqlAssociation('product', 'p').' - WHERE p.`id_product` = '.(int)$id_product - ); + public static function getSellingPreferenceType($id_product) + { + $cache_key = 'Product::getSellingPreferenceType_'.(int) $id_product; + if (!Cache::isStored($cache_key)) { + $res = Db::getInstance()->getValue( + 'SELECT p.`selling_preference_type` + 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; } - public static function getProductPriceCalculation($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').' @@ -3112,6 +3129,25 @@ class ProductCore extends ObjectModel } } + if (!Product::isBookingProduct($id_product) + && (Product::getSellingPreferenceType($id_product) == Product::SELLING_PREFERENCE_STANDALONE) + && !$id_address + ) { + $serviceAddressPrefrenceType = Configuration::get('PS_STANDARD_PRODUCT_ORDER_ADDRESS_PREFRENCE'); + if ($serviceAddressPrefrenceType == Product::STANDARD_PRODUCT_ADDRESS_PREFERENCE_CUSTOMER) { + $customer = $context->customer; + if (isset($customer->id) + && ($idCustomerAddress = Address::getFirstCustomerAddressId($customer->id)) + ) { + $id_address = $idCustomerAddress; + } + } else if (($serviceAddressPrefrenceType == Product::STANDARD_PRODUCT_ADDRESS_PREFERENCE_CUSTOM) + && ($idCustomAddress = Configuration::get('PS_STANDARD_PRODUCT_ORDER_ADDRESS_ID')) + ) { + $id_address = $idCustomAddress; + } + } + if (is_null($id_customer) && Validate::isLoadedObject($context->customer)) { $id_customer = $context->customer->id; } diff --git a/controllers/admin/AdminOrderPreferencesController.php b/controllers/admin/AdminOrderPreferencesController.php index 241ed1c29..d9254c35a 100644 --- a/controllers/admin/AdminOrderPreferencesController.php +++ b/controllers/admin/AdminOrderPreferencesController.php @@ -282,7 +282,6 @@ class AdminOrderPreferencesControllerCore extends AdminController 'validation' => 'isInt', 'type' => 'text', 'class' => 'fixed-width-xxl', - 'validation' => 'isInt', 'id' => 'PS_MAX_OVERBOOKING_PER_HOTEL_PER_DAY', ), 'OVERBOOKING_ORDER_CANCEL_ACTION' => array( @@ -312,6 +311,69 @@ class AdminOrderPreferencesControllerCore extends AdminController ), ); + if (Hook::exec('actionEnableStandartProductConfig') || _PS_MODE_DEV_) { + $addressPrefrenceTypes = array( + array( + 'value' => Product::STANDARD_PRODUCT_ADDRESS_PREFERENCE_CUSTOMER, + 'name' => $this->l('Customer Address') + ), + array( + 'value' => Product::STANDARD_PRODUCT_ADDRESS_PREFERENCE_HOTEL, + 'name' => $this->l('Hotel Address') + ), + array( + 'value' => Product::STANDARD_PRODUCT_ADDRESS_PREFERENCE_CUSTOM, + 'name' => $this->l('Custom Address') + ) + ); + + $customAddressDetails = array(); + $objHotelBranch = new HotelBranchInformation(); + if ((($idCustomAddress = Configuration::get('PS_STANDARD_PRODUCT_ORDER_ADDRESS_ID')) + && Validate::isLoadedObject($objAddress = new Address($idCustomAddress)) + ) || (($primaryHotelId = Configuration::get('WK_PRIMARY_HOTEL')) + && ($idHotelAddresss = HotelBranchInformation::getAddress((int) $primaryHotelId)) + && Validate::isLoadedObject($objAddress = new Address($idHotelAddresss['id_address'])) + ) || (($hotels = $objHotelBranch->hotelBranchesInfo(false, 1)) + && ($idHotelAddresss = HotelBranchInformation::getAddress((int) $hotels[0]['id'])) + && Validate::isLoadedObject($objAddress = new Address($idHotelAddresss)) + )) { + $customAddressDetails = array( + 'id_country' => $objAddress->id_country, + 'id_state' => $objAddress->id_state, + 'city' => $objAddress->city, + 'postcode' => $objAddress->postcode, + ); + + $this->context->smarty->assign('custom_address_details', $customAddressDetails); + } + + $countries = Country::getCountries($this->context->language->id); + $this->context->smarty->assign('countries', $countries); + $this->fields_options['standard_product'] = array( + 'title' => $this->l('Standard Product'), + 'icon' => 'icon-cogs', + 'fields' => array( + 'PS_STANDARD_PRODUCT_ORDER_ADDRESS_PREFRENCE' => array( + 'title' => $this->l('Standard product address type'), + 'validation' => 'isInt', + 'type' => 'select', + 'cast' => 'intval', + 'list' => $addressPrefrenceTypes, + 'identifier' => 'value', + 'js' => "changeStandardProductAddressType()", + 'hint' => $this->l('Select Standard product Address type, this will be used to calculate the tax for the standard product price.'), + 'desc' => $this->l('The selected address type will be used to calculate tax for the standard product.') + ), + 'PS_STANDARD_PRODUCT_ORDER_ADDRESS' => array( + 'title' => $this->l('Standard product custom address'), + 'hint' => $this->l('Set the Address details'), + 'type' => 'html', + ), + ), + 'submit' => array('title' => $this->l('Save')) + ); + } if (!Configuration::get('PS_ALLOW_MULTISHIPPING')) { unset($this->fields_options['general']['fields']['PS_ALLOW_MULTISHIPPING']); } @@ -350,11 +412,80 @@ class AdminOrderPreferencesControllerCore extends AdminController } elseif ($globalPreparationTime !== '0' && !Validate::isUnsignedInt($globalPreparationTime)) { $this->errors[] = Tools::displayError('Field \'Minimum booking offset\' is invalid.'); } + + $idCountry = Tools::getValue('service_id_country'); + $idSate = Tools::getValue('service_id_state'); + $city = trim(Tools::getValue('service_city')); + $postcode = trim(Tools::getValue('service_postcode')); + $serviceAddressPrefrenceType = Tools::getValue('PS_STANDARD_PRODUCT_ORDER_ADDRESS_PREFRENCE'); + if ($serviceAddressPrefrenceType == Product::STANDARD_PRODUCT_ADDRESS_PREFERENCE_CUSTOM) { + $addressValidation = Address::getValidationRules('Address'); + if (!$idCountry) { + $this->errors[] = Tools::displayError('Field country is required'); + } elseif (!Validate::isLoadedObject($objCountry = new Country($idCountry))) { + $this->errors[] = Tools::displayError('Invaid country'); + } elseif ($objCountry->zip_code_format && !$objCountry->checkZipCode($postcode)) { + $this->errors[] = sprintf($this->l('The Zip/Postal code you\'ve entered is invalid. It must follow this format: %s'), str_replace('C', $objCountry->iso_code, str_replace('N', '0', str_replace('L', 'A', $objCountry->zip_code_format)))); + } elseif (!$postcode && $objCountry->need_zip_code) { + $this->errors[] = $this->l('Zip / Postal code is required.'); + } elseif ($postcode && !Validate::isPostCode($postcode)) { + $this->errors[] = $this->l('The Zip / Postal code is invalid.'); + } elseif (Tools::strlen($postcode) > $addressValidation['postcode']) { + $this->errors[] = sprintf(Tools::displayError('The zip code is too long (%1$d chars max).'), $addressValidation['postcode']); + } elseif ($objCountry->contains_states && !$idSate) { + $this->errors[] = Tools::displayError('Field State is required'); + } elseif (!Validate::isLoadedObject(new State($idSate))) { + $this->errors[] = Tools::displayError('Invaid state'); + } + + if (!$city || $city == '') { + $this->errors[] = $this->l('City is required field.'); + } elseif (!Validate::isCityName($city)) { + $this->errors[] = $this->l('Enter a valid city name.'); + } elseif (Tools::strlen($city) > $addressValidation['city']) { + $this->errors[] = sprintf(Tools::displayError('The city name is too long (%1$d chars max).'), $addressValidation['city']); + } + } + } + + public function processUpdateOptions() + { + parent::processUpdateOptions(); + if (empty($this->errors)) { + $serviceAddressPrefrenceType = Tools::getValue('PS_STANDARD_PRODUCT_ORDER_ADDRESS_PREFRENCE'); + if ($serviceAddressPrefrenceType == Product::STANDARD_PRODUCT_ADDRESS_PREFERENCE_CUSTOM) { + $idCountry = Tools::getValue('service_id_country'); + if ($idCustomAddress = Configuration::get('PS_STANDARD_PRODUCT_ORDER_ADDRESS_ID')) { + $objAddress = new Address($idCustomAddress); + } else { + $objAddress = new Address(); + } + + $objCountry = new Country($idCountry, $this->context->language->id); + $objAddress->alias = 'Standard Product -'.$objCountry->name; + $objAddress->address1 = 'Standard Product -'.$objCountry->name; + $objAddress->firstname = 'Stand Alone'; + $objAddress->lastname = 'Standard Product'; + $objAddress->id_country = $idCountry; + $objAddress->id_customer = 0; + $objAddress->id_hotel = 0; + $objAddress->id_state = Tools::getValue('service_id_state'); + + $objAddress->city = Tools::getValue('service_city'); + $objAddress->postcode = Tools::getValue('service_postcode'); + if ($objAddress->save()) { + Configuration::updateValue('PS_STANDARD_PRODUCT_ORDER_ADDRESS_ID', $objAddress->id); + } + } + } } public function setMedia() { parent::setMedia(); + Media::addJsDef(array( + 'STANDARD_PRODUCT_ADDRESS_PREFERENCE_CUSTOM' => Product::STANDARD_PRODUCT_ADDRESS_PREFERENCE_CUSTOM + )); $this->addCSS(_MODULE_DIR_.'hotelreservationsystem/views/css/HotelReservationAdmin.css'); $this->addJS(_MODULE_DIR_.'hotelreservationsystem/views/js/HotelReservationAdmin.js'); } diff --git a/install/data/xml/configuration.xml b/install/data/xml/configuration.xml index 490e2b691..e596914c4 100644 --- a/install/data/xml/configuration.xml +++ b/install/data/xml/configuration.xml @@ -946,6 +946,9 @@ 0 + + + 0 diff --git a/js/admin.js b/js/admin.js index 4afd690dd..48507c79a 100644 --- a/js/admin.js +++ b/js/admin.js @@ -512,6 +512,14 @@ function changeOverbookingOrderAction() $('#PS_MAX_OVERBOOKING_PER_HOTEL_PER_DAY').closest('.form-group').show(); } +function changeStandardProductAddressType() +{ + if (getE('PS_STANDARD_PRODUCT_ORDER_ADDRESS_PREFRENCE').value == STANDARD_PRODUCT_ADDRESS_PREFERENCE_CUSTOM) + $('#conf_id_PS_STANDARD_PRODUCT_ORDER_ADDRESS').closest('.form-group').show(); + else + $('#conf_id_PS_STANDARD_PRODUCT_ORDER_ADDRESS').closest('.form-group').hide(); +} + function disableZipFormat() { if ($('#need_zip_code_on').prop('checked') == false)