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.     #[Route('/productsJson/{brand}'name'productsJson')]
  69.     public function allProductJson(EntityManagerInterface $manager$brand null): JsonResponse
  70.     {
  71.         $checkControl $manager->getRepository(CustomerControl::class)->findOneBy(array('user' => $this->getUser(), 'finish' => false));
  72.         if (!$checkControl) {
  73.             $user $this->getUser();
  74.         } else {
  75.             $user $checkControl->getCustomer();
  76.         }
  77.         $information $manager->getRepository(AdditionalCompanyInformation::class)->findOneBy(array('user' => $user));
  78.         if ($brand == null) {
  79.             $products $manager->getRepository(Products::class)->getAllProduct($information);
  80.         } elseif ($brand == 'dlu-gooddeal') {
  81.             $products $manager->getRepository(Products::class)->getAllProductWithGoodDeals($information);
  82.         } elseif ($brand == 'action-shock') {
  83.             $products $manager->getRepository(Products::class)->getAllProductshockDeals($information);
  84.         } elseif ($brand == 'special-discount') {
  85.             $products $manager->getRepository(Products::class)->getAllProductSpecialDiscount($information);
  86.         } elseif ($brand == 'action-special') {
  87.             // Combine les produits "action-shock" et "special-discount"
  88.             $productsActionShock $manager->getRepository(Products::class)->getAllProductshockDeals($information);
  89.             $productsSpecialDiscount $manager->getRepository(Products::class)->getAllProductSpecialDiscount($information);
  90.             
  91.             // Fusionne les deux tableaux
  92.             $products array_merge($productsActionShock$productsSpecialDiscount);
  93.             
  94.             // Élimine les doublons éventuels en se basant sur l'ID du produit
  95.             $uniqueProducts = [];
  96.             $uniqueIds = [];
  97.             foreach ($products as $product) {
  98.                 if (!in_array($product['id'], $uniqueIds)) {
  99.                     $uniqueIds[] = $product['id'];
  100.                     $uniqueProducts[] = $product;
  101.                 }
  102.             }
  103.             $products $uniqueProducts;
  104.         } elseif ($brand == 'tools') {
  105.             $products $manager->getRepository(Products::class)->getAllProductTools($information);
  106.         } else {
  107.             if ($brand == "Jacob") {
  108.                 $products $manager->getRepository(Products::class)->getAllProductWithBrandJacob($brand$information);
  109.             } else {
  110.                 $products $manager->getRepository(Products::class)->getAllProductWithBrand($brand$information);
  111.             }
  112.         }
  113.         // Liège === stock de la boutique physique
  114.         // online === stock boutique en ligne si produit pas mercator
  115.         $excludedDepots LotNumber::EXCLUDED_DEPOTS;
  116.         foreach ($products as $index => $product) {
  117.             $quantity 0;
  118.             if ($products[$index]['lotNumber']) {
  119.                 // keep stock only for valid warehouses
  120.                 $products[$index]['lotNumber'] = array_values(array_filter($products[$index]['lotNumber'], function ($lot) use ($excludedDepots) {
  121.                     return $lot['depot'] !== LotNumber::PHYSICAL_DEPOT && $lot['stock'] > 0;
  122.                 }));
  123.                 if (count($products[$index]['lotNumber']) === && $products[$index]['lotNumber'][0]['depot'] === LotNumber::ONLINE_DEPOT) {
  124.                     $quantity $products[$index]['lotNumber'][0]['stock'];
  125.                 } else {
  126.                     array_walk($products[$index]['lotNumber'], function ($lot) use (&$quantity$excludedDepots) {
  127.                         if (!in_array($lot['depot'], $excludedDepots)) {
  128.                             $quantity += $lot['stock'];
  129.                         }
  130.                     });
  131.                 }
  132.             }
  133.             $products[$index]['quantity'] = $quantity;
  134.         }
  135.         return new JsonResponse($productsheaders: ['Content-Type' => 'application/json;charset=UTF-8']);
  136.     }
  137. }