<?php
namespace App\Controller\Front;
use App\Entity\Cart;
use App\Entity\Category;
use App\Entity\Group;
use App\Entity\Notification;
use App\Entity\Order;
use App\Entity\Step;
use App\Entity\User;
use App\Service\ModuleService;
use App\Service\Tools;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\NonUniqueResultException;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class AppController
* @package App\Controller\Admin
* @Route(path="", name="app_", options={"expose"=true})
*/
class AppController extends AbstractController
{
private EntityManagerInterface $em;
private Tools $tools;
private ModuleService $moduleService;
public function __construct(
EntityManagerInterface $em,
Tools $tools,
ModuleService $moduleService
) {
$this->em = $em;
$this->tools = $tools;
$this->moduleService = $moduleService;
}
/**
* @return Response
* @throws Exception
* @Route("/", name="index")
*/
public function index(): Response
{
$from = new DateTime($this->tools->getSettingByName('STATISTICS_FROM_DATE'));
/** @var Notification $notification */
$notification = $this->em->getRepository(Notification::class)->findOneBy([
'user' => $this->getUser(),
'isRead' => 0,
'type' => Notification::TYPE_ALERT,
]);
$isParentValidator = false;
/** @var User $user */
$user = $this->getUser();
foreach ($user->getChildren() as $child) {
if ($this->moduleService->access('catalog', 'validationParent', $child)) {
$isParentValidator = true;
break;
}
}
return $this->render('front/app/index.html.twig', [
'page_title' => "<span class='text-primary'>Tableau</span> de bord",
'notification' => $notification,
'from' => $from,
'isParentValidator' => $isParentValidator,
]);
}
/**
* @return Response
* @Route(path="/widget/cart-validation", name="cart_validation")
*/
public function cartValidation(): Response
{
/** @var User $user */
$user = $this->getUser();
/** @var Cart[] $carts */
$carts = $this->em->getRepository(Cart::class)->findWaitingValidationByParent($user);
return $this->render('front/app/cart_validation.html.twig', [
'carts' => $carts,
]);
}
/**
* @return Response
* @Route(path="/widget/cart-current", name="cart_current")
*/
public function cartCurrent(): Response
{
/** @var User $user */
$user = $this->getUser();
/** @var Cart[] $carts */
$carts = $this->em->getRepository(Cart::class)->findByRepresentative($user);
return $this->render('front/app/cart_current.html.twig', [
'carts' => $carts,
]);
}
/**
* @return Response
* @Route(path="/widget/order-current", name="order_current")
*/
public function orderCurrent(): Response
{
/** @var User $user */
$user = $this->getUser();
/** @var Order[] $orders */
$orders = $this->em->getRepository(Order::class)->findCurrent($user);
return $this->render('front/app/order_current.html.twig', [
'orders' => $orders,
]);
}
/**
* @return Response
* @Route(path="/widget/catalog", name="catalog")
*/
public function catalog(): Response
{
/** @var Category $categoriesTop */
$categoriesTop = $this->em->getRepository(Category::class)->getRootNodes('position');
return $this->render('front/app/catalog.html.twig', [
'categories' => $categoriesTop,
]);
}
/**
* @param Group $group
* @return Response
* @throws NonUniqueResultException
* @Route(path="/widget/order-count/{group}", name="order_count")
*/
public function orderCount(Group $group): Response
{
$from = Tools::dateFrToDateTime($this->tools->getSettingByName('STATISTICS_FROM_DATE'));
$orderCounts = [];
$orderCounts['cart'] = [
'name' => "Paniers en cours",
'color' => "#000000",
'count' => (int)$this->em->getRepository(Cart::class)->countCurrent($from, $group),
];
$orderCounts['waiting'] = [
'name' => "Enregistrement",
'color' => "#ff8903",
'count' => 0,
];
$steps = $this->em->getRepository(Step::class)->findAll();
foreach ($steps as $step) {
if ($step->getConstName() == Step::CANCELED or $step->getConstName() == Step::STOP_AND_BILL) {
continue;
}
$orderCounts[$step->getId()] = [
'name' => $step->getName(),
'color' => $step->getColor(),
'count' => 0,
];
}
/** @var Order[] $orders */
$orders = $this->em->getRepository(Order::class)->findByParameters([
'from' => $from,
'group' => $group,
]);
foreach ($orders as $order) {
if ($currentStep = $order->getCurrentStep()) {
if (array_key_exists($currentStep->getStep()->getId(), $orderCounts)) {
$orderCounts[$currentStep->getStep()->getId()]['count']++;
}
} else {
$orderCounts['waiting']['count']++;
}
}
return $this->render('front/app/order_count.html.twig', [
'orderCounts' => $orderCounts,
]);
}
/**
* @return JsonResponse|Response
* @Route(path="/widget/creation", name="creation")
*/
public function creation(): JsonResponse|Response
{
if (!$this->moduleService->access('catalog_wallet', 'creation')) {
return new JsonResponse([
'success' => false,
'url' => $this->redirectToRoute('app_index'),
]);
}
/** @var User $user */
$user = $this->getUser();
/** @var Cart[] $carts */
$carts = $this->em->getRepository(Cart::class)->findBy([
'user' => $user,
]);
return $this->render('front/app/creation.html.twig', [
'carts' => $carts,
]);
}
}