<?php
namespace App\Controller\Accounting;
use DateTimeImmutable;
use App\Entity\LogOperation;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\AccountingStudentRegistrationFeeShedul;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Repository\AccountingStudentRegistrationFeeShedulRepository;
/**
* @Route("/accounting/student-registration-fee-shedul")
*/
class AccountingStudentRegistrationFeeShedulController extends AbstractController
{
/**
* @Route("/delete-selection", name="accounting_student_registration_fee_sheduls_selected_delete", methods={"GET"})
*/
public function deleteSelected(Request $request, AccountingStudentRegistrationFeeShedulRepository $entityRepository): Response
{
$list = $request->get('entities');
$entityManager = $this->getDoctrine()->getManager();
$errors = 0;
foreach ($list as $key => $id) {
$entity = $entityRepository->findOneBy(['id' => intval($id)], []);
if ($entity != null) {
if (count($entity->getAccountingStudentRegistrationFeeShedulPayments()) <= 0) {
$entityManager->remove($entity);
}
}
}
try {
$entityManager->flush();
$this->addFlash('success', "Traitement effectué");
return $this->json(['code' => 200, 'message' => "Traitement effectué :)"], 200);
} catch (\Throwable $th) {
//$th->getMessage()
$this->addFlash('warning', $th->getMessage());
}
$this->addFlash('warning', "Traitement non effectué");
return $this->json(['code' => 500, 'message' => "Traitement non effectué"], 200);
}
/**
* @Route("/delete/{id}", name="accounting_student_registration_fee_shedul_delete", methods={"GET"})
*/
public function delete(AccountingStudentRegistrationFeeShedul $accountingStudentRegistrationFeeShedul): Response
{
if ($accountingStudentRegistrationFeeShedul->getAmountPaid() > 0) {
return $this->json(['code' => 500, 'message' => "Impossible de supprimer cet échéancier. Des paiement ont été effectués dessus."], 200);
}
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($accountingStudentRegistrationFeeShedul);
try {
$entityManager->flush();
return $this->json(['code' => 200, 'message' => "Suppression effectuée :)"], 200);
} catch (\Throwable $th) {
//$th->getMessage()
$this->addFlash('warning', $th->getMessage());
}
return $this->json(['code' => 500, 'message' => "Suppression non effectuée"], 200);
}
/**
* @Route("/cancel/{id}", name="accounting_student_registration_fee_shedul_cancel", methods={"GET"})
*/
public function cancel(Request $request, AccountingStudentRegistrationFeeShedul $accountingStudentRegistrationFeeShedul): Response
{
if ($accountingStudentRegistrationFeeShedul->getAmountCancel() > 0) {
$this->addFlash('warning', "Impossible, une remise à déjà été effectuée sur cet échéancier.");
return $this->redirectToRoute('registration_student_registration_show', ['id' => $accountingStudentRegistrationFeeShedul->getStudentRegistrationFee()->getStudentRegistration()->getId()]);
}
$amount = $request->get('shedulAmount');
if ($amount <= 0) {
$this->addFlash('warning', "Impossible, le montant de la remise doit être supérieur à zéro");
return $this->redirectToRoute('registration_student_registration_show', ['id' => $accountingStudentRegistrationFeeShedul->getStudentRegistrationFee()->getStudentRegistration()->getId()]);
}
if ($accountingStudentRegistrationFeeShedul->getAmountRest() <= 0) {
$this->addFlash('warning', "Impossible, le reste à payer doit être supérieur à zéro(0)");
return $this->redirectToRoute('registration_student_registration_show', ['id' => $accountingStudentRegistrationFeeShedul->getStudentRegistrationFee()->getStudentRegistration()->getId()]);
}
if ($accountingStudentRegistrationFeeShedul->getAmountRest() < $amount) {
$this->addFlash('warning', "Impossible, le montant de la remise doit être inférieur ou égale au reste à payer");
return $this->redirectToRoute('registration_student_registration_show', ['id' => $accountingStudentRegistrationFeeShedul->getStudentRegistrationFee()->getStudentRegistration()->getId()]);
}
$entityManager = $this->getDoctrine()->getManager();
$accountingStudentRegistrationFeeShedul->setAmountCancel($amount);
//$accountingStudentRegistrationFeeShedul->setIsCancel(true);
try {
$entityManager->flush();
$this->addFlash('success', "Remise effectué :)");
} catch (\Throwable $th) {
$this->addFlash('warning', $th->getMessage());
}
return $this->redirectToRoute('registration_student_registration_show', ['id' => $accountingStudentRegistrationFeeShedul->getStudentRegistrationFee()->getStudentRegistration()->getId()]);
}
/**
*
* @Route("/api/shedul-update-date-due/{id}", name="api_accounting_student_registration_fee_shedul_update_date_due", methods={"GET"})
*/
public function api_fee_shedul_update_date_due(Request $request, AccountingStudentRegistrationFeeShedul $accountingStudentRegistrationFeeShedul, EntityManagerInterface $entityManager): Response
{
if ($request->get('date_due') == '' or $request->get('date_due') == null) {
return $this->json(['code' => 500, 'message' => "Impossible: Veuillez selectionner une date SVP."], 200);
}
if ($request->get('date_due') <= date('Y-m-d')) {
return $this->json(['code' => 500, 'message' => "Impossible: Veuillez selectionner une date superieur à la date du jour SVP."], 200);
}
try {
$date_due = new DateTimeImmutable($request->get('date_due'));
} catch (\Throwable $th) {
return $this->json(['code' => 500, 'message' => $th->getMessage()], 200);
}
try {
$logOperation = new LogOperation(
$request->getClientIp(),
$accountingStudentRegistrationFeeShedul->getId(),
AccountingStudentRegistrationFeeShedul::class,
"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>",
$this->getUser()->getUserIdentifier(),
"danger");
$logOperation->setEstablishment($accountingStudentRegistrationFeeShedul->getEstablishment());
$entityManager->persist($logOperation);
$accountingStudentRegistrationFeeShedul->setDateDue($date_due);
} catch (\Throwable $th) {
return $this->json(['code' => 500, 'message' => "Impossible: Veuillez selectionner une date valide SVP."], 200);
}
try {
$entityManager->flush();
return $this->json(['code' => 200, 'message' => "Modification effectuée :)"], 200);
} catch (\Throwable $th) {
//$th->getMessage()
return $this->json(['code' => 500, 'message' => $th->getMessage()], 200);
$this->addFlash('warning', $th->getMessage());
}
return $this->json(['code' => 500, 'message' => "Une erreur s'est produite!"], 200);
}
/**
* @Route("/{id}/re-shedul", name="accounting_student_registration_fee_shedul_re_shedul", methods={"GET","POST"})
*/
public function re_shedul(Request $request, AccountingStudentRegistrationFeeShedul $accountingStudentRegistrationFeeShedul, AccountingStudentRegistrationFeeShedulRepository $accountingStudentRegistrationFeeShedulRepository): Response
{
/* Ajout des echeancies */
$accountingStudentRegistrationFee = $accountingStudentRegistrationFeeShedul->getStudentRegistrationFee();
$fee_shedul_amount = $request->get('fee_shedul_amount');
$fee_shedul_date = $request->get('fee_shedul_date');
if ($fee_shedul_amount <= 0) {
$this->addFlash('warning', "Montant invalide.");
return $this->redirectToRoute('accounting_student_registration_fee_edit', ['id' => $accountingStudentRegistrationFee->getId()]);
}
if ($fee_shedul_amount >= $accountingStudentRegistrationFeeShedul->getAmount()) {
$this->addFlash('warning', "Montant trop élevé.");
return $this->redirectToRoute('accounting_student_registration_fee_edit', ['id' => $accountingStudentRegistrationFee->getId()]);
}
if ($fee_shedul_date <= date('Y-m-d')) {
$this->addFlash('warning', "Date invalide.");
return $this->redirectToRoute('accounting_student_registration_fee_edit', ['id' => $accountingStudentRegistrationFee->getId()]);
}
$newAccountingStudentRegistrationFeeShedul = new AccountingStudentRegistrationFeeShedul();
$newAccountingStudentRegistrationFeeShedul->setOrderNum(count($accountingStudentRegistrationFeeShedul->getStudentRegistrationFee()->getAccountingStudentRegistrationFeeSheduls()) + 1);
$newAccountingStudentRegistrationFeeShedul->setEstablishment($accountingStudentRegistrationFee->getEstablishment());
$newAccountingStudentRegistrationFeeShedul->setStudentRegistrationFee($accountingStudentRegistrationFee);
$newAccountingStudentRegistrationFeeShedul->setAmount($fee_shedul_amount);
$newAccountingStudentRegistrationFeeShedul->setAmountPaid(0);
$newAccountingStudentRegistrationFeeShedul->setAmountCancel(0);
$newAccountingStudentRegistrationFeeShedul->setAmountReported(0);
$newAccountingStudentRegistrationFeeShedul->setLastAmountPaid(0);
$newAccountingStudentRegistrationFeeShedul->setDateDue(new DateTimeImmutable($fee_shedul_date));
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($newAccountingStudentRegistrationFeeShedul);
$accountingStudentRegistrationFeeShedul->setAmount($accountingStudentRegistrationFeeShedul->getAmount() - $fee_shedul_amount);
try {
$entityManager->flush();
$this->addFlash('success', "un écheancier à été ajouté.");
} catch (\Throwable $th) {
$this->addFlash('warning', "Une erreur est survenue lors de l'ajout de l'échéancier.");
$this->addFlash('info', $th->getMessage());
}
return $this->redirectToRoute('accounting_student_registration_fee_edit', ['id' => $accountingStudentRegistrationFee->getId()]);
}
}