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.        
  56.         $year $this->schoolYearService->sessionYearById();
  57.         $rooms $this->clRepo->findAll(array('id' => 'ASC'));
  58.         
  59.         return $this->render('paymentPlan/index.html.twig', [
  60.             'year' => $year,
  61.             'rooms' => $rooms
  62.         ]);
  63.     }
  64.     /**
  65.      * @Route("/create", name="admin_paymentPlans_new", methods={"GET","POST"})
  66.      */
  67.     public function newPaymentPlan(Request $request): Response
  68.     {
  69.         // Créez une nouvelle instance de PaymentPlan
  70.         $paymentPlan = new PaymentPlan();
  71.         $paymentPlan->setSchoolYear($this->schoolYearService->sessionYearById());
  72.         $installment=null;
  73.         foreach ($request->request->all() as $key => $value) {
  74.             if(strstr($key'tranche_class')){
  75.                     $installment = new Installment();
  76.                     $segments explode("_"$key);
  77.                     $nbSegments count($segments);
  78.                     $roomId $segments[$nbSegments 1];
  79.                     $order $segments[$nbSegments 2];
  80.                     $installment->setPaymentPlan($paymentPlan);
  81.                     $installment->setClassRoom($this->clRepo->findOneBy(array('id' => $roomId)));
  82.                     $installment->setRanking($order);
  83.                     $installment->setAmount(intval($request->request->get($key)));
  84.                     $this->em->persist($installment);
  85.             } else if(strstr($key'deadline_class')) {
  86.                     if($installment!=null)  {
  87.                         $installment->setDeadline(new \DateTime($request->request->get($key)));
  88.                         $paymentPlan->addInstallment($installment);
  89.                         $this->em->persist($installment);
  90.                     } else {
  91.                         continue;
  92.                     }
  93.              }
  94.              $paymentPlan->addInstallment($installment);
  95.              
  96.         }
  97.         $this->addFlash('info''Payment plan succesfully created');
  98.         $this->em->persist($paymentPlan);
  99.         $this->em->flush();
  100.         return $this->redirectToRoute('admin_paymentPlans');
  101.        
  102.     }
  103.     /**
  104.      * Displays a form to edit an existing Programme entity.
  105.      *
  106.      * @Route("/{id}/edit", name="admin_paymentPlans_edit", requirements={"id"="\d+"}, methods={"GET", "PUT"})
  107.      * @Template()
  108.      */
  109.     public function edit(Request $request): Response
  110.     {
  111.         $paymentPlan $this->repo->findOneBy(array("id" => $request->attributes->get('id')));
  112.         $rooms $this->clRepo->findAll(array('id' => 'ASC'));
  113.         $installments = array(); 
  114.         foreach ($paymentPlan->getInstallments() as $installment) {
  115.             $installments[$installment->getClassRoom()->getId()][$installment->getRanking()]=$installment;
  116.         }
  117.         $form $this->createForm(PaymentPlanType::class, $paymentPlan, [
  118.             'method' => 'PUT'
  119.         ]);
  120.         $form->handleRequest($request);
  121.         if ($form->isSubmitted() && $form->isValid()) {
  122.             foreach ($request->request->all() as $key => $value) {
  123.                 if(strstr($key'tranche_class')){
  124.                         $segments explode("_"$key);
  125.                         $nbSegments count($segments);
  126.                         $roomId $segments[$nbSegments 1];
  127.                         $order $segments[$nbSegments 2];
  128.                         $installments[$roomId][$order]->setAmount(intval($request->request->get($key)));
  129.                         $this->em->persist($installment);
  130.                 } else if(strstr($key'deadline_class')) {
  131.                         $installments[$roomId][$order]->setDeadline(new \DateTime($request->request->get($key)));
  132.                  }
  133.                  $this->em->persist($installment);
  134.                  
  135.             }
  136.             $this->addFlash('success''Payment plan succesfully updated');
  137.             $this->em->flush();
  138.             return $this->redirect($this->generateUrl('admin_paymentPlans'));
  139.         }
  140.         return $this->render('paymentPlan/edit.html.twig', [
  141.             'paymentPlan' => $paymentPlan,
  142.             'rooms' => $rooms,
  143.             'installments' => $installments,
  144.             'form' => $form->createView()
  145.         ]);
  146.     }
  147.     
  148.     #[Route('/{id}'name'admin_paymentPlans_delete'methods: ['POST'])]
  149.     public function delete(Request $requestPaymentPlan $ppEntityManagerInterface $entityManager): Response
  150.     {
  151.         dd($pp);
  152.         if ($this->isCsrfTokenValid('delete'.$pp->getId(), $request->request->get('_token'))) {
  153.             foreach($pp->getInstallments() as $p){
  154.                 $entityManager->remove($p);
  155.             }
  156.             $entityManager->remove($payment);
  157.             $entityManager->flush();
  158.         }
  159.         return $this->redirectToRoute('app_payment_index', [], Response::HTTP_SEE_OTHER);
  160.     }
  161.      /**
  162.      * Displays a pdf of schedule of payment of a class or all the scholl.
  163.      *
  164.      * @Route("print/{id}", name="admin_payment_plan_print", defaults={"id"=0}  )
  165.      * @Method("GET")
  166.      * @Template()
  167.      */
  168.     public function print(Pdf $pdfint $id=0): Response
  169.     {
  170.         $year $this->schoolYearService->sessionYearById();
  171.         $rooms $this->clRepo->findAll();
  172.         if($id 0){
  173.             $rooms $this->clRepo->findBy(array("id" => $id));
  174.             $installments $this->instRepo->findBy(array("paymentPlan" => $year->getPaymentPlan(), "classRoom"=>$rooms[0]));
  175.         } else {
  176.             $installments $this->instRepo->findBy(array("paymentPlan" => $year->getPaymentPlan()), array( "classRoom"=>"ASC"));
  177.         }
  178.         $html $this->renderView('paymentPlan/pdf.html.twig', array(
  179.             'plan' => $year->getPaymentPlan(),
  180.             "rooms"=>$rooms
  181.             "installments" => $installments
  182.         ));
  183.         return new Response(
  184.             $pdf->getOutputFromHtml($html),
  185.             200,
  186.             array(
  187.                 'Content-Type'          => 'application/pdf',
  188.                 'Content-Disposition'   => 'inline; filename="plan_scolarite_'.$year->getCode() . "_". ( count($rooms)==?  $rooms[0]->getName():"") . '.pdf"'
  189.             )
  190.         );
  191.     }
  192.     
  193. }