/** * @param AB_Notification $notification */ public function processNotification(AB_Notification $notification) { /** @var $wpdb wpdb */ global $wpdb; $date = new DateTime(); switch ($notification->get('type')) { case 'staff_agenda': if ($date->format('H') >= 18) { $rows = $wpdb->get_results('SELECT `a`.*, `c`.`name` AS `customer_name`, `s`.`title` AS `service_title`, `st`.`email` AS `staff_email`, `st`.`phone` AS `staff_phone`, `st`.`full_name` AS `staff_name` FROM `' . AB_CustomerAppointment::getTableName() . '` `ca` LEFT JOIN `' . AB_Appointment::getTableName() . '` `a` ON `a`.`id` = `ca`.`appointment_id` LEFT JOIN `' . AB_Customer::getTableName() . '` `c` ON `c`.`id` = `ca`.`customer_id` LEFT JOIN `' . AB_Service::getTableName() . '` `s` ON `s`.`id` = `a`.`service_id` LEFT JOIN `' . AB_Staff::getTableName() . '` `st` ON `st`.`id` = `a`.`staff_id` LEFT JOIN `' . AB_StaffService::getTableName() . '` `ss` ON `ss`.`staff_id` = `a`.`staff_id` AND `ss`.`service_id` = `a`.`service_id` WHERE DATE(DATE_ADD("' . $this->mysql_now . '", INTERVAL 1 DAY)) = DATE(`a`.`start_date`) AND NOT EXISTS ( SELECT * FROM `' . AB_SentNotification::getTableName() . '` `sn` WHERE DATE(`sn`.`created`) = DATE("' . $this->mysql_now . '") AND `sn`.`gateway` = "' . $notification->get('gateway') . '" AND `sn`.`type` = "staff_agenda" AND `sn`.`staff_id` = `a`.`staff_id` )'); if ($rows) { $appointments = array(); foreach ($rows as $row) { $appointments[$row->staff_id][] = $row; } foreach ($appointments as $staff_id => $collection) { $sent = false; $staff_email = null; $staff_phone = null; $table = $notification->get('gateway') == 'email' ? '<table>%s</table>' : '%s'; $tr = $notification->get('gateway') == 'email' ? '<tr><td>%s</td><td>%s</td><td>%s</td></tr>' : "%s %s %s\n"; $agenda = ''; foreach ($collection as $appointment) { $startDate = new DateTime($appointment->start_date); $endDate = new DateTime($appointment->end_date); $agenda .= sprintf($tr, $startDate->format('H:i') . '-' . $endDate->format('H:i'), $appointment->service_title, $appointment->customer_name); $staff_email = $appointment->staff_email; $staff_phone = $appointment->staff_phone; } $agenda = sprintf($table, $agenda); if ($staff_email || $staff_phone) { $replacement = new AB_NotificationCodes(); $replacement->set('next_day_agenda', $agenda); $replacement->set('appointment_datetime', $appointment->start_date); $replacement->set('staff_name', $appointment->staff_name); if ($notification->get('gateway') == 'email' && $staff_email) { $message = $replacement->replace($notification->get('message')); $subject = $replacement->replace($notification->get('subject')); // Send email. $sent = wp_mail($staff_email, $subject, wpautop($message), AB_Utils::getEmailHeaders()); } else { if ($notification->get('gateway') == 'sms' && $staff_phone) { $message = $replacement->replace($notification->get('message'), $notification->get('gateway')); // Send sms. $sent = $this->sms->sendSms($staff_phone, $message); } } } if ($sent) { $sent_notification = new AB_SentNotification(); $sent_notification->set('staff_id', $staff_id); $sent_notification->set('gateway', $notification->get('gateway')); $sent_notification->set('type', 'staff_agenda'); $sent_notification->set('created', $date->format('Y-m-d H:i:s')); $sent_notification->save(); } } } } break; case 'client_follow_up': if ($date->format('H') >= 21) { $rows = $wpdb->get_results('SELECT `a`.*, `ca`.* FROM `' . AB_CustomerAppointment::getTableName() . '` `ca` LEFT JOIN `' . AB_Appointment::getTableName() . '` `a` ON `a`.`id` = `ca`.`appointment_id` WHERE DATE("' . $this->mysql_now . '") = DATE(`a`.`start_date`) AND NOT EXISTS ( SELECT * FROM `' . AB_SentNotification::getTableName() . '` `sn` WHERE DATE(`sn`.`created`) = DATE("' . $this->mysql_now . '") AND `sn`.`gateway` = "' . $notification->get('gateway') . '" AND `sn`.`type` = "client_follow_up" AND `sn`.`customer_appointment_id` = `ca`.`id` )', ARRAY_A); if ($rows) { foreach ($rows as $row) { $customer_appointment = new AB_CustomerAppointment(); $customer_appointment->load($row['id']); if (AB_NotificationSender::sendFromCron(AB_NotificationSender::CRON_FOLLOW_UP_EMAIL, $notification, $customer_appointment)) { $sent_notification = new AB_SentNotification(); $sent_notification->set('customer_appointment_id', $customer_appointment->get('id')); $sent_notification->set('gateway', $notification->get('gateway')); $sent_notification->set('type', 'client_follow_up'); $sent_notification->set('created', $date->format('Y-m-d H:i:s')); $sent_notification->save(); } } } } break; case 'client_reminder': if ($date->format('H') >= 18) { $rows = $wpdb->get_results('SELECT `ca`.`id` FROM `' . AB_CustomerAppointment::getTableName() . '` `ca` LEFT JOIN `' . AB_Appointment::getTableName() . '` `a` ON `a`.`id` = `ca`.`appointment_id` WHERE DATE(DATE_ADD("' . $this->mysql_now . '", INTERVAL 1 DAY)) = DATE(`a`.`start_date`) AND NOT EXISTS ( SELECT * FROM `' . AB_SentNotification::getTableName() . '` `sn` WHERE DATE(`sn`.`created`) = DATE("' . $this->mysql_now . '") AND `sn`.`gateway` = "' . $notification->get('gateway') . '" AND `sn`.`type` = "client_reminder" AND `sn`.`customer_appointment_id` = `ca`.`id` )', ARRAY_A); if ($rows) { foreach ($rows as $row) { $customer_appointment = new AB_CustomerAppointment(); $customer_appointment->load($row['id']); if (AB_NotificationSender::sendFromCron(AB_NotificationSender::CRON_NEXT_DAY_APPOINTMENT, $notification, $customer_appointment)) { $sent_notification = new AB_SentNotification(); $sent_notification->set('customer_appointment_id', $customer_appointment->get('id')); $sent_notification->set('gateway', $notification->get('gateway')); $sent_notification->set('type', 'client_reminder'); $sent_notification->set('created', $date->format('Y-m-d H:i:s')); $sent_notification->save(); } } } } break; } }
/** * 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); }