<?php
namespace App\Controller;
use App\Entity\Planning\User;
use App\Entity\Vetoadom\ChargeService;
use App\Entity\Vetoadom\Consultation;
use App\Entity\Vetoadom\Secteur;
use App\Entity\Vetoadom\SecteurConsigne;
use App\Entity\Vetoadom\SecteurConsigneType;
use App\Entity\Vetoadom\SecteurForcageChargeService;
use App\Entity\Vetoadom\SecteurForcageTarif;
use App\Entity\Vetoadom\Tarif;
use App\Service\CommonHelper;
use App\Service\CustomJsonResponse;
use App\Service\PriseAppelHelper;
use App\Service\Translation;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class DashboardController extends AbstractController
{
private $customJsonResponse;
private $vetoadomEntityManager;
private $priseAppelHelper;
private $commonHelper;
private $translation;
private $translator;
private $urlGenerator;
private $validator;
public function __construct(
CustomJsonResponse $customJsonResponse,
CommonHelper $commonHelper,
PriseAppelHelper $priseAppelHelper,
EntityManagerInterface $vetoadomEntityManager,
Translation $translation,
TranslatorInterface $translator,
UrlGeneratorInterface $urlGenerator,
ValidatorInterface $validator
)
{
$this->commonHelper = $commonHelper;
$this->customJsonResponse = $customJsonResponse;
$this->priseAppelHelper = $priseAppelHelper;
$this->vetoadomEntityManager = $vetoadomEntityManager;
$this->translation = $translation;
$this->translator = $translator;
$this->urlGenerator = $urlGenerator;
$this->validator = $validator;
}
/**
* @Route("/dashboard", name="app_dashboard", methods={"GET", "POST"})
* @param Request $request
* @return Response
* @throws \Exception
*/
public function index(Request $request): Response
{
/** @var User $user */
$user = $this->getUser();
$params = [];
if ($request->getMethod() == 'GET') {
#$params['locale'] = $request->getLocale();
$params['secteurs'] = [];
foreach ($this->vetoadomEntityManager->getRepository(Secteur::class)->getUserSecteurs($user) as $secteur) {
$secteurForcageChargeService = $this->vetoadomEntityManager->getRepository(SecteurForcageChargeService::class)->findOneBy(['secteur' => $secteur, 'dateSuppression' => null]);
# light
if ($secteurForcageChargeService) {
$light = $secteurForcageChargeService->getChargeService()->getCouleur();
} else {
$lightConsultations = $this->vetoadomEntityManager->getRepository(Consultation::class)->getLightConsultations($secteur->getId());
$currentVetsNumber = $this->commonHelper->getCurrentVetsNumber($secteur);
$lightCode = (
$currentVetsNumber == 0
|| ($lightConsultations['consultationsNumber'] / $currentVetsNumber) > $secteur->getCoeffFeux()
) ? 'orange' : 'green';
$light = $this->vetoadomEntityManager->getRepository(ChargeService::class)->findOneBy(['code' => $lightCode])->getCouleur();
}
$langues = $secteur->getLanguesArray();
$priseAppelUrl = $this->urlGenerator->generate('app_prise_appel',['secteurId' => $secteur->getId(), '_locale' => $langues['0']['locale']]);
$params['secteurs'][] = [
'id' => $secteur->getId(),
'light' => $light,
'nomRaccourci' => $secteur->getNomRaccourci(),
'couleur' => $secteur->getCouleur(),
'consignes' => $this->vetoadomEntityManager->getRepository(SecteurConsigne::class)->getSecteurConsignes($secteur->getId()),
'fonctionnementCode' => $secteur->getFonctionnement()->getCode(),
'lienExterneMessage' => $secteur->getLienExterneMessage() ?? '',
'lienExternePriseAppel' => $secteur->getLienExternePriseAppel() ?? '',
'lienExterneVeterinairePlanning' => $secteur->getLienExterneVeterinairePlanning() ?? '',
'loadingForcageLock' => false,
'forcageChargeService' => $secteurForcageChargeService ? $secteurForcageChargeService->getChargeService()->getId() : 1,
'priseAppelResumeUrl' => $priseAppelUrl . '?tab=resume',
'priseAppelPriseAppelUrl' => $priseAppelUrl . '?tab=prise-appel',
'priseAppelMessagesUrl' => $priseAppelUrl . '?tab=messages',
'stats' => $this->priseAppelHelper->getSecteurStats($secteur)
];
}
$params['chargeServices'] = [];
foreach ($this->vetoadomEntityManager->getRepository(ChargeService::class)->findAll() as $chargeService) {
$params['chargeServices'][] = [
'id' => $chargeService->getId(),
'nom' => $this->translator->trans('charge_service.'.$chargeService->getCode(), [], 'messages'),
'couleur' => $chargeService->getCouleur()
];
}
$params['tarifs'] = [];
foreach ($this->vetoadomEntityManager->getRepository(Tarif::class)->findAll() as $tarif) {
$params['tarifs'][] = [
'id' => $tarif->getId(),
'nom' => $this->translator->trans('tarif.'.$tarif->getCode(), [], 'messages'),
];
}
$params['secteurConsigneTypes'] = [];
foreach ($this->vetoadomEntityManager->getRepository(SecteurConsigneType::class)->findAll() as $secteurConsigneType) {
$params['secteurConsigneTypes'][] = [
'id' => $secteurConsigneType->getId(),
'nom' => $this->translator->trans('secteur_consigne_type.'.$secteurConsigneType->getCode(), [], 'messages'),
];
}
// scripts translation
$domainContext = [
'scripts' => [
'alert_modal',
'error_alert_modal',
'secteur_consignes_modal',
'secteur_forcage_modal'
]
];
$params['trans'] = $this->translation->getTranslation($domainContext);
return $this->render(
'dashboard.html.twig',
[
'user' => $user,
'params' => $params
]
);
}
// POST
$action = $request->get('action');
switch($action){
case 'ajouterSecteurConsigne':
{
if (!$this->isGranted('ROLE_ADMIN')) {
$this->customJsonResponse->setForbidden(true);
$this->customJsonResponse->setField('alert', $this->translator->trans('common.action_unknown', [], 'validators'));
return $this->customJsonResponse->sendResponse();
}
$secteurId = (int)($request->get('secteur') ?? 0);
$secteur = $secteurId ? $this->vetoadomEntityManager->getRepository(Secteur::class)->getUserSecteur($user,$secteurId) : null;
if (!$secteur) {
$this->customJsonResponse->setForbidden(true);
$this->customJsonResponse->setField('alert', $this->translator->trans('secteur_consignes_modal.secteur_unknown', [], 'validators'));
return $this->customJsonResponse->sendResponse();
}
$secteurConsigneTypeId = (int)($request->get('secteurConsigneType') ?? 0);
$secteurConsigneType = $secteurConsigneTypeId ? $this->vetoadomEntityManager->getRepository(SecteurConsigneType::class)->findOneBy(['id' => $secteurConsigneTypeId]) : null;
if (!$secteurConsigneType) {
$this->customJsonResponse->setField('consigne-secteurConsigneType', $this->translator->trans('common.blank', [], 'validators'));
}
if (($dateDebut = \DateTime::createFromFormat("!Y-m-d", $request->get('dateDebut') ?? '')) === false) {
$this->customJsonResponse->setField('consigne-dateDebut', $this->translator->trans('common.blank', [], 'validators'));
}
if (($heureDebut = \DateTime::createFromFormat("!H:i", $request->get('heureDebut') ?? '')) === false) {
$this->customJsonResponse->setField('consigne-heureDebut', $this->translator->trans('common.blank', [], 'validators'));
}
if (($dateFin = \DateTime::createFromFormat("!Y-m-d", $request->get('dateFin') ?? '')) === false) {
$this->customJsonResponse->setField('consigne-dateFin', $this->translator->trans('common.blank', [], 'validators'));
}
if (($heureFin = \DateTime::createFromFormat("!H:i", $request->get('heureFin') ?? '')) === false) {
$this->customJsonResponse->setField('consigne-heureFin', $this->translator->trans('common.blank', [], 'validators'));
}
if (
$dateDebut !== false
&& $heureDebut !== false
&& $dateFin !== false
&& $heureFin !== false
) {
$dateDebut->setTime($heureDebut->format('H'),$heureDebut->format('i'));
$dateFin->setTime($heureFin->format('H'),$heureFin->format('i'));
if ($dateDebut < new \DateTime(date('Y-m-d H:i:00'))) {
$this->customJsonResponse->setField('alert', $this->translator->trans('secteur_consignes_modal.date_debut_wrong', [], 'validators'));
}
if ($dateDebut > $dateFin) {
$this->customJsonResponse->setField('alert', $this->translator->trans('secteur_consignes_modal.date_debut_date_fin_wrong', [], 'validators'));
}
}
$consigne = $request->get('consigne') ?? '';
if ($consigne === '') {
$this->customJsonResponse->setField('consigne-consigne', $this->translator->trans('common.blank', [], 'validators'));
}
if ($this->customJsonResponse->hasAnyField()) {
$this->customJsonResponse->setForbidden(true);
return $this->customJsonResponse->sendResponse();
}
$secteurConsigne = new SecteurConsigne();
$secteurConsigne->setSecteur($secteur);
$secteurConsigne->setSecteurConsigneType($secteurConsigneType);
$secteurConsigne->setSaisieUserId($user->getId());
$secteurConsigne->setDateDebut($dateDebut);
$secteurConsigne->setDateFin($dateFin);
$secteurConsigne->setConsigne($consigne);
$this->vetoadomEntityManager->persist($secteurConsigne);
$this->vetoadomEntityManager->flush();
$this->customJsonResponse->setField('consignes', $this->vetoadomEntityManager->getRepository(SecteurConsigne::class)->getSecteurConsignes($secteur->getId()));
return $this->customJsonResponse->sendResponse();
}
break;
case 'supprimerSecteurConsigne':
{
if (!$this->isGranted('ROLE_ADMIN')) {
$this->customJsonResponse->setForbidden(true);
$this->customJsonResponse->setField('alert', $this->translator->trans('common.action_unknown', [], 'validators'));
return $this->customJsonResponse->sendResponse();
}
$consigneId = (int)($request->get('consigne') ?? 0);
$secteurConsigne = $consigneId ? $this->vetoadomEntityManager->getRepository(SecteurConsigne::class)->findOneBy([
'id' => $consigneId,
'secteur' => $this->vetoadomEntityManager->getRepository(Secteur::class)->getUserSecteurIds($user),
'dateSuppression' => null
]) : null;
if (!$secteurConsigne) {
$this->customJsonResponse->setForbidden(true);
$this->customJsonResponse->setField('alert', $this->translator->trans('secteur_consignes_modal.consigne_unknown', [], 'validators'));
return $this->customJsonResponse->sendResponse();
}
$secteurConsigne->setDateSuppression(new \DateTime());
$secteurConsigne->setSuppressionUserId($user->getId());
$this->vetoadomEntityManager->flush();
$this->customJsonResponse->setField('consignes', $this->vetoadomEntityManager->getRepository(SecteurConsigne::class)->getSecteurConsignes($secteurConsigne->getSecteur()->getId()));
return $this->customJsonResponse->sendResponse();
}
break;
case 'getSecteurForcageInfo':
{
if (!$this->isGranted('ROLE_ADMIN')) {
$this->customJsonResponse->setForbidden(true);
$this->customJsonResponse->setField('alert', $this->translator->trans('common.action_unknown', [], 'validators'));
return $this->customJsonResponse->sendResponse();
}
$secteurId = (int)($request->get('secteur') ?? 0);
$secteur = $secteurId ? $this->vetoadomEntityManager->getRepository(Secteur::class)->getUserSecteur($user,$secteurId) : null;
if (!$secteur) {
$this->customJsonResponse->setForbidden(true);
$this->customJsonResponse->setField('alert', $this->translator->trans('secteur_forcage_modal.secteur_unknown', [], 'validators'));
return $this->customJsonResponse->sendResponse();
}
# checkFilledAllSecteurTarifs
if (!$this->vetoadomEntityManager->getRepository(Tarif::class)->checkFilledAllSecteurTarifs($secteurId)) {
$this->customJsonResponse->setForbidden(true);
$this->customJsonResponse->setField('alert', $this->translator->trans('secteur_forcage_modal.secteur_tarifs_not_filled', [], 'validators'));
return $this->customJsonResponse->sendResponse();
}
$secteurForcageChargeService = $this->vetoadomEntityManager->getRepository(SecteurForcageChargeService::class)->findOneBy(['secteur' => $secteur, 'dateSuppression' => null]);
$secteurForcageTarif = $this->vetoadomEntityManager->getRepository(SecteurForcageTarif::class)->getCurrentForcageTarif($secteur->getId());
$this->customJsonResponse->setField('forcageChargeService', $secteurForcageChargeService ? $secteurForcageChargeService->getChargeService()->getId() : 0);
$this->customJsonResponse->setField('forcageTarif', $secteurForcageTarif ? $secteurForcageTarif->getTarif()->getId() : 0);
return $this->customJsonResponse->sendResponse();
}
break;
case 'secteurForcage':
{
if (!$this->isGranted('ROLE_ADMIN')) {
$this->customJsonResponse->setForbidden(true);
$this->customJsonResponse->setField('alert', $this->translator->trans('common.action_unknown', [], 'validators'));
return $this->customJsonResponse->sendResponse();
}
$secteurId = (int)($request->get('secteur') ?? 0);
$secteur = $secteurId ? $this->vetoadomEntityManager->getRepository(Secteur::class)->getUserSecteur($user,$secteurId) : null;
if (!$secteur) {
$this->customJsonResponse->setForbidden(true);
$this->customJsonResponse->setField('alert', $this->translator->trans('secteur_forcage_modal.secteur_unknown', [], 'validators'));
return $this->customJsonResponse->sendResponse();
}
if (!$this->vetoadomEntityManager->getRepository(Tarif::class)->checkFilledAllSecteurTarifs($secteurId)) {
$this->customJsonResponse->setForbidden(true);
$this->customJsonResponse->setField('alert', $this->translator->trans('secteur_forcage_modal.secteur_tarifs_not_filled', [], 'validators'));
return $this->customJsonResponse->sendResponse();
}
# secteurForcageChargeService
$secteurForcageChargeService = $this->vetoadomEntityManager->getRepository(SecteurForcageChargeService::class)->findOneBy(['secteur' => $secteur, 'dateSuppression' => null]);
$forcageChargeServiceId = (int)($request->get('forcageChargeService') ?? 0);
if ($forcageChargeServiceId) {
$chargeService = $this->vetoadomEntityManager->getRepository(ChargeService::class)->findOneBy(['id' => $forcageChargeServiceId]);
if (!$chargeService) {
$this->customJsonResponse->setForbidden(true);
$this->customJsonResponse->setField('alert', $this->translator->trans('secteur_forcage_modal.charge_service_unknown', [], 'validators'));
return $this->customJsonResponse->sendResponse();
}
if (!$secteurForcageChargeService) {
$secteurForcageChargeService = new SecteurForcageChargeService();
$secteurForcageChargeService->setSecteur($secteur);
$secteurForcageChargeService->setChargeService($chargeService);
$this->vetoadomEntityManager->persist($secteurForcageChargeService);
}
else {
$secteurForcageChargeService->setChargeService($chargeService);
}
}
else if ($secteurForcageChargeService) {
$secteurForcageChargeService->setDateSuppression(new \DateTime());
}
if (!$forcageChargeServiceId) {
$forcageChargeServiceId = 1;
}
# secteurForcageTarif
$secteurForcageTarif = $this->vetoadomEntityManager->getRepository(SecteurForcageTarif::class)->getCurrentForcageTarif($secteur->getId()) ?? new SecteurForcageTarif();
$forcageTarifId = (int)($request->get('forcageTarif') ?? 0);
if ($forcageTarifId) {
$tarif = $this->vetoadomEntityManager->getRepository(Tarif::class)->findOneBy(['id' => $forcageTarifId]);
if (!$tarif) {
$this->customJsonResponse->setForbidden(true);
$this->customJsonResponse->setField('alert', $this->translator->trans('secteur_forcage_modal.tarif_unknown', [], 'validators'));
return $this->customJsonResponse->sendResponse();
}
if (!$secteurForcageTarif->getId()) {
$secteurForcageTarif = new SecteurForcageTarif();
$secteurForcageTarif->setSecteur($secteur);
$secteurForcageTarif->setTarif($tarif);
$this->vetoadomEntityManager->persist($secteurForcageTarif);
}
else {
$secteurForcageTarif->setTarif($tarif);
}
}
else if ($secteurForcageTarif) {
$secteurForcageTarif->setDateSuppression(new \DateTime());
}
$this->vetoadomEntityManager->flush();
# light
if (
$secteurForcageChargeService
&& $secteurForcageChargeService->getDateSuppression() === null
) {
$light = $secteurForcageChargeService->getChargeService()->getCouleur();
} else {
$lightConsultations = $this->vetoadomEntityManager->getRepository(Consultation::class)->getLightConsultations($secteur->getId());
$currentVetsNumber = $this->commonHelper->getCurrentVetsNumber($secteur);
$lightCode = (
$currentVetsNumber == 0
|| ($lightConsultations['consultationsNumber'] / $currentVetsNumber) > $secteur->getCoeffFeux()
) ? 'orange' : 'green';
$light = $this->vetoadomEntityManager->getRepository(ChargeService::class)->findOneBy(['code' => $lightCode])->getCouleur();
}
$this->customJsonResponse->setField('forcageChargeService', $forcageChargeServiceId);
$this->customJsonResponse->setField('light', $light);
//$this->customJsonResponse->setField('forcageTarif', $forcageTarifId);
return $this->customJsonResponse->sendResponse();
}
break;
default:{
$this->customJsonResponse->setForbidden(true);
$this->customJsonResponse->setField('alert', $this->translator->trans('common.action_unknown', [], 'validators'));
return $this->customJsonResponse->sendResponse();
}
}
}
}