/** * 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; }
/** * Get list of appointments. */ public function executeGetAppointments() { $response = array('appointments' => array(), 'total' => 0, 'pages' => 0, 'active_page' => 0); $page = intval($this->getParameter('page')); $sort = in_array($this->getParameter('sort'), array('staff_name', 'service_title', 'start_date', 'price')) ? $this->getParameter('sort') : 'start_date'; $order = in_array($this->getParameter('order'), array('asc', 'desc')) ? $this->getParameter('order') : 'asc'; $start_date = new DateTime($this->getParameter('date_start')); $start_date = $start_date->format('Y-m-d H:i:s'); $end_date = new DateTime($this->getParameter('date_end')); $end_date = $end_date->modify('+1 day')->format('Y-m-d H:i:s'); $items_per_page = 20; $total = AB_Appointment::query()->whereBetween('start_date', $start_date, $end_date)->count(); $pages = ceil($total / $items_per_page); if ($page < 1 || $page > $pages) { $page = 1; } if ($total) { $query = AB_CustomerAppointment::query('ca')->select('ca.id, ca.number_of_persons, ca.coupon_discount, ca.coupon_deduction, ca.appointment_id, st.full_name AS staff_name, s.title AS service_title, s.duration AS service_duration, c.name AS customer_name, a.start_date, ss.price')->leftJoin('AB_Appointment', 'a', 'a.id = ca.appointment_id')->leftJoin('AB_Service', 's', 's.id = a.service_id')->leftJoin('AB_Customer', 'c', 'c.id = ca.customer_id')->leftJoin('AB_Staff', 'st', 'st.id = a.staff_id')->leftJoin('AB_StaffService', 'ss', 'ss.staff_id = st.id AND ss.service_id = s.id')->whereBetween('a.start_date', $start_date, $end_date)->sortBy($sort)->order($order); // LIMIT. $start = ($page - 1) * $items_per_page; $query->offset($start)->limit($items_per_page); $rows = $query->fetchArray(); foreach ($rows as &$row) { if ($row['coupon_discount'] or $row['coupon_deduction']) { $coupon = new AB_Coupon(); $coupon->set('discount', $row['coupon_discount']); $coupon->set('deduction', $row['coupon_deduction']); $row['price'] = $coupon->apply($row['price']); } $row['price'] *= $row['number_of_persons']; $row['price'] = AB_Utils::formatPrice($row['price']); $row['start_date'] = AB_DateTimeUtils::formatDateTime($row['start_date']); $row['service_duration'] = AB_Service::durationToString($row['service_duration']); } // Populate response. $response['appointments'] = $rows; $response['total'] = $total; $response['pages'] = $pages; $response['active_page'] = $page; } wp_send_json_success($response); }
/** * @param $start_date * @param $end_date * @param $staff_id * @param $appointment_id * @return bool */ private function dateIntervalIsAvailableForAppointment($start_date, $end_date, $staff_id, $appointment_id) { return AB_Appointment::query()->whereNot('id', $appointment_id)->where('staff_id', $staff_id)->whereRaw('(start_date > %s AND start_date < %s OR (end_date > %s AND end_date < %s) OR (start_date < %s AND end_date > %s) OR (start_date = %s OR end_date = %s) )', array($start_date, $end_date, $start_date, $end_date, $start_date, $end_date, $start_date, $end_date))->count() == 0; }