1: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2:
3: class Appointments extends CI_Controller {
4: 5: 6: 7:
8: public function index() {
9: if (strtoupper($_SERVER['REQUEST_METHOD']) != 'POST') {
10:
11:
12: $this->load->model('Settings_Model');
13: $view_data['company_name'] = $this->Settings_Model->get_setting('company_name');
14:
15:
16: $this->load->model('Services_Model');
17: $view_data['available_services'] = $this->Services_Model->get_available_services();
18:
19: $this->load->model('Providers_Model');
20: $view_data['available_providers'] = $this->Providers_Model->get_available_providers();
21:
22:
23: $this->load->view('appointments/book', $view_data);
24: } else {
25: $post_data = json_decode($_POST['post_data'], true);
26:
27:
28: $this->load->model('Customers_Model');
29: $customer_id = $this->Customers_Model->add($post_data['customer']);
30:
31:
32: $post_data['appointment']['id_users_customer'] = $customer_id;
33: $this->load->model('Appointments_Model');
34: $view_data['appointment_id'] = $this->Appointments_Model->add($post_data['appointment']);
35:
36:
37: $this->load->library('Notifications');
38: try {
39: $this->notifications->send_book_success($post_data['customer'], $post_data['appointment']);
40: $this->notifications->send_new_appointment($post_data['customer'], $post_data['appointment']);
41: } catch (NotificationException $not_exc) {
42: $view_data['notification_error'] = '<br><br><pre>An unexpected error occured while sending '
43: . 'you an email. Please backup the appointment details so that you can restore them '
44: . 'later. <br><br>Error:<br>' . $not_exc->getMessage() . '</pre>';
45: }
46:
47:
48: $this->load->view('appointments/book_success', $view_data);
49: }
50: }
51:
52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62:
63: public function ajax_get_available_hours() {
64: $this->load->model('Providers_Model');
65: $this->load->model('Appointments_Model');
66: $this->load->model('Settings_Model');
67:
68:
69: $working_plan = json_decode($this->Providers_Model
70: ->get_value('working_plan', $_POST['provider_id']), true);
71:
72: $reserved_appointments = $this->Appointments_Model->get_batch(
73: array(
74: 'DATE(start_datetime)' => date('Y-m-d', strtotime($_POST['selected_date'])),
75: 'id_users_provider' => $_POST['provider_id'],
76: 'id_services' => $_POST['service_id']
77: ));
78:
79:
80:
81:
82: $sel_date_working_plan = $working_plan[strtolower(date('l',
83: strtotime($_POST['selected_date'])))];
84: $empty_spaces_with_breaks = array();
85:
86: if (isset($sel_date_working_plan['breaks'])) {
87: foreach($sel_date_working_plan['breaks'] as $index=>$break) {
88:
89:
90: $last_break_index = $index - 1;
91:
92: if (count($empty_spaces_with_breaks) === 0) {
93: $start_hour = $sel_date_working_plan['start'];
94: $end_hour = $break['start'];
95: } else {
96: $start_hour = $sel_date_working_plan['breaks'][$last_break_index]['end'];
97: $end_hour = $break['start'];
98: }
99:
100: $empty_spaces_with_breaks[] = array(
101: 'start' => $start_hour,
102: 'end' => $end_hour
103: );
104: }
105:
106:
107: $empty_spaces_with_breaks[] = array(
108: 'start' => $sel_date_working_plan['breaks'][$index]['end'],
109: 'end' => $sel_date_working_plan['end']
110: );
111: }
112:
113:
114: $empty_spaces_with_appointments = array();
115: if (count($reserved_appointments) > 0) {
116: foreach($empty_spaces_with_breaks as $space) {
117: foreach($reserved_appointments as $index=>$appointment) {
118: $appointment_start = date('H:i', strtotime($appointment['start_datetime']));
119: $appointment_end = date('H:i', strtotime($appointment['end_datetime']));
120: $space_start = date('H:i', strtotime($space['start']));
121: $space_end = date('H:i', strtotime($space['end']));
122:
123: if ($space_start < $appointment_start && $space_end > $appointment_end) {
124:
125:
126:
127: $empty_spaces_with_appointments[] = array(
128: 'start' => $space_start,
129: 'end' => $appointment_start
130: );
131: $empty_spaces_with_appointments[] = array(
132: 'start' => $appointment_end,
133: 'end' => $space_end
134: );
135: } else {
136: $empty_spaces_with_appointments[] = array(
137: 'start' => $space_start,
138: 'end' => $space_end
139: );
140: }
141: }
142: }
143: } else {
144: $empty_spaces_with_appointments = $empty_spaces_with_breaks;
145: }
146:
147: $empty_spaces = $empty_spaces_with_appointments;
148:
149:
150:
151:
152:
153: $available_hours = array();
154:
155: foreach($empty_spaces as $space) {
156: $start_hour = new DateTime($_POST['selected_date'] . ' ' . $space['start']);
157: $end_hour = new DateTime($_POST['selected_date'] . ' ' . $space['end']);
158: $curr_hour = $start_hour;
159:
160: $diff = $curr_hour->diff($end_hour);
161: while(($diff->h * 60 + $diff->i) > intval($_POST['service_duration'])) {
162: $available_hours[] = $curr_hour->format('H:i');
163: $curr_hour->add(new DateInterval("PT15M"));
164: $diff = $curr_hour->diff($end_hour);
165: }
166: }
167:
168:
169:
170:
171:
172:
173: if (date('m/d/Y', strtotime($_POST['selected_date'])) == date('m/d/Y')) {
174: $book_advance_timeout = $this->Settings_Model->get_setting('book_advance_timeout');
175:
176: foreach($available_hours as $index=>$value) {
177: $available_hour = strtotime($value);
178: $current_hour = strtotime('+' . $book_advance_timeout
179: . ' minutes', strtotime('now'));
180:
181: if ($available_hour <= $current_hour) {
182: unset($available_hours[$index]);
183: }
184: }
185: }
186:
187: $available_hours = array_values($available_hours);
188:
189: echo json_encode($available_hours);
190: }
191:
192: 193: 194: 195: 196: 197: 198: 199: 200: 201:
202: public function google_sync($appointment_id) {
203: try {
204: $this->load->library('Google_Sync');
205: $this->google_sync->sync_appointment($appointment_id);
206: $view_data['message'] = 'Your appointment has been successfully added to Google Calendar!';
207: $view_data['image'] = 'success.png';
208: } catch (Exception $exc) {
209: $view_data['message'] = 'An unexpected error occured during the sync '
210: . 'operation: <br/><pre>' . $exc->getMessage() . '<br/>'
211: . $exc->getTraceAsString() . '</pre>';
212: $view_data['image'] = 'error.png';
213: }
214:
215: $this->load->view('appointments/google_sync', $view_data);
216: }
217: }
218:
219:
220: