示例#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;
 }
 /**
  * Export Appointment to CSV
  */
 public function executeExportToCSV()
 {
     $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');
     $delimiter = $this->getParameter('delimiter', ',');
     header('Content-Type: text/csv; charset=utf-8');
     header('Content-Disposition: attachment; filename=Appointments.csv');
     $header = array(__('Booking Time', 'bookly'), __('Staff Member', 'bookly'), __('Service', 'bookly'), __('Duration', 'bookly'), __('Price', 'bookly'), __('Customer', 'bookly'), __('Phone', 'bookly'), __('Email', 'bookly'));
     $custom_fields = array();
     $fields_data = json_decode(get_option('ab_custom_fields'));
     foreach ($fields_data as $field_data) {
         $custom_fields[$field_data->id] = '';
         $header[] = $field_data->label;
     }
     $output = fopen('php://output', 'w');
     fwrite($output, pack("CCC", 0xef, 0xbb, 0xbf));
     fputcsv($output, $header, $delimiter);
     $rows = AB_CustomerAppointment::query()->select('r.id,
            r.number_of_persons,
            r.coupon_discount,
            r.coupon_deduction,
            st.full_name  AS staff_name,
            s.title       AS service_title,
            s.duration    AS service_duration,
            c.name        AS customer_name,
            c.phone       AS customer_phone,
            c.email       AS customer_email,
            ss.price,
            a.start_date')->leftJoin('AB_Appointment', 'a', 'a.id = r.appointment_id')->leftJoin('AB_Service', 's', 's.id = a.service_id')->leftJoin('AB_Staff', 'st', 'st.id = a.staff_id')->leftJoin('AB_Customer', 'c', 'c.id = r.customer_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('a.start_date')->order(AB_Query::ORDER_DESCENDING)->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_data = array($row['start_date'], $row['staff_name'], $row['service_title'], AB_Service::durationToString($row['service_duration']), AB_Utils::formatPrice($row['price']), $row['customer_name'], $row['customer_phone'], $row['customer_email']);
         $customer_appointment = new AB_CustomerAppointment();
         $customer_appointment->load($row['id']);
         foreach ($customer_appointment->getCustomFields() as $custom_field) {
             $custom_fields[$custom_field['id']] = $custom_field['value'];
         }
         fputcsv($output, array_merge($row_data, $custom_fields), $delimiter);
         $custom_fields = array_map(function () {
             return '';
         }, $custom_fields);
     }
     fclose($output);
     exit;
 }