src/AppBundle/Controller/v1/RecurringClaimsController.php line 100

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller\v1;
  3. use AppBundle\Claims\RecurringClaimService;
  4. use AppBundle\Claims\DocumentService;
  5. use AppBundle\Entity\ClaimDocument;
  6. use AppBundle\Entity\ClaimView;
  7. use AppBundle\Entity\RecurringClaim;
  8. use AppBundle\Entity\Repository\ClaimViewRepository;
  9. use AppBundle\Entity\Supervisor;
  10. use AppBundle\Form\Type\RecurringClaimType;
  11. use DateTime;
  12. use FOS\RestBundle\Controller\Annotations as REST;
  13. use FOS\RestBundle\Controller\AbstractFOSRestController;
  14. use FOS\RestBundle\Request\ParamFetcher;
  15. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Component\Serializer\Encoder\JsonDecoder;
  20. use Symfony\Component\HttpFoundation\JsonRequest;
  21. class RecurringClaimsController extends AbstractFOSRestController
  22. {
  23.     /**
  24.      * @REST\RequestParam(name="files", map=true, nullable=false)
  25.      * @Security("is_granted('EDIT', claim)")
  26.      */
  27.     public function postRecurringclaimAction(RecurringClaim $claimRequest $request)
  28.     {
  29.         
  30.         $form $this->createForm(RecurringClaimType::class, $claim, array('participant' => $claim->getParticipant()));
  31.         
  32.         $form->submit($request->request->all());
  33.         
  34.         if ($form->isSubmitted() && $form->isValid()) {
  35.             $this->getRecurringClaimService()->markUpdated($claim);
  36.             $this->getDoctrine()->getManager()->flush();
  37.             return $claim;
  38.         }
  39.         return $form;
  40.     }
  41.     
  42.     
  43.     /**
  44.      * Uploads go directly to S3 scratch bucket, at which point they can be
  45.      * attached to the claim and moved to the appropriate location in S3
  46.      * @REST\RequestParam(name="files", map=true, nullable=false)
  47.      * @Security("is_granted('EDIT', claim)")
  48.      * @REST\Post(path="recurringclaims/{claim}/documents")
  49.      * @return array
  50.      */
  51.     public function postRecurringclaimsDocumentsAction(RecurringClaim $claimParamFetcher $fetcher)
  52.     {
  53.         $service $this->getRecurringClaimService();
  54.         
  55.         $user $this->getUser();
  56.         $documents = array();
  57.         $data json_decodefile_get_contents('php://input') );
  58.         // foreach ($fetcher->get('files') as $info) {
  59.         //     $documents[] = $service->attachS3Upload($claim, $user, $info['name'], $info['type']);
  60.         // }
  61.         foreach ($data->files as $info) {
  62.             $documents[] = $service->attachS3Upload($claim$user$info->name$info->type);
  63.         }
  64.         $this->getDoctrine()->getManager()->flush();
  65.         return $documents;
  66.     }
  67.     
  68.     /**
  69.      * @param RecurringClaim $claim
  70.      * @param ClaimDocument $document
  71.      * @Security("is_granted('EDIT', claim)")
  72.      * @REST\Post(path="recurringclaims/{claim}/documents/{id}")
  73.      */
  74.     public function deleteRecurringclaimsDocumentsAction(RecurringClaim $claimClaimDocument $document)
  75.     {
  76.         if ($claim !== $document->getOwner()) {
  77.             throw new NotFoundHttpException();
  78.         }
  79.         $this->getDocumentService()->removeDocument($document);
  80.         $this->getDoctrine()->getManager()->flush();
  81.     }
  82.     
  83.     /**
  84.      * Record a claim view and return a list of other supervisors who are currently viewing this claim
  85.      * @Security("is_granted('PROCESS', claim)")
  86.      * @return Supervisor[]
  87.      */
  88.     public function postRecurringclaimViewsAction(RecurringClaim $claim)
  89.     {
  90.         $view = new ClaimView();
  91.         $view->setRecurringClaim($claim)
  92.             ->setViewer($this->getUser());
  93.         
  94.         $em $this->getDoctrine()->getManager();
  95.         $em->persist($view);
  96.         $em->flush();
  97.         
  98.         if (rand(110) === 1) {
  99.             $this->getClaimViewRepository()->clean();
  100.         }
  101.         
  102.         return $this->getClaimViewRepository()->findViewers($claim$this->getUser());
  103.     }
  104.     /**
  105.      * @param RecurringClaim $claim
  106.      * @Security("is_granted('VIEW', claim)")
  107.      * @return array
  108.      */
  109.     public function getRecurringclaimsDocumentsAction(RecurringClaim $claim)
  110.     {
  111.         return $claim->getDocuments();
  112.     }
  113.     /**
  114.      * Return any one-time claims that have been generated for a recurring claim, with
  115.      * optional parameter to only return claims after a given date.
  116.      *
  117.      * @param RecurringClaim $claim
  118.      * @REST\QueryParam(name="after", requirements="\d{4}-\d{2}-\d{2}", strict=true, nullable=true)
  119.      * @Security("is_granted('VIEW', claim)")
  120.      * @return array
  121.      */
  122.     public function getRecurringclaimsOnetimeAction(RecurringClaim $claimParamFetcher $fetcher)
  123.     {
  124.         $after $fetcher->get('after') ? new DateTime($fetcher->get('after')) : null;
  125.         
  126.         if ($after) {
  127.             return array_merge(array_filter($claim->getClaims(), function($claim) use($after) {
  128.                 
  129.                 return $claim->getDateOfService() >= $after;
  130.             }));
  131.         }
  132.         
  133.         return $claim->getClaims();
  134.     }
  135.     /**
  136.      * Cancel the recurring claim as of a given date
  137.      *
  138.      * @param RecurringClaim $claim
  139.      * @REST\RequestParam(name="date", requirements="\d{4}-\d{2}-\d{2}", strict=true, nullable=false)
  140.      * @Security("is_granted('EDIT', claim)")
  141.      * @REST\Post(path="recurringclaims/{claim}/cancel")  (avoid pluralization)
  142.      * @return bool
  143.      */
  144.     public function postRecurringclaimsCancelAction(RecurringClaim $claimParamFetcher $fetcher)
  145.     {
  146.         $user $this->getUser();
  147.         $supervisorId $user instanceof Supervisor $this->getUser()->getId() : null;
  148.         if (!$claim->isFinal()) {
  149.             $this->getRecurringClaimService()->cancel($claim);
  150.             return true;
  151.         }
  152.         $data json_decodefile_get_contents('php://input') );
  153.         
  154.         // Run through legacy code for the time being
  155.         /* @var $legacy \AppBundle\Bridge\LegacyBridge */
  156.         $legacy $this->get2('bridge.legacy');
  157.         $legacy->load();
  158.         $result $legacy->API_CancelRecurringClaim($claim->getId(), array("OtherEvent"=>"Canceled by {$user->getName()}"), false$data->date$supervisorIdtrue);
  159.         if (!$result['Success']) {
  160.             throw new \RuntimeException(print_r($result['Errors'], true));
  161.         }
  162.         return true;
  163.     }
  164.     
  165.     /**
  166.      * @return ClaimViewRepository
  167.      */
  168.     protected function getClaimViewRepository()
  169.     {
  170.         return $this->getDoctrine()->getRepository(ClaimView::class);
  171.     }
  172.     
  173.     /**
  174.      * @return DocumentService
  175.      */
  176.     protected function getDocumentService()
  177.     {
  178.         return $this->get2('claim_documents');
  179.     }
  180.     
  181.     /**
  182.      * @return RecurringClaimService
  183.      */
  184.     protected function getRecurringClaimService()
  185.     {
  186.         return $this->get2('recurring_claims');
  187.     }
  188. }