Пример #1
0
 /**
  * Get appointments for FullCalendar.
  *
  * @param DateTime $start_date
  * @param DateTime $end_date
  *
  * @return array
  */
 public function getAppointmentsForFC(DateTime $start_date, DateTime $end_date)
 {
     if (!$this->isLoaded()) {
         return array();
     }
     $appointments = AB_Appointment::query('a')->select('a.id, a.start_date, a.end_date,
                 s.title AS service_title, s.color AS service_color,
                 ss.capacity AS max_capacity,
                 SUM(ca.number_of_persons) AS total_number_of_persons, ca.custom_fields,
                 c.name AS customer_name, c.phone AS customer_phone, c.email AS customer_email')->leftJoin('AB_CustomerAppointment', 'ca', 'ca.appointment_id = a.id')->leftJoin('AB_Customer', 'c', 'c.id = ca.customer_id')->leftJoin('AB_Service', 's', 's.id = a.service_id')->leftJoin('AB_Staff', 'st', 'st.id = a.staff_id')->leftJoin('AB_StaffService', 'ss', 'ss.staff_id = a.staff_id AND ss.service_id = a.service_id')->where('st.id', $this->get('id'))->whereBetween('DATE(a.start_date)', $start_date->format('Y-m-d'), $end_date->format('Y-m-d'))->groupBy('a.start_date')->fetchArray();
     foreach ($appointments as $key => $appointment) {
         $desc = array();
         if ($appointment['max_capacity'] == 1) {
             foreach (array('customer_name', 'customer_phone', 'customer_email') as $data_entry) {
                 if ($appointment[$data_entry]) {
                     $desc[] = '<div class="fc-employee">' . esc_html($appointment[$data_entry]) . '</div>';
                 }
             }
             $ca = new AB_CustomerAppointment();
             $ca->set('custom_fields', $appointment['custom_fields']);
             foreach ($ca->getCustomFields() as $custom_field) {
                 $desc[] = sprintf('<div class="fc-notes">%s : %s</div>', wp_strip_all_tags($custom_field['label']), esc_html($custom_field['value']));
             }
         } else {
             $desc[] = sprintf('<div class="fc-notes">%s %s</div>', __('Signed up', 'bookly'), $appointment['total_number_of_persons']);
             $desc[] = sprintf('<div class="fc-notes">%s %s</div>', __('Capacity', 'bookly'), $appointment['max_capacity']);
         }
         $appointments[$key] = array('id' => $appointment['id'], 'start' => $appointment['start_date'], 'end' => $appointment['end_date'], 'title' => $appointment['service_title'] ? esc_html($appointment['service_title']) : __('Untitled', 'bookly'), 'desc' => implode('', $desc), 'color' => $appointment['service_color'], 'staffId' => $this->get('id'));
     }
     return $appointments;
 }
Пример #2
0
 /**
  * Set array of customers associated with this appointment.
  *
  * @param array $data  Array of customer IDs, custom_fields and number_of_persons
  */
 public function setCustomers(array $data)
 {
     // Prepare array of customers.
     $customers = array();
     foreach ($data as $customer) {
         $customers[$customer['id']] = $customer;
     }
     // Retrieve customer IDs currently associated with this appointment.
     $current_ids = array_map(function ($ca) {
         return $ca->customer->get('id');
     }, $this->getCustomerAppointments());
     // Remove redundant customers.
     $customer_appointment = new AB_CustomerAppointment();
     foreach (array_diff($current_ids, array_keys($customers)) as $id) {
         if ($customer_appointment->loadBy(array('appointment_id' => $this->get('id'), 'customer_id' => $id))) {
             $customer_appointment->delete();
         }
     }
     // Add new customers.
     foreach (array_diff(array_keys($customers), $current_ids) as $id) {
         $customer_appointment = new AB_CustomerAppointment();
         $customer_appointment->set('appointment_id', $this->get('id'));
         $customer_appointment->set('customer_id', $id);
         $customer_appointment->set('custom_fields', json_encode($customers[$id]['custom_fields']));
         $customer_appointment->set('number_of_persons', $customers[$id]['number_of_persons']);
         $customer_appointment->save();
     }
     // Update existing customers.
     foreach (array_intersect($current_ids, array_keys($customers)) as $id) {
         $customer_appointment = new AB_CustomerAppointment();
         $customer_appointment->loadBy(array('appointment_id' => $this->get('id'), 'customer_id' => $id));
         $customer_appointment->set('custom_fields', json_encode($customers[$id]['custom_fields']));
         $customer_appointment->set('number_of_persons', $customers[$id]['number_of_persons']);
         $customer_appointment->save();
     }
 }
Пример #3
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;
 }