src/Controller/SubscriptionController.php line 56

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Student;
  4. use App\Entity\ClassRoom;
  5. use App\Entity\Subscription;
  6. use App\Form\SubscriptionType;
  7. //use App\Form\Subscription2Type;
  8. use App\Form\Subscription2Type;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use App\Repository\SchoolYearRepository;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use App\Repository\SubscriptionRepository;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  17. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  18. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  19. use App\Service\SchoolYearService;
  20. /**
  21. * Subscription controller.
  22. *
  23. * @Route("/admin/subscriptions")
  24. */
  25. class SubscriptionController extends AbstractController
  26. {
  27. private $em;
  28. private $repo;
  29. private $scRepo;
  30. private SessionInterface $session;
  31. private SchoolYearService $schoolYearService;
  32. public function __construct(SchoolYearService $schoolYearService,EntityManagerInterface $em, SubscriptionRepository $repo, SchoolYearRepository $scRepo, SessionInterface $session)
  33. {
  34. $this->em = $em;
  35. $this->repo = $repo;
  36. $this->scRepo = $scRepo;
  37. $this->session = $session;
  38. $this->schoolYearService = $schoolYearService;
  39. }
  40. /**
  41. * Lists all Subscription entities.
  42. *
  43. * @Route("/", name="admin_subscriptions")
  44. * @Method("GET")
  45. * @Template()
  46. */
  47. public function indexAction()
  48. {
  49. $year = $this->schoolYearService->sessionYearById();
  50. $subscriptions = $this->repo->findEnrollementThisYear($year);
  51. return $this->render('subscription/index.html.twig', compact("subscriptions"));
  52. }
  53. /**
  54. * Finds and displays a subscription entity.
  55. *
  56. * @Route("/{id}/show", name="admin_subscriptions_show", requirements={"id"="\d+"})
  57. * @Method("GET")
  58. * @Template()
  59. */
  60. public function showAction(Subscription $subscription)
  61. {
  62. return $this->render('subscription/show.html.twig', compact("subscription"));
  63. }
  64. /**
  65. * @Route("/create",name="admin_subscriptions_new", methods={"GET","POST"})
  66. */
  67. public function create(Request $request): Response
  68. {
  69. $subscription = new Subscription();
  70. $form = $this->createForm(SubscriptionType::class, $subscription);
  71. $form->handleRequest($request);
  72. if ($form->isSubmitted() && $form->isValid()) {
  73. $student = $subscription->getStudent();
  74. $student->addSubscription($subscription);
  75. $student->setEnrolled(true);
  76. // $subscription->setInstant(new \DateTime());
  77. $this->em->persist($subscription);
  78. $this->em->persist($subscription);
  79. $this->em->flush();
  80. $this->addFlash('success', 'Subscription succesfully created');
  81. return $this->redirectToRoute('admin_subscriptions');
  82. }
  83. return $this->render(
  84. 'subscription/new.html.twig',
  85. ['form' => $form->createView()]
  86. );
  87. }
  88. /**
  89. * Creates a new Subscription entity.
  90. *
  91. * @Route("/create", name="admin_subscriptions_create")
  92. * @Method("POST")
  93. * @Template("AppBundle:Subscription:new.html.twig")
  94. */
  95. public function createAction(Request $request)
  96. {
  97. $subscription = new Subscription();
  98. $form = $this->createForm(new SubscriptionType(), $subscription, ['entityManager' => $this->getDoctrine()->getManager(),]);
  99. if ($form->isSubmitted() && $form->isValid()) {
  100. $student = $subscription->getStudent();
  101. $student->addSubscription($subscription);
  102. $student->setEnrolled(true);
  103. $subscription->setInstant(new \DateTime());
  104. $this->em->persist($subscription);
  105. $this->em->persist($student);
  106. $this->em->flush();
  107. return $this->redirect($this->generateUrl('admin_subscriptions'));
  108. }
  109. return $this->render('subscription/edit.html.twig', array(
  110. 'subscription' => $subscription,
  111. 'form' => $form->createView(),
  112. ));
  113. }
  114. /**
  115. * Retourne vrai ou faux selon que l'élève a déja versé les frais d'inscription pour le compte de l'année
  116. */
  117. public function situation(Student $std, ClassRoom $room)
  118. {
  119. $em = $this->getDoctrine()->getManager();
  120. $year = $this->schoolYearService->sessionYearById();
  121. $payments = $em->getRepository('AppBundle:Payment')->findAnnualPaymentsOfStudent($std, $year);
  122. $total = 0;
  123. foreach ($payments as $p) {
  124. if ($p->getSchoolYear()->getId() == $year->getId()) {
  125. $total += $p->getAmount();
  126. }
  127. }
  128. $inscription = $room->getLevel()->getInscription();
  129. if ($inscription == null) {
  130. return;
  131. }
  132. return ($total >= $inscription) ? true : false;
  133. }
  134. /**
  135. * Displays a form to edit an existing Programme entity.
  136. *
  137. * @Route("/{id}/edit", name="admin_subscriptions_edit", requirements={"id"="\d+"}, methods={"GET","PUT"})
  138. * @Template()
  139. */
  140. public function edit(Request $request, Subscription $subscription): Response
  141. {
  142. $form = $this->createForm(Subscription2Type::class, $subscription, [
  143. 'method' => 'PUT'
  144. ]);
  145. $form->handleRequest($request);
  146. if ($form->isSubmitted() && $form->isValid()) {
  147. $this->em->flush();
  148. $this->addFlash('success', 'Subscription succesfully updated');
  149. return $this->redirectToRoute('admin_subscriptions');
  150. }
  151. return $this->render('subscription/edit.html.twig', [
  152. 'subscription' => $subscription,
  153. 'form' => $form->createView()
  154. ]);
  155. }
  156. /**
  157. * Deletes a Programme entity.
  158. *
  159. * @Route("/{id}/delete", name="admin_subscriptions_delete", requirements={"id"="\d+"}, methods={"DELETE"})
  160. */
  161. public function delete(Subscription $subscription, Request $request): Response
  162. {
  163. if ($this->isCsrfTokenValid('subscriptions_deletion' . $subscription->getId(), $request->request->get('csrf_token'))) {
  164. $this->em->remove($subscription);
  165. $this->em->flush();
  166. $this->addFlash('info', 'Subscription succesfully deleted');
  167. }
  168. return $this->redirectToRoute('admin_subscriptions');
  169. }
  170. }