2020-04-22 22:48:56 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
2016-01-09 23:29:28 +02:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2020-03-11 12:10:59 +03:00
|
|
|
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
|
2016-01-09 23:29:28 +02:00
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link http://easyappointments.org
|
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appointments Model
|
|
|
|
*
|
2020-04-22 22:48:56 +03:00
|
|
|
* @property CI_DB_query_builder db
|
|
|
|
* @property CI_Loader load
|
|
|
|
*
|
2016-01-09 23:29:28 +02:00
|
|
|
* @package Models
|
|
|
|
*/
|
|
|
|
class Appointments_Model extends CI_Model {
|
|
|
|
/**
|
|
|
|
* Add an appointment record to the database.
|
|
|
|
*
|
2017-09-08 15:03:45 +03:00
|
|
|
* This method adds a new appointment to the database. If the appointment doesn't exists it is going to be inserted,
|
|
|
|
* otherwise the record is going to be updated.
|
|
|
|
*
|
|
|
|
* @param array $appointment Associative array with the appointment data. Each key has the same name with the
|
|
|
|
* database fields.
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
|
|
|
* @return int Returns the appointments id.
|
2020-04-22 22:48:56 +03:00
|
|
|
* @throws Exception
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function add($appointment)
|
|
|
|
{
|
2016-01-09 23:29:28 +02:00
|
|
|
// Validate the appointment data before doing anything.
|
|
|
|
$this->validate($appointment);
|
|
|
|
|
|
|
|
// Perform insert() or update() operation.
|
2017-09-15 14:36:37 +03:00
|
|
|
if ( ! isset($appointment['id']))
|
|
|
|
{
|
2016-04-27 09:21:40 +03:00
|
|
|
$appointment['id'] = $this->_insert($appointment);
|
2018-01-23 12:08:37 +03:00
|
|
|
}
|
|
|
|
else
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2016-04-27 09:21:40 +03:00
|
|
|
$this->_update($appointment);
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $appointment['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-22 22:48:56 +03:00
|
|
|
* Validate appointment data before the insert or update operations are executed.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2020-04-22 22:48:56 +03:00
|
|
|
* @param array $appointment Contains the appointment data.
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
2020-04-22 22:48:56 +03:00
|
|
|
* @return bool Returns the validation result.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2020-04-22 22:48:56 +03:00
|
|
|
* @throws Exception If appointment validation fails.
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2020-04-22 22:48:56 +03:00
|
|
|
public function validate($appointment)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-04-22 22:48:56 +03:00
|
|
|
$this->load->helper('data_validation');
|
|
|
|
|
|
|
|
// If a appointment id is given, check whether the record exists
|
|
|
|
// in the database.
|
|
|
|
if (isset($appointment['id']))
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-04-22 22:48:56 +03:00
|
|
|
$num_rows = $this->db->get_where('ea_appointments',
|
|
|
|
['id' => $appointment['id']])->num_rows();
|
|
|
|
if ($num_rows == 0)
|
|
|
|
{
|
|
|
|
throw new Exception('Provided appointment id does not exist in the database.');
|
|
|
|
}
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
// Check if appointment dates are valid.
|
|
|
|
if ( ! validate_mysql_datetime($appointment['start_datetime']))
|
|
|
|
{
|
|
|
|
throw new Exception('Appointment start datetime is invalid.');
|
|
|
|
}
|
2016-01-09 23:29:28 +02:00
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
if ( ! validate_mysql_datetime($appointment['end_datetime']))
|
|
|
|
{
|
|
|
|
throw new Exception('Appointment end datetime is invalid.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the provider's id is valid.
|
|
|
|
$num_rows = $this->db
|
|
|
|
->select('*')
|
|
|
|
->from('ea_users')
|
|
|
|
->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
|
|
|
|
->where('ea_users.id', $appointment['id_users_provider'])
|
|
|
|
->where('ea_roles.slug', DB_SLUG_PROVIDER)
|
|
|
|
->get()->num_rows();
|
|
|
|
if ($num_rows == 0)
|
|
|
|
{
|
|
|
|
throw new Exception('Appointment provider id is invalid.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($appointment['is_unavailable'] == FALSE)
|
|
|
|
{
|
|
|
|
// Check if the customer's id is valid.
|
|
|
|
$num_rows = $this->db
|
|
|
|
->select('*')
|
|
|
|
->from('ea_users')
|
|
|
|
->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
|
|
|
|
->where('ea_users.id', $appointment['id_users_customer'])
|
|
|
|
->where('ea_roles.slug', DB_SLUG_CUSTOMER)
|
|
|
|
->get()->num_rows();
|
|
|
|
if ($num_rows == 0)
|
|
|
|
{
|
|
|
|
throw new Exception('Appointment customer id is invalid.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the service id is valid.
|
|
|
|
$num_rows = $this->db->get_where('ea_services',
|
|
|
|
['id' => $appointment['id_services']])->num_rows();
|
|
|
|
if ($num_rows == 0)
|
|
|
|
{
|
|
|
|
throw new Exception('Appointment service id is invalid.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a new appointment record to the database.
|
|
|
|
*
|
2017-09-08 15:03:45 +03:00
|
|
|
* @param array $appointment Associative array with the appointment's data. Each key has the same name with the
|
|
|
|
* database fields.
|
|
|
|
*
|
2016-01-09 23:29:28 +02:00
|
|
|
* @return int Returns the id of the new record.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
|
|
|
* @throws Exception If appointment record could not be inserted.
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
protected function _insert($appointment)
|
|
|
|
{
|
2016-01-09 23:29:28 +02:00
|
|
|
$appointment['book_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
$appointment['hash'] = $this->generate_hash();
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ( ! $this->db->insert('ea_appointments', $appointment))
|
|
|
|
{
|
2016-01-09 23:29:28 +02:00
|
|
|
throw new Exception('Could not insert appointment record.');
|
|
|
|
}
|
|
|
|
|
2017-08-08 10:49:39 +03:00
|
|
|
return (int)$this->db->insert_id();
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
/**
|
|
|
|
* Generate a unique hash for the given appointment data.
|
|
|
|
*
|
|
|
|
* This method uses the current date-time to generate a unique hash string that is later used to identify this
|
|
|
|
* appointment. Hash is needed when the email is send to the user with an edit link.
|
|
|
|
*
|
|
|
|
* @return string Returns the unique appointment hash.
|
|
|
|
*/
|
|
|
|
public function generate_hash()
|
|
|
|
{
|
|
|
|
$current_date = new DateTime();
|
|
|
|
return md5($current_date->getTimestamp());
|
|
|
|
}
|
|
|
|
|
2016-01-09 23:29:28 +02:00
|
|
|
/**
|
|
|
|
* Update an existing appointment record in the database.
|
|
|
|
*
|
2017-09-08 15:03:45 +03:00
|
|
|
* The appointment data argument should already include the record ID in order to process the update operation.
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
2017-09-08 15:03:45 +03:00
|
|
|
* @param array $appointment Associative array with the appointment's data. Each key has the same name with the
|
|
|
|
* database fields.
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
2017-09-08 15:03:45 +03:00
|
|
|
* @throws Exception If appointment record could not be updated.
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
protected function _update($appointment)
|
|
|
|
{
|
2016-01-09 23:29:28 +02:00
|
|
|
$this->db->where('id', $appointment['id']);
|
2017-09-15 14:36:37 +03:00
|
|
|
if ( ! $this->db->update('ea_appointments', $appointment))
|
|
|
|
{
|
2016-01-09 23:29:28 +02:00
|
|
|
throw new Exception('Could not update appointment record.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
/**
|
|
|
|
* Check if a particular appointment record already exists.
|
|
|
|
*
|
|
|
|
* This method checks whether the given appointment already exists in the database. It doesn't search with the id,
|
|
|
|
* but by using the following fields: "start_datetime", "end_datetime", "id_users_provider", "id_users_customer",
|
|
|
|
* "id_services".
|
|
|
|
*
|
|
|
|
* @param array $appointment Associative array with the appointment's data. Each key has the same name with the
|
|
|
|
* database fields.
|
|
|
|
*
|
|
|
|
* @return bool Returns whether the record exists or not.
|
|
|
|
*
|
|
|
|
* @throws Exception If appointment fields are missing.
|
|
|
|
*/
|
|
|
|
public function exists($appointment)
|
|
|
|
{
|
|
|
|
if ( ! isset($appointment['start_datetime'])
|
|
|
|
|| ! isset($appointment['end_datetime'])
|
|
|
|
|| ! isset($appointment['id_users_provider'])
|
|
|
|
|| ! isset($appointment['id_users_customer'])
|
|
|
|
|| ! isset($appointment['id_services']))
|
|
|
|
{
|
|
|
|
throw new Exception('Not all appointment field values are provided: '
|
|
|
|
. print_r($appointment, TRUE));
|
|
|
|
}
|
|
|
|
|
|
|
|
$num_rows = $this->db->get_where('ea_appointments', [
|
|
|
|
'start_datetime' => $appointment['start_datetime'],
|
|
|
|
'end_datetime' => $appointment['end_datetime'],
|
|
|
|
'id_users_provider' => $appointment['id_users_provider'],
|
|
|
|
'id_users_customer' => $appointment['id_users_customer'],
|
|
|
|
'id_services' => $appointment['id_services'],
|
|
|
|
])
|
|
|
|
->num_rows();
|
|
|
|
|
|
|
|
return ($num_rows > 0) ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
|
2016-01-09 23:29:28 +02:00
|
|
|
/**
|
|
|
|
* Find the database id of an appointment record.
|
|
|
|
*
|
2017-09-08 15:03:45 +03:00
|
|
|
* The appointment data should include the following fields in order to get the unique id from the database:
|
|
|
|
* "start_datetime", "end_datetime", "id_users_provider", "id_users_customer", "id_services".
|
|
|
|
*
|
|
|
|
* IMPORTANT: The record must already exists in the database, otherwise an exception is raised.
|
|
|
|
*
|
|
|
|
* @param array $appointment Array with the appointment data. The keys of the array should have the same names as
|
|
|
|
* the db fields.
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
2017-09-08 15:03:45 +03:00
|
|
|
* @return int Returns the db id of the record that matches the appointment data.
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
2017-09-08 15:03:45 +03:00
|
|
|
* @throws Exception If appointment could not be found.
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function find_record_id($appointment)
|
|
|
|
{
|
|
|
|
$this->db->where([
|
|
|
|
'start_datetime' => $appointment['start_datetime'],
|
|
|
|
'end_datetime' => $appointment['end_datetime'],
|
2016-01-09 23:29:28 +02:00
|
|
|
'id_users_provider' => $appointment['id_users_provider'],
|
|
|
|
'id_users_customer' => $appointment['id_users_customer'],
|
2017-09-15 14:36:37 +03:00
|
|
|
'id_services' => $appointment['id_services']
|
|
|
|
]);
|
2016-01-09 23:29:28 +02:00
|
|
|
|
|
|
|
$result = $this->db->get('ea_appointments');
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($result->num_rows() == 0)
|
|
|
|
{
|
2016-01-09 23:29:28 +02:00
|
|
|
throw new Exception('Could not find appointment record id.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result->row()->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an existing appointment record from the database.
|
|
|
|
*
|
2017-09-08 15:03:45 +03:00
|
|
|
* @param int $appointment_id The record id to be deleted.
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
|
|
|
* @return bool Returns the delete operation result.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
|
|
|
* @throws Exception If $appointment_id argument is invalid.
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function delete($appointment_id)
|
|
|
|
{
|
|
|
|
if ( ! is_numeric($appointment_id))
|
|
|
|
{
|
2016-01-09 23:29:28 +02:00
|
|
|
throw new Exception('Invalid argument type $appointment_id (value:"' . $appointment_id . '")');
|
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$num_rows = $this->db->get_where('ea_appointments', ['id' => $appointment_id])->num_rows();
|
2016-01-09 23:29:28 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($num_rows == 0)
|
|
|
|
{
|
2016-01-09 23:29:28 +02:00
|
|
|
return FALSE; // Record does not exist.
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->db->where('id', $appointment_id);
|
|
|
|
return $this->db->delete('ea_appointments');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a specific row from the appointments table.
|
|
|
|
*
|
2017-09-08 15:03:45 +03:00
|
|
|
* @param int $appointment_id The record's id to be returned.
|
|
|
|
*
|
|
|
|
* @return array Returns an associative array with the selected record's data. Each key has the same name as the
|
|
|
|
* database field names.
|
|
|
|
*
|
|
|
|
* @throws Exception If $appointment_id argumnet is invalid.
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function get_row($appointment_id)
|
|
|
|
{
|
|
|
|
if ( ! is_numeric($appointment_id))
|
|
|
|
{
|
2017-09-08 15:03:45 +03:00
|
|
|
throw new Exception('Invalid argument given. Expected integer for the $appointment_id: '
|
|
|
|
. $appointment_id);
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
2017-09-08 15:03:45 +03:00
|
|
|
|
2020-03-29 15:17:56 +03:00
|
|
|
$appointment = $this->db->get_where('ea_appointments', ['id' => $appointment_id])->row_array();
|
|
|
|
|
|
|
|
$this->load->model('timezones_model');
|
|
|
|
|
|
|
|
$appointment = $this->timezones_model->convert_event_timezone($appointment);
|
|
|
|
|
|
|
|
return $appointment;
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a specific field value from the database.
|
|
|
|
*
|
|
|
|
* @param string $field_name The field name of the value to be returned.
|
2017-09-08 15:03:45 +03:00
|
|
|
* @param int $appointment_id The selected record's id.
|
|
|
|
*
|
2016-01-09 23:29:28 +02:00
|
|
|
* @return string Returns the records value from the database.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
|
|
|
* @throws Exception If $appointment_id argument is invalid.
|
|
|
|
* @throws Exception If $field_name argument is invalid.
|
|
|
|
* @throws Exception If requested appointment record was not found.
|
|
|
|
* @throws Exception If requested field name does not exist.
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function get_value($field_name, $appointment_id)
|
|
|
|
{
|
|
|
|
if ( ! is_numeric($appointment_id))
|
|
|
|
{
|
2017-09-08 15:03:45 +03:00
|
|
|
throw new Exception('Invalid argument given, expected integer for the $appointment_id: '
|
|
|
|
. $appointment_id);
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ( ! is_string($field_name))
|
|
|
|
{
|
2017-09-08 15:03:45 +03:00
|
|
|
throw new Exception('Invalid argument given, expected string for the $field_name: ' . $field_name);
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($this->db->get_where('ea_appointments', ['id' => $appointment_id])->num_rows() == 0)
|
|
|
|
{
|
2016-01-09 23:29:28 +02:00
|
|
|
throw new Exception('The record with the provided id '
|
2017-09-15 14:36:37 +03:00
|
|
|
. 'does not exist in the database: ' . $appointment_id);
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$row_data = $this->db->get_where('ea_appointments', ['id' => $appointment_id])->row_array();
|
2016-01-09 23:29:28 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ( ! isset($row_data[$field_name]))
|
|
|
|
{
|
2017-09-08 15:03:45 +03:00
|
|
|
throw new Exception('The given field name does not exist in the database: ' . $field_name);
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
2020-03-29 15:17:56 +03:00
|
|
|
$this->load->model('timezones_model');
|
|
|
|
|
|
|
|
$row_data = $this->timezones_model->convert_event_timezone($row_data);
|
|
|
|
|
2016-01-09 23:29:28 +02:00
|
|
|
return $row_data[$field_name];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all, or specific records from appointment's table.
|
|
|
|
*
|
2020-04-06 21:34:32 +03:00
|
|
|
* Example:
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
2020-04-06 21:34:32 +03:00
|
|
|
* $this->Model->getBatch('id = ' . $recordId);
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2020-04-06 21:34:32 +03:00
|
|
|
* @param mixed|null $where (OPTIONAL) The WHERE clause of the query to be executed. DO NOT INCLUDE 'WHERE'
|
|
|
|
* KEYWORD.
|
|
|
|
* @param mixed|null $order_by
|
|
|
|
* @param int|null $limit
|
|
|
|
* @param int|null $offset
|
2017-12-01 11:12:09 +03:00
|
|
|
* @param bool $aggregates (OPTIONAL) Defines whether to add aggregations or not.
|
|
|
|
*
|
2016-01-09 23:29:28 +02:00
|
|
|
* @return array Returns the rows from the database.
|
|
|
|
*/
|
2020-04-06 21:34:32 +03:00
|
|
|
public function get_batch($where = NULL, $order_by = NULL, $limit = NULL, $offset = NULL, $aggregates = FALSE)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-04-06 21:34:32 +03:00
|
|
|
if ($where !== NULL)
|
|
|
|
{
|
|
|
|
$this->db->where($where);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($order_by)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-04-06 21:34:32 +03:00
|
|
|
$this->db->order_by($order_by);
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
2020-04-06 21:34:32 +03:00
|
|
|
$appointments = $this->db->get('ea_appointments', $limit, $offset)->result_array();
|
2017-12-01 11:12:09 +03:00
|
|
|
|
2020-03-29 15:17:56 +03:00
|
|
|
$this->load->model('timezones_model');
|
|
|
|
|
|
|
|
foreach ($appointments as &$appointment)
|
2018-01-23 12:08:37 +03:00
|
|
|
{
|
2020-03-29 15:17:56 +03:00
|
|
|
$appointment = $this->timezones_model->convert_event_timezone($appointment);
|
|
|
|
|
|
|
|
if ($aggregates)
|
2018-01-23 12:08:37 +03:00
|
|
|
{
|
2017-12-01 11:12:09 +03:00
|
|
|
$appointment = $this->get_aggregates($appointment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $appointments;
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-22 22:48:56 +03:00
|
|
|
* Get the aggregates of an appointment.
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
2020-04-22 22:48:56 +03:00
|
|
|
* @param array $appointment Appointment data.
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
2020-04-22 22:48:56 +03:00
|
|
|
* @return array Returns the appointment with the aggregates.
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2020-04-22 22:48:56 +03:00
|
|
|
private function get_aggregates(array $appointment)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-04-22 22:48:56 +03:00
|
|
|
$appointment['service'] = $this->db->get_where('ea_services',
|
|
|
|
['id' => $appointment['id_services']])->row_array();
|
|
|
|
$appointment['provider'] = $this->db->get_where('ea_users',
|
|
|
|
['id' => $appointment['id_users_provider']])->row_array();
|
|
|
|
$appointment['customer'] = $this->db->get_where('ea_users',
|
|
|
|
['id' => $appointment['id_users_customer']])->row_array();
|
|
|
|
return $appointment;
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts or updates an unavailable period record in the database.
|
|
|
|
*
|
2016-10-10 19:29:48 +03:00
|
|
|
* @param array $unavailable Contains the unavailable data.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2016-01-09 23:29:28 +02:00
|
|
|
* @return int Returns the record id.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
|
|
|
* @throws Exception If unavailability validation fails.
|
|
|
|
* @throws Exception If provider record could not be found in database.
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function add_unavailable($unavailable)
|
|
|
|
{
|
2016-01-09 23:29:28 +02:00
|
|
|
// Validate period
|
|
|
|
$start = strtotime($unavailable['start_datetime']);
|
|
|
|
$end = strtotime($unavailable['end_datetime']);
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($start > $end)
|
|
|
|
{
|
2016-01-09 23:29:28 +02:00
|
|
|
throw new Exception('Unavailable period start must be prior to end.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate provider record
|
2017-09-15 14:36:37 +03:00
|
|
|
$where_clause = [
|
2016-01-09 23:29:28 +02:00
|
|
|
'id' => $unavailable['id_users_provider'],
|
2017-09-15 14:36:37 +03:00
|
|
|
'id_roles' => $this->db->get_where('ea_roles', ['slug' => DB_SLUG_PROVIDER])->row()->id
|
|
|
|
];
|
2016-01-09 23:29:28 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($this->db->get_where('ea_users', $where_clause)->num_rows() == 0)
|
|
|
|
{
|
2016-01-09 23:29:28 +02:00
|
|
|
throw new Exception('Provider id was not found in database.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add record to database (insert or update).
|
2017-09-15 14:36:37 +03:00
|
|
|
if ( ! isset($unavailable['id']))
|
|
|
|
{
|
2016-01-09 23:29:28 +02:00
|
|
|
$unavailable['book_datetime'] = date('Y-m-d H:i:s');
|
2017-09-15 14:36:37 +03:00
|
|
|
$unavailable['is_unavailable'] = TRUE;
|
2016-01-09 23:29:28 +02:00
|
|
|
|
|
|
|
$this->db->insert('ea_appointments', $unavailable);
|
|
|
|
$unavailable['id'] = $this->db->insert_id();
|
2018-01-23 12:08:37 +03:00
|
|
|
}
|
|
|
|
else
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
|
|
|
$this->db->where(['id' => $unavailable['id']]);
|
2016-01-09 23:29:28 +02:00
|
|
|
$this->db->update('ea_appointments', $unavailable);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $unavailable['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an unavailable period.
|
|
|
|
*
|
2017-09-08 15:03:45 +03:00
|
|
|
* @param int $unavailable_id Record id to be deleted.
|
|
|
|
*
|
|
|
|
* @return bool Returns the delete operation result.
|
|
|
|
*
|
|
|
|
* @throws Exception If $unavailable_id argument is invalid.
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function delete_unavailable($unavailable_id)
|
|
|
|
{
|
|
|
|
if ( ! is_numeric($unavailable_id))
|
|
|
|
{
|
2017-09-08 15:03:45 +03:00
|
|
|
throw new Exception('Invalid argument type $unavailable_id: ' . $unavailable_id);
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$num_rows = $this->db->get_where('ea_appointments', ['id' => $unavailable_id])->num_rows();
|
2017-09-08 15:03:45 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($num_rows == 0)
|
|
|
|
{
|
2016-01-09 23:29:28 +02:00
|
|
|
return FALSE; // Record does not exist.
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->db->where('id', $unavailable_id);
|
2017-09-08 15:03:45 +03:00
|
|
|
|
2016-01-09 23:29:28 +02:00
|
|
|
return $this->db->delete('ea_appointments');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear google sync IDs from appointment record.
|
|
|
|
*
|
2017-09-08 15:03:45 +03:00
|
|
|
* @param int $provider_id The appointment provider record id.
|
|
|
|
*
|
|
|
|
* @throws Exception If $provider_id argument is invalid.
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function clear_google_sync_ids($provider_id)
|
|
|
|
{
|
|
|
|
if ( ! is_numeric($provider_id))
|
|
|
|
{
|
2017-09-08 15:03:45 +03:00
|
|
|
throw new Exception('Invalid argument type $provider_id: ' . $provider_id);
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$this->db->update('ea_appointments', ['id_google_calendar' => NULL],
|
|
|
|
['id_users_provider' => $provider_id]);
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
2016-07-21 23:36:17 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get appointment count for the provided start datetime.
|
|
|
|
*
|
|
|
|
* @param int $service_id Selected service ID.
|
|
|
|
* @param string $selected_date Selected date string.
|
|
|
|
* @param string $hour Selected hour string.
|
|
|
|
*
|
|
|
|
* @return int Returns the appointment number at the selected start time.
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function appointment_count_for_hour($service_id, $selected_date, $hour)
|
|
|
|
{
|
2016-07-21 23:36:17 +03:00
|
|
|
return $this->db->get_where('ea_appointments', [
|
|
|
|
'id_services' => $service_id,
|
|
|
|
'start_datetime' => date('Y-m-d H:i:s', strtotime($selected_date . ' ' . $hour . ':00'))
|
|
|
|
])->num_rows();
|
|
|
|
}
|
2017-12-01 11:12:09 +03:00
|
|
|
|
2018-01-02 13:20:11 +03:00
|
|
|
/**
|
|
|
|
* Returns the attendants number for selection period.
|
|
|
|
*
|
|
|
|
* @param DateTime $slot_start When the slot starts
|
|
|
|
* @param DateTime $slot_end When the slot ends.
|
|
|
|
* @param int $service_id Selected service ID.
|
|
|
|
*
|
|
|
|
* @return int Returns the number of attendants for selected time period.
|
|
|
|
*/
|
2018-01-23 12:08:37 +03:00
|
|
|
public function get_attendants_number_for_period(DateTime $slot_start, DateTime $slot_end, $service_id)
|
|
|
|
{
|
|
|
|
return (int)$this->db
|
2018-01-02 13:20:11 +03:00
|
|
|
->select('count(*) AS attendants_number')
|
|
|
|
->from('ea_appointments')
|
|
|
|
->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()
|
|
|
|
->where('id_services', $service_id)
|
|
|
|
->get()
|
|
|
|
->row()
|
|
|
|
->attendants_number;
|
|
|
|
}
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|