<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
use Symfony\Contracts\Translation\TranslatorInterface;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use App\Entity\Entreprise;
use App\Entity\Reseau;
use App\Entity\ReseauDivision;
use App\Entity\Commercial;
use App\Entity\Dossier;
use App\Entity\Offre;
use App\Entity\Garantie;
use App\Entity\StructureCotisation;
use App\Entity\Activite;
class AjaxController extends AbstractController
{
private $paginator;
private $em;
private $translator;
public function __construct(
PaginatorInterface $paginator,
EntityManagerInterface $em,
TranslatorInterface $translator
) {
$this->paginator = $paginator;
$this->em = $em;
$this->translator = $translator;
}
/**
* @param Request $request
*
* @Route("/autocomplete/activites", name="activite_autocomplete")
*
* @return JsonResponse
*/
public function autocompleteAction(Request $request)
{
// Check security etc. if needed
$search = $request->query->get('q');
$page_limit = $request->query->get('page_limit');
$page = $request->query->getInt('page', 1);
$query = $this->em->getRepository(Activite::class)->search($search);
$pagination = $this->paginator->paginate(
$query, /* query NOT result */
$page, /*page number*/
$page_limit /*limit per page*/
);
$response = [];
foreach($pagination as $activite) {
$response[] = [
'id' => $activite->getId(),
'text' => $activite->__toString()
];
}
return new JsonResponse($response);
}
/**
* @param Request $request
*
* @Route("/autocomplete/entreprise/{id}/reseau", name="reseau_ajax")
* @Entity("entreprise", expr="repository.find(id)")
*
* @return JsonResponse
*/
public function EntrepriseReseauChangeAction(Request $request, Entreprise $entreprise)
{
$default_reseau_division_id = $entreprise->getReseauDivision()->getId();
$default_commercial_id = is_null($entreprise->getCommercial()) ? 0 : $entreprise->getCommercial()->getId();
$reseau_id = $request->query->get('reseau_id');
$reseau = $this->em->getRepository(Reseau::class)->find($reseau_id);
// On récupère les reseauDivisions avec éventuellement l'option sélectionnée pour l'entreprise
$reseauDivisions = $this->em->getRepository(ReseauDivision::class)->findByReseau($reseau_id);
$rdArray = [];
$defautSelected = false;
foreach($reseauDivisions as $reseauDivision) {
$selected = false;
if($default_reseau_division_id == $reseauDivision->getId()) {
$selected = true;
$defautSelected = true;
}
$rdArray[] = [
'id' => $reseauDivision->getId(),
'text' => $reseauDivision->getNom(),
'selected' => $selected
];
}
if(!$defautSelected) {
$rdArray[0]['selected'] = true;
$default_reseau_division_id = 0;
if(array_key_exists('id', $rdArray[0])) {
$default_reseau_division_id = $rdArray[0]['id'];
}
}
// On récupère les commerciaux de reseauDivision par défaut ou sélectionné, avec éventuellement le commercial sélectionné pour l'entreprise
$commerciaux = $this->em->getRepository(Commercial::class)->findByReseauDivision($default_reseau_division_id);
$cArray = [];
$selected = false;
foreach($commerciaux as $commercial) {
$selected = false;
if($default_commercial_id == $commercial->getId()) {
$selected = true;
}
$cArray[] = [
'id' => $commercial->getId(),
'text' => $commercial->__toString(),
'selected' => $selected
];
}
if(!$selected) {
$cArray[0]['selected'] = true;
}
$response = [
'type_reseau' => $entreprise->getReseau()->getType(),
'reseauDivisionLabel' => $this->translator->trans('reseau.type.' . $reseau->getType() . '.reseau_division.label', [], 'app'),
'reseauDivisions' => $rdArray,
'commercialLabel' => $this->translator->trans('reseau.type.' . $reseau->getType() . '.commercial.label', [], 'app'),
'commerciaux' => $cArray,
'saisieProducteur' =>$reseau->getSaisieProducteur(),
'producteurCode' => $entreprise->getProducteurCodeIsWaiting(),
'producteurCodeIsWaiting' => $entreprise->getProducteurCode(),
'producteurNom' => $entreprise->getProducteurNom(),
'producteurPrenom' => $entreprise->getProducteurPrenom(),
'producteurEmail' => $entreprise->getProducteurEmail(),
'producteurTelephone' => $entreprise->getProducteurTelephone(),
];
return new JsonResponse($response);
}
/**
* @param Request $request
*
* @Route("/autocomplete/entreprise/{id}/reseauDivision", name="reseau_division_ajax")
* @Entity("entreprise", expr="repository.find(id)")
*
* @return JsonResponse
*/
public function EntrepriseReseauDivisionChangeAction(Request $request, Entreprise $entreprise)
{
$default_commercial_id = is_null($entreprise->getCommercial()) ? 0 : $entreprise->getCommercial()->getId();
$reseau_division_id = $request->query->get('reseau_division_id');
$reseauDivision = $this->em->getRepository(reseauDivision::class)->find($reseau_division_id);
// On récupère les commerciaux avec éventuellement le commercial sélectionné pour l'entreprise
$commerciaux = $this->em->getRepository(Commercial::class)->findByReseauDivision($reseau_division_id);
$cArray = [];
$selected = false;
foreach($commerciaux as $commercial) {
$selected = false;
if($default_commercial_id == $commercial->getId()) {
$selected = true;
}
$cArray[] = [
'id' => $commercial->getId(),
'text' => $commercial->__toString(),
'selected' => $selected
];
}
if(!$selected) {
$cArray[0]['selected'] = true;
}
$response = [
'type_reseau' => $entreprise->getReseau()->getType(),
'commercialLabel' => $this->translator->trans('reseau.type.' . $reseauDivision->getReseau()->getType() . '.commercial.label', [], 'app'),
'commerciaux' => $cArray
];
return new JsonResponse($response);
}
/**
* @Route("/setDocuments", name="dossierMajDocuments")
*/
public function setDocuments(Request $request): JsonResponse
{
if ($request->isXmlHttpRequest()) {
$dossier_id = $request->request->get('dossier_id');
$checkedDocuments = false;
if($request->request->has('documents') && count($request->request->get('documents')) > 0) {
$checkedDocuments = $request->request->get('documents');
}
$dossier = $this->em->getRepository(Dossier::class)->find($dossier_id );
if(!$dossier instanceof Dossier) {
return new JsonResponse('Dossier ' . $dossier_id . ' introuvable', Response::HTTP_NOT_FOUND); // constant for 404
}
$tempDocuments = [];
// Initialisation du tableau de document du dossier à false
$documents = $dossier->getDocuments();
if(is_countable($documents) && count($documents) > 0) {
foreach($documents as $key => $value) {
$tempDocuments[$key] = false;
}
// Mise à jour des documents cochés pour le dossier
if($checkedDocuments) {
foreach($checkedDocuments as $document) {
$tempDocuments[$document['value']] = true;
}
}
$dossier->setDocuments($tempDocuments);
} else {
$dossier->setDocuments(null);
}
$this->em->persist($dossier);
$this->em->flush();
return new JsonResponse($tempDocuments);
}
return new Response('Interdit', Response::HTTP_NOT_FOUND);
}
/**
* @Route("/setControles", name="dossierMajControles")
*/
public function setControles(Request $request): JsonResponse
{
if ($request->isXmlHttpRequest()) {
$dossier_id = $request->request->get('dossier_id');
$checkedControles = $request->request->get('controles');
$tempControles = [];
$dossier = $this->em->getRepository(Dossier::class)->find($dossier_id );
if(!$dossier instanceof Dossier) {
return new JsonResponse('Dossier introuvable', Response::HTTP_NOT_FOUND); // constant for 404
}
// Initialisation du tableau de document du dossier à false
$controles = $dossier->getControles();
if(!is_null($controles)) {
foreach($controles as $key => $value) {
$tempControles[$key] = false;
}
}
// Mise à jour des documents cochés pour le dossier
if(!is_null($checkedControles)) {
foreach($checkedControles as $controle) {
$tempControles[$controle['value']] = true;
}
}
$dossier->setControles($tempControles);
$this->em->persist($dossier);
$this->em->flush();
return new JsonResponse($tempControles);
}
return new Response('Interdit', Response::HTTP_NOT_FOUND); // constant for 404
}
/**
* @Route("/getGaranties", name="offreGetGaranties")
*/
public function getGaranties(Request $request): JsonResponse
{
$offre_id = $request->request->get('offre_id');
$offre = $this->em->getRepository(Offre::class)->find($offre_id );
$garanties = $this->em->getRepository(Garantie::class)->findByOffre($offre );
$garantieArray = [];
foreach($garanties as $garantie) {
$garantieArray[] = [
'id' => $garantie->getId(),
'text' => $garantie->getNom()
];
}
return new JsonResponse(['garanties' => $garantieArray]);
}
/**
* @Route("/getStructureCotisations", name="offreGetStructureCotisations")
*/
public function getStructureCotisations(Request $request): JsonResponse
{
$offre_id = $request->request->get('offre_id');
$offre = $this->em->getRepository(Offre::class)->find($offre_id );
$structureCotisations = $this->em->getRepository(StructureCotisation::class)->findByOffre($offre );
$structureCotisationArray = [];
foreach($structureCotisations as $structureCotisation) {
$structureCotisationArray[] = [
'id' => $structureCotisation->getId(),
'text' => $structureCotisation->getNom()
];
}
return new JsonResponse(['structureCotisations' => $structureCotisationArray]);
}
/**
* @param Request $request
*
* @Route("/dossier/{id}/motif/setVariables", name="setDossierMotifVariables")
* @Entity("dossier", expr="repository.find(id)")
*
* @return JsonResponse
*/
public function setDossierMotifVariables(Request $request, Dossier $dossier)
{
if ($request->isXmlHttpRequest()) {
$form = $request->request->get('form');
$motif_id = $request->request->get('motif_id');
parse_str($form, $vars);
$vars = [$motif_id => $vars];
$dossierVars = $dossier->getVars();
if(!empty($dossierVars))
{
$variables = array_replace($dossierVars, $vars);
} else {
$variables = $vars;
}
$dossier->setVars($variables);
$this->em->persist($dossier);
$this->em->flush();
return new JsonResponse($variables);
}
return new Response('Interdit', Response::HTTP_NOT_FOUND); // constant for 404
}
}