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: Prevent room deletion and room status update to inactive when room has future booking #841

Merged
merged 5 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@
</td>
<td class="col-sm-1 center">
{if isset($room_info['id'])}
<input type="hidden" class="booked-dates" name="{$var_name_room_info|cat:'[booked_dates]'}" value="{$room_info['booked_dates']|escape:'html':'UTF-8'}">
<a href="#" class="view_htl_room btn btn-default" data-toggle="modal" data-target="#room-dates-modal" data-id-room="{$room_info['id']}"><i class="icon-info"></i></a>
<a href="#" class="rm_htl_room btn btn-default" data-id-htl-info="{$room_info['id']}"><i class="icon-trash"></i></a>
<input type="hidden" name="{$var_name_room_info|cat:'[id]'}" value="{$room_info['id']}">
{else}
Expand Down Expand Up @@ -224,6 +226,36 @@
</div>
{*END*}

<div class="modal fade" id="room-dates-modal" tabindex="-1" role="dialog" aria-labelledby="">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close margin-right-10" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<h4 class="modal-title"><i class="icon-calendar"></i>&nbsp; {l s='Upcoming bookings'}</h4>
</div>
<div class="room-booked-dates-table modal-body">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th><span>{l s='Order'}</span></th>
<th><span>{l s='Date From'}</span></th>
<th><span>{l s='Date To'}</span></th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" aria-label="Close">{l s='Done'}</button>
</div>
</div>
</div>
</div>

<style>
.deactiveDatesModal {
cursor: pointer;
Expand Down Expand Up @@ -400,6 +432,30 @@
});
});

{literal}
$('#room-dates-modal').on('show.bs.modal', function(e) {
const triggerRoom = $(e.relatedTarget);
$('#room-dates-modal tbody').html('');
var bookedDates = JSON.parse($(triggerRoom).closest('tr').find('.booked-dates').val());
if (bookedDates.length) {
if (bookedDates.length) {
$('#room-dates-modal .room-booked-dates-table').show();
$(bookedDates).each(function() {
$('#room-dates-modal .room-booked-dates-table tbody').append(`<tr>
<td><a href="{/literal}{$link->getAdminLink('AdminOrders')|escape:'html':'UTF-8'}{literal}&vieworder&id_order=${this.id_order}" target="_blank">#${this.id_order}</a></td>
<td>${this.date_from_formatted}</td>
<td>${this.date_to_formatted}</td>
</tr>`);
});
}
} else {
$('#room-dates-modal .room-booked-dates-table tbody').append(`<tr>
<td colspan="3" class="center">{/literal}{l s='No Booking for this room'}{literal}</td>
</tr>`);
}
});
{/literal}

// Add new room detail
$('#add-more-rooms-button').on('click',function() {
var lengthRooms = parseInt($('.room_data_values').length);
Expand Down Expand Up @@ -444,18 +500,19 @@
$.ajax({
url: prod_link,
type: 'POST',
dataType: 'text',
dataType: 'JSON',
data: {
ajax:true,
action:'deleteHotelRoom',
id: id_htl_info,
},
success: function (result) {
if (parseInt(result) == 1) {
success: function (response) {
if (response.success) {
showSuccessMessage("{l s='Removed successfully'}");
$current.closest(".room_data_values").remove();
} else {
showErrorMessage("{l s='Some error occurred'}");
if (response.errors)
showErrorMessage(response.errors);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
Expand Down
36 changes: 31 additions & 5 deletions controllers/admin/AdminProductsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2763,6 +2763,13 @@ public function initFormConfiguration($obj)
$hotelRoomInfo = $objRoomInfo->getHotelRoomInfo($obj->id, $hotelRoomType['id_hotel']);
if ($hotelRoomInfo) {
foreach ($hotelRoomInfo as &$room) {
$bookedDates = $objRoomInfo->getFutureBookings($room['id']);
foreach($bookedDates as &$bookedDate) {
$bookedDate['date_from_formatted'] = Tools::displayDate($bookedDate['date_from']);
$bookedDate['date_to_formatted'] = Tools::displayDate($bookedDate['date_to']);
}
$room['booked_dates'] = json_encode($bookedDates);

if ($room['id_status'] == HotelRoomInformation::STATUS_TEMPORARY_INACTIVE) {
$disabledDates = $objRoomDisableDates->getRoomDisableDates($room['id']);
$room['disable_dates_json'] = json_encode($disabledDates);
Expand Down Expand Up @@ -3243,10 +3250,15 @@ public function validateConfigurationPostData()
$this->errors[] = sprintf(Tools::displayError('Invalid floor for room %s.'), $roomIndex);
}

if ($roomInfo['id_status'] == HotelRoomInformation::STATUS_TEMPORARY_INACTIVE) {
if ($roomInfo['id_status'] == HotelRoomInformation::STATUS_INACTIVE) {
$objHotelRoomInformation = new HotelRoomInformation();
if (count($objHotelRoomInformation->getFutureBookings($roomInfo['id']))) {
$this->errors[] = sprintf(Tools::displayError('Cannot change room %s status to inactive as it already has some bookings, Please check the bookings and move those bookings to another room if you want make this room inactive'), $roomInfo['room_num']);
}
} elseif ($roomInfo['id_status'] == HotelRoomInformation::STATUS_TEMPORARY_INACTIVE) {
$disableDates = json_decode($roomInfo['disable_dates_json'], true);
if ($roomInfo['disable_dates_json'] !== 0) {
$this->validateDisableDateRanges($disableDates, $roomIndex, $roomInfo['id']);
$this->validateDisableDateRanges($disableDates, $roomInfo['room_num'], $roomInfo['id']);
}
}
}
Expand All @@ -3257,14 +3269,28 @@ public function validateConfigurationPostData()

public function ajaxProcessDeleteHotelRoom()
{
$response = array(
'success' => false
);
if ($this->tabAccess['edit'] == 1) {
$idRoom = Tools::getValue('id');
$objRoomInfo = new HotelRoomInformation((int)$idRoom);
if ($objRoomInfo->delete()) {
die('1');
$objHotelRoomInformation = new HotelRoomInformation();
if ($objHotelRoomInformation->getFutureBookings($idRoom)) {
$this->errors[] = $this->l('This room cannot be deleted as this room contains future booking.');
}
if (empty($this->errors)) {
if ($objRoomInfo->delete()) {
$response['success'] = true;
} else {
$this->errors[] = $this->l('Unable to delete room. Please try again!.');
}
}
}
die('0');
if (!empty($this->errors)) {
$response['errors'] = $this->errors;
}
die(json_encode($response));
}

public function initFormAdditionalFacilities($obj)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,12 @@ public function getRoomTypeDisabledRoomsForDateRange($id_hotel, $id_product, $da

public function getRoomTypeBookedRoomsForDateRange($id_hotel, $id_product, $date_from, $date_to)
{
return Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'htl_booking_detail` where `date_from`< \''.pSQL($date_to).'\' AND `date_to`>\''.$date_from.'\' AND `id_product`='.(int) $id_product.' AND `id_hotel`='.(int) $id_hotel);
return Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'htl_booking_detail` where `date_from`< \''.pSQL($date_to).'\' AND `date_to`>\''.pSQL($date_from).'\' AND `id_product`='.(int) $id_product.' AND `id_hotel`='.(int) $id_hotel);
}

public function getFutureBookings($idRoom)
{
return Db::getInstance()->executeS('SELECT `id`, `id_order`, `date_from`, `date_to` FROM `'._DB_PREFIX_.'htl_booking_detail` where `date_to` > \''.pSQL(date('Y-m-d')).'\' AND `is_refunded` = 0 AND `id_room`='.(int) $idRoom);
}

// Webservice :: webservice add function
Expand Down