src/Controller/Dashboard/DashboardController.php line 99

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Dashboard;
  4. use App\Entity\Order\CheckoutInterface;
  5. use App\Entity\Order\OrderInterface;
  6. use App\Entity\Payment\PaymentInterface;
  7. use App\Service\Statistics\StatisticsService;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. /**
  15.  * @IsGranted("ROLE_SHOP_MANAGER")
  16.  */
  17. class DashboardController extends AbstractController
  18. {
  19.     private string $currency;
  20.     public function __construct(
  21.         private ContainerBagInterface $containerBag,
  22.         private TranslatorInterface $translator,
  23.         private StatisticsService $statisticsService,
  24.     ) {
  25.         $this->currency = $containerBag->get('foxorders_default_currency_symbol');
  26.     }
  27.     public function salesByPeriod(?int $id): ?JsonResponse
  28.     {
  29.         $result = $this->statisticsService->prepareSalesPerPeriod($id);
  30.         return new JsonResponse([
  31.             'xaxisTitle' => $this->translator->trans('app.global.pages.dashboard.components.xaxis_title'),
  32.             'xaxis' => $result['xaxis'],
  33.             'currency' => $this->currency,
  34.             'firstCurveTitle' => $this->translator->trans('app.global.pages.dashboard.sales.title'),
  35.             'secondCurveTitle' => $this->translator->trans('app.global.pages.dashboard.sales.turnover_short'),
  36.             'firstCurve' => $this->translator->trans('app.global.pages.dashboard.sales.total'),
  37.             'secondCurve' => $this->translator->trans('app.global.pages.dashboard.sales.turnover'),
  38.             'legend' => array_values($result['legend']),
  39.             'firstCurveValues' => array_values($result['sales']),
  40.             'firstCurveTotal' => number_format($result['totalSales'], 0, ',', ' '),
  41.             'secondCurveValues' => array_values($result['turnovers']),
  42.             'secondCurveTotal' => number_format($result['totalTurnover'], 2, ',', ' '),
  43.             'colors' => $result['colors'],
  44.         ]);
  45.     }
  46.     public function salesPerMode(?int $id): ?JsonResponse
  47.     {
  48.         $result = $this->statisticsService->prepareSalesPerMode($id);
  49.         return new JsonResponse([
  50.             'serieTitle' => $this->translator->trans('app.global.pages.dashboard.sales.mode.total'),
  51.             'series' => array_values($result),
  52.         ]);
  53.     }
  54.     public function salesByPaymentMethod(?int $id): JsonResponse
  55.     {
  56.         $result = $this->statisticsService->prepareSalesByPaymentMethod($id);
  57.         return new JsonResponse([
  58.             'serieTitle' => $this->translator->trans('app.global.pages.dashboard.sales.turnover'),
  59.             'series' => array_values($result),
  60.             'currency' => $this->currency,
  61.         ]);
  62.     }
  63.     public function salesByhours(?int $id): JsonResponse
  64.     {
  65.         $result = $this->statisticsService->prepareSalesperHours($id);
  66.         return new JsonResponse([
  67.             'title' => $this->translator->trans('app.global.pages.dashboard.sales.total'),
  68.             'total' => number_format($result['total'], 0, ',', ' '),
  69.             'series' => array_values($result['sales']),
  70.             'xaxis' => array_keys($result['sales']),
  71.         ]);
  72.     }
  73.     public function totalSales(?int $id): ?JsonResponse
  74.     {
  75.         $result = $this->statisticsService->prepareSales($id);
  76.         return new JsonResponse([
  77.             'title' => $this->translator->trans('app.global.pages.dashboard.sales.title'),
  78.             'titleJx' => $this->translator->trans('app.global.pages.dashboard.sales.title_jx'),
  79.             'total' => number_format($result['sales'][0]['total'], 0, ',', ' '),
  80.             'totalJx' => number_format($result['salesJx'][0]['total'], 0, ',', ' '),
  81.         ]);
  82.     }
  83.     public function ordersByDeviceType(?int $id): JsonResponse
  84.     {
  85.         $result = $this->statisticsService->prepareOrdersPerDeviceType($id);
  86.         return new JsonResponse([
  87.             'serieTitle' => $this->translator->trans('app.global.pages.dashboard.shops.total'),
  88.             'series' => array_values($result),
  89.         ]);
  90.     }
  91.     public function totalCustomers(?int $id): ?JsonResponse
  92.     {
  93.         $result = $this->statisticsService->prepareCustomers($id);
  94.         return new JsonResponse([
  95.             'title' => $this->translator->trans('app.global.pages.dashboard.customers.title'),
  96.             'annonymousOrdersTitle' => $this->translator->trans('app.global.pages.dashboard.orders.anonymous_orders'),
  97.             'total' => $result['customers'][0]['total'],
  98.             'validAnnonymousOrders' => $result['validAnnonymousOrders'][0]['total'],
  99.         ]);
  100.     }
  101.     public function topSellingProducts(?int $id): Response
  102.     {
  103.         $products = $this->statisticsService->prepareBestProducts($id);
  104.         return $this->renderForm('Dashboard/Components/Containers/products-best-selling.html.twig', compact('products'));
  105.     }
  106.     public function lastArrivingOrders(?int $id): Response
  107.     {
  108.         return $this->renderForm('Dashboard/Components/Containers/orders-last-arriving.html.twig', [
  109.             'orders' => $this->statisticsService->prepareLastOrders($id),
  110.             'checkoutBadges' => CheckoutInterface::BADGES,
  111.             'paymentBadges' => PaymentInterface::BADGES,
  112.             'orderBadges' => OrderInterface::BADGES,
  113.         ]);
  114.     }
  115.     public function topSellingShops(?int $id): Response
  116.     {
  117.         $shops = $this->statisticsService->prepareBestShops($id);
  118.         return $this->renderForm('Dashboard/Components/Containers/shops-best-selling.html.twig', compact('shops'));
  119.     }
  120. }