src/Controller/MouvementController.php line 247

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\LocalAgence;
  4. use App\Entity\Mouvement;
  5. use App\Entity\Stock;
  6. use App\Form\Type\MouvementFormType;
  7. use App\Manager\RoleManager;
  8. use App\Repository\MouvementRepository;
  9. use App\Repository\StockRepository;
  10. use Doctrine\ORM\EntityManager;
  11. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. class MouvementController extends AbstractController
  20. {
  21.     public function index(MouvementRepository $mouvementRepository): Response
  22.     {
  23.         return $this->render('mouvement/index.html.twig', [
  24.             'mouvements' => $mouvementRepository->findAll(),
  25.         ]);
  26.     }
  27.     private function validateStock(Stock $stockMouvement $mouvement)
  28.     {
  29.         $em $this->container->get('doctrine')->getManager();
  30.         $stock->setAgence($mouvement->getAgenceArrivee());
  31.         $stock->setLocal(null);
  32.         $stock->setNanoReseau(null);
  33.         $stock->setClient(null);
  34.         $stock->setUtilisateur(null);
  35.         if (!is_null($mouvement->getLocalArrivee())) {
  36.             $stock->setLocal($mouvement->getLocalArrivee());
  37.         }
  38.         if (!is_null($mouvement->getNanoReseauArrivee())) {
  39.             $stock->setNanoReseau($mouvement->getNanoReseauArrivee());
  40.         }
  41.         if (!is_null($mouvement->getClientArrivee())) {
  42.             $stock->setClient($mouvement->getClientArrivee());
  43.         }
  44.         if (!is_null($mouvement->getUserArrivee())) {
  45.             $stock->setUtilisateur($mouvement->getUserArrivee());
  46.         }
  47.         $em->persist($stock);
  48.         return;
  49.     }
  50.     public function new(Request $requestMouvementRepository $mouvementRepositoryTranslatorInterface $translatorStockRepository $stockRepositoryUserPasswordHasherInterface $passwordHasher): Response
  51.     {
  52.         $mouvement = new Mouvement();
  53.         $form $this->createForm(MouvementFormType::class, $mouvement);
  54.         $form->handleRequest($request);
  55.         if ($form->isSubmitted() && $form->isValid()) {
  56.             $btn $request->request->get('btn_submit');
  57.             if ($btn == '1') {
  58.                 if (!$passwordHasher->isPasswordValid($mouvement->getGestionnaire(), $mouvement->getCodeSecret())) {
  59.                     // Password does not match, handle the error (e.g., add a flash message, return an error response)
  60.                     $this->addFlash('error''Code secret incorrect.');
  61.                     return $this->renderForm('mouvement/form.html.twig', [
  62.                         'mouvement' => $mouvement,
  63.                         'form' => $form,
  64.                         'pageTitle' => $translator->trans("Création d'un mouvement"),
  65.                     ]);
  66.                 }
  67.                 $mouvement->setValide(true);
  68.             }
  69.             // ⚠️ Validation métier minimale côté serveur
  70.             foreach ($mouvement->getMaterielMouvements() as $mm) {
  71.                 $stock $mm->getStock();
  72.                 if (!$stock) {
  73.                     $this->addFlash('error''Stock manquant pour un matériel.');
  74.                     return $this->renderForm('mouvement/form.html.twig', [
  75.                         'mouvement' => $mouvement,
  76.                         'form' => $form,
  77.                         'pageTitle' => $translator->trans("Création d'un mouvement"),
  78.                     ]);
  79.                 }
  80.                 $qteDispo $stockRepository->getDisponibleForStockId($stock->getId()); // implémentez une requête rapide
  81.                 // if ($mm->getQte() > $qteDispo) {
  82.                 //     $this->addFlash('error', sprintf('Quantité demandée (%d) > disponible (%d) pour %s', $mm->getQte(), $qteDispo, $stock->getId()));
  83.                 //     return $this->renderForm('mouvement/form.html.twig', [
  84.                 //         'mouvement' => $mouvement,
  85.                 //         'form' => $form,
  86.                 //         'pageTitle' => $translator->trans("Création d'un mouvement"),
  87.                 //     ]);
  88.                 // }
  89.                 if ($request->request->get('btn_submit') == 1) {
  90.                     $modele $mm->getModele();
  91.                     $identifiants $mm->getIdentifiants();
  92.                     if (!$modele || !$modele->getProduit()->getCategorie()->isIndividuel()) {
  93.                         $stocks $stockRepository->findBy([
  94.                             'modele' => $modele
  95.                         ]);
  96.                         for ($i 0$i $mm->getQte(); $i++) {
  97.                             if (isset($stocks[$i])) {
  98.                                 $stock $stocks[$i];
  99.                             }
  100.                         }
  101.                         $this->validateStock($stock$mouvement);
  102.                     } else {
  103.                         foreach ($identifiants as $key => $identifiant) {
  104.                             $stock $stockRepository->findOneBy([
  105.                                 'identifiant' => $identifiant
  106.                             ]);
  107.                             if ($stock) {
  108.                                 $this->validateStock($stock$mouvement);
  109.                             }
  110.                         }
  111.                     }
  112.                 }
  113.             }
  114.             $mouvementRepository->add($mouvement);
  115.             return $this->redirectToRoute('app.mouvement.list');
  116.         }
  117.         return $this->renderForm('mouvement/form.html.twig', [
  118.             'mouvement' => $mouvement,
  119.             'form' => $form,
  120.             'pageTitle' => $translator->trans("Création d'un mouvement"),
  121.         ]);
  122.     }
  123.     public function new_old(Request $requestMouvementRepository $mouvementRepositoryTranslatorInterface $translatorInterfaceStockRepository $stockRepository): Response
  124.     {
  125.         $mouvement = new Mouvement();
  126.         $stock null;
  127.         $stockId $request->query->get('stock_id');
  128.         if ($stockId) {
  129.             $stock $stockRepository->find($stockId);
  130.         }
  131.         $form $this->createForm(MouvementFormType::class, $mouvement);
  132.         $form->handleRequest($request);
  133.         if ($form->isSubmitted() && $form->isValid()) {
  134.             $em $this->container->get('doctrine')->getManager();
  135.             $materiels $form->get('materielMouvements')->getData();
  136.             foreach ($materiels as $mat) {
  137.                 $mat->setModele($mat->getStock()->getModele());
  138.                 $em->persist($mat);
  139.                 if ($request->request->get('btn_submit') == 1) {
  140.                     $modele $mat->getModele();
  141.                     $identifiant $mat->getIdentifiant();
  142.                     if (is_null($identifiant)) {
  143.                         $stocks $stockRepository->findBy([
  144.                             'modele' => $modele
  145.                         ]);
  146.                         for ($i 0$i $mat->getQte(); $i++) {
  147.                             $stock $stocks[$i];
  148.                         }
  149.                         $this->validateStock($stock$mouvement);
  150.                     } else {
  151.                         $stock $stockRepository->findOneBy([
  152.                             'identifiant' => $identifiant
  153.                         ]);
  154.                         if ($stock) {
  155.                             $this->validateStock($stock$mouvement);
  156.                         }
  157.                     }
  158.                 }
  159.             }
  160.             if ($request->request->get('btn_submit') == 1) {
  161.                 $mouvement->setValide(true);
  162.             }
  163.             // dd($materiels);
  164.             $mouvementRepository->add($mouvement);
  165.             return $this->redirectToRoute('app.mouvement.list', [], Response::HTTP_SEE_OTHER);
  166.         } else {
  167.             // dump((string) $form->getErrors(true, false));die;
  168.         }
  169.         return $this->renderForm('mouvement/form.html.twig', [
  170.             'mouvement' => $mouvement,
  171.             'form' => $form,
  172.             'pageTitle' => $translatorInterface->trans("Création d'un mouvement"),
  173.         ]);
  174.     }
  175.     public function show(Mouvement $mouvement): Response
  176.     {
  177.         return $this->render('mouvement/show.html.twig', [
  178.             'mouvement' => $mouvement,
  179.         ]);
  180.     }
  181.     public function edit(Request $requestMouvement $mouvementMouvementRepository $mouvementRepositoryTranslatorInterface $translatorInterfaceStockRepository $stockRepositoryUserPasswordHasherInterface $passwordHasher): Response
  182.     {
  183.         $form $this->createForm(MouvementFormType::class, $mouvement);
  184.         $form->handleRequest($request);
  185.         if ($form->isSubmitted() && $form->isValid() && !$mouvement->getValide()) {
  186.             $btn $request->request->get('btn_submit');
  187.             if ($btn == '1') {
  188.                 if (!$passwordHasher->isPasswordValid($mouvement->getGestionnaire(), $mouvement->getCodeSecret())) {
  189.                     // Password does not match, handle the error (e.g., add a flash message, return an error response)
  190.                     $this->addFlash('error''Code secret incorrect.');
  191.                     return $this->renderForm('mouvement/form.html.twig', [
  192.                         'mouvement' => $mouvement,
  193.                         'form' => $form,
  194.                         'pageTitle' => $translatorInterface->trans("Création d'un mouvement"),
  195.                     ]);
  196.                 }
  197.                 $mouvement->setValide(true);
  198.             }
  199.             // ⚠️ Validation métier minimale côté serveur
  200.             foreach ($mouvement->getMaterielMouvements() as $mm) {
  201.                 $stock $mm->getStock();
  202.                 if (!$stock) {
  203.                     $this->addFlash('error''Stock manquant pour un matériel.');
  204.                     return $this->renderForm('mouvement/form.html.twig', [
  205.                         'mouvement' => $mouvement,
  206.                         'form' => $form,
  207.                         'pageTitle' => $translatorInterface->trans("Création d'un mouvement"),
  208.                     ]);
  209.                 }
  210.                 $qteDispo $stockRepository->getDisponibleForStockId($stock->getId()); // implémentez une requête rapide
  211.                 // if ($mm->getQte() > $qteDispo) {
  212.                 //     $this->addFlash('error', sprintf('Quantité demandée (%d) > disponible (%d) pour %s', $mm->getQte(), $qteDispo, $stock->getId()));
  213.                 //     return $this->renderForm('mouvement/form.html.twig', [
  214.                 //         'mouvement' => $mouvement,
  215.                 //         'form' => $form,
  216.                 //         'pageTitle' => $translatorInterface->trans("Création d'un mouvement"),
  217.                 //     ]);
  218.                 // }
  219.                 if ($request->request->get('btn_submit') == 1) {
  220.                     $modele $mm->getModele();
  221.                     $identifiants $mm->getIdentifiants();
  222.                     if (!$modele->getProduit()->getCategorie()->isIndividuel()) {
  223.                         $stocks $stockRepository->findBy([
  224.                             'modele' => $modele
  225.                         ]);
  226.                         for ($i 0$i $mm->getQte(); $i++) {
  227.                             $stock $stocks[$i];
  228.                         }
  229.                         $this->validateStock($stock$mouvement);
  230.                     } else {
  231.                         foreach ($identifiants as $key => $identifiant) {
  232.                             $stock $stockRepository->findOneBy([
  233.                                 'identifiant' => $identifiant
  234.                             ]);
  235.                             if ($stock) {
  236.                                 $this->validateStock($stock$mouvement);
  237.                             }
  238.                         }
  239.                     }
  240.                 }
  241.             }
  242.             $mouvementRepository->add($mouvement);
  243.             return $this->redirectToRoute('app.mouvement.list', [], Response::HTTP_SEE_OTHER);
  244.         }
  245.         return $this->renderForm('mouvement/form.html.twig', [
  246.             'mouvement' => $mouvement,
  247.             'form' => $form,
  248.             'pageTitle' => $translatorInterface->trans("Création d'un mouvement"),
  249.         ]);
  250.     }
  251.     public function delete(Request $requestMouvement $itemMouvementRepository $mouvementRepositoryTranslatorInterface $trans): Response
  252.     {
  253.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  254.         $this->denyAccessUnlessGranted(RoleManager::ROLE_SUPER_ADMIN$this->getUser());
  255.         try {
  256.             $em $this->container->get('doctrine')->getManager();
  257.             $materiels $item->getMaterielMouvements();
  258.             foreach ($materiels as $mat) {
  259.                 $item->removeMaterielMouvement($mat);
  260.                 $mat->setMouvement(null);
  261.                 $em->persist($mat);
  262.             }
  263.             $mouvementRepository->remove($item);
  264.             $em->flush();
  265.             $message $trans->trans('Successfully deleted');
  266.             $titre $trans->trans('Deleted');
  267.             $classe 'success';
  268.         } catch (\Exception $e) {
  269.             $message $e->getMessage();
  270.             $titre 'Erreur';
  271.             $classe 'warning';
  272.         }
  273.         return new JsonResponse([
  274.             'message' => $message,
  275.             'titre' => $titre,
  276.             'classe' => $classe
  277.         ]);
  278.     }
  279. }