src/EventSubscriber/GlobalVariablesSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\User;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Twig\Environment;
  9. use Symfony\Component\Security\Core\Security;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use App\Repository\BonDeLivraisonRepository;
  12. use App\Repository\ProduitIMEIRepository;
  13. use App\Repository\DevisRepository;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpFoundation\Response;
  16. class GlobalVariablesSubscriber implements EventSubscriberInterface
  17. {
  18. private Environment $twig;
  19. private RequestStack $requestStack;
  20. private BonDeLivraisonRepository $bonLivRepo;
  21. private ProduitIMEIRepository $stokRepo;
  22. private DevisRepository $devisRepos;
  23. private Security $security;
  24. private $em;
  25. public function __construct(Environment $twig, BonDeLivraisonRepository $bonLivRepo, RequestStack $requestStack, ProduitIMEIRepository $stokRepo, DevisRepository $devisRepos, Security $security, EntityManagerInterface $em)
  26. {
  27. $this->twig = $twig;
  28. $this->bonLivRepo = $bonLivRepo;
  29. $this->requestStack = $requestStack;
  30. $this->stokRepo = $stokRepo;
  31. $this->devisRepos = $devisRepos;
  32. $this->security = $security; // Injection du service Security
  33. $this->em = $em;
  34. }
  35. public function onKernelController(ControllerEvent $event)
  36. {
  37. // Récupérer l'objet Request à partir de RequestStack
  38. $request = $this->requestStack->getCurrentRequest();
  39. if (!$request) {
  40. return; // Sécurité : éviter d'accéder à une requête inexistante
  41. }
  42. $user = $this->security->getUser();
  43. if ($user instanceof User) {
  44. $roles = $user->getRoles();
  45. if (empty(array_diff(["ROLE_USER", "ROLE_CAISSIERE"], $roles)) && count($roles) === 2) {
  46. $userSiege = $user->getSiege()->getId();
  47. $stockProduit = $this->stokRepo->findBy(["siege" => $userSiege, "dateSortie" => null]);
  48. $stock = 0;
  49. $bonLivraison = 0;
  50. if ($stockProduit) {
  51. $stock = $stock + count($stockProduit);
  52. }
  53. // bon de livraison
  54. $qb = $this->bonLivRepo->createQueryBuilder('b')
  55. ->andWhere('b.receiver is not null')
  56. ->andWhere('b.validRefus IS NULL')
  57. ->andWhere('b.siegeReceiver = :siegeId OR b.siegeSender = :siegeId')
  58. ->andWhere('b.isValidByDA = :isValidByDA')
  59. ->setParameter('siegeId', $userSiege)
  60. ->andWhere('b.isValid = false or b.isValid is null')
  61. ->setParameter('isValidByDA', 1);
  62. $bonLivr = $qb->getQuery()->getResult();
  63. if ($bonLivr) {
  64. $bonLivraison = $bonLivraison + count($bonLivr);
  65. }
  66. // Ajout des variables globales à Twig
  67. $this->twig->addGlobal('stocks', $stock);
  68. $this->twig->addGlobal('bonLivraisons', $bonLivraison);
  69. }
  70. if (empty(array_diff(["ROLE_USER", "ROLE_CSAF_AGENCE"], $roles)) && count($roles) === 2) {
  71. $userSiege = $user->getSiege() == null ? null : $user->getSiege()->getId();
  72. $userAgence = $user->getAgence()->getId();
  73. $allDevis = $this->devisRepos->findBy(["agence" => $userAgence, "valide" => 0]);
  74. $commandes = 0;
  75. $bonLivraison = 0;
  76. if ($allDevis) {
  77. $commandes = $commandes + count($allDevis);
  78. }
  79. // bon de livraison
  80. $qb = $this->bonLivRepo->createQueryBuilder('b')
  81. ->andWhere('b.receiver is not null')
  82. ->andWhere('b.bonType =:interne')
  83. ->setParameter('interne', 'interne')
  84. ->andWhere('b.validRefus IS NULL')
  85. ->andWhere('b.siegeReceiver = :siegeReceiver')
  86. ->andWhere('b.isValidByDA = false or b.isValidByDA is null')
  87. ->setParameter('siegeReceiver', $userSiege);
  88. $bonLivr = $qb->getQuery()->getResult();
  89. if ($bonLivr) {
  90. $bonLivraison = $bonLivraison + count($bonLivr);
  91. }
  92. // Ajout des variables globales à Twig
  93. $this->twig->addGlobal('commandes', $commandes);
  94. $this->twig->addGlobal('bonLivraisons', $bonLivraison);
  95. }
  96. if (empty(array_diff(["ROLE_USER", "ROLE_DIRECTEUR_OPERATION"], $roles)) && count($roles) === 2) {
  97. $allDevis = $this->devisRepos->findBy(["valide" => 2]);
  98. $commandes = 0;
  99. if ($allDevis) {
  100. $commandes = $commandes + count($allDevis);
  101. }
  102. // Ajout des variables globales à Twig
  103. $this->twig->addGlobal('commandes', $commandes);
  104. }
  105. if (empty(array_diff(["ROLE_USER", "ROLE_DIRECTEUR_AGENCE"], $roles)) && count($roles) === 2) {
  106. $userAgence = $user->getAgence()->getId();
  107. $allDevis = $this->devisRepos->findBy(["agence" => $userAgence, "valide" => [1]]);
  108. $commandes = 0;
  109. $bonLivraison = 0;
  110. if ($allDevis) {
  111. $commandes = $commandes + count($allDevis);
  112. }
  113. // bon de livraison
  114. $qb = $this->bonLivRepo->createQueryBuilder('b')
  115. ->join('b.siegeReceiver', 's')
  116. ->where('s.agence = :agence')
  117. ->andWhere('b.isValidByDA IS NULL OR b.isValidByDA = 0')
  118. ->andWhere('b.isValid IS NULL OR b.isValid = 0')
  119. ->setParameter('agence', $userAgence);
  120. $bonLiv = $qb->getQuery()->getResult();
  121. if ($bonLiv) {
  122. $bonLivraison = $bonLivraison + count($bonLiv);
  123. }
  124. // Ajout des variables globales à Twig
  125. $this->twig->addGlobal('commandes', $commandes);
  126. $this->twig->addGlobal('bonLivraisons', $bonLivraison);
  127. }
  128. if (empty(array_diff(["ROLE_USER", "ROLE_DAF"], $roles)) && count($roles) === 2) {
  129. $allDevis = $this->devisRepos->findBy(["valide" => 4]);
  130. $commandes = 0;
  131. $bonLivraison = 0;
  132. if ($allDevis) {
  133. $commandes = $commandes + count($allDevis);
  134. }
  135. // bon de livraison
  136. $qb = $this->bonLivRepo->createQueryBuilder('b')
  137. ->where('b.siegeReceiver = :siegeReceiver')
  138. ->andWhere('b.isValidByDA IS NULL OR b.isValidByDA = 0')
  139. ->andWhere('b.isValid IS NULL OR b.isValid = 0')
  140. ->setParameter('siegeReceiver', 36);
  141. $bonLivr = $qb->getQuery()->getResult();
  142. if ($bonLivr) {
  143. $bonLivraison = $bonLivraison + count($bonLivr);
  144. }
  145. // Ajout des variables globales à Twig
  146. $this->twig->addGlobal('commandes', $commandes);
  147. $this->twig->addGlobal('bonLivraisons', $bonLivraison);
  148. }
  149. if (empty(array_diff(["ROLE_USER", "ROLE_DIR_SP"], $roles)) && count($roles) === 2) {
  150. $allDevis = $this->devisRepos->findBy(["valide" => [5]]);
  151. $commandes = 0;
  152. if ($allDevis) {
  153. $commandes = $commandes + count($allDevis);
  154. }
  155. // Ajout des variables globales à Twig
  156. $this->twig->addGlobal('commandes', $commandes);
  157. }
  158. if (empty(array_diff(["ROLE_USER", "ROLE_LOGISTIQUE"], $roles)) && count($roles) === 2) {
  159. $userSiege = $user->getSiege()->getId();
  160. $stockProduit = $this->stokRepo->findBy(["siege" => 36, "dateSortie" => null]);
  161. $stock = 0;
  162. if ($stockProduit) {
  163. $stock = $stock + count($stockProduit);
  164. }
  165. $this->twig->addGlobal('stocks', $stock);
  166. }
  167. }
  168. }
  169. public static function getSubscribedEvents(): array
  170. {
  171. return [
  172. KernelEvents::CONTROLLER => 'onKernelController',
  173. ];
  174. }
  175. }