<?php
namespace AppBundle\Controller\v1;
use AppBundle\Claims\RecurringClaimService;
use AppBundle\Claims\DocumentService;
use AppBundle\Entity\ClaimDocument;
use AppBundle\Entity\ClaimView;
use AppBundle\Entity\RecurringClaim;
use AppBundle\Entity\Repository\ClaimViewRepository;
use AppBundle\Entity\Supervisor;
use AppBundle\Form\Type\RecurringClaimType;
use DateTime;
use FOS\RestBundle\Controller\Annotations as REST;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use FOS\RestBundle\Request\ParamFetcher;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Encoder\JsonDecoder;
use Symfony\Component\HttpFoundation\JsonRequest;
class RecurringClaimsController extends AbstractFOSRestController
{
/**
* @REST\RequestParam(name="files", map=true, nullable=false)
* @Security("is_granted('EDIT', claim)")
*/
public function postRecurringclaimAction(RecurringClaim $claim, Request $request)
{
$form = $this->createForm(RecurringClaimType::class, $claim, array('participant' => $claim->getParticipant()));
$form->submit($request->request->all());
if ($form->isSubmitted() && $form->isValid()) {
$this->getRecurringClaimService()->markUpdated($claim);
$this->getDoctrine()->getManager()->flush();
return $claim;
}
return $form;
}
/**
* Uploads go directly to S3 scratch bucket, at which point they can be
* attached to the claim and moved to the appropriate location in S3
* @REST\RequestParam(name="files", map=true, nullable=false)
* @Security("is_granted('EDIT', claim)")
* @REST\Post(path="recurringclaims/{claim}/documents")
* @return array
*/
public function postRecurringclaimsDocumentsAction(RecurringClaim $claim, ParamFetcher $fetcher)
{
$service = $this->getRecurringClaimService();
$user = $this->getUser();
$documents = array();
$data = json_decode( file_get_contents('php://input') );
// foreach ($fetcher->get('files') as $info) {
// $documents[] = $service->attachS3Upload($claim, $user, $info['name'], $info['type']);
// }
foreach ($data->files as $info) {
$documents[] = $service->attachS3Upload($claim, $user, $info->name, $info->type);
}
$this->getDoctrine()->getManager()->flush();
return $documents;
}
/**
* @param RecurringClaim $claim
* @param ClaimDocument $document
* @Security("is_granted('EDIT', claim)")
* @REST\Post(path="recurringclaims/{claim}/documents/{id}")
*/
public function deleteRecurringclaimsDocumentsAction(RecurringClaim $claim, ClaimDocument $document)
{
if ($claim !== $document->getOwner()) {
throw new NotFoundHttpException();
}
$this->getDocumentService()->removeDocument($document);
$this->getDoctrine()->getManager()->flush();
}
/**
* Record a claim view and return a list of other supervisors who are currently viewing this claim
* @Security("is_granted('PROCESS', claim)")
* @return Supervisor[]
*/
public function postRecurringclaimViewsAction(RecurringClaim $claim)
{
$view = new ClaimView();
$view->setRecurringClaim($claim)
->setViewer($this->getUser());
$em = $this->getDoctrine()->getManager();
$em->persist($view);
$em->flush();
if (rand(1, 10) === 1) {
$this->getClaimViewRepository()->clean();
}
return $this->getClaimViewRepository()->findViewers($claim, $this->getUser());
}
/**
* @param RecurringClaim $claim
* @Security("is_granted('VIEW', claim)")
* @return array
*/
public function getRecurringclaimsDocumentsAction(RecurringClaim $claim)
{
return $claim->getDocuments();
}
/**
* Return any one-time claims that have been generated for a recurring claim, with
* optional parameter to only return claims after a given date.
*
* @param RecurringClaim $claim
* @REST\QueryParam(name="after", requirements="\d{4}-\d{2}-\d{2}", strict=true, nullable=true)
* @Security("is_granted('VIEW', claim)")
* @return array
*/
public function getRecurringclaimsOnetimeAction(RecurringClaim $claim, ParamFetcher $fetcher)
{
$after = $fetcher->get('after') ? new DateTime($fetcher->get('after')) : null;
if ($after) {
return array_merge(array_filter($claim->getClaims(), function($claim) use($after) {
return $claim->getDateOfService() >= $after;
}));
}
return $claim->getClaims();
}
/**
* Cancel the recurring claim as of a given date
*
* @param RecurringClaim $claim
* @REST\RequestParam(name="date", requirements="\d{4}-\d{2}-\d{2}", strict=true, nullable=false)
* @Security("is_granted('EDIT', claim)")
* @REST\Post(path="recurringclaims/{claim}/cancel") (avoid pluralization)
* @return bool
*/
public function postRecurringclaimsCancelAction(RecurringClaim $claim, ParamFetcher $fetcher)
{
$user = $this->getUser();
$supervisorId = $user instanceof Supervisor ? $this->getUser()->getId() : null;
if (!$claim->isFinal()) {
$this->getRecurringClaimService()->cancel($claim);
return true;
}
$data = json_decode( file_get_contents('php://input') );
// Run through legacy code for the time being
/* @var $legacy \AppBundle\Bridge\LegacyBridge */
$legacy = $this->get2('bridge.legacy');
$legacy->load();
$result = $legacy->API_CancelRecurringClaim($claim->getId(), array("OtherEvent"=>"Canceled by {$user->getName()}"), false, $data->date, $supervisorId, true);
if (!$result['Success']) {
throw new \RuntimeException(print_r($result['Errors'], true));
}
return true;
}
/**
* @return ClaimViewRepository
*/
protected function getClaimViewRepository()
{
return $this->getDoctrine()->getRepository(ClaimView::class);
}
/**
* @return DocumentService
*/
protected function getDocumentService()
{
return $this->get2('claim_documents');
}
/**
* @return RecurringClaimService
*/
protected function getRecurringClaimService()
{
return $this->get2('recurring_claims');
}
}