<?php
namespace App\Controller\Api;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Entity\StudentScolarity;
use App\Entity\StudentYear;
use App\Repository\StudentYearRepository;
use App\Entity\ScolarityHistory;
use App\Repository\ScolarityHistoryRepository;
use App\Repository\StudentScolarityRepository;
use App\Repository\SchoolYearRepository;
use App\Repository\ScolarityRepository;
use Doctrine\ORM\EntityManagerInterface;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerInterface;
use Knp\Component\Pager\PaginatorInterface;
use OpenApi\Annotations as OA;
class StudentScolarityController extends AbstractController
{
/**
* get user scolarity information
* @OA\Tag(name="api_student_scolarity")
*/
#[Route("/api/parent/student/scolarity/get/v2", name:"api_student_scolarity_v_2", methods:["GET", "POST"])]
public function getScolarityInformationV2(
Request $request,
StudentScolarityRepository $studentScolarityRepository,
ScolarityRepository $scolarityRepo,
StudentYearRepository $studentYearRepo,
SerializerInterface $serializer,
PaginatorInterface $paginator,
SchoolYearRepository $schoolYearRepository
)
{
$year = $schoolYearRepository->findOneBy(['active'=>true],[]);
$school = $this->getUser()->getSchool();
$parentStudents = $studentYearRepo->findParentStudentsById($this->getUser()->getId(), $school->getId());
//dd($parentStudents);
$globalStudentScolarities = [];
$group = [];
foreach ($parentStudents as $sy) {
$scolarities = $studentScolarityRepository->findAllByStudent($sy->getId());
$newTable = [];
foreach ($scolarities as $sco) {
$newTable[] = [
'paid' => $sco->getAmount(),
'restToPaid' => $sco->getScolarity()->getAmount() - $sco->getAmount(),
'totalToPaid' => $sco->getScolarity()->getAmount(),
'endAt' => $sco->getScolarity()->getEndAt(),
'name' => $sco->getScolarity()->getName()
];
}
$sumPaid = (int)$studentScolarityRepository->getSumPaid($sy->getId())[0]['sum'];
$sumToPaid = (int)$scolarityRepo->getSumToPaidByClass($sy->getClasse()->getId())[0]['sum'];
$restToPaid = $sumToPaid - $sumPaid;
$group[] = [
'student' => [
'name' => $sy->getStudent()->getFullName(),
'classe' => $sy->getClasse()->getName()
],
'scolarities' => $newTable
];
$globalStudentScolarities[] = [
'student' => [
'id' => $sy->getStudent()->getId(),
'name' => $sy->getStudent()->getFullName(),
'classe' => $sy->getClasse()->getName(),
'studentYearId' => $sy->getId()
],
'totalPaid' => $sumPaid,
'sumToPaid' => $sumToPaid,
'restToPaid' => $restToPaid
];
}
$data = $paginator->paginate($group,
$request->query->getInt('page', 1),4000);
$response = [
'current_page_number'=>$data->getCurrentPageNumber(),
'number_per_page'=>$data->getItemNumberPerPage(),
'globalStudentScolarities' => $globalStudentScolarities,
'total_count'=>$data->getTotalItemCount(),
'data'=>$data->getItems(),
];
$context = SerializationContext::create()->setGroups(["getScolarity"]);
$jsonScolarity = $serializer->serialize($response,'json', $context);
return new JsonResponse($jsonScolarity, Response::HTTP_OK, ['accept'=>'application/json'], true);
}
/**
* get user scolarity information
* @OA\Tag(name="api_student_scolarity")
*/
#[Route("/api/scolarity/get/", name:"api_student_scolarity", methods:["GET", "POST"])]
public function getScolarityInformation(
Request $request,
StudentScolarityRepository $studentScolarityRepository,
ScolarityRepository $scolarityRepo,
StudentYearRepository $studentYearRepo,
SerializerInterface $serializer,
)
{
$data = json_decode($request->getContent(), true);
$student = $data['student'];
$studentY = $studentYearRepo->findStudentByActiveYear($student);
$scolarities = $studentScolarityRepository->findByStudent($studentY->getId());
$sumPaid = $studentScolarityRepository->getSumPaid($studentY->getId())[0]['sum'];
$sumToPaid = $scolarityRepo->getSumToPaidByClass($studentY->getClasse()->getId())[0]['sum'];
$restToPaid = $sumToPaid - $sumPaid;
$response = [
'scolarities' => $scolarities,
'sumPaid' => $sumToPaid,
'sumPaid' => $sumToPaid,
'resToPaid' => $restToPaid
];
$context = SerializationContext::create()->setGroups(["getScolarity"]);
$jsonScolarity = $serializer->serialize($response,'json', $context);
return new JsonResponse($jsonScolarity, Response::HTTP_OK, ['accept'=>'application/json'], true);
}
/**
* get user scolarity information
* @OA\Tag(name="api_get_student")
*/
#[Route("/api/student/get/", name:"api_student_scolarity", methods:["GET", "POST"])]
public function getStudents(
Request $request,
StudentScolarityRepository $studentScolarityRepository,
ScolarityRepository $scolarityRepo,
StudentYearRepository $studentYearRepo,
SerializerInterface $serializer,
)
{
$data = json_decode($request->getContent(), true);
$student = $data['student'];
$studentY = $studentYearRepo->findStudentByActiveYear($student);
$scolarities = $studentScolarityRepository->findByStudent($studentY->getId());
$sumPaid = $studentScolarityRepository->getSumPaid($studentY->getId())[0]['sum'];
$sumToPaid = $scolarityRepo->getSumToPaidByClass($studentY->getClasse()->getId())[0]['sum'];
$restToPaid = $sumToPaid - $sumPaid;
$response = [
'scolarities' => $scolarities,
'sumPaid' => $sumToPaid,
'sumPaid' => $sumToPaid,
'resToPaid' => $restToPaid
];
$context = SerializationContext::create()->setGroups(["getScolarity"]);
$jsonScolarity = $serializer->serialize($response,'json', $context);
return new JsonResponse($jsonScolarity, Response::HTTP_OK, ['accept'=>'application/json'], true);
}
/**
* get user scolarity information
* @OA\Tag(name="api_student_scolarity_history")
*/
#[Route("/api/scolarity/history/get/", name:"api_student_scolarity_history", methods:["GET", "POST"])]
public function getScolarityHistory(
Request $request,
ScolarityHistoryRepository $scolarityHistoryRepository,
StudentYearRepository $studentYearRepo,
SerializerInterface $serializer
)
{
$data = json_decode($request->getContent(), true);
$student = $data['student'];
$studentY = $studentYearRepo->findStudentByActiveYear($student);
$history = $scolarityHistoryRepository->findByStudent($studentY->getId());
$context = SerializationContext::create()->setGroups(["getScolarityHistory"]);
$Jsonhistory = $serializer->serialize($history,'json', $context);
return new JsonResponse($Jsonhistory, Response::HTTP_OK, ['accept'=>'application/json'], true);
}
/**
* get user scolarity information
* @OA\Tag(name="api_student_scolarity_history")
*/
#[Route("/api/scolarity/paid", name:"api_student_scolarity_paid", methods:["GET", "POST"])]
public function PaidScolarity(
Request $request,
ScolarityHistoryRepository $scolarityHistoryRepository,
StudentYearRepository $studentYearRepo,
SerializerInterface $serializer,
EntityManagerInterface $entityManager,
StudentYear $student,
ScolarityRepository $scolarityRepo,
StudentScolarityRepository $studentScolarityRepository,
)
{
$data = json_decode($request->getContent(), true);
$formAmount = $data['amount'];
$studentScolarity = $studentScolarityRepository->findById($data['scolarityId']);
$student = $studentYearRepo->findStudentByActiveYear($data['student']);
$actualRest = $studentScolarity->getScolarity()->getAmount() - $studentScolarity->getAmount();
$sumPaidT = $studentScolarityRepository->getSumPaid($student->getId())[0]['sum'];
$sumToPaidT = $scolarityRepo->getSumToPaidByClass($student->getClasse()->getId())[0]['sum'];
$restToPaidT = $sumToPaidT - $sumPaidT;
$sumPaid = $studentScolarityRepository->getSumPaid($student->getId())[0]['sum'];
$sumToPaid = $scolarityRepo->getSumToPaidByClass($student->getClasse()->getId())[0]['sum'];
//on verifie que la somme a payer n'est pas superieure a la somme qui reste a payer
$restToPaid = $sumToPaid - $sumPaid;
if ($formAmount > 0) {
if ($restToPaid >= $formAmount) {
//on cree l'historique de paiement...
$scolarityHistory = new ScolarityHistory();
$scolarityHistory->setTotalAmount($formAmount);
$scolarityHistory->setPaidAt(new \DateTimeImmutable('now'));
$scolarityHistory->setStudent($student);
$arrayAmount = [];
$restSliceToPaid = $studentScolarity->getScolarity()->getAmount() - $studentScolarity->getAmount();
$rest = $formAmount - $restSliceToPaid;
if ($rest > 0) {
$scolarities = $studentScolarityRepository->findByStudent($student->getId());
$key = 0;
$studentScolarity->setAmount($studentScolarity->getScolarity()->getAmount());
$entityManager->persist($studentScolarity);
$entityManager->flush();
//on ajoute le montant paye de la tranche dans l'historique avec l'id de la tranche...
$arrayAmount += [$studentScolarity->getId() => $restSliceToPaid];
$scolarityHistory->addScolarity($studentScolarity);
$studentScolarity->addScolarityHistory($scolarityHistory);
$entityManager->persist($scolarityHistory);
$entityManager->persist($studentScolarity);
$entityManager->flush();
while ($rest > 0) {
$scolarity = $scolarities[$key];
$restSliceToPaid = $scolarity->getScolarity()->getAmount() - $scolarity->getAmount();
$rest1 = $rest;
$rest = $rest - $restSliceToPaid;
if ($rest > 0) {
if ($restSliceToPaid > 0) {
$scolarity->setAmount($scolarity->getScolarity()->getAmount());
//on ajoute le montant paye de la tranche dans l'historique avec l'id de la tranche...
$arrayAmount += [$scolarity->getId() => $scolarity->getScolarity()->getAmount()];
$scolarityHistory->addScolarity($scolarity);
$scolarity->addScolarityHistory($scolarityHistory);
$entityManager->persist($scolarity);
$entityManager->persist($scolarityHistory);
$entityManager->flush();
$key += 1;
} else {
$key += 1;
}
} else if ($rest == 0) {
$scolarity->setAmount($scolarity->getAmount() + $restSliceToPaid);
//on ajoute le montant paye de la tranche dans l'historique avec l'id de la tranche...
$arrayAmount += [$scolarity->getId() => ($restSliceToPaid)];
$scolarityHistory->addScolarity($scolarity);
$scolarity->addScolarityHistory($scolarityHistory);
$entityManager->persist($scolarity);
$entityManager->persist($scolarityHistory);
$entityManager->flush();
$key += 1;
} else {
$scolarity->setAmount($scolarity->getAmount() + $rest1);
//on ajoute le montant paye de la tranche dans l'historique avec l'id de la tranche...
$arrayAmount += [$scolarity->getId() => ($rest1)];
$scolarityHistory->addScolarity($scolarity);
$scolarity->addScolarityHistory($scolarityHistory);
$entityManager->persist($scolarity);
$entityManager->persist($scolarityHistory);
$entityManager->flush();
$rest1 = $rest;
$key += 1;
}
}
//on enregistre les differents montants de l'historique de la scolarite
$scolarityHistory->setAmounts($arrayAmount);
$student->addScolarityHistory($scolarityHistory);
$entityManager->persist($scolarityHistory);
$entityManager->persist($student);
$entityManager->flush();
return new JsonResponse(1);
} else {
$studentScolarity->setAmount($formAmount + $studentScolarity->getAmount());
//on ajoute le montant paye de la tranche dans l'historique avec l'id de la tranche...
$scolarityHistory->setAmounts([$studentScolarity->getId() => ($formAmount)]);
$scolarityHistory->addScolarity($studentScolarity);
$studentScolarity->addScolarityHistory($scolarityHistory);
$scolarityHistory->setStudent($student);
$student->addScolarityHistory($scolarityHistory);
$entityManager->persist($studentScolarity);
$entityManager->persist($scolarityHistory);
$entityManager->persist($student);
$entityManager->flush();
return new JsonResponse(1);
}
} else {
return new JsonResponse(2);
}
} else {
return new JsonResponse(1);
}
return new JsonResponse(1);
}
}