src/Controller/Api/StudentScolarityController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\JsonResponse;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use App\Entity\StudentScolarity;
  9. use App\Entity\StudentYear;
  10. use App\Repository\StudentYearRepository;
  11. use App\Entity\ScolarityHistory;
  12. use App\Repository\ScolarityHistoryRepository;
  13. use App\Repository\StudentScolarityRepository;
  14. use App\Repository\SchoolYearRepository;
  15. use App\Repository\ScolarityRepository;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use JMS\Serializer\SerializationContext;
  18. use JMS\Serializer\SerializerInterface;
  19. use Knp\Component\Pager\PaginatorInterface;
  20. use OpenApi\Annotations as OA;
  21. class StudentScolarityController extends AbstractController
  22. {
  23.     /**
  24.      * get user scolarity information
  25.      * @OA\Tag(name="api_student_scolarity")
  26.     */ 
  27.     #[Route("/api/parent/student/scolarity/get/v2"name:"api_student_scolarity_v_2"methods:["GET""POST"])]
  28.     public function getScolarityInformationV2(
  29.         Request $request,
  30.         StudentScolarityRepository $studentScolarityRepository,
  31.         ScolarityRepository $scolarityRepo,
  32.         StudentYearRepository $studentYearRepo,
  33.         SerializerInterface $serializer,
  34.         PaginatorInterface $paginator,
  35.         SchoolYearRepository $schoolYearRepository
  36.     )
  37.     {
  38.         
  39.         $year $schoolYearRepository->findOneBy(['active'=>true],[]);
  40.         $school $this->getUser()->getSchool();
  41.         $parentStudents $studentYearRepo->findParentStudentsById($this->getUser()->getId(), $school->getId());
  42.         //dd($parentStudents);
  43.         $globalStudentScolarities = [];
  44.         $group = [];
  45.         foreach ($parentStudents as $sy) {
  46.             $scolarities $studentScolarityRepository->findAllByStudent($sy->getId());
  47.             $newTable = [];
  48.             foreach ($scolarities as $sco) {
  49.                 $newTable[] = [
  50.                     'paid' => $sco->getAmount(),
  51.                     'restToPaid' => $sco->getScolarity()->getAmount() - $sco->getAmount(),
  52.                     'totalToPaid' => $sco->getScolarity()->getAmount(),
  53.                     'endAt' => $sco->getScolarity()->getEndAt(),
  54.                     'name' => $sco->getScolarity()->getName()
  55.                 ];
  56.             }
  57.             $sumPaid = (int)$studentScolarityRepository->getSumPaid($sy->getId())[0]['sum'];
  58.             $sumToPaid = (int)$scolarityRepo->getSumToPaidByClass($sy->getClasse()->getId())[0]['sum'];
  59.             $restToPaid $sumToPaid $sumPaid;
  60.             $group[] = [
  61.                 'student' => [
  62.                     'name' => $sy->getStudent()->getFullName(),
  63.                     'classe' => $sy->getClasse()->getName()
  64.                 ],
  65.                 'scolarities' => $newTable
  66.             ];
  67.             $globalStudentScolarities[] = [
  68.                 'student' => [
  69.                     'id' => $sy->getStudent()->getId(),
  70.                     'name' => $sy->getStudent()->getFullName(),
  71.                     'classe' => $sy->getClasse()->getName(),
  72.                     'studentYearId' => $sy->getId()
  73.                 ],
  74.                 'totalPaid' => $sumPaid,
  75.                 'sumToPaid' => $sumToPaid,
  76.                 'restToPaid' => $restToPaid
  77.             ];
  78.         }
  79.         $data $paginator->paginate($group,
  80.             $request->query->getInt('page'1),4000);
  81.         $response = [
  82.             'current_page_number'=>$data->getCurrentPageNumber(),
  83.             'number_per_page'=>$data->getItemNumberPerPage(),
  84.             'globalStudentScolarities' => $globalStudentScolarities,
  85.             'total_count'=>$data->getTotalItemCount(),
  86.             'data'=>$data->getItems(),
  87.         ];
  88.         $context SerializationContext::create()->setGroups(["getScolarity"]);
  89.         $jsonScolarity $serializer->serialize($response,'json'$context);
  90.         return new JsonResponse($jsonScolarityResponse::HTTP_OK, ['accept'=>'application/json'], true);
  91.     }
  92.     /**
  93.      * get user scolarity information
  94.      * @OA\Tag(name="api_student_scolarity")
  95.     */ 
  96.     #[Route("/api/scolarity/get/"name:"api_student_scolarity"methods:["GET""POST"])]
  97.     public function getScolarityInformation(
  98.         Request $request,
  99.         StudentScolarityRepository $studentScolarityRepository,
  100.         ScolarityRepository $scolarityRepo,
  101.         StudentYearRepository $studentYearRepo,
  102.         SerializerInterface $serializer,
  103.     )
  104.     {
  105.         $data json_decode($request->getContent(), true);
  106.         $student $data['student'];
  107.         $studentY $studentYearRepo->findStudentByActiveYear($student);
  108.         $scolarities $studentScolarityRepository->findByStudent($studentY->getId());
  109.         $sumPaid $studentScolarityRepository->getSumPaid($studentY->getId())[0]['sum'];
  110.         $sumToPaid $scolarityRepo->getSumToPaidByClass($studentY->getClasse()->getId())[0]['sum'];
  111.         $restToPaid $sumToPaid $sumPaid;
  112.         $response = [
  113.             'scolarities' => $scolarities,
  114.             'sumPaid' => $sumToPaid,
  115.             'sumPaid' => $sumToPaid,
  116.             'resToPaid' => $restToPaid
  117.         ];
  118.         $context SerializationContext::create()->setGroups(["getScolarity"]);
  119.         $jsonScolarity $serializer->serialize($response,'json'$context);
  120.         return new JsonResponse($jsonScolarityResponse::HTTP_OK, ['accept'=>'application/json'], true);
  121.     }
  122.     /**
  123.      * get user scolarity information
  124.      * @OA\Tag(name="api_get_student")
  125.     */ 
  126.     #[Route("/api/student/get/"name:"api_student_scolarity"methods:["GET""POST"])]
  127.     public function getStudents(
  128.         Request $request,
  129.         StudentScolarityRepository $studentScolarityRepository,
  130.         ScolarityRepository $scolarityRepo,
  131.         StudentYearRepository $studentYearRepo,
  132.         SerializerInterface $serializer,
  133.     )
  134.     {
  135.         $data json_decode($request->getContent(), true);
  136.         $student $data['student'];
  137.         $studentY $studentYearRepo->findStudentByActiveYear($student);
  138.         $scolarities $studentScolarityRepository->findByStudent($studentY->getId());
  139.         $sumPaid $studentScolarityRepository->getSumPaid($studentY->getId())[0]['sum'];
  140.         $sumToPaid $scolarityRepo->getSumToPaidByClass($studentY->getClasse()->getId())[0]['sum'];
  141.         $restToPaid $sumToPaid $sumPaid;
  142.         $response = [
  143.             'scolarities' => $scolarities,
  144.             'sumPaid' => $sumToPaid,
  145.             'sumPaid' => $sumToPaid,
  146.             'resToPaid' => $restToPaid
  147.         ];
  148.         $context SerializationContext::create()->setGroups(["getScolarity"]);
  149.         $jsonScolarity $serializer->serialize($response,'json'$context);
  150.         return new JsonResponse($jsonScolarityResponse::HTTP_OK, ['accept'=>'application/json'], true);
  151.     }
  152.     
  153.     /**
  154.      * get user scolarity information
  155.      * @OA\Tag(name="api_student_scolarity_history")
  156.     */ 
  157.     #[Route("/api/scolarity/history/get/"name:"api_student_scolarity_history"methods:["GET""POST"])]
  158.     public function getScolarityHistory(
  159.         Request $request,
  160.         ScolarityHistoryRepository $scolarityHistoryRepository,
  161.         StudentYearRepository $studentYearRepo,
  162.         SerializerInterface $serializer
  163.     )
  164.     {
  165.         $data json_decode($request->getContent(), true);
  166.         $student $data['student'];
  167.         $studentY $studentYearRepo->findStudentByActiveYear($student);
  168.         $history $scolarityHistoryRepository->findByStudent($studentY->getId());
  169.         $context SerializationContext::create()->setGroups(["getScolarityHistory"]);
  170.         $Jsonhistory $serializer->serialize($history,'json'$context);
  171.         return new JsonResponse($JsonhistoryResponse::HTTP_OK, ['accept'=>'application/json'], true);
  172.     }
  173.     
  174.     /**
  175.      * get user scolarity information
  176.      * @OA\Tag(name="api_student_scolarity_history")
  177.     */ 
  178.     #[Route("/api/scolarity/paid"name:"api_student_scolarity_paid"methods:["GET""POST"])]
  179.     public function PaidScolarity(
  180.         Request $request,
  181.         ScolarityHistoryRepository $scolarityHistoryRepository,
  182.         StudentYearRepository $studentYearRepo,
  183.         SerializerInterface $serializer,
  184.         EntityManagerInterface $entityManager,
  185.         StudentYear $student,
  186.         ScolarityRepository $scolarityRepo,
  187.         StudentScolarityRepository $studentScolarityRepository,
  188.     )
  189.     {
  190.         
  191.         $data json_decode($request->getContent(), true);
  192.         $formAmount $data['amount'];
  193.         $studentScolarity $studentScolarityRepository->findById($data['scolarityId']);
  194.         $student $studentYearRepo->findStudentByActiveYear($data['student']);
  195.         $actualRest $studentScolarity->getScolarity()->getAmount() - $studentScolarity->getAmount();
  196.         $sumPaidT $studentScolarityRepository->getSumPaid($student->getId())[0]['sum'];
  197.         $sumToPaidT $scolarityRepo->getSumToPaidByClass($student->getClasse()->getId())[0]['sum'];
  198.         $restToPaidT $sumToPaidT $sumPaidT;
  199.         
  200.         $sumPaid $studentScolarityRepository->getSumPaid($student->getId())[0]['sum'];
  201.         $sumToPaid $scolarityRepo->getSumToPaidByClass($student->getClasse()->getId())[0]['sum'];
  202.         //on verifie que la somme a payer n'est pas superieure a la somme qui reste a payer
  203.         $restToPaid $sumToPaid $sumPaid;
  204.         if ($formAmount 0) {
  205.             if ($restToPaid >= $formAmount) {
  206.                 //on cree l'historique de paiement...
  207.                 $scolarityHistory = new ScolarityHistory();
  208.                 $scolarityHistory->setTotalAmount($formAmount);
  209.                 $scolarityHistory->setPaidAt(new \DateTimeImmutable('now'));
  210.                 $scolarityHistory->setStudent($student);
  211.                 $arrayAmount = [];
  212.                 $restSliceToPaid $studentScolarity->getScolarity()->getAmount() - $studentScolarity->getAmount();
  213.                 $rest $formAmount $restSliceToPaid;
  214.                 if ($rest 0) {
  215.                     $scolarities $studentScolarityRepository->findByStudent($student->getId());
  216.                     $key 0;
  217.                     $studentScolarity->setAmount($studentScolarity->getScolarity()->getAmount());
  218.                     $entityManager->persist($studentScolarity);
  219.                     $entityManager->flush();
  220.                     //on ajoute le montant paye de la tranche dans l'historique avec l'id de la tranche...
  221.                     $arrayAmount += [$studentScolarity->getId() => $restSliceToPaid];
  222.                     $scolarityHistory->addScolarity($studentScolarity);
  223.                     $studentScolarity->addScolarityHistory($scolarityHistory);
  224.                     $entityManager->persist($scolarityHistory);
  225.                     $entityManager->persist($studentScolarity);
  226.                     $entityManager->flush();
  227.                     while ($rest 0) {
  228.                         $scolarity $scolarities[$key];
  229.                         $restSliceToPaid $scolarity->getScolarity()->getAmount() - $scolarity->getAmount();
  230.                         $rest1 $rest;
  231.                         $rest $rest $restSliceToPaid;
  232.                         if ($rest 0) {
  233.                             if ($restSliceToPaid 0) {
  234.                                 $scolarity->setAmount($scolarity->getScolarity()->getAmount());
  235.                                 //on ajoute le montant paye de la tranche dans l'historique avec l'id de la tranche...
  236.                                 $arrayAmount += [$scolarity->getId() => $scolarity->getScolarity()->getAmount()];
  237.                                 $scolarityHistory->addScolarity($scolarity);
  238.                                 $scolarity->addScolarityHistory($scolarityHistory);
  239.                                 $entityManager->persist($scolarity);
  240.                                 $entityManager->persist($scolarityHistory);
  241.                                 $entityManager->flush();
  242.                                 $key += 1;
  243.                             } else {
  244.                                 $key += 1;
  245.                             }
  246.                         } else if ($rest == 0) {
  247.                             $scolarity->setAmount($scolarity->getAmount() + $restSliceToPaid);
  248.                             //on ajoute le montant paye de la tranche dans l'historique avec l'id de la tranche...
  249.                             $arrayAmount += [$scolarity->getId() => ($restSliceToPaid)];
  250.                             $scolarityHistory->addScolarity($scolarity);
  251.                             $scolarity->addScolarityHistory($scolarityHistory);
  252.                             $entityManager->persist($scolarity);
  253.                             $entityManager->persist($scolarityHistory);
  254.                             $entityManager->flush();
  255.                             $key += 1;
  256.                         } else {
  257.                             $scolarity->setAmount($scolarity->getAmount() + $rest1);
  258.                             //on ajoute le montant paye de la tranche dans l'historique avec l'id de la tranche...
  259.                             $arrayAmount += [$scolarity->getId() => ($rest1)];
  260.                             $scolarityHistory->addScolarity($scolarity);
  261.                             $scolarity->addScolarityHistory($scolarityHistory);
  262.                             $entityManager->persist($scolarity);
  263.                             $entityManager->persist($scolarityHistory);
  264.                             $entityManager->flush();
  265.                             $rest1 $rest;
  266.                             $key += 1;
  267.                         }
  268.                     }
  269.                     //on enregistre les differents montants de l'historique de la scolarite
  270.                     $scolarityHistory->setAmounts($arrayAmount);
  271.                     $student->addScolarityHistory($scolarityHistory);
  272.                     $entityManager->persist($scolarityHistory);
  273.                     $entityManager->persist($student);
  274.                     $entityManager->flush();
  275.                     return new JsonResponse(1);
  276.                 } else {
  277.                     $studentScolarity->setAmount($formAmount $studentScolarity->getAmount());
  278.                     //on ajoute le montant paye de la tranche dans l'historique avec l'id de la tranche...
  279.                     $scolarityHistory->setAmounts([$studentScolarity->getId() => ($formAmount)]);
  280.                     $scolarityHistory->addScolarity($studentScolarity);
  281.                     $studentScolarity->addScolarityHistory($scolarityHistory);
  282.                     $scolarityHistory->setStudent($student);
  283.                     $student->addScolarityHistory($scolarityHistory);
  284.                     $entityManager->persist($studentScolarity);
  285.                     $entityManager->persist($scolarityHistory);
  286.                     $entityManager->persist($student);
  287.                     $entityManager->flush();
  288.                     return new JsonResponse(1);
  289.                 }
  290.             } else {
  291.                 
  292.                 return new JsonResponse(2);
  293.             }
  294.         } else {
  295.             return new JsonResponse(1);
  296.         }
  297.         
  298.         return new JsonResponse(1);
  299.     }
  300. }