public function load($staff_id) { $data = $this->wpdb->get_results(' SELECT c.name AS category_name, s.* FROM ' . AB_Category::getTableName() . ' c INNER JOIN ' . AB_Service::getTableName() . ' s ON c.id = s.category_id ', ARRAY_A); if (!$data) { $data = array(); } $this->uncategorized_services = AB_Service::query('s')->where('s.category_id', null)->fetchArray(); $staff_services = AB_StaffService::query('ss')->select('ss.service_id, ss.price, ss.capacity')->where('ss.staff_id', $staff_id)->fetchArray(); if ($staff_services) { foreach ($staff_services as $staff_service) { $this->selected[$staff_service['service_id']] = array('price' => $staff_service['price'], 'capacity' => $staff_service['capacity']); } } foreach ($data as $row) { if (!isset($this->collection[$row['category_id']])) { $abCategory = new AB_Category(); $abCategory->set('id', $row['category_id']); $abCategory->set('name', $row['category_name']); $this->collection[$row['category_id']] = $abCategory; } unset($row['category_name']); $abService = new AB_Service($row); $this->category_services[$row['category_id']][] = $abService->get('id'); $this->collection[$row['category_id']]->addService($abService); } }
/** * Get AB_StaffService entities associated with this staff member. * * @return array Array of entities */ public function getStaffServices() { $result = array(); if ($this->get('id')) { $staff_services = AB_StaffService::query('ss')->select('ss.*, s.title, s.duration, s.price AS service_price, s.color, s.capacity AS service_capacity')->leftJoin('AB_Service', 's', 's.id = ss.service_id')->where('ss.staff_id', $this->get('id'))->fetchArray(); foreach ($staff_services as $data) { $ss = new AB_StaffService($data); // Inject AB_Service entity. $ss->service = new AB_Service(); $data['id'] = $data['service_id']; $data['price'] = $data['service_price']; $data['capacity'] = $data['service_capacity']; $ss->service->setFields($data, true); $result[] = $ss; } } return $result; }
/** * Prepare data for staff. * * @param DateTime $start_date */ private function _prepareStaffData(DateTime $start_date) { $this->staffData = array(); $services = AB_StaffService::query('ss')->select('ss.staff_id, ss.price, ss.capacity')->whereIn('ss.staff_id', $this->staff_ids)->where('ss.service_id', $this->userData->get('service_id'))->fetchArray(); foreach ($services as $item) { $this->staffData[$item['staff_id']] = array('price' => $item['price'], 'capacity' => $item['capacity'], 'holidays' => array(), 'bookings' => array(), 'working_hours' => array()); } // Load holidays. $holidays = AB_Holiday::query('h')->whereIn('h.staff_id', $this->staff_ids)->fetchArray(); foreach ($holidays as $item) { $this->staffData[$item['staff_id']]['holidays'][] = $item; } // Load working schedule. $working_schedule = AB_StaffScheduleItem::query('ssi')->select('ssi.*, break.start_time AS break_start, break.end_time AS break_end')->leftJoin('AB_ScheduleItemBreak', 'break', 'break.staff_schedule_item_id = ssi.id')->whereIn('ssi.staff_id', $this->staff_ids)->whereNot('ssi.start_time', null)->fetchArray(); foreach ($working_schedule as $item) { if (!isset($this->staffData[$item['staff_id']]['working_hours'][$item['day_index']])) { $this->staffData[$item['staff_id']]['working_hours'][$item['day_index']] = array('start_time' => $item['start_time'], 'end_time' => $item['end_time'], 'breaks' => array()); } if ($item['break_start']) { $this->staffData[$item['staff_id']]['working_hours'][$item['day_index']]['breaks'][] = array('start' => $item['break_start'], 'end' => $item['break_end']); } } // Load bookings. $bookings = AB_CustomerAppointment::query('ca')->select('a.*, SUM(ca.number_of_persons) AS number_of_bookings')->leftJoin('AB_Appointment', 'a', 'a.id = ca.appointment_id')->leftJoin('AB_StaffService', 'ss', 'ss.staff_id = a.staff_id AND ss.service_id = a.service_id')->whereIn('a.staff_id', $this->staff_ids)->whereGte('a.start_date', $this->userData->get('date_from'))->groupBy('a.start_date')->groupBy('a.staff_id')->groupBy('a.service_id')->fetchArray(); foreach ($bookings as $item) { $item['from_google'] = false; // Handle bookings which end at 24:00. if (substr($item['end_date'], 11) == '00:00:00') { // Set time to 24:00:00 (date part does not matter, it just needs to be 10 characters length). $item['end_date'] = '10_symbols 24:00:00'; } $this->staffData[$item['staff_id']]['bookings'][] = $item; } // Handle Google Calendar events. if (get_option('ab_settings_google_two_way_sync')) { $query = AB_Staff::query('s')->whereIn('s.id', array_merge($this->userData->get('staff_ids'), array(0))); foreach ($query->find() as $staff) { $google = new AB_Google(); if ($google->loadByStaff($staff)) { $this->staffData[$staff->get('id')]['bookings'] = array_merge($this->staffData[$staff->get('id')]['bookings'], $google->getCalendarEvents($start_date) ?: array()); } } } }