public function cancel($reason_id, $comment = null, $cancellation_user_id = false)
{
if (!($reason = OphTrOperationbooking_Operation_Cancellation_Reason::model()->findByPk($reason_id))) {
return array('result' => false, 'errors' => array(array('Please select a cancellation reason')));
}
$this->operation_cancellation_date = date('Y-m-d H:i:s');
$this->cancellation_reason_id = $reason_id;
$this->cancellation_comment = $comment;
$this->cancellation_user_id = $cancellation_user_id ? $cancellation_user_id : Yii::app()->session['user']->id;
$this->status_id = OphTrOperationbooking_Operation_Status::model()->find('name=?', array('Cancelled'))->id;
if (!$this->save()) {
return array('result' => false, 'errors' => $this->getErrors());
}
OELog::log("Operation cancelled: {$this->id}");
$this->audit('operation', 'cancel');
$episode = $this->event->episode;
$episode->episode_status_id = 5;
if (!$episode->save()) {
throw new Exception('Unable to change episode status for episode ' . $episode->id);
}
if ($this->booking) {
$this->booking->booking_cancellation_date = date('Y-m-d H:i:s');
$this->booking->cancellation_reason_id = $reason_id;
$this->booking->cancellation_comment = $comment;
$this->booking->cancellation_user_id = $cancellation_user_id ? $cancellation_user_id : Yii::app()->session['user']->id;
if (!$this->booking->save()) {
return array('result' => false, 'errors' => $this->booking->getErrors());
}
OELog::log("Booking cancelled: {$this->booking->id}");
$this->booking->audit('booking', 'cancel');
if (Yii::app()->params['urgent_booking_notify_hours'] && Yii::app()->params['urgent_booking_notify_email']) {
if (strtotime($this->booking->session_date) <= strtotime(date('Y-m-d')) + Yii::app()->params['urgent_booking_notify_hours'] * 3600) {
if (!is_array(Yii::app()->params['urgent_booking_notify_email'])) {
$targets = array(Yii::app()->params['urgent_booking_notify_email']);
} else {
$targets = Yii::app()->params['urgent_booking_notify_email'];
}
foreach ($targets as $email) {
mail($email, '[OpenEyes] Urgent cancellation made', "A cancellation was made with a TCI date within the next 24 hours.\n\nDisorder: " . $this->getDisorderText() . "\n\nPlease see: http://" . @$_SERVER['SERVER_NAME'] . Yii::app()->createUrl('/OphTrOperationbooking/transport') . "\n\nIf you need any assistance you can reply to this email and one of the OpenEyes support personnel will respond.", 'From: ' . Yii::app()->params['urgent_booking_notify_email_from'] . "\r\n");
}
}
}
}
return array('result' => true);
}