From 3381b998a14cc2c05054065119007ae742110e71 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Fri, 11 Dec 2020 12:44:29 +0200 Subject: [PATCH] Check for other services when calculating multiple attendants number availability (#948). --- application/libraries/Availability.php | 17 ++++++++++ application/models/Appointments_model.php | 41 ++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/application/libraries/Availability.php b/application/libraries/Availability.php index 883e91db..5cfe0786 100644 --- a/application/libraries/Availability.php +++ b/application/libraries/Availability.php @@ -388,11 +388,28 @@ class Availability { while ($slot_end <= $period['end']) { + // Make sure there is no other service appointment for this time slot. + $other_service_attendants_number = $this->CI->appointments_model->get_other_service_attendants_number( + $slot_start, + $slot_end, + $service['id'], + $provider['id'], + $exclude_appointment_id + ); + + if ($other_service_attendants_number > 0) + { + $slot_start->add($interval); + $slot_end->add($interval); + continue; + } + // Check reserved attendants for this time slot and see if current attendants fit. $appointment_attendants_number = $this->CI->appointments_model->get_attendants_number_for_period( $slot_start, $slot_end, $service['id'], + $provider['id'], $exclude_appointment_id ); diff --git a/application/models/Appointments_model.php b/application/models/Appointments_model.php index 9defb2f4..25973d56 100644 --- a/application/models/Appointments_model.php +++ b/application/models/Appointments_model.php @@ -556,11 +556,12 @@ class Appointments_model extends EA_Model { * @param DateTime $slot_start When the slot starts * @param DateTime $slot_end When the slot ends. * @param int $service_id Selected service ID. + * @param int $provider_id Selected provider ID. * @param int|null $exclude_appointment_id Exclude an appointment from the availability generation. * * @return int Returns the number of attendants for selected time period. */ - public function get_attendants_number_for_period(DateTime $slot_start, DateTime $slot_end, $service_id, $exclude_appointment_id = NULL) + public function get_attendants_number_for_period(DateTime $slot_start, DateTime $slot_end, $service_id, $provider_id, $exclude_appointment_id = NULL) { if ($exclude_appointment_id) { @@ -581,6 +582,44 @@ class Appointments_model extends EA_Model { ->group_end() ->group_end() ->where('id_services', $service_id) + ->where('id_users_provider', $provider_id) + ->get() + ->row() + ->attendants_number; + } + + /** + * Returns the number of the other service attendants number for the provided time slot. + * + * @param DateTime $slot_start When the slot starts + * @param DateTime $slot_end When the slot ends. + * @param int $service_id Selected service ID. + * @param int|null $exclude_appointment_id Exclude an appointment from the availability generation. + * + * @return int Returns the number of attendants for selected time period. + */ + public function get_other_service_attendants_number(DateTime $slot_start, DateTime $slot_end, $service_id, $provider_id, $exclude_appointment_id = NULL) + { + if ($exclude_appointment_id) + { + $this->db->where('id !=', $exclude_appointment_id); + } + + return (int)$this->db + ->select('count(*) AS attendants_number') + ->from('appointments') + ->group_start() + ->group_start() + ->where('start_datetime <=', $slot_start->format('Y-m-d H:i:s')) + ->where('end_datetime >', $slot_start->format('Y-m-d H:i:s')) + ->group_end() + ->or_group_start() + ->where('start_datetime <', $slot_end->format('Y-m-d H:i:s')) + ->where('end_datetime >=', $slot_end->format('Y-m-d H:i:s')) + ->group_end() + ->group_end() + ->where('id_services !=', $service_id) + ->where('id_users_provider', $provider_id) ->get() ->row() ->attendants_number;