src/Controller/StockController.php line 118

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Stock;
  4. use App\Entity\User;
  5. use App\Form\StockType;
  6. use App\Repository\StockRepository;
  7. use App\Service\InventaireService;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. /**
  14.  * @Route("/stock")
  15.  */
  16. class StockController extends AbstractController
  17. {
  18.     // /**
  19.     //  * @Route("/", name="app_stock_index", methods={"GET"})
  20.     //  */
  21.     // public function index(StockRepository $stockRepository): Response
  22.     // {
  23.     //     $user = $this->getUser();
  24.     //     if ($user) {
  25.            
  26.     //         return $this->render('stock/index.html.twig', [
  27.     //             'stocks' => $stockRepository->findStockByUser($user),
  28.     //         ]);
  29.     //     }else{
  30.     //         return $this->redirectToRoute('app_produit_new', [], Response::HTTP_SEE_OTHER);
  31.     //     }
  32.  
  33.     // }
  34.     // /**
  35.     //  * @Route("/new", name="app_stock_new", methods={"GET", "POST"})
  36.     //  */
  37.     // public function new(Request $request, StockRepository $stockRepository): Response
  38.     // {
  39.     //     $stock = new Stock();
  40.     //     $form = $this->createForm(StockType::class, $stock);
  41.     //     $form->handleRequest($request);
  42.     //     if ($form->isSubmitted() && $form->isValid()) {
  43.     //         $stockRepository->add($stock);
  44.     //         return $this->redirectToRoute('app_stock_index', [], Response::HTTP_SEE_OTHER);
  45.     //     }
  46.     //     return $this->renderForm('stock/new.html.twig', [
  47.     //         'stock' => $stock,
  48.     //         'form' => $form,
  49.     //     ]);
  50.     // }
  51.     // /**
  52.     //  * @Route("/{id}", name="app_stock_show", methods={"GET"})
  53.     //  */
  54.     // public function show(Stock $stock): Response
  55.     // {
  56.     //     return $this->render('stock/show.html.twig', [
  57.     //         'stock' => $stock,
  58.     //     ]);
  59.     // }
  60.     // /**
  61.     //  * @Route("/{id}/edit", name="app_stock_edit", methods={"GET", "POST"})
  62.     //  */
  63.     // public function edit(Request $request, Stock $stock, StockRepository $stockRepository): Response
  64.     // {
  65.     //     $form = $this->createForm(StockType::class, $stock);
  66.     //     $form->handleRequest($request);
  67.     //     if ($form->isSubmitted() && $form->isValid()) {
  68.     //         $stockRepository->add($stock);
  69.     //         return $this->redirectToRoute('app_stock_index', [], Response::HTTP_SEE_OTHER);
  70.     //     }
  71.     //     return $this->renderForm('stock/edit.html.twig', [
  72.     //         'stock' => $stock,
  73.     //         'form' => $form,
  74.     //     ]);
  75.     // }
  76.     // /**
  77.     //  * @Route("/{id}", name="app_stock_delete", methods={"POST"})
  78.     //  */
  79.     // public function delete(Request $request, Stock $stock, StockRepository $stockRepository): Response
  80.     // {
  81.     //     if ($this->isCsrfTokenValid('delete'.$stock->getId(), $request->request->get('_token'))) {
  82.     //         $stockRepository->remove($stock);
  83.     //     }
  84.     //     return $this->redirectToRoute('app_stock_index', [], Response::HTTP_SEE_OTHER);
  85.     // }
  86.  
  87.     /**
  88.      * @Route("/", name="app_stock_index", methods={"GET"})
  89.      */
  90.     public function index(StockRepository $stockRepository): Response
  91.     {
  92.         $user = $this->getUser();
  93.         if ($user) {
  94.             return $this->render('stock/index.html.twig', [
  95.                 'stocks' => $stockRepository->findStockByUser($user),
  96.             ]);
  97.         } else {
  98.             return $this->redirectToRoute('app_produit_new', [], Response::HTTP_SEE_OTHER);
  99.         }
  100.     }
  101.     /**
  102.      * @Route("/new", name="app_stock_new", methods={"GET", "POST"})
  103.      */
  104.     public function new(Request $request, StockRepository $stockRepository): Response
  105.     {
  106.         $stock = new Stock();
  107.         $form = $this->createForm(StockType::class, $stock);
  108.         $form->handleRequest($request);
  109.         if ($form->isSubmitted() && $form->isValid()) {
  110.             $stockRepository->add($stock);
  111.             return $this->redirectToRoute('app_stock_index', [], Response::HTTP_SEE_OTHER);
  112.         }
  113.         return $this->renderForm('stock/new.html.twig', [
  114.             'stock' => $stock,
  115.             'form' => $form,
  116.         ]);
  117.     }
  118.     /**
  119.      * @Route("/{id}", name="app_stock_show", methods={"GET"})
  120.      */
  121.     public function show(Stock $stock): Response
  122.     {
  123.         return $this->render('stock/show.html.twig', [
  124.             'stock' => $stock,
  125.         ]);
  126.     }
  127.     /**
  128.      * @Route("/{id}/edit", name="app_stock_edit", methods={"GET", "POST"})
  129.      */
  130.     public function edit(Request $request, Stock $stock, StockRepository $stockRepository, InventaireService $inventaireService): Response
  131.     {
  132.         $quantiteAvant = $stock->getQuantite();
  133.         $form = $this->createForm(StockType::class, $stock);
  134.         $form->handleRequest($request);
  135.         if ($form->isSubmitted() && $form->isValid()) {
  136.             $stockRepository->add($stock);
  137.             $user = $this->getUser();
  138.             if ($user instanceof User) {
  139.                 $inventaireService->enregistrerModificationQuantite($user, $stock, $quantiteAvant, $stock->getQuantite());
  140.             }
  141.             return $this->redirectToRoute('app_stock_index', [], Response::HTTP_SEE_OTHER);
  142.         }
  143.         return $this->renderForm('stock/edit.html.twig', [
  144.             'stock' => $stock,
  145.             'form' => $form,
  146.         ]);
  147.     }
  148.     /**
  149.      * Vérifier si un stock peut être supprimé (AJAX)
  150.      * @Route("/check-delete/{id}", name="app_stock_check_delete", methods={"GET"}, options={"expose"=true})
  151.      */
  152.     public function checkDelete(int $id, StockRepository $stockRepository): JsonResponse
  153.     {
  154.         $result = $stockRepository->canBeDeleted($id);
  155.         
  156.         if ($result['stock'] && $result['stock']->getIdProduit()) {
  157.             // Vérification supplémentaire si nécessaire
  158.         }
  159.         
  160.         return $this->json([
  161.             'success' => true,
  162.             'canDelete' => $result['canDelete'],
  163.             'message' => $result['message'],
  164.             'stockName' => $result['produit'] ?? 'Stock #' . $id,
  165.             'reference' => $result['produit'] ?? 'N/A'
  166.         ]);
  167.     }
  168.     /**
  169.      * Supprimer un stock
  170.      * @Route("/{id}/delete", name="app_stock_delete", methods={"POST"}, options={"expose"=true})
  171.      */
  172.     public function delete(Request $request, Stock $stock, StockRepository $stockRepository): JsonResponse
  173.     {
  174.         // Token CSRF
  175.         $token = $request->request->get('_token');
  176.         if (!$this->isCsrfTokenValid('delete_stock', $token)) {
  177.             return $this->json([
  178.                 'success' => false, 
  179.                 'message' => 'Token CSRF invalide'
  180.             ], 403);
  181.         }
  182.         // Vérification des associations via le repository
  183.         $result = $stockRepository->canBeDeleted($stock->getId());
  184.         
  185.         if (!$result['canDelete']) {
  186.             return $this->json([
  187.                 'success' => false, 
  188.                 'message' => $result['message']
  189.             ], 400);
  190.         }
  191.         try {
  192.             $stockRepository->remove($stock, true);
  193.             
  194.             return $this->json([
  195.                 'success' => true, 
  196.                 'message' => 'Stock supprimé avec succès'
  197.             ]);
  198.         } catch (\Exception $e) {
  199.             return $this->json([
  200.                 'success' => false, 
  201.                 'message' => 'Erreur lors de la suppression : ' . $e->getMessage()
  202.             ], 500);
  203.         }
  204.     }
  205. }