<?php
namespace App\Controller;
use App\Entity\Prestation;
use App\Form\PrestationType;
use App\Repository\PrestationRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use \Knp\Component\Pager\PaginatorInterface;
use \App\Services\AppSevices;
use Doctrine\ORM\EntityManagerInterface;
/**
* @Route("/user/prestation")
*/
class PrestationController extends AbstractController {
/**
* @Route("/prestation/vue-cga", name="prestation_vue_cga")
*/
public function vuePrestationCga(EntityManagerInterface $em): Response
{
$conn = $em->getConnection();
$sql = "SELECT * FROM vue_prestation_cga";
$stmt = $conn->prepare($sql);
$resultSet = $stmt->executeQuery();
$rows = $resultSet->fetchAllAssociative();
return $this->render('prestation/vue_cga.html.twig', [
'prestations' => $rows,
]);
}
/**
* @Route("/Admin/", name="app_prestation_index", methods={"GET"})
*/
public function index(Request $request, PaginatorInterface $paginator, AppSevices $services): Response {
$annee = $services->getAnneExercice(['sort' => 'u.annee', "order" => "desc"])->setMaxResults(1)->getQuery()->getOneOrNullResult();
$etat = ($request->query->get('etat')) == "" ? "all" : $request->get('etat');
$anne = ($request->query->get('anne')) == "" ? $annee->getId() : $request->get('anne');
$type = ($request->query->get('type')) == "" ? "all" : $request->get('type');
$adherent = ($request->query->get('adherent')) == "" ? "all" : $request->get('adherent');
$prestations = $services->getPrestations(array("etat" => $etat, "anne" => $anne, "type" => $type, "adherent" => $adherent))->getQuery()->getResult();
return $this->render('prestation/index.html.twig', [
'prestations' => $prestations,
]);
}
/**
* @Route("/{id}/{type}/new", name="app_prestation_new", methods={"GET", "POST"})
* @Route("/{id}/Admin/edit", name="app_prestation_edit", methods={"GET", "POST"})
*/
public function new(Request $request, $type = null, $id, \App\Services\AppSevices $service, PrestationRepository $prestationRepository, \App\Repository\EnfantRepository $enfantRepository, Prestation $prestation = null): Response {
if (!$prestation) {
$prestation = new Prestation();
}
if ($type && $type == 'en') {
$enfant = $enfantRepository->find($id);
$adr = $enfant->getAdherent();
$prestation->setEnfant($enfant);
} else {
$adr = $service->getAdherents(['id' => $id])->getQuery()->getOneOrNullResult();
}
if (!$prestation->getAdherent()) {
$prestation->setAdherent($adr);
$prestation->setEtat('Cour');
$prestation->setDateInsertion(new \DateTime());
}
$form = $this->createForm(PrestationType::class, $prestation);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($prestation->getMontantMois() && $prestation->getValeur()) {
$this->addFlash(
'danger',
'Vous devez saisir soit le montant par mois, soit le montant global'
);
return $this->renderForm('prestation/new.html.twig', [
'prestation' => $prestation,
'form' => $form,
'id' => $id
, 'type' => $type
]);
}
if ($prestation->getMontantMois() && !$prestation->getNbMois()) {
$this->addFlash(
'danger',
'Vous devez saisir le nbr mois'
);
return $this->renderForm('prestation/new.html.twig', [
'prestation' => $prestation,
'form' => $form,
'id' => $id
, 'type' => $type
]);
}
if ($prestation->getTypePrestation()->getPerodisite() == "1" && !$prestation->getValeur()) {
$this->addFlash(
'danger',
'Vous devez saisir le montant global et non pas le montant par mois.'
);
return $this->renderForm('prestation/new.html.twig', [
'prestation' => $prestation,
'form' => $form,
'id' => $id
, 'type' => $type
]);
}
if ($prestation->getTypePrestation()->getPerodisite() == "1" && $prestation->getNbMois()) {
$this->addFlash(
'danger',
'Vous ne devez pas saisir le nombre de mois'
);
return $this->renderForm('prestation/new.html.twig', [
'prestation' => $prestation,
'form' => $form,
'id' => $id
, 'type' => $type
]);
}
//if(count($form['documents'])<1){
// $this->addFlash(
// 'danger',
// 'vous devez ajouter au moins un document'
// );
// return $this->renderForm('prestation/new.html.twig', [
// 'prestation' => $prestation,
// 'form' => $form,
// 'id' => $id
// , 'type' => $type
//]);
//}
$e = null;
if ($prestation->getEnfant()) {
$e = $prestation->getEnfant()->getId();
if ($prestation->getTypePrestation()->getId() == 13 && $prestation->getEnfant()->getClassEtude()) {
$prestation->setValeur($prestation->getEnfant()->getClassEtude()->getMotantInscription());
} elseif ($prestation->getTypePrestation()->getId() == 14 && $prestation->getEnfant()->getClassEtude()) {
$prestation->setValeur($prestation->getEnfant()->getClassEtude()->getMontantReuissite());
}
else if($prestation->getTypePrestation()->getId() != 11) {
$this->addFlash(
'danger',
'ce type de prestation ne correspond pas aux enfant'
);
return $this->renderForm('prestation/new.html.twig', [
'prestation' => $prestation,
'form' => $form,
'id' => $id
, 'type' => $type
]);
}
}
if ($prestation->getTypePrestation()->getId() == 13
|| $prestation->getTypePrestation()->getId() == 14
|| $prestation->getTypePrestation()->getId()==11) {
if (!$prestation->getEnfant()) {
$this->addFlash(
'danger',
'pour bénéficier de cette prestation vous devez avoir un enfant '
);
return $this->renderForm('prestation/new.html.twig', [
'prestation' => $prestation,
'form' => $form,
'id' => $id,
'type' => $type
]);
}
}
if ($prestation->getTypePrestation()->getTaux() ||
$prestation->getTypePrestation()->getTaux() != 0) {
if ($prestation->getMontantMois())
$val = $prestation->getTypePrestation()->getTaux() * $prestation->getMontantMois();
else
$val = $prestation->getTypePrestation()->getTaux() * $prestation->getValeur();
} else {
if ($prestation->getMontantMois())
$val = $prestation->getMontantMois();
else
$val = $prestation->getValeur();
}
if ($prestation->getTypePrestation()->getValeutMax()) {
if ($prestation->getTypePrestation()->getValeutMax() > $val) {
$prestation->setValeurPayer($val);
} else {
$prestation->setValeurPayer($prestation->getTypePrestation()->getValeutMax());
}
} else {
$prestation->setValeurPayer($val);
}
foreach ($prestation->getDocuments() as $d) {
$prestation->removeDocument($d);
}
if ($prestation->getTypePrestation()->getPerodisite() == "2" && $prestation->getNbMois()) {
$prestation->setValeurPayer($prestation->getValeurPayer() * $prestation->getNbMois());
$prestation->setValeur($prestation->getMontantMois() * $prestation->getNbMois());
}
$per = $service->getPrestations(['id' => $prestation->getId()])->setMaxResults(1)->getQuery()->getOneOrNullResult();
if ($per) {
$plafon = ($service->getAnneExercice(['anne' => $prestation->getAnneeExercice()->getAnnee()])->setMaxResults(1)->getQuery()->getOneOrNullResult()->getPalafon()) - $service->getSumPrestation(['idp'=>$prestation->getId(),'id' => $prestation->getAdherent()->getId(), 'anne' => $prestation->getAnneeExercice()->getId(), 'etat' => 'Total']);
} else {
$plafon = $service->getAnneExercice(['anne' => $prestation->getAnneeExercice()->getAnnee()])->setMaxResults(1)->getQuery()->getOneOrNullResult()->getPalafon() - $service->getSumPrestation(['id' => $prestation->getAdherent()->getId(), 'anne' => $prestation->getAnneeExercice()->getId(), 'etat' => 'Total']);
}
$prestationVoyage = $service->getPrestations(["anne" => $prestation->getAnneeExercice()->getId(),
'adherent' => $prestation->getAdherent()->getId(),
'type' => '9',
])->getQuery()->getResult();
if (count($prestationVoyage) > 0) {
$plafon = $plafon + 500;
}
if ($prestation->getId()) {
$presUpdate = $service->getPrestations(['id' => $prestation->getId()])->setMaxResults(1)->getQuery()->getOneOrNullResult();
}
if ($prestation->getTypePrestation()->isPalfont() === true) {
if ($plafon <= 0) {
$this->addFlash(
'danger',
'Votre palfond=0'
);
return $this->renderForm('prestation/new.html.twig', [
'prestation' => $prestation,
'form' => $form,
'id' => $id,
'type' => $type
]);
} elseif ($prestation->getValeurPayer() > $plafon) {
$prestation->setValeurPayer($plafon);
}
$this->addFlash('success',
'Vous aveez ajoutez une prestation'
);
foreach ($form['documents'] as $value) {
$document = $value->getData();
$prestation->addDocument($document);
}
$prestationRepository->add($prestation, true);
} else {
foreach ($form['documents'] as $value) {
$document = $value->getData();
$prestation->addDocument($document);
}
$this->addFlash(
'success',
'Vous aveez ajoutez une prestation'
);
$prestationRepository->add($prestation, true);
}
return $this->redirectToRoute('app_adherent_show', ['id' => $prestation->getAdherent()->getId()], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('prestation/new.html.twig', [
'prestation' => $prestation,
'form' => $form,
'id' => $id,
'type' => $type
]);
}
public function creaDir($prestation) {
$dir = $this->getParameter('brochures_directory') . '/' . $prestation->getAdherent()->getNom() . '-' . $prestation->getAdherent()->getNumeroAdhesion();
if (!is_dir($dir)) {
mkdir($dir, 0700);
}
$dir = $this->getParameter('brochures_directory') . '/' . $prestation->getAdherent()->getNom() . '-' . $prestation->getAdherent()->getNumeroAdhesion() . '/' . $prestation->getTypePrestation()->getLib();
if (!is_dir($dir)) {
mkdir($dir, 0700);
}
return($dir);
}
/**
* @Route("/{id}", name="app_prestation_show", methods={"GET"})
*/
public function show(Prestation $prestation): Response {
return $this->render('prestation/show.html.twig', [
'prestation' => $prestation,
]);
}
// /**
// * @Route("/{id}/edit", name="app_prestation_edit", methods={"GET", "POST"})
// */
// public function edit(Request $request, Prestation $prestation, PrestationRepository $prestationRepository): Response {
// $form = $this->createForm(PrestationType::class, $prestation);
// $form->handleRequest($request);
//
// if ($form->isSubmitted() && $form->isValid()) {
// $prestationRepository->add($prestation, true);
//
// return $this->redirectToRoute('app_prestation_index', [], Response::HTTP_SEE_OTHER);
// }
//
// return $this->renderForm('prestation/edit.html.twig', [
// 'prestation' => $prestation,
// 'form' => $form,
// ]);
// }
/**
* @Route("/{id}/{etat}/Admin/edit_Etat", name="app_prestation_edit_etat", methods={"GET", "POST"})
*/
public function editEtat($id, $etat, PrestationRepository $prestationRepository, AppSevices $service): Response {
$prestation = $service->getPrestations(['id' => $id])->getQuery()->getOneOrNullResult();
$prestation->setEtat($etat);
$prestationRepository->add($prestation, true);
return $this->redirectToRoute('app_adherent_show', ['id' => $prestation->getAdherent()->getId()], Response::HTTP_SEE_OTHER);
}
/**
* @Route("/{id}", name="app_prestation_delete", methods={"POST"})
*/
public function delete(Request $request, Prestation $prestation, PrestationRepository $prestationRepository): Response {
if ($this->isCsrfTokenValid('delete' . $prestation->getId(), $request->request->get('_token'))) {
$prestationRepository->remove($prestation, true);
}
return $this->redirectToRoute('app_prestation_index', [], Response::HTTP_SEE_OTHER);
}
}