public function actionMoveTicket($id)
{
if (!($ticket = models\Ticket::model()->with('current_queue')->findByPk($id))) {
throw new \CHttpException(404, 'Invalid ticket id.');
}
$qs_svc = Yii::app()->service->getService(self::$QUEUESET_SERVICE);
$queueset = $qs_svc->getQueueSetForTicket($ticket->id);
if (!$this->checkQueueSetProcessAccess($queueset)) {
throw new \CHttpException(403, 'Not authorised to take ticket');
}
foreach (array('from_queue_id', 'to_queue_id') as $required_field) {
if (!@$_POST[$required_field]) {
throw new \CHttpException(400, "Missing required form field {$required_field}");
}
}
if ($ticket->current_queue->id != $_POST['from_queue_id']) {
throw new \CHttpException(409, 'Ticket has already moved to a different queue');
}
if (!($to_queue = models\Queue::model()->active()->findByPk($_POST['to_queue_id']))) {
throw new \CHttpException(404, "Cannot find queue with id {$_POST['to_queue_id']}");
}
$api = Yii::app()->moduleAPI->get('PatientTicketing');
list($data, $errs) = $api->extractQueueData($to_queue, $_POST, true);
if (count($errs)) {
echo json_encode(array('errors' => array_values($errs)));
Yii::app()->end();
}
$transaction = Yii::app()->db->beginTransaction();
try {
if ($to_queue->addTicket($ticket, Yii::app()->user, $this->firm, $data)) {
if ($ticket->assignee) {
$ticket->assignee_user_id = null;
$ticket->assignee_date = null;
$ticket->save();
}
$ticket->audit('ticket', 'move', $ticket->id);
$transaction->commit();
$t_svc = Yii::app()->service->getService('PatientTicketing_Ticket');
AutoSaveTicket::clear();
$flsh_id = 'patient-ticketing-';
$flsh_id .= $queueset->getId();
if ($to_queue->outcomes) {
Yii::app()->user->setFlash($flsh_id, $t_svc->getCategoryForTicket($ticket)->name . ' - ' . $ticket->patient->getHSCICName() . ' moved to ' . $to_queue->name);
} else {
Yii::app()->user->setFlash($flsh_id, $t_svc->getCategoryForTicket($ticket)->name . ' - ' . $ticket->patient->getHSCICName() . ' completed');
}
} else {
throw new Exception('unable to assign ticket to queue');
}
} catch (Exception $e) {
$transaction->rollback();
throw $e;
}
$queueset_id = $queueset->getId();
$queueset_model = models\QueueSet::model()->findByPk($queueset_id);
$queueset_category_id = $queueset_model->category_id;
echo json_encode(array('redirectURL' => "/PatientTicketing/default/?queueset_id={$queueset_id}&cat_id={$queueset_category_id}"));
}