src/Controller/SubscriptionController.php line 28

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. $year = $this->schoolYearService->sessionYearById();
  74. if( $subscription->getAmount() >= $year->getRate()) {
  75. $student = $subscription->getStudent();
  76. $student->addSubscription($subscription);
  77. $student->setEnrolled(true);
  78. $this->em->persist($subscription);
  79. $this->em->persist($subscription);
  80. $this->em->flush();
  81. $this->addFlash('success', 'Subscription succesfully created');
  82. return $this->redirectToRoute('admin_subscriptions');
  83. } else {
  84. $this->addFlash('warning', 'The amount indicated is not enough to cover the registration');
  85. }
  86. }
  87. return $this->render(
  88. 'subscription/new.html.twig',
  89. ['form' => $form->createView()]
  90. );
  91. }
  92. /**
  93. * Creates a new Subscription entity.
  94. *
  95. * @Route("/create", name="admin_subscriptions_create")
  96. * @Method("POST")
  97. * @Template("AppBundle:Subscription:new.html.twig")
  98. */
  99. public function createAction(Request $request)
  100. {
  101. $subscription = new Subscription();
  102. $form = $this->createForm(new SubscriptionType(), $subscription, ['entityManager' => $this->getDoctrine()->getManager(),]);
  103. if ($form->isSubmitted() && $form->isValid()) {
  104. $student = $subscription->getStudent();
  105. $student->addSubscription($subscription);
  106. $student->setEnrolled(true);
  107. $subscription->setInstant(new \DateTime());
  108. $this->em->persist($subscription);
  109. $this->em->persist($student);
  110. $this->em->flush();
  111. return $this->redirect($this->generateUrl('admin_subscriptions'));
  112. }
  113. return $this->render('subscription/edit.html.twig', array(
  114. 'subscription' => $subscription,
  115. 'form' => $form->createView(),
  116. ));
  117. }
  118. /**
  119. * Retourne vrai ou faux selon que l'élève a déja versé les frais d'inscription pour le compte de l'année
  120. */
  121. public function situation(Student $std, ClassRoom $room)
  122. {
  123. $em = $this->getDoctrine()->getManager();
  124. $year = $this->schoolYearService->sessionYearById();
  125. $payments = $em->getRepository('AppBundle:Payment')->findAnnualPaymentsOfStudent($std, $year);
  126. $total = 0;
  127. foreach ($payments as $p) {
  128. if ($p->getSchoolYear()->getId() == $year->getId()) {
  129. $total += $p->getAmount();
  130. }
  131. }
  132. $inscription = $room->getLevel()->getInscription();
  133. if ($inscription == null) {
  134. return;
  135. }
  136. return ($total >= $inscription) ? true : false;
  137. }
  138. /**
  139. * Displays a form to edit an existing Programme entity.
  140. *
  141. * @Route("/{id}/edit", name="admin_subscriptions_edit", requirements={"id"="\d+"}, methods={"GET","PUT"})
  142. * @Template()
  143. */
  144. public function edit(Request $request, Subscription $subscription): Response
  145. {
  146. $form = $this->createForm(Subscription2Type::class, $subscription, [
  147. 'method' => 'PUT'
  148. ]);
  149. $form->handleRequest($request);
  150. if ($form->isSubmitted() && $form->isValid()) {
  151. $year = $this->schoolYearService->sessionYearById();
  152. if( $subscription->getAmount() >= $year->getRate()) {
  153. $this->em->flush();
  154. $this->addFlash('success', 'Subscription succesfully updated');
  155. return $this->redirectToRoute('admin_subscriptions');
  156. } else {
  157. $this->addFlash('warning', 'The amount indicated is not enough to cover the registration');
  158. }
  159. }
  160. return $this->render('subscription/edit.html.twig', [
  161. 'subscription' => $subscription,
  162. 'form' => $form->createView()
  163. ]);
  164. }
  165. /**
  166. * Deletes a Programme entity.
  167. *
  168. * @Route("/{id}/delete", name="admin_subscriptions_delete", requirements={"id"="\d+"}, methods={"DELETE"})
  169. */
  170. public function delete(Subscription $subscription, Request $request): Response
  171. {
  172. if ($this->isCsrfTokenValid('subscriptions_deletion' . $subscription->getId(), $request->request->get('csrf_token'))) {
  173. $this->em->remove($subscription);
  174. $this->em->flush();
  175. $this->addFlash('info', 'Subscription succesfully deleted');
  176. }
  177. return $this->redirectToRoute('admin_subscriptions');
  178. }
  179. }