generateUrl('app_admin_document_check', ['uuid' => $document->getUuid()]); $form = $this->createForm( DocumentCheckType::class, $formData, [ 'action' => $formAction, 'ajax_submit' => true, 'document_type' => $document->getType(), ] ); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { switch ($formData->getStatus()) { case Upload::STATUS_CHECKED: $this->confirmDocument($document, 'confirm_contract', Upload::STATUS_CHECKED); break; case Upload::STATUS_PAID: $this->confirmDocument($document, 'confirm_invoice', Upload::STATUS_PAID); break; case Upload::STATUS_REJECTED: $this->rejectDocument($document, $formData->getComment()); $this->eventDispatcher->dispatch(new DocumentRejectedEvent($formData), DocumentRejectedEvent::NAME); } $returnUrl = $this->generateUrl('app_admin_document_index'); $response->setCloseAndRedirect($returnUrl); } else { $response->setContent($this->renderView('admin/document/check.html.twig', [ 'document' => $document, 'form' => $form, ])); } return $this->json($response); } private function rejectDocument(Upload $document, string $comment = null): void { $transition = Upload::TYPE_CONTRACT === $document->getType() ? 'reject_contract' : 'reject_invoice'; $this->workflow->apply($document->getDisposition(), $transition); $document ->setStatus(Upload::STATUS_REJECTED) ->setComment($comment) ; $this->entityManager->flush(); $this->addFlash('success', 'Das Dokument wurde abgelehnt'); $this->logger->info('Reject document', [ 'document' => $document->getOriginalFilename(), ]); } private function confirmDocument(Upload $document, string $transition, string $status): void { if (true === $this->workflow->can($document->getDisposition(), $transition)) { $document->setStatus($status); $this->workflow->apply($document->getDisposition(), $transition); $this->entityManager->flush(); $this->addFlash('success', 'Das Dokument wurde bestÃĪtigt'); $this->logger->info('Confirm document', [ 'document' => $document->getOriginalFilename(), ]); } else { $blockers = $this->workflow->buildTransitionBlockerList($document->getDisposition(), $transition); foreach ($blockers as $blocker) { $this->addFlash('error', $blocker->getMessage()); } } } }