function inline_edit_save()
 {
     global $appointments, $wpdb, $current_user;
     $app_id = absint($_POST["app_id"]);
     $app = appointments_get_appointment($app_id);
     $data = array();
     $data['user'] = $_POST['user'];
     $data['email'] = !empty($_POST['email']) && is_email($_POST['email']) ? $_POST['email'] : '';
     $data['name'] = $_POST['name'];
     $data['phone'] = $_POST['phone'];
     $data['address'] = $_POST['address'];
     $data['city'] = $_POST['city'];
     $data['service'] = $_POST['service'];
     $data['worker'] = $_POST['worker'];
     $data['price'] = $_POST['price'];
     $data['note'] = $_POST['note'];
     $data['status'] = $_POST['status'];
     $data['date'] = $_POST['date'];
     $data['time'] = $_POST['time'];
     $resend = $_POST["resend"];
     $data = apply_filters('app-appointment-inline_edit-save_data', $data);
     $update_result = $insert_result = false;
     if ($app) {
         // Update
         $update_result = appointments_update_appointment($app_id, $data);
         if ($update_result) {
             if (('pending' == $data['status'] || 'removed' == $data['status'] || 'completed' == $data['status']) && is_object($appointments->gcal_api)) {
                 $appointments->gcal_api->delete($app_id);
             } else {
                 if (is_object($appointments->gcal_api) && $appointments->gcal_api->is_syncable_status($data['status'])) {
                     $appointments->gcal_api->update($app_id);
                     // This also checks for event insert
                 }
             }
         }
         if ($resend && 'removed' != $data['status']) {
             appointments_send_confirmation($app_id);
         }
     } else {
         // Insert
         $app_id = appointments_insert_appointment($data);
         if ($app_id) {
             $insert_result = true;
             if ($resend) {
                 appointments_send_confirmation($app_id);
             }
             $appointments->sms_new_appointment($app_id);
         }
     }
     do_action('app-appointment-inline_edit-after_save', $app_id, $data);
     do_action('app-appointment-inline_edit-before_response', $app_id, $data);
     $app = appointments_get_appointment($app_id);
     if (!$app) {
         $insert_result = false;
         $update_result = false;
     }
     // Move mail sending here so the fields can expand
     if ($insert_result && is_object($appointments->gcal_api) && $appointments->gcal_api->is_syncable_status($data['status'])) {
         $appointments->gcal_api->insert($app->ID);
     }
     $result = array('app_id' => 0, 'message' => '');
     if ($update_result) {
         // Log change of status
         if ($data['status'] != $app->status) {
             $appointments->log(sprintf(__('Status changed from %s to %s by %s for appointment ID:%d', 'appointments'), $app->status, $data["status"], $current_user->user_login, $app->ID));
         }
         $result = array('app_id' => $app->ID, 'message' => __('<span style="color:green;font-weight:bold">Changes saved.</span>', 'appointments'));
     } else {
         if ($insert_result) {
             $result = array('app_id' => $app->ID, 'message' => __('<span style="color:green;font-weight:bold">Changes saved.</span>', 'appointments'));
         } else {
             $message = $resend && !empty($data['status']) && 'removed' != $data['status'] ? sprintf('<span style="color:green;font-weight:bold">%s</span>', __('Confirmation message (re)sent', 'appointments')) : sprintf('<span style="color:red;font-weight:bold">%s</span>', __('Record could not be saved OR you did not make any changes!', 'appointments'));
             $result = array('app_id' => $app_id, 'message' => $message);
         }
     }
     $result = apply_filters('app-appointment-inline_edit-result', $result, $app_id, $data);
     die(json_encode($result));
 }
 function test_timetables_cache()
 {
     global $appointments;
     // Insert a worker
     $args = $this->factory->user->generate_args();
     $user_id_1 = $this->factory->user->create_object($args);
     $args = $this->factory->user->generate_args();
     $user_id_2 = $this->factory->user->create_object($args);
     $service_id = appointments_insert_service(array('name' => 'My Service'));
     $args = array('ID' => $user_id_1, 'price' => '19.7', 'services_provided' => array($service_id), 'dummy' => true);
     $result = appointments_insert_worker($args);
     $time = time();
     $date = date('Y-m-01', $time);
     $capacity = appointments_get_capacity();
     $date_start = strtotime("{$date} 00:00");
     $key = $date_start . '-' . $capacity . '-0' . '-' . date('Ym', $appointments->local_time);
     // WORKERS
     // Trigger the timetables cache
     appointments_get_timetable($date_start, $capacity);
     $timetables_cache = get_transient('app_timetables');
     $this->assertNotEmpty($timetables_cache[$key]);
     // Insert another worker
     $args = array('ID' => $user_id_2, 'price' => '19.7', 'services_provided' => array($service_id), 'dummy' => true);
     appointments_insert_worker($args);
     $timetables_cache = get_transient('app_timetables');
     $this->assertEmpty($timetables_cache[$key]);
     // Trigger the timetables cache
     appointments_get_timetable($date_start, $capacity);
     $timetables_cache = get_transient('app_timetables');
     $this->assertNotEmpty($timetables_cache[$key]);
     // Delete a worker
     appointments_delete_worker($user_id_2);
     $timetables_cache = get_transient('app_timetables');
     $this->assertEmpty($timetables_cache[$key]);
     // Trigger the timetables cache
     appointments_get_timetable($date_start, $capacity);
     $timetables_cache = get_transient('app_timetables');
     $this->assertNotEmpty($timetables_cache[$key]);
     // Update worker
     appointments_update_worker($user_id_1, array('price' => '10'));
     $timetables_cache = get_transient('app_timetables');
     $this->assertEmpty($timetables_cache[$key]);
     // APPOINTMENTS
     // Trigger the timetables cache
     appointments_get_timetable($date_start, $capacity);
     $timetables_cache = get_transient('app_timetables');
     $this->assertNotEmpty($timetables_cache[$key]);
     // Insert appointment
     $args = array('user' => $user_id_2, 'email' => '*****@*****.**', 'name' => 'Tester', 'phone' => '667788', 'address' => 'An address', 'city' => 'Madrid', 'service' => $service_id, 'worker' => $user_id_1, 'price' => '90', 'date' => 'December 18, 2024', 'time' => '07:30', 'note' => 'It\'s a note', 'status' => 'paid', 'location' => 5, 'gcal_updated' => '2015-12-01', 'gcal_ID' => 'test');
     $app_id = appointments_insert_appointment($args);
     $timetables_cache = get_transient('app_timetables');
     $this->assertEmpty($timetables_cache[$key]);
     // Trigger the timetables cache
     appointments_get_timetable($date_start, $capacity);
     $timetables_cache = get_transient('app_timetables');
     $this->assertNotEmpty($timetables_cache[$key]);
     // Update Appointment
     appointments_update_appointment($app_id, array('address' => 'New address'));
     $timetables_cache = get_transient('app_timetables');
     $this->assertEmpty($timetables_cache[$key]);
     // Trigger the timetables cache
     appointments_get_timetable($date_start, $capacity);
     $timetables_cache = get_transient('app_timetables');
     $this->assertNotEmpty($timetables_cache[$key]);
     // Delete appointment
     appointments_delete_appointment($app_id);
     $timetables_cache = get_transient('app_timetables');
     $this->assertEmpty($timetables_cache[$key]);
     // SERVICES
     // Insert a service
     // Trigger the timetables cache
     appointments_get_timetable($date_start, $capacity);
     $timetables_cache = get_transient('app_timetables');
     $this->assertNotEmpty($timetables_cache[$key]);
     $service_id_2 = appointments_insert_service(array('name' => 'My Service 2'));
     $timetables_cache = get_transient('app_timetables');
     $this->assertEmpty($timetables_cache[$key]);
     // Trigger the timetables cache
     appointments_get_timetable($date_start, $capacity);
     $timetables_cache = get_transient('app_timetables');
     $this->assertNotEmpty($timetables_cache[$key]);
     // Update service
     appointments_update_service($service_id_2, array('name' => 'My Service updated'));
     $timetables_cache = get_transient('app_timetables');
     $this->assertEmpty($timetables_cache[$key]);
     // Trigger the timetables cache
     appointments_get_timetable($date_start, $capacity);
     $timetables_cache = get_transient('app_timetables');
     $this->assertNotEmpty($timetables_cache[$key]);
     // Delete service
     appointments_delete_service($service_id_2);
     $timetables_cache = get_transient('app_timetables');
     $this->assertEmpty($timetables_cache[$key]);
 }
 function test_delete_appointment()
 {
     $worker_id = $this->factory->user->create_object($this->factory->user->generate_args());
     $user_id = $this->factory->user->create_object($this->factory->user->generate_args());
     $service_args = array('name' => 'My Service', 'duration' => 90);
     $service_id = appointments_insert_service($service_args);
     $worker_args = array('ID' => $worker_id, 'services_provided' => array($service_id));
     appointments_insert_worker($worker_args);
     $args = array('user' => $user_id, 'email' => '*****@*****.**', 'name' => 'Tester', 'phone' => '667788', 'address' => 'An address', 'city' => 'Madrid', 'service' => $service_id, 'worker' => $worker_id, 'price' => '90', 'date' => 'December 18, 2024', 'time' => '07:30', 'note' => 'It\'s a note', 'status' => 'paid', 'location' => 5);
     $app_id = appointments_insert_appointment($args);
     $app = appointments_get_appointment($app_id);
     appointments_delete_appointment($app_id);
     $app = appointments_get_appointment($app_id);
     $this->assertFalse($app);
 }