src/Controller/PaymentPlanController.php line 179

Open in your IDE?
  1. <?php
  2. // src/Controller/PaymentController.php
  3. namespace App\Controller;
  4. use App\Entity\PaymentPlan;
  5. use App\Entity\Installment;
  6. use App\Entity\ClassRoom;
  7. use App\Form\PaymentPlanType;
  8. use App\Repository\ClassRoomRepository;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  15. use App\Repository\PaymentPlanRepository;
  16. use App\Repository\PaymentRepository;
  17. use App\Repository\InstallmentRepository;
  18. use App\Repository\SchoolYearRepository;
  19. use Doctrine\ORM\EntityManagerInterface;
  20. use App\Service\SchoolYearService;
  21. use Knp\Snappy\Pdf;
  22. /**
  23. * ClassRoom controller.
  24. *
  25. * @Route("/admin/paymentPlans")
  26. */
  27. class PaymentPlanController extends AbstractController
  28. {
  29. private $em;
  30. private $clRepo;
  31. private $scRepo;
  32. private $repo;
  33. private SchoolYearService $schoolYearService;
  34. public function __construct(
  35. EntityManagerInterface $em,
  36. PaymentPlanRepository $repo,
  37. SchoolYearRepository $scRepo,
  38. ClassRoomRepository $clRepo,
  39. InstallmentRepository $instRepo,
  40. SchoolYearService $schoolYearService
  41. ) {
  42. $this->em = $em;
  43. $this->repo = $repo;
  44. $this->scRepo = $scRepo;
  45. $this->clRepo = $clRepo;
  46. $this->instRepo = $instRepo;
  47. $this->schoolYearService = $schoolYearService;
  48. }
  49. /**
  50. * @Route("/", name="admin_paymentPlans")
  51. */
  52. public function index(PaymentPlanRepository $paymentPlanRepository): Response
  53. {
  54. // Utilisez le PaymentRepository pour récupérer tous les paiements
  55. $year = $this->schoolYearService->sessionYearById();
  56. $rooms = $this->clRepo->findAll(array('id' => 'ASC'));
  57. return $this->render('paymentPlan/index.html.twig', [
  58. 'year' => $year,
  59. 'rooms' => $rooms
  60. ]);
  61. }
  62. /**
  63. * @Route("/create", name="admin_paymentPlans_new", methods={"GET","POST"})
  64. */
  65. public function newPaymentPlan(Request $request): Response
  66. {
  67. // Créez une nouvelle instance de PaymentPlan
  68. $paymentPlan = new PaymentPlan();
  69. $paymentPlan->setSchoolYear($this->schoolYearService->sessionYearById());
  70. $installment=null;
  71. foreach ($request->request->all() as $key => $value) {
  72. if(strstr($key, 'tranche_class')){
  73. $installment = new Installment();
  74. $segments = explode("_", $key);
  75. $nbSegments = count($segments);
  76. $roomId = $segments[$nbSegments - 1];
  77. $order = $segments[$nbSegments - 2];
  78. $installment->setPaymentPlan($paymentPlan);
  79. $installment->setClassRoom($this->clRepo->findOneBy(array('id' => $roomId)));
  80. $installment->setRanking($order);
  81. $installment->setAmount(intval($request->request->get($key)));
  82. $this->em->persist($installment);
  83. } else if(strstr($key, 'deadline_class')) {
  84. if($installment!=null) {
  85. $installment->setDeadline(new \DateTime($request->request->get($key)));
  86. $paymentPlan->addInstallment($installment);
  87. $this->em->persist($installment);
  88. } else {
  89. continue;
  90. }
  91. }
  92. $paymentPlan->addInstallment($installment);
  93. }
  94. $this->addFlash('info', 'Payment plan succesfully created');
  95. $this->em->persist($paymentPlan);
  96. $this->em->flush();
  97. return $this->redirectToRoute('admin_paymentPlans');
  98. }
  99. /**
  100. * Displays a form to edit an existing Programme entity.
  101. *
  102. * @Route("/{id}/edit", name="admin_paymentPlans_edit", requirements={"id"="\d+"}, methods={"GET", "PUT"})
  103. * @Template()
  104. */
  105. public function edit(Request $request): Response
  106. {
  107. $paymentPlan = $this->repo->findOneBy(array("id" => $request->attributes->get('id')));
  108. $rooms = $this->clRepo->findAll(array('id' => 'ASC'));
  109. $installments = array();
  110. foreach ($paymentPlan->getInstallments() as $installment) {
  111. $installments[$installment->getClassRoom()->getId()][$installment->getRanking()]=$installment;
  112. }
  113. $form = $this->createForm(PaymentPlanType::class, $paymentPlan, [
  114. 'method' => 'PUT'
  115. ]);
  116. $form->handleRequest($request);
  117. if ($form->isSubmitted() && $form->isValid()) {
  118. foreach ($request->request->all() as $key => $value) {
  119. if(strstr($key, 'tranche_class')){
  120. $segments = explode("_", $key);
  121. $nbSegments = count($segments);
  122. $roomId = $segments[$nbSegments - 1];
  123. $order = $segments[$nbSegments - 2];
  124. $installments[$roomId][$order]->setAmount(intval($request->request->get($key)));
  125. $this->em->persist($installment);
  126. } else if(strstr($key, 'deadline_class')) {
  127. $installments[$roomId][$order]->setDeadline(new \DateTime($request->request->get($key)));
  128. }
  129. $this->em->persist($installment);
  130. }
  131. $this->addFlash('success', 'Payment plan succesfully updated');
  132. $this->em->flush();
  133. return $this->redirect($this->generateUrl('admin_paymentPlans'));
  134. }
  135. return $this->render('paymentPlan/edit.html.twig', [
  136. 'paymentPlan' => $paymentPlan,
  137. 'rooms' => $rooms,
  138. 'installments' => $installments,
  139. 'form' => $form->createView()
  140. ]);
  141. }
  142. #[Route('/{id}', name: 'admin_paymentPlans_delete', methods: ['POST'])]
  143. public function delete(Request $request, PaymentPlan $pp, EntityManagerInterface $entityManager): Response
  144. {
  145. dd($pp);
  146. if ($this->isCsrfTokenValid('delete'.$pp->getId(), $request->request->get('_token'))) {
  147. foreach($pp->getInstallments() as $p){
  148. $entityManager->remove($p);
  149. }
  150. $entityManager->remove($payment);
  151. $entityManager->flush();
  152. }
  153. return $this->redirectToRoute('app_payment_index', [], Response::HTTP_SEE_OTHER);
  154. }
  155. /**
  156. * Displays a pdf of schedule of payment of a class or all the scholl.
  157. *
  158. * @Route("print/{id}", name="admin_payment_plan_print", defaults={"id"=0} )
  159. * @Method("GET")
  160. * @Template()
  161. */
  162. public function print(Pdf $pdf, int $id=0): Response
  163. {
  164. $year = $this->schoolYearService->sessionYearById();
  165. $rooms = $this->clRepo->findAll();
  166. if($id > 0){
  167. $rooms = $this->clRepo->findBy(array("id" => $id));
  168. $installments = $this->instRepo->findBy(array("paymentPlan" => $year->getPaymentPlan(), "classRoom"=>$rooms[0]));
  169. } else {
  170. $installments = $this->instRepo->findBy(array("paymentPlan" => $year->getPaymentPlan()), array( "classRoom"=>"ASC"));
  171. }
  172. $html = $this->renderView('paymentPlan/pdf.html.twig', array(
  173. 'plan' => $year->getPaymentPlan(),
  174. "rooms"=>$rooms,
  175. "installments" => $installments
  176. ));
  177. return new Response(
  178. $pdf->getOutputFromHtml($html),
  179. 200,
  180. array(
  181. 'Content-Type' => 'application/pdf',
  182. 'Content-Disposition' => 'inline; filename="plan_scolarite_'.$year->getCode() . "_". ( count($rooms)==1 ? $rooms[0]->getName():"") . '.pdf"'
  183. )
  184. );
  185. }
  186. }