<?php
namespace App\Controller;
use App\Entity\Order;
use App\Repository\OrderRepository;
use App\Repository\WidgetRepository;
use Ramsey\Uuid\Uuid;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Nelmio\ApiDocBundle\Annotation\Security;
class StatsController extends AbstractController
{
/**
* @Security(name="Bearer")
*/
#[Route('/api-area/stats/all-widgets', name: 'stats_widgets')]
public function index(Request $request, WidgetRepository $widgetRepository): Response
{
return $this->json(
$widgetRepository->getWidgetsShortList()
);
}
/**
* @Security(name="Bearer")
*/
#[Route('/api-area/stats/details', name: 'stats_details')]
public function getStats(Request $request, OrderRepository $orderRepository)
{
$ids = explode(',', $request->get('widgets'));
$from = new \DateTimeImmutable($request->get('from') ?? '-1 day');
$to = new \DateTimeImmutable($request->get('to') ?? 'now');
$data = $orderRepository->getStats($ids, $from, $to);
$stats = [];
/** @var Order $order */
foreach ($data as $order) {
$object = [
'id' => $order->getId(),
'widgetId' => $order->getWidget()->getId(),
'createdAt' => $order->getCreatedAt()->format('d.m.Y H:i:s'),
'paymentStatus' => $order->getPaymentStatus(),
'paymentToken' => $order->getPaymentToken(),
'paymentData' => $order->getPaymentData(),
];
$orderItems = $order->getOrderItems();
$items = [];
foreach($orderItems as $orderItem) {
$getDeliveredAt = $orderItem->getDeliveredAt()?$orderItem->getDeliveredAt()->format('d.m.Y H:i'):null;
$items[] = [
'basketKey' => $orderItem->getBasketKey(),
'productId' => $orderItem->getProductId(),
'quantity' => $orderItem->getQuantity(),
'deliveryType' => $orderItem->getDeliveryType(),
'timeToSend' => $orderItem->getTimeToSend()->format('d.m.Y H:i'),
'deliveredAt' => $getDeliveredAt,
'tiberiumOrderId' => $orderItem->getTiberiumOrderId(),
'amount' => $orderItem->getAmount(),
];
}
$object['items'] = $items;
$stats[] = $object;
}
return $this->json($stats);
}
}