src/Controller/StatsController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Order;
  4. use App\Repository\OrderRepository;
  5. use App\Repository\WidgetRepository;
  6. use Ramsey\Uuid\Uuid;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Nelmio\ApiDocBundle\Annotation\Security;
  12. class StatsController extends AbstractController
  13. {
  14. /**
  15. * @Security(name="Bearer")
  16. */
  17. #[Route('/api-area/stats/all-widgets', name: 'stats_widgets')]
  18. public function index(Request $request, WidgetRepository $widgetRepository): Response
  19. {
  20. return $this->json(
  21. $widgetRepository->getWidgetsShortList()
  22. );
  23. }
  24. /**
  25. * @Security(name="Bearer")
  26. */
  27. #[Route('/api-area/stats/details', name: 'stats_details')]
  28. public function getStats(Request $request, OrderRepository $orderRepository)
  29. {
  30. $ids = explode(',', $request->get('widgets'));
  31. $from = new \DateTimeImmutable($request->get('from') ?? '-1 day');
  32. $to = new \DateTimeImmutable($request->get('to') ?? 'now');
  33. $data = $orderRepository->getStats($ids, $from, $to);
  34. $stats = [];
  35. /** @var Order $order */
  36. foreach ($data as $order) {
  37. $object = [
  38. 'id' => $order->getId(),
  39. 'widgetId' => $order->getWidget()->getId(),
  40. 'createdAt' => $order->getCreatedAt()->format('d.m.Y H:i:s'),
  41. 'paymentStatus' => $order->getPaymentStatus(),
  42. 'paymentToken' => $order->getPaymentToken(),
  43. 'paymentData' => $order->getPaymentData(),
  44. ];
  45. $orderItems = $order->getOrderItems();
  46. $items = [];
  47. foreach($orderItems as $orderItem) {
  48. $getDeliveredAt = $orderItem->getDeliveredAt()?$orderItem->getDeliveredAt()->format('d.m.Y H:i'):null;
  49. $items[] = [
  50. 'basketKey' => $orderItem->getBasketKey(),
  51. 'productId' => $orderItem->getProductId(),
  52. 'quantity' => $orderItem->getQuantity(),
  53. 'deliveryType' => $orderItem->getDeliveryType(),
  54. 'timeToSend' => $orderItem->getTimeToSend()->format('d.m.Y H:i'),
  55. 'deliveredAt' => $getDeliveredAt,
  56. 'tiberiumOrderId' => $orderItem->getTiberiumOrderId(),
  57. 'amount' => $orderItem->getAmount(),
  58. ];
  59. }
  60. $object['items'] = $items;
  61. $stats[] = $object;
  62. }
  63. return $this->json($stats);
  64. }
  65. }