/** * 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(); } }
/** * 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; }