Skip to content

Commit

Permalink
Add new setting for limiting new public bookings in the future (#1203).
Browse files Browse the repository at this point in the history
  • Loading branch information
alextselegidis committed Jun 27, 2022
1 parent bc80d2c commit 3d8d5af
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 21 deletions.
57 changes: 44 additions & 13 deletions application/libraries/Availability.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Availability library.
*
* Handles availability related functionality.
*
*
* @package Libraries
*/
class Availability {
Expand Down Expand Up @@ -68,7 +68,9 @@ public function get_available_hours(string $date, array $service, array $provide
$available_hours = $this->generate_available_hours($date, $service, $available_periods);
}

return $this->consider_book_advance_timeout($date, $available_hours, $provider);
$available_hours = $this->consider_book_advance_timeout($date, $available_hours, $provider);

return $this->consider_future_booking_limit($date, $available_hours, $provider);
}

/**
Expand All @@ -93,22 +95,22 @@ protected function get_available_periods(string $date, array $provider, int $exc

// Get the provider's working plan exceptions.
$working_plan_exceptions_json = $provider['settings']['working_plan_exceptions'];

$working_plan_exceptions = $working_plan_exceptions_json ? json_decode($provider['settings']['working_plan_exceptions'], TRUE) : NULL;

$escaped_provider_id = $this->CI->db->escape($provider['id']);
$escaped_date = $this->CI->db->escape($date);
$where = 'id_users_provider = ' . $escaped_provider_id

$escaped_date = $this->CI->db->escape($date);

$where = 'id_users_provider = ' . $escaped_provider_id
. ' AND DATE(start_datetime) <= ' . $escaped_date . ' AND DATE(end_datetime) >= ' . $escaped_date;

// Sometimes it might be necessary to exclude an appointment from the calculation (e.g. when editing an
// existing appointment).
if ($exclude_appointment_id)
{
$escaped_exclude_appointment_id = $this->CI->db->escape($exclude_appointment_id);
$where .= ' AND id != ' . $escaped_exclude_appointment_id;
$where .= ' AND id != ' . $escaped_exclude_appointment_id;
}

$appointments = array_values(
Expand Down Expand Up @@ -304,8 +306,8 @@ protected function get_available_periods(string $date, array $provider, int $exc
*/
protected function generate_available_hours(
string $date,
array $service,
array $empty_periods
array $service,
array $empty_periods
): array
{
$available_hours = [];
Expand Down Expand Up @@ -351,9 +353,9 @@ protected function generate_available_hours(
*/
protected function consider_multiple_attendants(
string $date,
array $service,
array $provider,
int $exclude_appointment_id = NULL
array $service,
array $provider,
int $exclude_appointment_id = NULL
): array
{
$unavailability_events = $this->CI->appointments_model->get([
Expand Down Expand Up @@ -610,4 +612,33 @@ protected function consider_book_advance_timeout(string $date, array $available_

return array_values($available_hours);
}

/**
* Remove times if succeed the future booking limit.
*
* @param string $selected_date
* @param array $available_hours
* @param array $provider
*
* @return array|mixed
*
* @throws Exception
*/
protected function consider_future_booking_limit(string $selected_date, array $available_hours, array $provider): array
{
$provider_timezone = new DateTimeZone($provider['timezone']);

$future_booking_limit = setting('future_booking_limit'); // in days

$threshold = new DateTime('+' . $future_booking_limit . ' days', $provider_timezone);

$selected_date_time = new DateTime($selected_date);

if ($threshold < $selected_date_time)
{
return [];
}

return $threshold > $selected_date_time ? $available_hours : [];
}
}
47 changes: 47 additions & 0 deletions application/migrations/042_add_future_booking_limit_setting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');

/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
* @link http://easyappointments.org
* @since v1.4.0
* ---------------------------------------------------------------------------- */

/**
* @property CI_DB_query_builder $db
* @property CI_DB_forge $dbforge
*/
class Migration_Add_future_booking_limit_setting extends CI_Migration {
/**
* Upgrade method.
*
* @throws Exception
*/
public function up()
{
if ( ! $this->db->get_where('settings', ['name' => 'future_booking_limit'])->num_rows())
{
$this->db->insert('settings', [
'name' => 'future_booking_limit',
'value' => '90' // days
]);
}
}

/**
* Downgrade method.
*
* @throws Exception
*/
public function down()
{
if ($this->db->get_where('settings', ['name' => 'future_booking_limit'])->num_rows())
{
$this->db->delete('settings', ['name' => 'future_booking_limit']);
}
}
}
29 changes: 22 additions & 7 deletions application/views/pages/business_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@

<table class="working-plan table table-striped">
<thead>
<tr>
<th><?= lang('day') ?></th>
<th><?= lang('start') ?></th>
<th><?= lang('end') ?></th>
</tr>
<tr>
<th><?= lang('day') ?></th>
<th><?= lang('start') ?></th>
<th><?= lang('end') ?></th>
</tr>
</thead>
<tbody><!-- Dynamic Content --></tbody>
</table>
Expand Down Expand Up @@ -69,9 +69,9 @@
</thead>
<tbody><!-- Dynamic Content --></tbody>
</table>

<h4><?= lang('book_advance_timeout') ?></h4>

<div class="mb-3">
<label for="book-advance-timeout" class="form-label">
<?= lang('timeout_minutes') ?>
Expand All @@ -84,6 +84,21 @@
</small>
</div>
</div>

<h4><?= lang('future_booking_limit') ?></h4>

<div class="mb-3">
<label for="future-booking-limit" class="form-label">
<?= lang('limit_days') ?>
</label>
<input id="future-booking-limit" data-field="future_booking_limit" class="form-control"
type="number" min="15">
<div class="form-text text-muted">
<small>
<?= lang('future_booking_limit_hint') ?>
</small>
</div>
</div>
</fieldset>
</form>
</div>
Expand Down
3 changes: 2 additions & 1 deletion assets/css/layouts/backend_layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,8 @@ body .form-horizontal .controls {
cursor: pointer;
}

#business-logic #book-advance-timeout {
#business-logic #book-advance-timeout,
#business-logic #future-booking-limit {
width: 100px;
}

Expand Down

0 comments on commit 3d8d5af

Please sign in to comment.