src/Controller/DashboardController.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\StbUser;
  4. use App\Service\EntityProcessor;
  5. use App\Service\ProfileLoader;
  6. use App\Service\System\DashboardRepository;
  7. use App\Utility\Profile;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Contracts\HttpClient\HttpClientInterface;
  13. class DashboardController extends AbstractController {
  14.     /**
  15.      * @var EntityProcessor
  16.      */
  17.     private $entityProcessor;
  18.     /**
  19.      * @var DashboardRepository
  20.      */
  21.     private $repository;
  22.     /**
  23.      * @var ProfileLoader
  24.      */
  25.     private $profileLoader;
  26.     /**
  27.      * @var HttpClientInterface
  28.      */
  29.     private $client;
  30.     /**
  31.      * DashboardController constructor.
  32.      * @param EntityProcessor $entityProcessor
  33.      */
  34.     public function __construct(EntityProcessor $entityProcessorDashboardRepository $repositoryProfileLoader $profileLoaderHttpClientInterface $client) {
  35.         $this->entityProcessor $entityProcessor;
  36.         $this->repository $repository;
  37.         $this->profileLoader $profileLoader;
  38.         $this->client $client;
  39.     }
  40.     public function home(): Response {
  41.         $profile $this->profileLoader->getProfile();
  42.         if ($profile->isCloudProfile()) {
  43.             return $this->edgeHome();
  44.         }
  45.         $date = new \DateTime();
  46.         $res $this->repository->getDailyStats($date);
  47.         if ($res->isOk()) {
  48.             $dto $res->getDto();
  49.             $accettazioni $dto["dailyEntries"];
  50.             $produzioni $dto["dailyProductions"];
  51.             $spedizioni $dto["dailyShipments"];
  52.             $giacenzaPulito $dto["stock"];
  53.         } else {
  54.             $this->addFlash("danger""Errore nel caricamento statistiche: " $res->getErrorMessage());
  55.             $accettazioni 0;
  56.             $produzioni 0;
  57.             $spedizioni 0;
  58.             $giacenzaPulito 0;
  59.         }
  60.         $res $this->repository->getMonthlyProduction($this->getUser()->getCodMdep());
  61.         if ($res->isOk()) {
  62.             $monthlyProductions $res->getDto();
  63.         } else {
  64.             $monthlyProductions = [];
  65.         }
  66.         $res $this->repository->getMonthlyShippings($this->getUser()->getCodMdep());
  67.         if ($res->isOk()) {
  68.             $monthlyShippings $res->getDto();
  69.         } else {
  70.             $monthlyShippings = [];
  71.         }
  72.         $res $this->repository->getProductionForType();
  73.         if ($res->isOk()) {
  74.             $productionForType $res->getDto();
  75.         } else {
  76.             $productionForType = [];
  77.         }
  78.         return $this->render("dashboard.html.twig", [
  79.             "page" => "Dashboard",
  80.             "accettazioni" => $accettazioni,
  81.             "produzioni" => $produzioni,
  82.             "spedizioni" => $spedizioni,
  83.             "monthlyProductions" => $monthlyProductions,
  84.             "monthlyShippings" => $monthlyShippings,
  85.             "productionForType" => $productionForType,
  86.             "giacenzaPulito" => $giacenzaPulito
  87.         ]);
  88.     }
  89.     public function edgeHome(): Response {
  90.         $monthlyEntries 0;
  91.         $monthlyProds 0;
  92.         $monthlyShips 0;
  93.         $monthlyTransf 0;
  94.         $totalProductions 0;
  95.         $depoProductions = [];
  96.         $typeProductions = [];
  97.         $productProductions = [];
  98.         $monthlyStats $this->repository->getMonthlyStats();
  99.         if ($monthlyStats->isOk()) {
  100.             $dto $monthlyStats->getDto();
  101.             $monthlyEntries $dto["monthlyEntries"];
  102.             $monthlyProds $dto["monthlyProductions"];
  103.             $monthlyShips $dto["monthlyShipments"];
  104.             $monthlyTransf $dto["monthlyTransfers"];
  105.         } else {
  106.             $this->addFlash("danger""Errore nel caricamento statistiche mensili: " $monthlyStats->getErrorMessage());
  107.         }
  108.         $yearlyProductionsRet $this->repository->getMonthlyProduction();
  109.         if ($yearlyProductionsRet->isOk()) {
  110.             $yearlyProductions $yearlyProductionsRet->getDto();
  111.         } else {
  112.             $this->addFlash("danger""Errore nel caricamento produzioni mensili: " $yearlyProductionsRet->getErrorMessage());
  113.             $yearlyProductions = [];
  114.         }
  115.         $yearlyShippingRet $this->repository->getMonthlyShippings();
  116.         if ($yearlyShippingRet->isOk()) {
  117.             $yearlyShipping $yearlyShippingRet->getDto();
  118.         } else {
  119.             $this->addFlash("danger""Errore nel caricamento spedizioni mensili: " $yearlyShippingRet->getErrorMessage());
  120.             $yearlyShipping = [];
  121.         }
  122.         return $this->render("dashboard_edge.html.twig", [
  123.             "page" => "Dashboard",
  124.             "monthlyEntries" => $monthlyEntries,
  125.             "monthlyProds" => $monthlyProds,
  126.             "monthlyShips" => $monthlyShips,
  127.             "monthlyTransf" => $monthlyTransf,
  128.             "totalProductions" => $totalProductions,
  129.             "depoProductions" => $depoProductions,
  130.             "typeProductions" => $typeProductions,
  131.             "productProductions" => $productProductions,
  132.             "yearlyProductions" => $yearlyProductions,
  133.             "yearlyShippings" => $yearlyShipping
  134.         ]);
  135.     }
  136.     /**
  137.      * @param Request $request
  138.      * @param $locale
  139.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  140.      */
  141.     public function switchLanguage(Request $request$locale) {
  142.         $fallbackRoute $request->query->get("_route""index");
  143.         $fallbackParams $request->query->get("_params", []);
  144.         /**
  145.          * @var StbUser $user
  146.          */
  147.         $user $this->getUser();
  148.         $user->setCodLang($locale);
  149.         $user->update();
  150.         $response $this->entityProcessor->processEntity($user);
  151.         if (!$response->isOk()) {
  152.             $this->addFlash("danger"$response->getErrorMessage());
  153.             return $this->redirectToRoute($fallbackRoute$fallbackParams);
  154.         }
  155.         $fallbackParams["_locale"] = $locale;
  156.         return $this->redirectToRoute($fallbackRoute$fallbackParams);
  157.     }
  158.     public function getConnectionStatusAction($codMdep): JsonResponse {
  159.         $profiles $this->profileLoader->getAllProfiles();
  160.         $depoProfilef array_filter($profiles, function (Profile $profile) use ($codMdep) {
  161.             return empty($codMdep) || $profile->getCodMdep() == $codMdep;
  162.         });
  163.         $connections = [];
  164.         $lastSyncData $this->repository->getLastSyncDate();
  165.         foreach ($depoProfilef as $depoProfile) {
  166.             $codMdep $depoProfile->getCodMdep();
  167.             if (!$codMdep)
  168.                 continue;
  169.             $systemUrl $depoProfile->getPublicUrl() . "/system";
  170.             $lastSync array_filter($lastSyncData->getDto(), function ($data) use ($codMdep) {
  171.                 return $data["codMdep"] == $codMdep;
  172.             });
  173.             try {
  174.                 $response $this->client->request("GET"$systemUrl, ["timeout" => 10]);
  175.                 $status $response->getStatusCode() === Response::HTTP_OK;
  176.             } catch (\Exception $exception) {
  177.                 $status false;
  178.             }
  179.             $connections[$depoProfile->getProfileId()] = [
  180.                 "connection" => $status,
  181.                 "lastUpdate" => empty($lastSync) ? null reset($lastSync)["lastData"]
  182.             ];
  183.         }
  184. //        $systemUrl = 'http://192.168.2.33:8080/ems-api/#/';
  185.         return new JsonResponse($connections);
  186.     }
  187.     public function getMonthlyStatsForProduct(): JsonResponse {
  188.         $productProductions = [];
  189.         $monthlyProductions $this->repository->getMonthlyProductionByProductAndDepo();
  190.         if ($monthlyProductions->isOk()) {
  191.             $arr $monthlyProductions->getDto();
  192.             foreach ($arr as $production) {
  193.                 if (!array_key_exists($production["codMart"], $productProductions)) {
  194.                     $productProductions[$production["codMart"]] = [
  195.                         "codMart" => $production["codMart"],
  196.                         "codMtip" => $production["codMtip"],
  197.                         "descrizione" => $production["descrizione"]
  198.                     ];
  199.                 }
  200.                 $productProductions[$production["codMart"]][$production["codMdep"]] = ["qtaProd" => $production["qtaProd"], "qtaSel" => $production["qtaSel"]];
  201.             }
  202.         } else {
  203.             return new JsonResponse(nullResponse::HTTP_BAD_REQUEST);
  204.         }
  205.         return new JsonResponse($productProductions);
  206.     }
  207.     public function getDailyData(): JsonResponse {
  208.         $dailyStats $this->repository->getDailyStats();
  209.         if ($dailyStats->isOk()) {
  210.             $dto $dailyStats->getDto();
  211.             $dailyEntries $dto["dailyEntries"];
  212.             $dailyProds $dto["dailyProductions"];
  213.             $dailyShips $dto["dailyShipments"];
  214.             $dailyTransf $dto["dailyTransfers"];
  215.             $currentStock $dto["stock"];
  216.         } else {
  217.             $this->addFlash("danger""Errore nel caricamento statistiche giornaliere: " $dailyStats->getErrorMessage());
  218.         }
  219.         return new JsonResponse([
  220.             "dailyEntries" => $dailyEntries,
  221.             "dailyProds" => $dailyProds,
  222.             "dailyShips" => $dailyShips,
  223.             "dailyTransf" => $dailyTransf,
  224.             "currentStock" => $currentStock,
  225.         ]);
  226.     }
  227. }