public function save() { $is_new = !$this->get('id'); if ($is_new && $this->get('wp_user_id')) { $user = get_user_by('id', $this->get('wp_user_id')); if ($user) { $this->set('email', $user->get('user_email')); } } parent::save(); if ($is_new) { // Schedule items. $staff_id = $this->get('id'); $index = 1; foreach (array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday') as $week_day) { $item = new AB_StaffScheduleItem(); $item->set('staff_id', $staff_id); $item->set('day_index', $index++); $item->set('start_time', get_option("ab_settings_{$week_day}_start") ?: null); $item->set('end_time', get_option("ab_settings_{$week_day}_end") ?: null); $item->save(); } // Create holidays for staff $this->wpdb->query(sprintf('INSERT INTO `' . AB_Holiday::getTableName() . '` (`parent_id`, `staff_id`, `date`, `repeat_event`, `title`) SELECT `id`, %d, `date`, `repeat_event`, `title` FROM `' . AB_Holiday::getTableName() . '` WHERE `staff_id` IS NULL', $staff_id)); } }
/** * Get data for FullCalendar. * * @return json */ public function executeGetStaffAppointments() { $result = array(); $staff_members = array(); if (AB_Utils::isCurrentUserAdmin()) { $staff_ids = $this->getParameter('staff_ids'); $query = AB_Staff::query()->whereIn('id', array_merge(array(0), explode(',', $staff_ids))); $staff_members = $query->find(); } else { $staff = AB_Staff::query()->where('wp_user_id', get_current_user_id())->findOne(); $staff_members[] = $staff; } foreach ($staff_members as $staff) { $staff_id = $staff->get('id'); $one_day = new DateInterval('P1D'); $start_date = new DateTime($this->getParameter('start')); $end_date = new DateTime($this->getParameter('end')); // FullCalendar sends end date as 1 day further. $end_date->sub($one_day); /** @var AB_Staff $staff */ $result = array_merge($result, $staff->getAppointmentsForFC($start_date, $end_date)); // Schedule. $items = $staff->getScheduleItems(); $day = clone $start_date; while ($day <= $end_date) { do { /** @var AB_StaffScheduleItem $item */ $item = $items[$day->format('w') + 1]; if ($item->get('start_time')) { // Check one-time holidays. if (AB_Holiday::query()->where('date', $day->format('Y-m-d'))->whereRaw('staff_id = %d OR staff_id IS NULL', array($staff_id))->count() == 0) { // Check repeating holidays. if (AB_Holiday::query()->whereRaw('MONTH(date) = %d AND DAY(date) = %s', array($day->format('m'), $day->format('d')))->whereRaw('staff_id = %d OR staff_id IS NULL', array($staff_id))->count() == 0) { // No holidays for this day. // Continue schedule. if ($item->get('start_time') != '00:00:00') { $result[] = array('start' => $day->format('Y-m-d 00:00:00'), 'end' => $day->format("Y-m-d {$item->get('start_time')}"), 'rendering' => 'background', 'staffId' => $staff->get('id')); } if ($item->get('end_time') != '24:00:00') { $result[] = array('start' => $day->format("Y-m-d {$item->get('end_time')}"), 'end' => $day->format('Y-m-d 24:00:00'), 'rendering' => 'background', 'staffId' => $staff->get('id')); } // Breaks. foreach ($item->getBreaksList() as $break) { $result[] = array('start' => $day->format("Y-m-d {$break['start_time']}"), 'end' => $day->format("Y-m-d {$break['end_time']}"), 'rendering' => 'background', 'staffId' => $staff->get('id')); } break; } } } $result[] = array('start' => $day->format('Y-m-d 00:00:00'), 'end' => $day->format('Y-m-d 24:00:00'), 'rendering' => 'background', 'staffId' => $staff->get('id')); } while (0); $day->add($one_day); } } wp_send_json($result); }
/** * @return mixed|string|void */ protected function getHolidays() { $collection = AB_Holiday::query()->where('staff_id', null)->fetchArray(); $holidays = array(); if (count($collection)) { foreach ($collection as $holiday) { $holidays[$holiday['id']] = array('m' => intval(date('m', strtotime($holiday['date']))), 'd' => intval(date('d', strtotime($holiday['date']))), 'title' => $holiday['title']); // if not repeated holiday, add the year if (!$holiday['repeat_event']) { $holidays[$holiday['id']]['y'] = intval(date('Y', strtotime($holiday['date']))); } } } return json_encode($holidays); }
function update_7_0() { global $wpdb; $wpdb->query('ALTER TABLE `ab_customer_appointment` ADD `coupon_deduction` DECIMAL(10,2) DEFAULT NULL AFTER `coupon_discount`'); $wpdb->query('ALTER TABLE `ab_coupons` CHANGE COLUMN `used` `used` INT UNSIGNED NOT NULL DEFAULT 0, ADD COLUMN `deduction` DECIMAL(10,2) NOT NULL DEFAULT 0 AFTER `discount`, ADD COLUMN `usage_limit` INT UNSIGNED NOT NULL DEFAULT 1'); $wpdb->query('ALTER TABLE `ab_notifications` CHANGE `slug` `type` VARCHAR(255) NOT NULL DEFAULT ""'); // SMS. $wpdb->query('ALTER TABLE `ab_notifications` ADD `gateway` ENUM("email","sms") NOT NULL DEFAULT "email"'); $wpdb->query('UPDATE `ab_notifications` SET `gateway` = "email"'); $sms_notifies = array(array('type' => 'client_new_appointment', 'message' => __("Dear [[CLIENT_NAME]].\nThis is confirmation that you have booked [[SERVICE_NAME]].\nWe are waiting you at [[COMPANY_ADDRESS]] on [[APPOINTMENT_DATE]] at [[APPOINTMENT_TIME]].\nThank you for choosing our company.\n[[COMPANY_NAME]]\n[[COMPANY_PHONE]]\n[[COMPANY_WEBSITE]]", 'bookly'), 'active' => 1), array('type' => 'staff_new_appointment', 'message' => __("Hello.\nYou have new booking.\nService: [[SERVICE_NAME]]\nDate: [[APPOINTMENT_DATE]]\nTime: [[APPOINTMENT_TIME]]\nClient name: [[CLIENT_NAME]]\nClient phone: [[CLIENT_PHONE]]\nClient email: [[CLIENT_EMAIL]]", 'bookly'), 'active' => 0), array('type' => 'client_reminder', 'message' => __("Dear [[CLIENT_NAME]].\nWe would like to remind you that you have booked [[SERVICE_NAME]] tomorrow on [[APPOINTMENT_TIME]]. We are waiting you at [[COMPANY_ADDRESS]].\nThank you for choosing our company.\n[[COMPANY_NAME]]\n[[COMPANY_PHONE]]\n[[COMPANY_WEBSITE]]", 'bookly'), 'active' => 0), array('type' => 'client_follow_up', 'message' => __("Dear [[CLIENT_NAME]].\nThank you for choosing [[COMPANY_NAME]]. We hope you were satisfied with your [[SERVICE_NAME]].\nThank you and we look forward to seeing you again soon.\n[[COMPANY_NAME]]\n[[COMPANY_PHONE]]\n[[COMPANY_WEBSITE]]", 'bookly'), 'active' => 0), array('type' => 'staff_agenda', 'message' => __("Hello.\nYour agenda for tomorrow is:\n[[NEXT_DAY_AGENDA]]", 'bookly'), 'active' => 0), array('type' => 'staff_cancelled_appointment', 'message' => __("Hello.\nThe following booking has been cancelled.\nService: [[SERVICE_NAME]]\nDate: [[APPOINTMENT_DATE]]\nTime: [[APPOINTMENT_TIME]]\nClient name: [[CLIENT_NAME]]\nClient phone: [[CLIENT_PHONE]]\nClient email: [[CLIENT_EMAIL]]", 'bookly'), 'active' => 0), array('type' => 'client_new_wp_user', 'message' => __("Hello.\nAn account was created for you at [[SITE_ADDRESS]]\nYour user details:\nuser: [[NEW_USERNAME]]\npassword: [[NEW_PASSWORD]]\n\nThanks.", 'bookly'), 'active' => 1)); // Insert notifications. foreach ($sms_notifies as $data) { $wpdb->insert('ab_notifications', array('gateway' => 'sms', 'type' => $data['type'], 'subject' => '', 'message' => $data['message'], 'active' => $data['active'])); } // Rename notifications. $notifications = array('client_info' => 'client_new_appointment', 'provider_info' => 'staff_new_appointment', 'evening_next_day' => 'client_reminder', 'evening_after' => 'client_follow_up', 'event_next_day' => 'staff_agenda', 'cancel_appointment' => 'staff_cancelled_appointment', 'new_wp_user' => 'client_new_wp_user'); foreach ($notifications as $from => $to) { $wpdb->query("UPDATE `ab_notifications` SET `type` = '{$to}' WHERE `type` = '{$from}'"); } $this->drop('ab_email_notification'); // Rename tables. $ab_tables = array('ab_appointment' => AB_Appointment::getTableName(), 'ab_category' => AB_Category::getTableName(), 'ab_coupons' => AB_Coupon::getTableName(), 'ab_customer' => AB_Customer::getTableName(), 'ab_customer_appointment' => AB_CustomerAppointment::getTableName(), 'ab_holiday' => AB_Holiday::getTableName(), 'ab_notifications' => AB_Notification::getTableName(), 'ab_payment' => AB_Payment::getTableName(), 'ab_schedule_item_break' => AB_ScheduleItemBreak::getTableName(), 'ab_service' => AB_Service::getTableName(), 'ab_staff' => AB_Staff::getTableName(), 'ab_staff_schedule_item' => AB_StaffScheduleItem::getTableName(), 'ab_staff_service' => AB_StaffService::getTableName()); foreach ($ab_tables as $from => $to) { $wpdb->query("ALTER TABLE `{$from}` RENAME TO `{$to}`"); } $wpdb->query("CREATE TABLE IF NOT EXISTS `" . AB_SentNotification::getTableName() . "` (\n `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,\n `customer_appointment_id` INT UNSIGNED,\n `staff_id` INT UNSIGNED,\n `gateway` ENUM('email','sms') NOT NULL DEFAULT 'email',\n `type` VARCHAR(60) NOT NULL,\n `created` DATETIME NOT NULL,\n CONSTRAINT fk_" . AB_SentNotification::getTableName() . "_" . AB_CustomerAppointment::getTableName() . "_id\n FOREIGN KEY (customer_appointment_id)\n REFERENCES " . AB_CustomerAppointment::getTableName() . "(id)\n ON DELETE CASCADE\n ON UPDATE CASCADE,\n CONSTRAINT fk_" . AB_SentNotification::getTableName() . "_" . AB_Staff::getTableName() . "_id\n FOREIGN KEY (staff_id)\n REFERENCES " . AB_Staff::getTableName() . "(id)\n ON DELETE CASCADE\n ON UPDATE CASCADE\n ) ENGINE = INNODB\n DEFAULT CHARACTER SET = utf8\n COLLATE = utf8_general_ci"); // Google Calendar. add_option('ab_settings_google_event_title', '[[SERVICE_NAME]]'); // Link assets. add_option('ab_settings_link_assets_method', 'enqueue'); // SMS. add_option('ab_sms_default_country_code', ''); }
private function _drop_tables() { /** @var wpdb $wpdb */ global $wpdb; $ab_tables = array(AB_Appointment::getTableName(), AB_Category::getTableName(), AB_Coupon::getTableName(), AB_Customer::getTableName(), AB_CustomerAppointment::getTableName(), AB_Holiday::getTableName(), AB_Notification::getTableName(), AB_Payment::getTableName(), AB_ScheduleItemBreak::getTableName(), AB_SentNotification::getTableName(), AB_Service::getTableName(), AB_Staff::getTableName(), AB_StaffScheduleItem::getTableName(), AB_StaffService::getTableName()); $this->_drop_fk($ab_tables); $wpdb->query('DROP TABLE IF EXISTS `' . implode('`, `', $ab_tables) . '` CASCADE;'); }
/** * 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()); } } } }