/** * Prepare data for email. * * @param AB_CustomerAppointment $ca * @return array */ private static function _prepareData(AB_CustomerAppointment $ca) { $appointment = new AB_Appointment(); $appointment->load($ca->get('appointment_id')); $customer = new AB_Customer(); $customer->load($ca->get('customer_id')); $staff = new AB_Staff(); $staff->load($appointment->get('staff_id')); $service = new AB_Service(); $service->load($appointment->get('service_id')); $staff_service = new AB_StaffService(); $staff_service->loadBy(array('staff_id' => $staff->get('id'), 'service_id' => $service->get('id'))); $price = $staff_service->get('price'); if ($ca->get('coupon_discount') or $ca->get('coupon_deduction')) { $coupon = new AB_Coupon(); $coupon->set('discount', $ca->get('coupon_discount')); $coupon->set('deduction', $ca->get('coupon_deduction')); $price = $coupon->apply($price); } $codes = new AB_NotificationCodes(); $codes->set('appointment_datetime', $appointment->get('start_date')); $codes->set('appointment_token', $ca->get('token')); $codes->set('category_name', $service->getCategoryName()); $codes->set('client_name', $customer->get('name')); $codes->set('client_phone', $customer->get('phone')); $codes->set('client_email', $customer->get('email')); $codes->set('custom_fields', $ca->getFormattedCustomFields('text')); $codes->set('custom_fields_2c', $ca->getFormattedCustomFields('html')); $codes->set('number_of_persons', $ca->get('number_of_persons')); $codes->set('service_name', $service->getTitle()); $codes->set('service_price', $price); $codes->set('staff_name', $staff->get('full_name')); $codes->set('staff_email', $staff->get('email')); $codes->set('staff_phone', $staff->get('phone')); $codes->set('staff_photo', $staff->get('avatar_url')); return array($codes, $staff, $appointment, $customer); }
/** * Get service price. * * @return string|false */ public function getServicePrice() { $staff_service = new AB_StaffService(); $staff_service->loadBy(array('staff_id' => $this->getStaffId(), 'service_id' => $this->get('service_id'))); return $staff_service->isLoaded() ? $staff_service->get('price') : false; }
/** * 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); }