src/Controller/Accounting/AccountingStudentRegistrationFeeShedulController.php line 77

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Accounting;
  3. use App\Entity\User;
  4. use DateTimeImmutable;
  5. use App\Entity\LogOperation;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use App\Entity\AccountingStudentRegistrationFeeShedul;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use App\Repository\AccountingStudentRegistrationFeeShedulRepository;
  13. /**
  14.  * @Route("/accounting/student-registration-fee-shedul")
  15.  */
  16. class AccountingStudentRegistrationFeeShedulController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("/delete-selection", name="accounting_student_registration_fee_sheduls_selected_delete", methods={"GET"})
  20.     */
  21.     public function deleteSelected(Request $requestAccountingStudentRegistrationFeeShedulRepository $entityRepository): Response
  22.     {
  23.         $list $request->get('entities');
  24.         $entityManager $this->getDoctrine()->getManager();
  25.         $errors 0;
  26.         foreach ($list as $key => $id) {
  27.             $entity $entityRepository->findOneBy(['id' => intval($id)], []);
  28.             if ($entity != null) {
  29.                 if (count($entity->getAccountingStudentRegistrationFeeShedulPayments()) <= 0) {
  30.                     $entityManager->remove($entity);
  31.                 }
  32.             }
  33.         }
  34.         try {
  35.             $entityManager->flush();
  36.             $this->addFlash('success'"Traitement effectué");
  37.             return $this->json(['code' => 200'message' => "Traitement effectué :)"], 200);
  38.         } catch (\Throwable $th) {
  39.             //$th->getMessage()
  40.             $this->addFlash('warning'$th->getMessage());
  41.         }
  42.         
  43.         $this->addFlash('warning'"Traitement non effectué");
  44.         return $this->json(['code' => 500'message' => "Traitement non effectué"], 200);
  45.     }
  46.     /**
  47.      * @Route("/delete/{id}", name="accounting_student_registration_fee_shedul_delete", methods={"GET"})
  48.     */
  49.     public function delete(AccountingStudentRegistrationFeeShedul $accountingStudentRegistrationFeeShedul): Response
  50.     {
  51.         if ($accountingStudentRegistrationFeeShedul->getAmountPaid() > 0) {
  52.             return $this->json(['code' => 500'message' => "Impossible de supprimer cet échéancier. Des paiement ont été effectués dessus."], 200);
  53.         }
  54.         $entityManager $this->getDoctrine()->getManager();
  55.         $entityManager->remove($accountingStudentRegistrationFeeShedul);
  56.         try {
  57.             $entityManager->flush();
  58.             return $this->json(['code' => 200'message' => "Suppression effectuée :)"], 200);
  59.         } catch (\Throwable $th) {
  60.             //$th->getMessage()
  61.             $this->addFlash('warning'$th->getMessage());
  62.         }
  63.         return $this->json(['code' => 500'message' => "Suppression non effectuée"], 200);
  64.     }
  65.     /**
  66.      * @Route("/cancel/{id}", name="accounting_student_registration_fee_shedul_cancel", methods={"GET"})
  67.     */
  68.     public function cancel(Request $requestAccountingStudentRegistrationFeeShedul $accountingStudentRegistrationFeeShedul): Response
  69.     {
  70.         if ($accountingStudentRegistrationFeeShedul->getAmountCancel() > 0) {
  71.             $this->addFlash('warning'"Impossible, une remise à déjà été effectuée sur cet échéancier.");
  72.             return $this->redirectToRoute('registration_student_registration_show', ['id' => $accountingStudentRegistrationFeeShedul->getStudentRegistrationFee()->getStudentRegistration()->getId()]);
  73.         }
  74.         $amount $request->get('shedulAmount');
  75.         if ($amount <= 0) {
  76.             $this->addFlash('warning'"Impossible, le montant de la remise doit être supérieur à zéro");
  77.             return $this->redirectToRoute('registration_student_registration_show', ['id' => $accountingStudentRegistrationFeeShedul->getStudentRegistrationFee()->getStudentRegistration()->getId()]);
  78.         }
  79.         if ($accountingStudentRegistrationFeeShedul->getAmountRest() <= 0) {
  80.             $this->addFlash('warning'"Impossible, le reste à payer doit être supérieur à zéro(0)");
  81.             return $this->redirectToRoute('registration_student_registration_show', ['id' => $accountingStudentRegistrationFeeShedul->getStudentRegistrationFee()->getStudentRegistration()->getId()]);
  82.         }
  83.         if ($accountingStudentRegistrationFeeShedul->getAmountRest() < $amount) {
  84.             $this->addFlash('warning'"Impossible, le montant de la remise doit être inférieur ou égale au reste à payer");
  85.             return $this->redirectToRoute('registration_student_registration_show', ['id' => $accountingStudentRegistrationFeeShedul->getStudentRegistrationFee()->getStudentRegistration()->getId()]);
  86.         }
  87.         
  88.         $entityManager $this->getDoctrine()->getManager();
  89.         $accountingStudentRegistrationFeeShedul->setAmountCancel($amount);
  90.         //$accountingStudentRegistrationFeeShedul->setIsCancel(true);
  91.         try {
  92.             $entityManager->flush();
  93.             $this->addFlash('success'"Remise effectué :)");
  94.         } catch (\Throwable $th) {
  95.             $this->addFlash('warning'$th->getMessage());
  96.         }
  97.         
  98.         return $this->redirectToRoute('registration_student_registration_show', ['id' => $accountingStudentRegistrationFeeShedul->getStudentRegistrationFee()->getStudentRegistration()->getId()]);
  99.     }
  100.     /**
  101.      * 
  102.      * @Route("/api/shedul-update-date-due/{id}", name="api_accounting_student_registration_fee_shedul_update_date_due", methods={"GET"})
  103.     */
  104.     public function api_fee_shedul_update_date_due(Request $requestAccountingStudentRegistrationFeeShedul $accountingStudentRegistrationFeeShedulEntityManagerInterface $entityManager): Response
  105.     {
  106.         /**@var User $user */
  107.         $user $this->getUser();
  108.         $schoolYear $user->getSchoolYear();
  109.         $establishment $user->getEstablishment();
  110.         if ($request->get('date_due') == '' or $request->get('date_due') == null) {
  111.             return $this->json(['code' => 500'message' => "Impossible: Veuillez selectionner une date SVP."], 200);
  112.         }
  113.         if ($request->get('date_due') <= date('Y-m-d')) {
  114.             return $this->json(['code' => 500'message' => "Impossible: Veuillez selectionner une date superieur à la date du jour SVP."], 200);
  115.         }
  116.         if (!$this->isGranted("ROLE_ADMIN")) {
  117.             if ($request->get('date_due') > $establishment->getFeeShedulEndDate()->format('Y-12-31')) {
  118.                 return $this->json(['code' => 500'message' => "Impossible: date trop éloigné. Veuillez contacter votre administrateur"], 200);
  119.             }
  120.         }
  121.         try {
  122.             $date_due = new DateTimeImmutable($request->get('date_due'));
  123.         } catch (\Throwable $th) {
  124.             return $this->json(['code' => 500'message' => $th->getMessage()], 200);
  125.         }
  126.         try {
  127.             $logOperation = new LogOperation(
  128.                 $request->getClientIp(), 
  129.                 $accountingStudentRegistrationFeeShedul->getId(), 
  130.                 AccountingStudentRegistrationFeeShedul::class, 
  131.                 "Modification échéance : <strong>".$accountingStudentRegistrationFeeShedul->getStudentRegistrationFee()->getFee()->getLabel()."( ".$accountingStudentRegistrationFeeShedul->getDateDue()->format('d/m/Y').": ".number_format($accountingStudentRegistrationFeeShedul->getAmount(), 0','' ')." XOF)".", élève : " $accountingStudentRegistrationFeeShedul->getStudentRegistrationFee()->getStudentRegistration()->getName() . ", Nouvelle date: "$date_due->format('d/m/Y') . " </strong>"
  132.                 $this->getUser()->getUserIdentifier(), 
  133.             "danger");
  134.             $logOperation->setEstablishment($accountingStudentRegistrationFeeShedul->getEstablishment());
  135.             $entityManager->persist($logOperation);
  136.             $accountingStudentRegistrationFeeShedul->setDateDue($date_due);
  137.         } catch (\Throwable $th) {
  138.             return $this->json(['code' => 500'message' => "Impossible: Veuillez selectionner une date valide SVP."], 200);
  139.         }
  140.         
  141.         try {
  142.             $entityManager->flush();
  143.             return $this->json(['code' => 200'message' => "Modification effectuée :)"], 200);
  144.         } catch (\Throwable $th) {
  145.             //$th->getMessage()
  146.             return $this->json(['code' => 500'message' => $th->getMessage()], 200);
  147.             $this->addFlash('warning'$th->getMessage());
  148.         }
  149.         return $this->json(['code' => 500'message' => "Une erreur s'est produite!"], 200);
  150.     }
  151.     /**
  152.      * @Route("/{id}/re-shedul", name="accounting_student_registration_fee_shedul_re_shedul", methods={"GET","POST"})
  153.      */
  154.     public function re_shedul(Request $requestAccountingStudentRegistrationFeeShedul $accountingStudentRegistrationFeeShedulAccountingStudentRegistrationFeeShedulRepository $accountingStudentRegistrationFeeShedulRepository): Response
  155.     {
  156.         /* Ajout des echeancies */
  157.         $accountingStudentRegistrationFee $accountingStudentRegistrationFeeShedul->getStudentRegistrationFee();
  158.         $fee_shedul_amount $request->get('fee_shedul_amount');
  159.         $fee_shedul_date $request->get('fee_shedul_date');
  160.         if ($fee_shedul_amount <= 0) {
  161.             $this->addFlash('warning'"Montant invalide.");
  162.             return $this->redirectToRoute('accounting_student_registration_fee_edit', ['id' => $accountingStudentRegistrationFee->getId()]);
  163.         }
  164.         if ($fee_shedul_amount >= $accountingStudentRegistrationFeeShedul->getAmount()) {
  165.             $this->addFlash('warning'"Montant trop élevé.");
  166.             return $this->redirectToRoute('accounting_student_registration_fee_edit', ['id' => $accountingStudentRegistrationFee->getId()]);
  167.         }
  168.         if ($fee_shedul_date <= date('Y-m-d')) {
  169.             $this->addFlash('warning'"Date invalide.");
  170.             return $this->redirectToRoute('accounting_student_registration_fee_edit', ['id' => $accountingStudentRegistrationFee->getId()]);
  171.         }
  172.         $newAccountingStudentRegistrationFeeShedul = new AccountingStudentRegistrationFeeShedul();
  173.         $newAccountingStudentRegistrationFeeShedul->setOrderNum(count($accountingStudentRegistrationFeeShedul->getStudentRegistrationFee()->getAccountingStudentRegistrationFeeSheduls()) + 1);
  174.         $newAccountingStudentRegistrationFeeShedul->setEstablishment($accountingStudentRegistrationFee->getEstablishment());
  175.         $newAccountingStudentRegistrationFeeShedul->setStudentRegistrationFee($accountingStudentRegistrationFee);
  176.         $newAccountingStudentRegistrationFeeShedul->setAmount($fee_shedul_amount);
  177.         $newAccountingStudentRegistrationFeeShedul->setAmountPaid(0);
  178.         $newAccountingStudentRegistrationFeeShedul->setAmountCancel(0);
  179.         $newAccountingStudentRegistrationFeeShedul->setAmountReported(0);
  180.         $newAccountingStudentRegistrationFeeShedul->setLastAmountPaid(0);
  181.         $newAccountingStudentRegistrationFeeShedul->setDateDue(new DateTimeImmutable($fee_shedul_date));
  182.         $entityManager $this->getDoctrine()->getManager();
  183.         $entityManager->persist($newAccountingStudentRegistrationFeeShedul);
  184.         $accountingStudentRegistrationFeeShedul->setAmount($accountingStudentRegistrationFeeShedul->getAmount() - $fee_shedul_amount);
  185.         
  186.         try {
  187.             $entityManager->flush();
  188.             $this->addFlash('success'"un écheancier à été ajouté.");
  189.         } catch (\Throwable $th) {
  190.             $this->addFlash('warning'"Une erreur est survenue lors de l'ajout de l'échéancier.");
  191.             $this->addFlash('info'$th->getMessage());
  192.         }
  193.         return $this->redirectToRoute('accounting_student_registration_fee_edit', ['id' => $accountingStudentRegistrationFee->getId()]);
  194.     }
  195. }