예제 #1
0
 /**
  * Cancel Appointment using token.
  */
 public function executeCancelAppointment()
 {
     $customer_appointment = new AB_CustomerAppointment();
     if ($customer_appointment->loadBy(array('token' => $this->getParameter('token')))) {
         // Send email.
         AB_NotificationSender::send(AB_NotificationSender::INSTANT_CANCELLED_APPOINTMENT, $customer_appointment);
         $customer_appointment->delete();
         $appointment = new AB_Appointment();
         $appointment->load($customer_appointment->get('appointment_id'));
         // Delete appointment, if there aren't customers.
         $count = AB_CustomerAppointment::query('ca')->where('ca.appointment_id', $customer_appointment->get('appointment_id'))->count();
         if (!$count) {
             $appointment->delete();
         } else {
             $appointment->handleGoogleCalendar();
         }
         if ($this->url = get_option('ab_settings_cancel_page_url')) {
             wp_redirect($this->url);
             $this->render('cancel_appointment');
             exit(0);
         }
     }
     $this->url = home_url();
     if (isset($_SERVER['HTTP_REFERER'])) {
         if (parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST) == parse_url($this->url, PHP_URL_HOST)) {
             // Redirect back if user came from our site.
             $this->url = $_SERVER['HTTP_REFERER'];
         }
     }
     wp_redirect($this->url);
     $this->render('cancel_appointment');
     exit(0);
 }
예제 #2
0
 /**
  * Save all data and create appointment.
  *
  * @return AB_Appointment
  */
 public function save()
 {
     $user_id = get_current_user_id();
     $customer = new AB_Customer();
     if ($user_id) {
         // Try to find customer by WP user ID.
         $customer->loadBy(array('wp_user_id' => $user_id));
     }
     if (!$customer->isLoaded()) {
         // If customer with such name & e-mail exists, append new booking to him, otherwise - create new customer
         $customer->loadBy(array('name' => $this->get('name'), 'email' => $this->get('email')));
     }
     $customer->set('name', $this->get('name'));
     $customer->set('email', $this->get('email'));
     $customer->set('phone', $this->get('phone'));
     if (get_option('ab_settings_create_account', 0) && !$customer->get('wp_user_id')) {
         // Create WP user and link it to customer.
         $customer->setWPUser($user_id ?: null);
     }
     $customer->save();
     $this->customer_id = $customer->get('id');
     $service = $this->getService();
     /**
      * Get appointment, with same params.
      * If it is -> create connection to this appointment,
      * otherwise create appointment and connect customer to new appointment
      */
     $appointment = new AB_Appointment();
     $appointment->loadBy(array('staff_id' => $this->getStaffId(), 'service_id' => $this->get('service_id'), 'start_date' => $this->get('appointment_datetime')));
     if ($appointment->isLoaded() == false) {
         $appointment->set('staff_id', $this->getStaffId());
         $appointment->set('service_id', $this->get('service_id'));
         $appointment->set('start_date', $this->get('appointment_datetime'));
         $endDate = new DateTime($this->get('appointment_datetime'));
         $di = "+ {$service->get('duration')} sec";
         $endDate->modify($di);
         $appointment->set('end_date', $endDate->format('Y-m-d H:i:s'));
         $appointment->save();
     }
     $customer_appointment = new AB_CustomerAppointment();
     $customer_appointment->loadBy(array('customer_id' => $customer->get('id'), 'appointment_id' => $appointment->get('id')));
     if ($customer_appointment->isLoaded()) {
         // Add number of persons to existing booking.
         $customer_appointment->set('number_of_persons', $customer_appointment->get('number_of_persons') + $this->get('number_of_persons'));
     } else {
         $customer_appointment->set('customer_id', $customer->get('id'));
         $customer_appointment->set('appointment_id', $appointment->get('id'));
         $customer_appointment->set('number_of_persons', $this->get('number_of_persons'));
     }
     $customer_appointment->set('custom_fields', $this->get('custom_fields'));
     $customer_appointment->set('time_zone_offset', $this->get('time_zone_offset'));
     $coupon = $this->getCoupon();
     if ($coupon) {
         $customer_appointment->set('coupon_code', $coupon->get('code'));
         $customer_appointment->set('coupon_discount', $coupon->get('discount'));
         $customer_appointment->set('coupon_deduction', $coupon->get('deduction'));
         $coupon->claim();
         $coupon->save();
     }
     $customer_appointment->save();
     // Create fake payment record for 100% discount coupons.
     if ($coupon && $coupon->get('discount') == '100') {
         $payment = new AB_Payment();
         $payment->set('total', '0.00');
         $payment->set('type', 'coupon');
         $payment->set('created', current_time('mysql'));
         $payment->set('customer_appointment_id', $customer_appointment->get('id'));
         $payment->save();
     }
     // Google Calendar.
     $appointment->handleGoogleCalendar();
     // Send email notifications.
     AB_NotificationSender::send(AB_NotificationSender::INSTANT_NEW_APPOINTMENT, $customer_appointment);
     return $appointment;
 }
예제 #3
0
 /**
  * Save appointment form (for both create and edit).
  */
 public function executeSaveAppointmentForm()
 {
     $response = array('success' => false);
     $start_date = date('Y-m-d H:i:s', strtotime($this->getParameter('start_date')));
     $end_date = date('Y-m-d H:i:s', strtotime($this->getParameter('end_date')));
     $staff_id = $this->getParameter('staff_id');
     $service_id = $this->getParameter('service_id');
     $appointment_id = $this->getParameter('id', 0);
     $customers = json_decode($this->getParameter('customers', '[]'), true);
     $staff_service = new AB_StaffService();
     $staff_service->loadBy(array('staff_id' => $staff_id, 'service_id' => $service_id));
     // Check for errors.
     if (!$service_id) {
         $response['errors']['service_required'] = true;
     }
     if (empty($customers)) {
         $response['errors']['customers_required'] = true;
     }
     if (!$this->dateIntervalIsAvailableForAppointment($start_date, $end_date, $staff_id, $appointment_id)) {
         $response['errors']['date_interval_not_available'] = true;
     }
     $number_of_persons = 0;
     foreach ($customers as $customer) {
         $number_of_persons += $customer['number_of_persons'];
     }
     if ($number_of_persons > $staff_service->get('capacity')) {
         $response['errors']['overflow_capacity'] = __('The number of customers should be not more than ', 'bookly') . $staff_service->get('capacity');
     }
     if (!$this->getParameter('start_date')) {
         $response['errors']['time_interval'] = __('Start time must not be empty', 'bookly');
     } elseif (!$this->getParameter('end_date')) {
         $response['errors']['time_interval'] = __('End time must not be empty', 'bookly');
     } elseif ($start_date == $end_date) {
         $response['errors']['time_interval'] = __('End time must not be equal to start time', 'bookly');
     }
     // If no errors then try to save the appointment.
     if (!isset($response['errors'])) {
         $appointment = new AB_Appointment();
         if ($appointment_id) {
             // Edit.
             $appointment->load($appointment_id);
         }
         $appointment->set('start_date', $start_date);
         $appointment->set('end_date', $end_date);
         $appointment->set('staff_id', $staff_id);
         $appointment->set('service_id', $service_id);
         if ($appointment->save() !== false) {
             // Save customers.
             $appointment->setCustomers($customers);
             // Google Calendar.
             $appointment->handleGoogleCalendar();
             if ($this->getParameter('email_notification') === 'true') {
                 foreach ($appointment->getCustomerAppointments() as $ca) {
                     AB_NotificationSender::send(AB_NotificationSender::INSTANT_NEW_APPOINTMENT, $ca);
                 }
             }
             $startDate = new DateTime($appointment->get('start_date'));
             $endDate = new DateTime($appointment->get('end_date'));
             $desc = array();
             if ($staff_service->get('capacity') == 1) {
                 $customer_appointments = $appointment->getCustomerAppointments();
                 if (!empty($customer_appointments)) {
                     $ca = $customer_appointments[0]->customer;
                     foreach (array('name', 'phone', 'email') as $data_entry) {
                         $entry_value = $ca->get($data_entry);
                         if ($entry_value) {
                             $desc[] = '<div class="fc-employee">' . esc_html($entry_value) . '</div>';
                         }
                     }
                     foreach ($customer_appointments[0]->getCustomFields() as $custom_field) {
                         $desc[] = '<div class="fc-notes">' . wp_strip_all_tags($custom_field['label']) . ': ' . esc_html($custom_field['value']) . '</div>';
                     }
                 }
             } else {
                 $signed_up = 0;
                 foreach ($appointment->getCustomerAppointments() as $ca) {
                     $signed_up += $ca->get('number_of_persons');
                 }
                 $desc[] = '<div class="fc-notes">' . __('Signed up', 'bookly') . ' ' . $signed_up . '</div>';
                 $desc[] = '<div class="fc-notes">' . __('Capacity', 'bookly') . ' ' . $staff_service->get('capacity') . '</div>';
             }
             $service = new AB_Service();
             $service->load($service_id);
             $response['success'] = true;
             $response['data'] = array('id' => (int) $appointment->get('id'), 'start' => $startDate->format('Y-m-d H:i:s'), 'end' => $endDate->format('Y-m-d H:i:s'), 'desc' => implode('', $desc), 'title' => $service->get('title') ? $service->get('title') : __('Untitled', 'bookly'), 'color' => $service->get('color'), 'staffId' => $appointment->get('staff_id'));
         } else {
             $response['errors'] = array('db' => __('Could not save appointment in database.', 'bookly'));
         }
     }
     wp_send_json($response);
 }