From 45874aee8b2316ee9c307de2436a6a7ded262cba Mon Sep 17 00:00:00 2001 From: Sumit Panwar Date: Wed, 5 Mar 2025 12:10:23 +0530 Subject: [PATCH] Fixed: issues in testing --- .../template/controllers/orders/form.tpl | 100 ++++----- .../_add_order_extra_services_tab_content.tpl | 119 +++++----- controllers/admin/AdminCartsController.php | 210 ++++++++++-------- controllers/admin/AdminOrdersController.php | 19 +- .../RoomTypeServiceProductCartDetail.php | 1 + 5 files changed, 232 insertions(+), 217 deletions(-) diff --git a/admin/themes/default/template/controllers/orders/form.tpl b/admin/themes/default/template/controllers/orders/form.tpl index abfb41c1b..bffa8d544 100644 --- a/admin/themes/default/template/controllers/orders/form.tpl +++ b/admin/themes/default/template/controllers/orders/form.tpl @@ -1636,25 +1636,6 @@ } }); - $(document).on('click', '.change_room_type_service_product', function() { - updateServiceProducts(this); - }); - - // when the price of a service product is changed and focus is out - $(document).on('change', '#room_type_services_desc .service_cart_price_input', function() { - updateServiceProducts(this); - }); - - $(document).on('change', '#rooms_type_extra_demands .qty', function(e) { - var qty_wntd = $(this).val(); - if (qty_wntd == '' || !$.isNumeric(qty_wntd)) { - $(this).val(1); - } - if ($(this).closest('.room_demand_block').find('.change_room_type_service_product').is(':checked')) { - updateServiceProducts($(this).closest('.room_demand_block').find('.change_room_type_service_product')); - } - }); - $(document).on('keyup', '#payment_module_name', function() { let paymentMethod = $('#payment_module_name').val().trim().toLowerCase(); @@ -1782,49 +1763,54 @@ }); }); - function updateServiceProducts(element) - { - var qty = $(element).closest('.room_demand_block').find('input.qty').val(); - var dataElement = $(element).closest('.room_demand_block').find('.change_room_type_service_product'); - var operator = $(dataElement).is(':checked') ? 'up' : 'down'; - var id_product = $(dataElement).val(); - var id_cart_booking = $(dataElement).data('id_cart_booking'); - var service_price = $("#service_cart_price_"+id_cart_booking+"_"+id_product).val(); - - if (typeof(qty) == 'undefined') { - qty = 1; - } + $(document).on('click', '.change_room_type_service_product', function() { + if ($(this).prop('checked')) { + $("#selected_service_product_"+$(this).val()).val(1); + } else { + $("#selected_service_product_"+$(this).val()).val(0); + } + }); + $(document).on('submit', '#update_selected_room_services_form', function(e) { + e.preventDefault(); + var form_data = new FormData(this); + form_data.append('ajax', true); + form_data.append('action', 'updateSelectedRoomServices'); $(".loading_overlay").show(); - $.ajax({ - type: 'POST', - headers: { - "cache-control": "no-cache" - }, - url: "{$link->getAdminLink('AdminCarts')|addslashes}", - dataType: 'JSON', - cache: false, - data: { - operator: operator, - id_product: id_product, - id_cart_booking: id_cart_booking, - qty: qty, - price: service_price, - action: 'updateServiceProduct', - ajax: true - }, - success: function(jsonData) { - if (!jsonData.hasError) { - showSuccessMessage(txtExtraDemandSucc); - } else { - showErrorMessage(jsonData.errors); - } - }, + $.ajax({ + type: 'POST', + headers: { + "cache-control": "no-cache" + }, + url: "{$link->getAdminLink('AdminCarts')|addslashes}", + dataType: 'JSON', + cache: false, + data: form_data, + processData: false, + contentType: false, + success: function(jsonData) { + if (!jsonData.hasError) { + if (jsonData.service_panel) { + $('#room_type_service_product_desc').replaceWith(jsonData.service_panel); + } + showSuccessMessage(txtExtraDemandSucc); + } else { + if (jsonData.errors != 'undefined' && jsonData.errors.length) { + var errorHtml = error_found_txt + ':
'; + errorHtml += '
    '; + $.each(jsonData.errors, function(key, errorMsg) { + errorHtml += '
  1. ' + errorMsg + '
  2. '; + }); + errorHtml += '
'; + showErrorMessage(errorHtml); + } + } + }, complete: function() { $(".loading_overlay").hide(); } - }); - } + }); + }); }); diff --git a/admin/themes/default/template/controllers/orders/modals/_add_order_extra_services_tab_content.tpl b/admin/themes/default/template/controllers/orders/modals/_add_order_extra_services_tab_content.tpl index 4f91abc10..768510abc 100644 --- a/admin/themes/default/template/controllers/orders/modals/_add_order_extra_services_tab_content.tpl +++ b/admin/themes/default/template/controllers/orders/modals/_add_order_extra_services_tab_content.tpl @@ -31,62 +31,71 @@
{if isset($serviceProducts) && $serviceProducts} - - - - - - - - - - - - {foreach $serviceProducts as $product} - - - - - - + +
{l s='Name'}{l s='Quantity'}{l s='Unit Price (tax excl.)'}
- - -

{$product['name']|escape:'html':'UTF-8'}

-
- {if $product['auto_add_to_cart'] && $product['price_addition_type'] == Product::PRICE_ADDITION_TYPE_INDEPENDENT} - {l s='Convenience fee'} - {/if} - {if $product['auto_add_to_cart'] && $product['price_addition_type'] == Product::PRICE_ADDITION_TYPE_WITH_ROOM} - {l s='Auto added'} - {/if} - - {if $product.allow_multiple_quantity} -
- -

- {l s='Maximum allowed quantity: %s' sprintf=$product.max_quantity} -

-
- {else} - -- - {/if} -
- {if ($product.show_price && !isset($restricted_country_mode)) || isset($groups)} -
- - {$cartCurrency->sign} - - - {if Product::PRICE_CALCULATION_METHOD_PER_DAY == $product['price_calculation_method']} - {l s='/ night'} - {/if} -
- {/if} -
+ + + + + + + - {/foreach} - -
{l s='Name'}{l s='Quantity'}{l s='Unit Price (tax excl.)'}
+ + + {foreach $serviceProducts as $product} + + + + + + + +

{$product['name']|escape:'html':'UTF-8'}

+ + + {if $product['auto_add_to_cart'] && $product['price_addition_type'] == Product::PRICE_ADDITION_TYPE_INDEPENDENT} + {l s='Convenience fee'} + {/if} + {if $product['auto_add_to_cart'] && $product['price_addition_type'] == Product::PRICE_ADDITION_TYPE_WITH_ROOM} + {l s='Auto added'} + {/if} + + + {if $product.allow_multiple_quantity} +
+ + +

+ {l s='Maximum allowed quantity: %s' sprintf=$product.max_quantity} +

+
+ {else} + -- + {/if} + + + {if ($product.show_price && !isset($restricted_country_mode)) || isset($groups)} +
+ + {$cartCurrency->sign} + + + {if Product::PRICE_CALCULATION_METHOD_PER_DAY == $product['price_calculation_method']} + {l s='/ night'} + {/if} +
+ {/if} + + + {/foreach} + + + + + {/if}
diff --git a/controllers/admin/AdminCartsController.php b/controllers/admin/AdminCartsController.php index efae86aea..c28b82020 100644 --- a/controllers/admin/AdminCartsController.php +++ b/controllers/admin/AdminCartsController.php @@ -1110,7 +1110,7 @@ class AdminCartsControllerCore extends AdminController if (Configuration::get('PS_ALLOW_ADD_ALL_SERVICES_IN_BOOKING')) { // get all services $objProduct = new Product(); - $serviceProducts = $objProduct->getServiceProducts(); + $serviceProducts = $objProduct->getServiceProducts(true); } else { $objRoomTypeServiceProduct = new RoomTypeServiceProduct(); $serviceProducts = $objRoomTypeServiceProduct->getServiceProductsData($idProduct, 1, 0, false, 2, null); @@ -1256,7 +1256,7 @@ class AdminCartsControllerCore extends AdminController { $response = array('hasError' => false); // Check tab access is allowed to edit - if ($this->tabAccess['edit'] == 1) { + if ($this->tabAccess['add'] == 1) { if (Configuration::get('PS_ALLOW_CREATE_CUSTOM_SERVICES_IN_BOOKING')) { $idHotelCartBooking = Tools::getValue('id_hotel_cart_booking'); // valiadate services being added @@ -1330,7 +1330,7 @@ class AdminCartsControllerCore extends AdminController $objServiceProduct->show_at_front = false; $objServiceProduct->indexed = false; $objServiceProduct->minimal_quantity = 1; - $objServiceProduct->active = false; + $objServiceProduct->active = true; $objServiceProduct->max_quantity = 99999; // ======= Create Service product END ========= @@ -1392,7 +1392,7 @@ class AdminCartsControllerCore extends AdminController $response['errors'][] = $this->l('You do not have permission to edit this order.'); } - die(json_encode($response)); + $this->ajaxDie(json_encode($response)); } public function getBookingServicesTabContent($idHotelCartBooking) @@ -1402,7 +1402,7 @@ class AdminCartsControllerCore extends AdminController if (Configuration::get('PS_ALLOW_ADD_ALL_SERVICES_IN_BOOKING')) { // get all services $objProduct = new Product(); - $serviceProducts = $objProduct->getServiceProducts(); + $serviceProducts = $objProduct->getServiceProducts(true); } else { $objRoomTypeServiceProduct = new RoomTypeServiceProduct(); $serviceProducts = $objRoomTypeServiceProduct->getServiceProductsData($objCartBookingData->id_product, 1, 0, false, 2, null); @@ -1455,111 +1455,127 @@ class AdminCartsControllerCore extends AdminController return $servicesTpl; } - - public function ajaxProcessUpdateServiceProduct() + // Process to update selected services + public function ajaxProcessUpdateSelectedRoomServices() { - $operator = Tools::getValue('operator', 'up'); - $idServiceProduct = Tools::getValue('id_product'); - $idCartBooking = Tools::getValue('id_cart_booking'); - $qty = Tools::getValue('qty'); - $price = Tools::getValue('price'); + $response = array('hasError' => false); + // Check tab access is allowed to edit + if ($this->tabAccess['add'] == 1) { + $idCartBooking = Tools::getValue('id_hotel_cart_booking'); + if (Validate::isLoadedObject($objHotelCartBookingData = new HotelCartBookingData($idCartBooking))) { + $objCart = new Cart($objHotelCartBookingData->id_cart); + if ($selectedServiceProducts = Tools::getValue('selected_service_product')) { + $serviceQuantities = Tools::getValue('service_qty'); + $serviceUnitPrices = Tools::getValue('service_price'); + // Validate selected services + foreach ($selectedServiceProducts as $idServiceProduct => $selected) { + if (Validate::isLoadedObject($objProduct = new Product($idServiceProduct, false, $objCart->id_lang))) { + if ($objProduct->allow_multiple_quantity) { + if (!isset($serviceQuantities[$idServiceProduct])) { + $response['hasError'] = true; + $response['errors'] = $this->l('Quantity not provided for service').': '.$objProduct->name; + } else if (!Validate::isUnsignedInt($serviceQuantities[$idServiceProduct])) { + $response['hasError'] = true; + $response['errors'] = $this->l('Invalid quantity provided for service').': '.$objProduct->name; + } + } elseif ($serviceQuantities[$idServiceProduct] > 1) { + $response['hasError'] = true; + $response['errors'] = $this->l('Can not order multiple quanitity for service').': '.$objProduct->name; + } - if (Validate::isLoadedObject($objHotelCartBookingData = new HotelCartBookingData($idCartBooking))) { - $objRoomTypeServiceProductCartDetail = new RoomTypeServiceProductCartDetail(); + if (!$objProduct->allow_multiple_quantity) { + $serviceQuantities[$idServiceProduct] = 1; + } - if ($operator == 'up') { - // validate quanitity - if (Validate::isLoadedObject($objProduct = new Product($idServiceProduct))) { - if ($objProduct->allow_multiple_quantity) { - if (!Validate::isUnsignedInt($qty)) { - $this->errors[] = Tools::displayError('The quantity you\'ve entered is invalid.'); - // } elseif ($objProduct->max_quantity && $qty > $objProduct->max_quantity) { - // $this->errors[] = Tools::displayError(sprintf('cannot add more than %d quantity.', $objProduct->max_quantity)); + if (!isset($serviceUnitPrices[$idServiceProduct]) + || !ValidateCore::isPrice($serviceUnitPrices[$idServiceProduct]) + ) { + $response['hasError'] = true; + $response['errors'] = $this->l('Invalid unit price for service').': '.$objProduct->name; + } + + } else { + die($idServiceProduct); + $response['errors'] = $this->l('Some services not found. please try after refreshing the page'); } - } else { - $qty = 1; } - // validate price - if (!ValidateCore::isPrice($price)) { - $this->errors[] = Tools::displayError('Invalid service unit price.'); - } - } else { - $this->errors[] = Tools::displayError('This Service is not available.'); - } - } - if (empty($this->errors)) { - if ($objRoomTypeServiceProductCartDetail->updateCartServiceProduct( - $idCartBooking, - $idServiceProduct, - $qty, - $objHotelCartBookingData->id_cart, - $operator - )) { - $objCart = new Cart($objHotelCartBookingData->id_cart); - $originalPrice = Product::getPriceStatic($idServiceProduct, false); - // if price is different than the original service price then update the price - if ($operator == 'up' && $originalPrice != $price) { - if ($specificPriceInfo = SpecificPrice::getSpecificPrice( - (int)$idServiceProduct, - 0, - $objCart->id_currency, - 0, - 0, - 1, - 0, - $objCart->id_customer, - $objCart->id - )) { - $objSpecificPrice = new SpecificPrice($specificPriceInfo['id_specific_price']); - } else { - $objSpecificPrice = new SpecificPrice(); + if (!$response['hasError']) { + $result = true; + $objRoomTypeServiceProductCart = new RoomTypeServiceProductCartDetail(); + foreach ($selectedServiceProducts as $idServiceProduct => $selected) { + if ($selected) { + $operator = 'up'; + } else { + $operator = 'down'; + } + $quantity = $serviceQuantities[$idServiceProduct]; + $unitPrice = $serviceUnitPrices[$idServiceProduct]; + if ($result &= $objRoomTypeServiceProductCart->updateCartServiceProduct( + $idCartBooking, + $idServiceProduct, + $quantity, + $objHotelCartBookingData->id_cart, + $operator, + )) { + $originalPrice = Product::getPriceStatic($idServiceProduct, false); + // if price is different than the original service price then update the price + if ($operator == 'up' && $originalPrice != $price) { + if ($specificPriceInfo = SpecificPrice::getSpecificPrice( + (int)$idServiceProduct, + 0, + $objCart->id_currency, + 0, + 0, + 1, + 0, + $objCart->id_customer, + $objCart->id + )) { + $objSpecificPrice = new SpecificPrice($specificPriceInfo['id_specific_price']); + } else { + $objSpecificPrice = new SpecificPrice(); + } + $objSpecificPrice->id_shop = 0; + $objSpecificPrice->id_shop_group = 0; + $objSpecificPrice->id_cart = $objCart->id; + $objSpecificPrice->id_currency = $objCart->id_currency; + $objSpecificPrice->id_country = 0; + $objSpecificPrice->id_group = 0; + $objSpecificPrice->id_customer = $objCart->id_customer; + $objSpecificPrice->id_product = $idServiceProduct; + $objSpecificPrice->id_product_attribute = 0; + $objSpecificPrice->price = $unitPrice; + $objSpecificPrice->from_quantity = 1; + $objSpecificPrice->reduction = 0; + $objSpecificPrice->reduction_type = 'amount'; + $objSpecificPrice->reduction_tax = 0; + $objSpecificPrice->from = '0000-00-00 00:00:00'; + $objSpecificPrice->to = '0000-00-00 00:00:00'; + + $result &= $objSpecificPrice->save(); + } + } } - $objSpecificPrice->id_shop = 0; - $objSpecificPrice->id_shop_group = 0; - $objSpecificPrice->id_cart = $objCart->id; - $objSpecificPrice->id_currency = $objCart->id_currency; - $objSpecificPrice->id_country = 0; - $objSpecificPrice->id_group = 0; - $objSpecificPrice->id_customer = $objCart->id_customer; - $objSpecificPrice->id_product = $idServiceProduct; - $objSpecificPrice->id_product_attribute = 0; - $objSpecificPrice->price = $price; - $objSpecificPrice->from_quantity = 1; - $objSpecificPrice->reduction = 0; - $objSpecificPrice->reduction_type = 'amount'; - $objSpecificPrice->reduction_tax = 0; - $objSpecificPrice->from = '0000-00-00 00:00:00'; - $objSpecificPrice->to = '0000-00-00 00:00:00'; - if ($objSpecificPrice->save()) { - $this->ajaxDie(json_encode(array( - 'hasError' => false - ))); - } else { - $this->errors[] = Tools::displayError('Some error has been occurred. Please try reloading the page.'); - } - } else { - // delete specific price if price is same as original price - if (SpecificPrice::deleteByIdCart($objCart->id, $idServiceProduct)) { - $this->ajaxDie(json_encode(array( - 'hasError' => false, - 'down' => 1, - ))); - } else { - $this->errors[] = Tools::displayError('Unable to update services. Please try reloading the page.'); + + if (!$result) { + $response['hasError'] = true; + $response['errors'] = $this->l('Error while updating services, please try again after refresing the page'); } } } else { - $this->errors[] = Tools::displayError('Unable to update services. Please try reloading the page.'); + $response['hasError'] = true; + $response['errors'] = $this->l('No service found'); } - + } else { + $response['hasError'] = true; + $response['errors'] = $this->l('Room info not found'); } } else { - $this->errors[] = Tools::displayError('Room not found. Please try reloading the page.'); + $response['hasError'] = true; + $response['errors'] = $this->l('You do not have permission to edit this order.'); } - $this->ajaxDie(json_encode(array( - 'hasError' => true, - 'errors' => $this->errors - ))); + + $this->ajaxDie(json_encode($response)); } } diff --git a/controllers/admin/AdminOrdersController.php b/controllers/admin/AdminOrdersController.php index 39f2ba0d6..91b298167 100644 --- a/controllers/admin/AdminOrdersController.php +++ b/controllers/admin/AdminOrdersController.php @@ -388,7 +388,10 @@ class AdminOrdersControllerCore extends AdminController // validate cart for removing invalid data from cart before new order creation // remove not linked services with room types from cart if not allowed to book them - $this->errors = array_merge($this->errors, HotelCartBookingData::validateCartBookings(!Configuration::get('PS_ALLOW_ADD_ALL_SERVICES_IN_BOOKING'))); + $this->errors = array_merge( + $this->errors, + HotelCartBookingData::validateCartBookings(!Configuration::get('PS_ALLOW_ADD_ALL_SERVICES_IN_BOOKING')) + ); $cart_detail_data = array(); $cart_detail_data_obj = new HotelCartBookingData(); @@ -953,7 +956,7 @@ class AdminOrdersControllerCore extends AdminController if (Configuration::get('PS_ALLOW_ADD_ALL_SERVICES_IN_BOOKING')) { // get all services $objProduct = new Product(); - $serviceProducts = $objProduct->getServiceProducts(); + $serviceProducts = $objProduct->getServiceProducts(true); } else { $objRoomTypeServiceProduct = new RoomTypeServiceProduct(); $serviceProducts = $objRoomTypeServiceProduct->getServiceProductsData($idProduct, 1, 0, false, 2, null); @@ -1700,7 +1703,7 @@ class AdminOrdersControllerCore extends AdminController $this->context->cart = $objCart; $this->context->currency = new Currency((int)$objCart->id_currency); - $this->errors = HotelCartBookingData::validateCartBookings(); + $this->errors = HotelCartBookingData::validateCartBookings(!Configuration::get('PS_ALLOW_ADD_ALL_SERVICES_IN_BOOKING')); $orderTotal = $objCart->getOrderTotal(true, Cart::BOTH); if ($objCart->is_advance_payment) { $advancePaymentAmount = $objCart->getOrderTotal(true, Cart::ADVANCE_PAYMENT); @@ -6297,7 +6300,7 @@ class AdminOrdersControllerCore extends AdminController if (Configuration::get('PS_ALLOW_ADD_ALL_SERVICES_IN_BOOKING')) { // get all services $objProduct = new Product(); - $serviceProducts = $objProduct->getServiceProducts(); + $serviceProducts = $objProduct->getServiceProducts(true); } else { $objRoomTypeServiceProduct = new RoomTypeServiceProduct(); $serviceProducts = $objRoomTypeServiceProduct->getServiceProductsData($idProduct, 1, 0, false, 2, null); @@ -6549,7 +6552,7 @@ class AdminOrdersControllerCore extends AdminController $response['errors'] = $this->l('You do not have permission to edit this order.'); } - die(json_encode($response)); + $this->ajaxDie(json_encode($response)); } public function ajaxProcessAddExistingRoomServices() @@ -6785,7 +6788,7 @@ class AdminOrdersControllerCore extends AdminController $response['errors'][] = $this->l('You do not have permission to edit this order.'); } - die(json_encode($response)); + $this->ajaxDie(json_encode($response)); } // Add new custom services to room @@ -7103,7 +7106,7 @@ class AdminOrdersControllerCore extends AdminController $response['errors'][] = $this->l('You do not have permission to edit this order.'); } - die(json_encode($response)); + $this->ajaxDie(json_encode($response)); } @@ -7187,7 +7190,7 @@ class AdminOrdersControllerCore extends AdminController $response['errors'] = $this->l('You do not have permission to edit this order.'); } - die(json_encode($response)); + $this->ajaxDie(json_encode($response)); } // Process when admin changes extra demands of any room while order creation process form.tpl diff --git a/modules/hotelreservationsystem/classes/RoomTypeServiceProductCartDetail.php b/modules/hotelreservationsystem/classes/RoomTypeServiceProductCartDetail.php index 59f03b65b..1449888ef 100644 --- a/modules/hotelreservationsystem/classes/RoomTypeServiceProductCartDetail.php +++ b/modules/hotelreservationsystem/classes/RoomTypeServiceProductCartDetail.php @@ -490,6 +490,7 @@ class RoomTypeServiceProductCartDetail extends ObjectModel if ($getTotalPrice) { return $totalPrice; } + return $selectedServiceProducts; }