<?php
namespace App\Controller\Front;
use App\Controller\AbstractScpController;
use App\Entity\Link;
use App\Entity\User;
use App\Manager\ConsumptionManager;
use App\Manager\ContractManager;
use App\Manager\ConversationManager;
use App\Manager\InvoiceManager;
use App\Manager\MeterReadingManager;
use App\Manager\NetworkInterventionManager;
use App\Manager\SupervisionManager;
use App\Manager\TimetableManager;
use App\Manager\WateringTips\WateringTipsManager;
use App\Manager\WaterPurificationApparatusManager;
use App\Repository\App\DashboardRepository;
use App\Repository\App\LinkRepository;
use App\Repository\App\UserRepository;
use App\Traits\DonneeCompteClientAvailableTrait;
use Doctrine\DBAL\Exception;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class DashboardController extends AbstractScpController
{
/**
* Injecte DonneeCompteClientManager dans le controlleur
* Ajoute automatiquement la variable donneeCompteClient aux paramètres de la méthode render
*/
use DonneeCompteClientAvailableTrait;
public function __construct(
protected DashboardRepository $dashboardRepository,
protected UserRepository $userRepository,
protected InvoiceManager $invoiceManager,
protected ContractManager $contractManager,
protected ConsumptionManager $consumptionManager,
protected LinkRepository $linkRepository,
protected MeterReadingManager $meterReadingManager,
protected ConversationManager $conversationManager,
protected TimetableManager $timetableManager,
protected WaterPurificationApparatusManager $wpApparatusManager,
protected NetworkInterventionManager $networkInterventionManager,
protected WateringTipsManager $wateringTipsManager,
protected SupervisionManager $supervisionManager,
) {
}
/**
* @Route("/", name="dashboard")
* @throws InvalidArgumentException
* @throws Exception
*/
public function display(): Response
{
/* @var User $user */
$user = $this->getUser();
$hasRoleWeather = $user->hasRole('ROLE_ELIGIBLE_WATERING_TIPS');
$guestNumber = 0;
if ($this->getUser()->isMainAccount()) {
$guestNumber = $this->userRepository->count(['type' => User::TYPE_GUEST, 'mainAccount' => $this->getUser()]);
}
$interventions = $this->getInterventions();
$hasWateringTips = false;
$address = [];
if ($hasRoleWeather) {
$hasWateringTips = !empty($this->wateringTipsManager->getActiveWateringTipsByUser($user));
$address = $this->wateringTipsManager->getAddressWithXYForWateringTips($user, true);
}
['stationWithoutIndex' => $stationWithoutIndex] = $this->meterReadingManager->getStationsWithIndexStatus($user);
return $this->render('front/dashboard/dashboard.html.twig', [
'dashboard' => $this->dashboardRepository->find(1),
'interventions' => $interventions,
'isMeterReadingAuthorised' => $this->meterReadingManager->isMeterReadingAuthorised($user),
'invoices' => $this->invoiceManager->getInvoicesToPay($user),
'guestNumber' => $guestNumber,
'hasEBDContracts' => $this->contractManager->hasEBDContracts($user),
'links' => $this->linkRepository->findAllByAreaAndRoles(Link::AREA_DASHBOARD, $this->getUser()->getRoles()),
'consumptions' => $this->consumptionManager->getDashboardConsumption($user),
'count_unread_conversation' => $this->conversationManager->countUnread($user),
'conversations' => $this->conversationManager->getConversations($user, 3),
'deadlines' => $this->timetableManager->getNextDeadlines($user),
'apparatus' => $this->wpApparatusManager->getAllForDashboard($user),
'weatherWeek' => $this->getParameter('weather_hidden') || $hasRoleWeather ?
'HIDDEN' : $this->wateringTipsManager->getWeatherWeekData($user),
'hasWateringTips' => $hasWateringTips,
'hasDroughtAlert' => $this->wateringTipsManager->isConcernedByDroughtAlert($user),
'activeTown' => $address['NomCommune'] ?? null,
'weatherHidden' => $this->getParameter('weather_hidden') ?? null,
'stationWithoutIndex' => $stationWithoutIndex,
]);
}
protected function getInterventions(): array
{
$data = $this->networkInterventionManager->getInterventions($this->getUser());
$interventions = [];
foreach ($data as $intervention) {
$interventionDate = md5($intervention['DateDebutIntervention'].$intervention['DateFinIntervention']);
if (!isset($interventions[$interventionDate])) {
$interventions[$interventionDate] = [
'DateDebutIntervention' => $intervention['DateDebutIntervention'],
'DateFinIntervention' => $intervention['DateFinIntervention'],
'deliveryStations' => [],
];
}
$interventions[$interventionDate]['deliveryStations'][] = $intervention;
}
return $interventions;
}
/**
* @throws InvalidArgumentException
*/
#[Route(
'/weather',
name: 'display_weather',
)]
public function displayWeather(): Response
{
$weatherWeek = $this->wateringTipsManager->getWeatherWeekData($this->getUser()) ?? [];
$address = $this->wateringTipsManager->getAddressWithXYForWateringTips($this->getUser(), true);
$status = array_key_exists('error', $weatherWeek) ? Response::HTTP_BAD_REQUEST : Response::HTTP_OK;
$content = $this->renderView('front/watering_tips/_weather_client_insert.html.twig', [
'dashboard' => $this->dashboardRepository->find(1),
'weatherWeek' => $weatherWeek,
'activeTown' => $address['LibelleCommune'] ?? null,
]);
return new Response(
content: $content,
status: $status
);
}
}