src/Controller/Front/AppController.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\Cart;
  4. use App\Entity\Category;
  5. use App\Entity\Group;
  6. use App\Entity\Notification;
  7. use App\Entity\Order;
  8. use App\Entity\Step;
  9. use App\Entity\User;
  10. use App\Service\ModuleService;
  11. use App\Service\Tools;
  12. use DateTime;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Doctrine\ORM\NonUniqueResultException;
  15. use Exception;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. /**
  21.  * Class AppController
  22.  * @package App\Controller\Admin
  23.  * @Route(path="", name="app_", options={"expose"=true})
  24.  */
  25. class AppController extends AbstractController
  26. {
  27.     private EntityManagerInterface $em;
  28.     private Tools $tools;
  29.     private ModuleService $moduleService;
  30.     public function __construct(
  31.         EntityManagerInterface $em,
  32.         Tools $tools,
  33.         ModuleService $moduleService
  34.     ) {
  35.         $this->em $em;
  36.         $this->tools $tools;
  37.         $this->moduleService $moduleService;
  38.     }
  39.     /**
  40.      * @return Response
  41.      * @throws Exception
  42.      * @Route("/", name="index")
  43.      */
  44.     public function index(): Response
  45.     {
  46.         $from = new DateTime($this->tools->getSettingByName('STATISTICS_FROM_DATE'));
  47.         /** @var Notification $notification */
  48.         $notification $this->em->getRepository(Notification::class)->findOneBy([
  49.             'user' => $this->getUser(),
  50.             'isRead' => 0,
  51.             'type' => Notification::TYPE_ALERT,
  52.         ]);
  53.         $isParentValidator false;
  54.         /** @var User $user */
  55.         $user $this->getUser();
  56.         foreach ($user->getChildren() as $child) {
  57.             if ($this->moduleService->access('catalog''validationParent'$child)) {
  58.                 $isParentValidator true;
  59.                 break;
  60.             }
  61.         }
  62.         return $this->render('front/app/index.html.twig', [
  63.             'page_title' => "<span class='text-primary'>Tableau</span> de bord",
  64.             'notification' => $notification,
  65.             'from' => $from,
  66.             'isParentValidator' => $isParentValidator,
  67.         ]);
  68.     }
  69.     /**
  70.      * @return Response
  71.      * @Route(path="/widget/cart-validation", name="cart_validation")
  72.      */
  73.     public function cartValidation(): Response
  74.     {
  75.         /** @var User $user */
  76.         $user $this->getUser();
  77.         /** @var Cart[] $carts */
  78.         $carts $this->em->getRepository(Cart::class)->findWaitingValidationByParent($user);
  79.         return $this->render('front/app/cart_validation.html.twig', [
  80.             'carts' => $carts,
  81.         ]);
  82.     }
  83.     /**
  84.      * @return Response
  85.      * @Route(path="/widget/cart-current", name="cart_current")
  86.      */
  87.     public function cartCurrent(): Response
  88.     {
  89.         /** @var User $user */
  90.         $user $this->getUser();
  91.         /** @var Cart[] $carts */
  92.         $carts $this->em->getRepository(Cart::class)->findByRepresentative($user);
  93.         return $this->render('front/app/cart_current.html.twig', [
  94.             'carts' => $carts,
  95.         ]);
  96.     }
  97.     /**
  98.      * @return Response
  99.      * @Route(path="/widget/order-current", name="order_current")
  100.      */
  101.     public function orderCurrent(): Response
  102.     {
  103.         /** @var User $user */
  104.         $user $this->getUser();
  105.         /** @var Order[] $orders */
  106.         $orders $this->em->getRepository(Order::class)->findCurrent($user);
  107.         return $this->render('front/app/order_current.html.twig', [
  108.             'orders' => $orders,
  109.         ]);
  110.     }
  111.     /**
  112.      * @return Response
  113.      * @Route(path="/widget/catalog", name="catalog")
  114.      */
  115.     public function catalog(): Response
  116.     {
  117.         /** @var Category $categoriesTop */
  118.         $categoriesTop $this->em->getRepository(Category::class)->getRootNodes('position');
  119.         return $this->render('front/app/catalog.html.twig', [
  120.             'categories' => $categoriesTop,
  121.         ]);
  122.     }
  123.     /**
  124.      * @param Group $group
  125.      * @return Response
  126.      * @throws NonUniqueResultException
  127.      * @Route(path="/widget/order-count/{group}", name="order_count")
  128.      */
  129.     public function orderCount(Group $group): Response
  130.     {
  131.         $from Tools::dateFrToDateTime($this->tools->getSettingByName('STATISTICS_FROM_DATE'));
  132.         $orderCounts = [];
  133.         $orderCounts['cart'] = [
  134.             'name' => "Paniers en cours",
  135.             'color' => "#000000",
  136.             'count' => (int)$this->em->getRepository(Cart::class)->countCurrent($from$group),
  137.         ];
  138.         $orderCounts['waiting'] = [
  139.             'name' => "Enregistrement",
  140.             'color' => "#ff8903",
  141.             'count' => 0,
  142.         ];
  143.         $steps $this->em->getRepository(Step::class)->findAll();
  144.         foreach ($steps as $step) {
  145.             if ($step->getConstName() == Step::CANCELED or $step->getConstName() == Step::STOP_AND_BILL) {
  146.                 continue;
  147.             }
  148.             $orderCounts[$step->getId()] = [
  149.                 'name' => $step->getName(),
  150.                 'color' => $step->getColor(),
  151.                 'count' => 0,
  152.             ];
  153.         }
  154.         /** @var Order[] $orders */
  155.         $orders $this->em->getRepository(Order::class)->findByParameters([
  156.             'from' => $from,
  157.             'group' => $group,
  158.         ]);
  159.         foreach ($orders as $order) {
  160.             if ($currentStep $order->getCurrentStep()) {
  161.                 if (array_key_exists($currentStep->getStep()->getId(), $orderCounts)) {
  162.                     $orderCounts[$currentStep->getStep()->getId()]['count']++;
  163.                 }
  164.             } else {
  165.                 $orderCounts['waiting']['count']++;
  166.             }
  167.         }
  168.         return $this->render('front/app/order_count.html.twig', [
  169.             'orderCounts' => $orderCounts,
  170.         ]);
  171.     }
  172.     /**
  173.      * @return JsonResponse|Response
  174.      * @Route(path="/widget/creation", name="creation")
  175.      */
  176.     public function creation(): JsonResponse|Response
  177.     {
  178.         if (!$this->moduleService->access('catalog_wallet''creation')) {
  179.             return new JsonResponse([
  180.                 'success' => false,
  181.                 'url' => $this->redirectToRoute('app_index'),
  182.             ]);
  183.         }
  184.         /** @var User $user */
  185.         $user $this->getUser();
  186.         /** @var Cart[] $carts */
  187.         $carts $this->em->getRepository(Cart::class)->findBy([
  188.             'user' => $user,
  189.         ]);
  190.         return $this->render('front/app/creation.html.twig', [
  191.             'carts' => $carts,
  192.         ]);
  193.     }
  194. }