/** * Creates a new work order for the specified * request. * * @param string|int $requestId * * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse */ public function store($requestId) { $workRequest = $this->workRequest->find($requestId); /* * If a work order already exists for this request, we'll return * an error and let the user know */ if ($workRequest->workOrder) { $link = link_to_route('maintenance.work-orders.show', 'Show Work Order', [$workRequest->workOrder->id]); $this->message = "A work order already exists for this work request. {$link}"; $this->messageType = 'warning'; $this->redirect = routeBack('maintenance.work-requests.index'); return $this->response(); } $workOrder = $this->workOrder->createFromWorkRequest($workRequest); if ($workOrder) { $link = link_to_route('maintenance.work-orders.show', 'Show', [$workOrder->id]); $this->message = "Successfully generated work order. {$link}"; $this->messageType = 'success'; $this->redirect = routeBack('maintenance.work-orders.show', [$workOrder->id]); } else { $message = 'There was an issue trying to generate a work order for this request. If a work order was deleted that was attached to this request, it will have to be removed/recovered by an administrator before generating another work order.'; $this->message = $message; $this->messageType = 'danger'; $this->redirect = routeBack('maintenance.work-orders.requests.create', [$requestId]); } return $this->response(); }
/** * Creates a work request. * * @return bool|WorkRequest */ public function create() { $this->dbStartTransaction(); try { $workRequest = new $this->model(); $workRequest->user_id = $this->sentry->getCurrentUserId(); $workRequest->subject = $this->getInput('subject', null, true); $workRequest->best_time = $this->getInput('best_time', null, true); $workRequest->description = $this->getInput('description', null, true); if ($workRequest->save()) { $autoGenerate = $this->config->setPrefix('maintenance')->get('rules.work-orders.auto_generate_from_request', true); if ($autoGenerate) { $this->workOrder->createFromWorkRequest($workRequest); } $this->dbCommitTransaction(); return $workRequest; } } catch (\Exception $e) { $this->dbRollbackTransaction(); } return false; }