<?php
namespace App\Controller\Admin;
use App\Repository\ArticleOrderOutOfStockRepository;
use App\Repository\RequestModificationRepository;
use App\Repository\RequestModificationStatusRepository;
use App\Repository\UsersRepository;
use App\Repository\OrdersRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use DateTime;
/**
* @Route("/admin")
*/
class AdminHomeController extends AbstractController
{
private $usersRepository;
private $requestModificationRepository;
public function __construct(
UsersRepository $usersRepository,
RequestModificationRepository $requestModificationRepository,
RequestModificationStatusRepository $requestModificationStatusRepository,
ArticleOrderOutOfStockRepository $articleOrderOutOfStockRepository,
OrdersRepository $ordersRepository
) {
$this->usersRepository = $usersRepository;
$this->requestModificationRepository = $requestModificationRepository;
$this->requestModificationStatusRepository = $requestModificationStatusRepository;
$this->articleOrderOutOfStockRepository = $articleOrderOutOfStockRepository;
$this->ordersRepository = $ordersRepository;
}
/**
* @Route("/", name="admin_index")
*/
public function index(): Response
{
$notPresentUsers = $this->usersRepository->findBy(['notPresent' => true, 'deleted' => false]);
$unreadTicketsNumbers = [];
foreach ($notPresentUsers as $user) {
$unreadTicketsNumbers[$user->getId()] = count($this->requestModificationRepository->findBy([
'responsable' => $user,
'isRead' => false,
'deleted' => false
]));
}
$requestStatus = $this->requestModificationStatusRepository->find(3);
$articlesOutOfStock = $this->articleOrderOutOfStockRepository->findBy(['deleted'=>false]);
$articleOutOfStock = [];
foreach ($articlesOutOfStock as $article){
$orderId = $article->getOrder()->getId();
$filteredArticlesOutOfStock = array_filter($articlesOutOfStock, function ($articleOrder) use ($article) {
return $articleOrder->getOrder() == $article->getOrder();
});
$numberMissing = count($filteredArticlesOutOfStock);
if (!isset($articleOutOfStock[$orderId])) {
$newArticleOutOfStock['id'] = $article->getId();
$newArticleOutOfStock['user'] = $article->getUser()->getId();
$newArticleOutOfStock['order'] = $article->getOrder()->getId();
$newArticleOutOfStock['establishmentId'] = $article->getEstablishment()->getId();
$newArticleOutOfStock['establishment'] = $article->getEstablishment()->getName();
$newArticleOutOfStock['name'] = $article->getArticle()->getName();
$newArticleOutOfStock['articleId'] = $article->getArticle()->getId();
$newArticleOutOfStock['quantitySent'] = $article->getQuantitySent();
$newArticleOutOfStock['quantityOrdered'] = $article->getQuantityOrdered();
$newArticleOutOfStock['date'] = $article->getOrder()->getCreatedAt()->format('d-m-Y');
$newArticleOutOfStock['numberArticles'] = $numberMissing;
$articleOutOfStock[$orderId] = $newArticleOutOfStock;
}
}
return $this->render('admin/base.html.twig', [
'unreadTickets' => count($this->requestModificationRepository->findBy(['responsable' => $this->getUser(), 'isRead' => false, 'deleted' => false])),
'closedTickets' => count($this->requestModificationRepository->findBy(['responsable' => $this->getUser(), 'isRead' => true, 'deleted' => false])),
'notPresentUsers' => $notPresentUsers,
'unreadTicketsNumbers' => $unreadTicketsNumbers,
'customerUnLinkRequests' => $this->requestModificationRepository->findBy(['deleted'=>false, 'responsable'=>null, 'RequestModificationStatus'=>$requestStatus]),
'articlesOutOfStock' => $articleOutOfStock
]);
}
/**
* @Route("/mark-as-missing", name="admin_mark_as_missing", methods={"POST"})
*/
public function markAsMissing(Request $request): Response
{
$user = $this->usersRepository->find($request->get('id'));
$user->setNotPresent(!$user->isNotPresent());
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return new JsonResponse(['missing' => $user->isNotPresent()], Response::HTTP_OK);
}
/**
* @Route("/get-outofstock-articles", name="admin_get_outofstock_articles", methods={"POST"})
*/
public function getOutOfStockArticles(Request $request): JsonResponse
{
$date = new DateTime($request->get('date'));
$articleOrders = $this->articleOrderOutOfStockRepository->findArticlesOnDate($date);
if (empty($articleOrders))
return new JsonResponse(['error' => 'no article found'], Response::HTTP_NOT_FOUND);
$articlesOutOfStock = array();
foreach ($articleOrders as $articleOrder)
{
$articlesOutOfStock[] = [
'id' => $articleOrder->getArticle()->getId(),
'punchId' => $articleOrder->getArticle()->getIdPunch(),
'name' => $articleOrder->getArticle()->getName(),
'quantitySent' => $articleOrder->getQuantitySent(),
'quantityOrdered' => $articleOrder->getQuantityOrdered(),
'date' => $articleOrder->getOrder()->getCreatedAt()->format('d/m/y'),
'customerId'=>$articleOrder->getEstablishment()->getIdPunch(),
'customerName'=>$articleOrder->getEstablishment()->getName(),
];
}
return new JsonResponse(['articles' => $articlesOutOfStock], Response::HTTP_OK);
}
}