/** * 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; }
/** * Import customers from CSV. */ private function importCustomers() { @ini_set('auto_detect_line_endings', true); $csv_mime_types = array('text/csv', 'application/csv', 'text/comma-separated-values', 'application/excel', 'application/vnd.ms-excel', 'application/vnd.msexcel'); if (in_array($_FILES['import_customers_file']['type'], $csv_mime_types)) { $file = fopen($_FILES['import_customers_file']['tmp_name'], 'r'); while ($line = fgetcsv($file, null, $this->getParameter('import_customers_delimiter'))) { if (!empty($line[0])) { $customer = new AB_Customer(); $customer->set('name', $line[0]); if (isset($line[1])) { $customer->set('phone', $line[1]); } if (isset($line[2])) { $customer->set('email', $line[2]); } $customer->save(); } } } }
/** * @return AB_Appointment */ public function save() { /** @var wpdb $wpdb */ global $wpdb; // #11094: if customer with such name & e-mail exists, append new booking to him, otherwise - create new customer $customer_exists = $wpdb->get_row($wpdb->prepare('SELECT * FROM ab_customer WHERE name = %s AND email = %s', $this->name, $this->email)); $customer = new AB_Customer(); if ($customer_exists) { $customer->set('id', $customer_exists->id); $customer->set('name', $customer_exists->name); $customer->set('email', $customer_exists->email); $customer->set('phone', $customer_exists->phone); } else { $customer->set('name', $this->name); $customer->set('email', $this->email); $customer->set('phone', $this->phone); $customer->save(); } $this->customer_id = $customer->get('id'); $service = new AB_Service(); $service->load($this->service_id); $category = new AB_Category(); $category->load($service->get('category_id')); /** * Get appointment, with same params. * If it is -> create connection to this appointment, * otherwise create appointment and connect customer to new appointment */ $booking = $wpdb->get_row($wpdb->prepare("SELECT * from ab_appointment a WHERE a.staff_id = %d and a.service_id = %d and a.start_date = %s LIMIT 1;", $this->getStaffId(), $this->service_id, $this->booked_datetime)); $appointment = new AB_Appointment(); if ($booking) { $appointment->load($booking->id); } else { $appointment->set('staff_id', $this->getStaffId()); $appointment->set('service_id', $this->service_id); $appointment->set('start_date', date('Y-m-d H:i:s', strtotime($this->booked_datetime))); $endDate = new DateTime($this->booked_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_Customer_Appointment(); $customer_appointment->set('appointment_id', $appointment->get('id')); $customer_appointment->set('customer_id', $customer->get('id')); $customer_appointment->set('token', md5($this->form_id)); $customer_appointment->set('notes', $this->notes); $customer_appointment->save(); $staff = new AB_Staff(); $staff->load($this->getStaffId()); return $appointment; }