<?php
declare(strict_types=1);
namespace App\Controller\Dashboard;
use App\Entity\Order\CheckoutInterface;
use App\Entity\Order\OrderInterface;
use App\Entity\Payment\PaymentInterface;
use App\Service\Statistics\StatisticsService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @IsGranted("ROLE_SHOP_MANAGER")
*/
class DashboardController extends AbstractController
{
private string $currency;
public function __construct(
private ContainerBagInterface $containerBag,
private TranslatorInterface $translator,
private StatisticsService $statisticsService,
) {
$this->currency = $containerBag->get('foxorders_default_currency_symbol');
}
public function salesByPeriod(?int $id): ?JsonResponse
{
$result = $this->statisticsService->prepareSalesPerPeriod($id);
return new JsonResponse([
'xaxisTitle' => $this->translator->trans('app.global.pages.dashboard.components.xaxis_title'),
'xaxis' => $result['xaxis'],
'currency' => $this->currency,
'firstCurveTitle' => $this->translator->trans('app.global.pages.dashboard.sales.title'),
'secondCurveTitle' => $this->translator->trans('app.global.pages.dashboard.sales.turnover_short'),
'firstCurve' => $this->translator->trans('app.global.pages.dashboard.sales.total'),
'secondCurve' => $this->translator->trans('app.global.pages.dashboard.sales.turnover'),
'legend' => array_values($result['legend']),
'firstCurveValues' => array_values($result['sales']),
'firstCurveTotal' => number_format($result['totalSales'], 0, ',', ' '),
'secondCurveValues' => array_values($result['turnovers']),
'secondCurveTotal' => number_format($result['totalTurnover'], 2, ',', ' '),
'colors' => $result['colors'],
]);
}
public function salesPerMode(?int $id): ?JsonResponse
{
$result = $this->statisticsService->prepareSalesPerMode($id);
return new JsonResponse([
'serieTitle' => $this->translator->trans('app.global.pages.dashboard.sales.mode.total'),
'series' => array_values($result),
]);
}
public function salesByPaymentMethod(?int $id): JsonResponse
{
$result = $this->statisticsService->prepareSalesByPaymentMethod($id);
return new JsonResponse([
'serieTitle' => $this->translator->trans('app.global.pages.dashboard.sales.turnover'),
'series' => array_values($result),
'currency' => $this->currency,
]);
}
public function salesByhours(?int $id): JsonResponse
{
$result = $this->statisticsService->prepareSalesperHours($id);
return new JsonResponse([
'title' => $this->translator->trans('app.global.pages.dashboard.sales.total'),
'total' => number_format($result['total'], 0, ',', ' '),
'series' => array_values($result['sales']),
'xaxis' => array_keys($result['sales']),
]);
}
public function totalSales(?int $id): ?JsonResponse
{
$result = $this->statisticsService->prepareSales($id);
return new JsonResponse([
'title' => $this->translator->trans('app.global.pages.dashboard.sales.title'),
'titleJx' => $this->translator->trans('app.global.pages.dashboard.sales.title_jx'),
'total' => number_format($result['sales'][0]['total'], 0, ',', ' '),
'totalJx' => number_format($result['salesJx'][0]['total'], 0, ',', ' '),
]);
}
public function ordersByDeviceType(?int $id): JsonResponse
{
$result = $this->statisticsService->prepareOrdersPerDeviceType($id);
return new JsonResponse([
'serieTitle' => $this->translator->trans('app.global.pages.dashboard.shops.total'),
'series' => array_values($result),
]);
}
public function totalCustomers(?int $id): ?JsonResponse
{
$result = $this->statisticsService->prepareCustomers($id);
return new JsonResponse([
'title' => $this->translator->trans('app.global.pages.dashboard.customers.title'),
'annonymousOrdersTitle' => $this->translator->trans('app.global.pages.dashboard.orders.anonymous_orders'),
'total' => $result['customers'][0]['total'],
'validAnnonymousOrders' => $result['validAnnonymousOrders'][0]['total'],
]);
}
public function topSellingProducts(?int $id): Response
{
$products = $this->statisticsService->prepareBestProducts($id);
return $this->renderForm('Dashboard/Components/Containers/products-best-selling.html.twig', compact('products'));
}
public function lastArrivingOrders(?int $id): Response
{
return $this->renderForm('Dashboard/Components/Containers/orders-last-arriving.html.twig', [
'orders' => $this->statisticsService->prepareLastOrders($id),
'checkoutBadges' => CheckoutInterface::BADGES,
'paymentBadges' => PaymentInterface::BADGES,
'orderBadges' => OrderInterface::BADGES,
]);
}
public function topSellingShops(?int $id): Response
{
$shops = $this->statisticsService->prepareBestShops($id);
return $this->renderForm('Dashboard/Components/Containers/shops-best-selling.html.twig', compact('shops'));
}
}