<?php
namespace App\Controller;
use App\Entity\StbUser;
use App\Service\EntityProcessor;
use App\Service\ProfileLoader;
use App\Service\System\DashboardRepository;
use App\Utility\Profile;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class DashboardController extends AbstractController {
/**
* @var EntityProcessor
*/
private $entityProcessor;
/**
* @var DashboardRepository
*/
private $repository;
/**
* @var ProfileLoader
*/
private $profileLoader;
/**
* @var HttpClientInterface
*/
private $client;
/**
* DashboardController constructor.
* @param EntityProcessor $entityProcessor
*/
public function __construct(EntityProcessor $entityProcessor, DashboardRepository $repository, ProfileLoader $profileLoader, HttpClientInterface $client) {
$this->entityProcessor = $entityProcessor;
$this->repository = $repository;
$this->profileLoader = $profileLoader;
$this->client = $client;
}
public function home(): Response {
$profile = $this->profileLoader->getProfile();
if ($profile->isCloudProfile()) {
return $this->edgeHome();
}
$date = new \DateTime();
$res = $this->repository->getDailyStats($date);
if ($res->isOk()) {
$dto = $res->getDto();
$accettazioni = $dto["dailyEntries"];
$produzioni = $dto["dailyProductions"];
$spedizioni = $dto["dailyShipments"];
$giacenzaPulito = $dto["stock"];
} else {
$this->addFlash("danger", "Errore nel caricamento statistiche: " . $res->getErrorMessage());
$accettazioni = 0;
$produzioni = 0;
$spedizioni = 0;
$giacenzaPulito = 0;
}
$res = $this->repository->getMonthlyProduction($this->getUser()->getCodMdep());
if ($res->isOk()) {
$monthlyProductions = $res->getDto();
} else {
$monthlyProductions = [];
}
$res = $this->repository->getMonthlyShippings($this->getUser()->getCodMdep());
if ($res->isOk()) {
$monthlyShippings = $res->getDto();
} else {
$monthlyShippings = [];
}
$res = $this->repository->getProductionForType();
if ($res->isOk()) {
$productionForType = $res->getDto();
} else {
$productionForType = [];
}
return $this->render("dashboard.html.twig", [
"page" => "Dashboard",
"accettazioni" => $accettazioni,
"produzioni" => $produzioni,
"spedizioni" => $spedizioni,
"monthlyProductions" => $monthlyProductions,
"monthlyShippings" => $monthlyShippings,
"productionForType" => $productionForType,
"giacenzaPulito" => $giacenzaPulito
]);
}
public function edgeHome(): Response {
$monthlyEntries = 0;
$monthlyProds = 0;
$monthlyShips = 0;
$monthlyTransf = 0;
$totalProductions = 0;
$depoProductions = [];
$typeProductions = [];
$productProductions = [];
$monthlyStats = $this->repository->getMonthlyStats();
if ($monthlyStats->isOk()) {
$dto = $monthlyStats->getDto();
$monthlyEntries = $dto["monthlyEntries"];
$monthlyProds = $dto["monthlyProductions"];
$monthlyShips = $dto["monthlyShipments"];
$monthlyTransf = $dto["monthlyTransfers"];
} else {
$this->addFlash("danger", "Errore nel caricamento statistiche mensili: " . $monthlyStats->getErrorMessage());
}
$yearlyProductionsRet = $this->repository->getMonthlyProduction();
if ($yearlyProductionsRet->isOk()) {
$yearlyProductions = $yearlyProductionsRet->getDto();
} else {
$this->addFlash("danger", "Errore nel caricamento produzioni mensili: " . $yearlyProductionsRet->getErrorMessage());
$yearlyProductions = [];
}
$yearlyShippingRet = $this->repository->getMonthlyShippings();
if ($yearlyShippingRet->isOk()) {
$yearlyShipping = $yearlyShippingRet->getDto();
} else {
$this->addFlash("danger", "Errore nel caricamento spedizioni mensili: " . $yearlyShippingRet->getErrorMessage());
$yearlyShipping = [];
}
return $this->render("dashboard_edge.html.twig", [
"page" => "Dashboard",
"monthlyEntries" => $monthlyEntries,
"monthlyProds" => $monthlyProds,
"monthlyShips" => $monthlyShips,
"monthlyTransf" => $monthlyTransf,
"totalProductions" => $totalProductions,
"depoProductions" => $depoProductions,
"typeProductions" => $typeProductions,
"productProductions" => $productProductions,
"yearlyProductions" => $yearlyProductions,
"yearlyShippings" => $yearlyShipping
]);
}
/**
* @param Request $request
* @param $locale
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function switchLanguage(Request $request, $locale) {
$fallbackRoute = $request->query->get("_route", "index");
$fallbackParams = $request->query->get("_params", []);
/**
* @var StbUser $user
*/
$user = $this->getUser();
$user->setCodLang($locale);
$user->update();
$response = $this->entityProcessor->processEntity($user);
if (!$response->isOk()) {
$this->addFlash("danger", $response->getErrorMessage());
return $this->redirectToRoute($fallbackRoute, $fallbackParams);
}
$fallbackParams["_locale"] = $locale;
return $this->redirectToRoute($fallbackRoute, $fallbackParams);
}
public function getConnectionStatusAction($codMdep): JsonResponse {
$profiles = $this->profileLoader->getAllProfiles();
$depoProfilef = array_filter($profiles, function (Profile $profile) use ($codMdep) {
return empty($codMdep) || $profile->getCodMdep() == $codMdep;
});
$connections = [];
$lastSyncData = $this->repository->getLastSyncDate();
foreach ($depoProfilef as $depoProfile) {
$codMdep = $depoProfile->getCodMdep();
if (!$codMdep)
continue;
$systemUrl = $depoProfile->getPublicUrl() . "/system";
$lastSync = array_filter($lastSyncData->getDto(), function ($data) use ($codMdep) {
return $data["codMdep"] == $codMdep;
});
try {
$response = $this->client->request("GET", $systemUrl, ["timeout" => 10]);
$status = $response->getStatusCode() === Response::HTTP_OK;
} catch (\Exception $exception) {
$status = false;
}
$connections[$depoProfile->getProfileId()] = [
"connection" => $status,
"lastUpdate" => empty($lastSync) ? null : reset($lastSync)["lastData"]
];
}
// $systemUrl = 'http://192.168.2.33:8080/ems-api/#/';
return new JsonResponse($connections);
}
public function getMonthlyStatsForProduct(): JsonResponse {
$productProductions = [];
$monthlyProductions = $this->repository->getMonthlyProductionByProductAndDepo();
if ($monthlyProductions->isOk()) {
$arr = $monthlyProductions->getDto();
foreach ($arr as $production) {
if (!array_key_exists($production["codMart"], $productProductions)) {
$productProductions[$production["codMart"]] = [
"codMart" => $production["codMart"],
"codMtip" => $production["codMtip"],
"descrizione" => $production["descrizione"]
];
}
$productProductions[$production["codMart"]][$production["codMdep"]] = ["qtaProd" => $production["qtaProd"], "qtaSel" => $production["qtaSel"]];
}
} else {
return new JsonResponse(null, Response::HTTP_BAD_REQUEST);
}
return new JsonResponse($productProductions);
}
public function getDailyData(): JsonResponse {
$dailyStats = $this->repository->getDailyStats();
if ($dailyStats->isOk()) {
$dto = $dailyStats->getDto();
$dailyEntries = $dto["dailyEntries"];
$dailyProds = $dto["dailyProductions"];
$dailyShips = $dto["dailyShipments"];
$dailyTransf = $dto["dailyTransfers"];
$currentStock = $dto["stock"];
} else {
$this->addFlash("danger", "Errore nel caricamento statistiche giornaliere: " . $dailyStats->getErrorMessage());
}
return new JsonResponse([
"dailyEntries" => $dailyEntries,
"dailyProds" => $dailyProds,
"dailyShips" => $dailyShips,
"dailyTransf" => $dailyTransf,
"currentStock" => $currentStock,
]);
}
}