src/Controller/ProductsController.php line 23

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ActionShock;
  4. use App\Entity\AdditionalCompanyInformation;
  5. use App\Entity\CustomerControl;
  6. use App\Entity\LotNumber;
  7. use App\Entity\Products;
  8. use App\Entity\SpecialDiscount;
  9. use App\Repository\LotNumberRepository;
  10. use App\Service\ActionShockService;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. class ProductsController extends AbstractController
  17. {
  18.     #[Route('/products'name'products')]
  19.     public function index(EntityManagerInterface $managerActionShockService $actionShockService): Response
  20.     {
  21.         $actionShockService->updateActionShocksStatus();
  22.         $additionalInformations null;
  23.         $checkControl $manager->getRepository(CustomerControl::class)->findOneBy(array('user' => $this->getUser(), 'finish' => false));
  24.         if (!$checkControl) {
  25.             $user $this->getUser();
  26.         } else {
  27.             $user $checkControl->getCustomer();
  28.         }
  29.         if ($user != null) {
  30.             $additionalInformations $manager->getRepository(AdditionalCompanyInformation::class)->findOneBy(array('user' => $user));
  31.             if ($additionalInformations->getCompanyCountry() == null) {
  32.                 $this->addFlash('danger''Veuillez remplir vos coordonnées!');
  33.                 return $this->redirectToRoute('profile');
  34.             }
  35.         }
  36.         $information $manager->getRepository(AdditionalCompanyInformation::class)->findOneBy(array('user' => $user));
  37.         $products $manager->getRepository(Products::class)->getAllProduct($information);
  38.         $products $this->calculateRebatePricePharmacy($products);
  39.         $specialDiscounts $manager->getRepository(SpecialDiscount::class)->findAll();
  40.         if ($specialDiscounts != null) {
  41.             $specialDiscount $specialDiscounts[0];
  42.         } else {
  43.             $specialDiscount null;
  44.         }
  45.         if (!$checkControl) {
  46.             $checkControl null;
  47.         }
  48.         return $this->render('products/index.html.twig', [
  49.             'products' => $products,
  50.             'shockActionTypes' => ActionShock::TYPES,
  51.             'user' => $user,
  52.             'information' => $additionalInformations,
  53.             'checkControl' => $checkControl,
  54.             'specialDiscount' => $specialDiscount
  55.         ]);
  56.     }
  57.     private function calculateRebatePricePharmacy($products)
  58.     {
  59.         foreach ($products as &$product) {
  60.             $totalRebate = ($product['pricePharmacy'] / 100) * $this->getUser()->getAdditionalPercentage();
  61.             $item['totalRebate'] = floatval(number_format($totalRebate2'.'''));
  62.             $item['PriceRebateInclude'] = floatval(number_format($product['pricePharmacy'] - $item['totalRebate'], 2'.'''));
  63.             $product['pricePharmacyWithRebate'] = $item['PriceRebateInclude'];
  64.         }
  65.         //        dd($products);
  66.         return $products;
  67.     }
  68.     private function slugify(string $text): string
  69.     {
  70.         // Remplace les caractères non-ASCII
  71.         $text iconv('UTF-8''ASCII//TRANSLIT'$text);
  72.         // Remplace les caractères non alphanumériques par des tirets
  73.         $text preg_replace('/[^a-z0-9]+/i''-'$text);
  74.         // Supprime les tirets en début et fin
  75.         $text trim($text'-');
  76.         // Convertit en minuscules
  77.         $text strtolower($text);
  78.         
  79.         return $text;
  80.     }
  81.     #[Route('/product/{id}-{slug}'name'product_show'requirements: ['id' => '\d+'])]
  82.     public function show(int $idstring $slugEntityManagerInterface $manager): Response
  83.     {
  84.         $checkControl $manager->getRepository(CustomerControl::class)->findOneBy(array('user' => $this->getUser(), 'finish' => false));
  85.         if (!$checkControl) {
  86.             $user $this->getUser();
  87.         } else {
  88.             $user $checkControl->getCustomer();
  89.         }
  90.         $information $manager->getRepository(AdditionalCompanyInformation::class)->findOneBy(array('user' => $user));
  91.         
  92.         // Récupère TOUS les produits avec la même logique que le DataTable
  93.         $allProducts $manager->getRepository(Products::class)->getAllProduct($information);
  94.         
  95.         // Applique le même filtrage de stock que allProductJson
  96.         $excludedDepots LotNumber::EXCLUDED_DEPOTS;
  97.         foreach ($allProducts as $index => $prod) {
  98.             $quantity 0;
  99.             if ($allProducts[$index]['lotNumber']) {
  100.                 // keep stock only for valid warehouses
  101.                 $allProducts[$index]['lotNumber'] = array_values(array_filter($allProducts[$index]['lotNumber'], function ($lot) use ($excludedDepots) {
  102.                     return $lot['depot'] !== LotNumber::PHYSICAL_DEPOT && $lot['stock'] > 0;
  103.                 }));
  104.                 if (count($allProducts[$index]['lotNumber']) === && $allProducts[$index]['lotNumber'][0]['depot'] === LotNumber::ONLINE_DEPOT) {
  105.                     $quantity $allProducts[$index]['lotNumber'][0]['stock'];
  106.                 } else {
  107.                     array_walk($allProducts[$index]['lotNumber'], function ($lot) use (&$quantity$excludedDepots) {
  108.                         if (!in_array($lot['depot'], $excludedDepots)) {
  109.                             $quantity += $lot['stock'];
  110.                         }
  111.                     });
  112.                 }
  113.             }
  114.             $allProducts[$index]['quantity'] = $quantity;
  115.         }
  116.         
  117.         // Trouve le produit spécifique dans le tableau
  118.         $productData null;
  119.         foreach ($allProducts as $prod) {
  120.             if ($prod['id'] == $id) {
  121.                 $productData $prod;
  122.                 break;
  123.             }
  124.         }
  125.         
  126.         if (!$productData) {
  127.             throw $this->createNotFoundException('Produit non trouvé');
  128.         }
  129.         // Récupère l'entité produit pour les relations
  130.         $product $manager->getRepository(Products::class)->find($id);
  131.         
  132.         // Filtre les lots valides avec DLU (pour l'affichage)
  133.         $validLots = [];
  134.         if (isset($productData['lotNumber']) && is_array($productData['lotNumber'])) {
  135.             foreach ($productData['lotNumber'] as $lotData) {
  136.                 if (isset($lotData['dlu']) && $lotData['dlu'] !== null && $lotData['stock'] > 0) {
  137.                     $validLots[] = $lotData;
  138.                 }
  139.             }
  140.             
  141.             // Trie par DLU (du plus lointain au plus proche)
  142.             usort($validLots, function($a$b) {
  143.                 if (!isset($a['dlu']) || !isset($b['dlu'])) return 0;
  144.                 if (!($a['dlu'] instanceof \DateTime) || !($b['dlu'] instanceof \DateTime)) return 0;
  145.                 return $b['dlu']->getTimestamp() - $a['dlu']->getTimestamp();
  146.             });
  147.         }
  148.         // Calcul du prix pharmacie avec remise si applicable
  149.         $pricePharmacyWithRebate null;
  150.         if ($information && $information->getCompanyTypePro() === 'Pharmacie' && $product->getPricePharmacy()) {
  151.             $percentPharmacy $user->getAdditionalPercentage();
  152.             $pricePharmacyWithRebate $product->getPricePharmacy() - ($product->getPricePharmacy() * $percentPharmacy 100);
  153.         }
  154.         return $this->render('products/show.html.twig', [
  155.             'product' => $product,
  156.             'productData' => $productData,
  157.             'information' => $information,
  158.             'user' => $user,
  159.             'validLots' => $validLots,
  160.             'totalQuantity' => $productData['quantity'],
  161.             'pricePharmacyWithRebate' => $pricePharmacyWithRebate,
  162.             'checkControl' => $checkControl
  163.         ]);
  164.     }
  165.     #[Route('/productsJson/{brand}'name'productsJson')]
  166.     public function allProductJson(EntityManagerInterface $manager$brand null): JsonResponse
  167.     {
  168.         $checkControl $manager->getRepository(CustomerControl::class)->findOneBy(array('user' => $this->getUser(), 'finish' => false));
  169.         if (!$checkControl) {
  170.             $user $this->getUser();
  171.         } else {
  172.             $user $checkControl->getCustomer();
  173.         }
  174.         $information $manager->getRepository(AdditionalCompanyInformation::class)->findOneBy(array('user' => $user));
  175.         if ($brand == null) {
  176.             $products $manager->getRepository(Products::class)->getAllProduct($information);
  177.         } elseif ($brand == 'dlu-gooddeal') {
  178.             $products $manager->getRepository(Products::class)->getAllProductWithGoodDeals($information);
  179.         } elseif ($brand == 'action-shock') {
  180.             $products $manager->getRepository(Products::class)->getAllProductshockDeals($information);
  181.         } elseif ($brand == 'special-discount') {
  182.             $products $manager->getRepository(Products::class)->getAllProductSpecialDiscount($information);
  183.         } elseif ($brand == 'action-special') {
  184.             // Combine les produits "action-shock" et "special-discount"
  185.             $productsActionShock $manager->getRepository(Products::class)->getAllProductshockDeals($information);
  186.             $productsSpecialDiscount $manager->getRepository(Products::class)->getAllProductSpecialDiscount($information);
  187.             
  188.             // Fusionne les deux tableaux
  189.             $products array_merge($productsActionShock$productsSpecialDiscount);
  190.             
  191.             // Élimine les doublons éventuels en se basant sur l'ID du produit
  192.             $uniqueProducts = [];
  193.             $uniqueIds = [];
  194.             foreach ($products as $product) {
  195.                 if (!in_array($product['id'], $uniqueIds)) {
  196.                     $uniqueIds[] = $product['id'];
  197.                     $uniqueProducts[] = $product;
  198.                 }
  199.             }
  200.             $products $uniqueProducts;
  201.         } elseif ($brand == 'tools') {
  202.             $products $manager->getRepository(Products::class)->getAllProductTools($information);
  203.         } else {
  204.             if ($brand == "Jacob") {
  205.                 $products $manager->getRepository(Products::class)->getAllProductWithBrandJacob($brand$information);
  206.             } else {
  207.                 $products $manager->getRepository(Products::class)->getAllProductWithBrand($brand$information);
  208.             }
  209.         }
  210.         // Liège === stock de la boutique physique
  211.         // online === stock boutique en ligne si produit pas mercator
  212.         $excludedDepots LotNumber::EXCLUDED_DEPOTS;
  213.         foreach ($products as $index => $product) {
  214.             $quantity 0;
  215.             if ($products[$index]['lotNumber']) {
  216.                 // keep stock only for valid warehouses
  217.                 $products[$index]['lotNumber'] = array_values(array_filter($products[$index]['lotNumber'], function ($lot) use ($excludedDepots) {
  218.                     return $lot['depot'] !== LotNumber::PHYSICAL_DEPOT && $lot['stock'] > 0;
  219.                 }));
  220.                 if (count($products[$index]['lotNumber']) === && $products[$index]['lotNumber'][0]['depot'] === LotNumber::ONLINE_DEPOT) {
  221.                     $quantity $products[$index]['lotNumber'][0]['stock'];
  222.                 } else {
  223.                     array_walk($products[$index]['lotNumber'], function ($lot) use (&$quantity$excludedDepots) {
  224.                         if (!in_array($lot['depot'], $excludedDepots)) {
  225.                             $quantity += $lot['stock'];
  226.                         }
  227.                     });
  228.                 }
  229.             }
  230.             $products[$index]['quantity'] = $quantity;
  231.         }
  232.         return new JsonResponse($productsheaders: ['Content-Type' => 'application/json;charset=UTF-8']);
  233.     }
  234. }