src/Controller/ProductsController.php line 23
<?php
namespace App\Controller;
use App\Entity\ActionShock;
use App\Entity\AdditionalCompanyInformation;
use App\Entity\CustomerControl;
use App\Entity\LotNumber;
use App\Entity\Products;
use App\Entity\SpecialDiscount;
use App\Repository\LotNumberRepository;
use App\Service\ActionShockService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ProductsController extends AbstractController
{
#[Route('/products', name: 'products')]
public function index(EntityManagerInterface $manager, ActionShockService $actionShockService): Response
{
$actionShockService->updateActionShocksStatus();
$additionalInformations = null;
$checkControl = $manager->getRepository(CustomerControl::class)->findOneBy(array('user' => $this->getUser(), 'finish' => false));
if (!$checkControl) {
$user = $this->getUser();
} else {
$user = $checkControl->getCustomer();
}
if ($user != null) {
$additionalInformations = $manager->getRepository(AdditionalCompanyInformation::class)->findOneBy(array('user' => $user));
if ($additionalInformations->getCompanyCountry() == null) {
$this->addFlash('danger', 'Veuillez remplir vos coordonnées!');
return $this->redirectToRoute('profile');
}
}
$information = $manager->getRepository(AdditionalCompanyInformation::class)->findOneBy(array('user' => $user));
$products = $manager->getRepository(Products::class)->getAllProduct($information);
$products = $this->calculateRebatePricePharmacy($products);
$specialDiscounts = $manager->getRepository(SpecialDiscount::class)->findAll();
if ($specialDiscounts != null) {
$specialDiscount = $specialDiscounts[0];
} else {
$specialDiscount = null;
}
if (!$checkControl) {
$checkControl = null;
}
return $this->render('products/index.html.twig', [
'products' => $products,
'shockActionTypes' => ActionShock::TYPES,
'user' => $user,
'information' => $additionalInformations,
'checkControl' => $checkControl,
'specialDiscount' => $specialDiscount
]);
}
private function calculateRebatePricePharmacy($products)
{
foreach ($products as &$product) {
$totalRebate = ($product['pricePharmacy'] / 100) * $this->getUser()->getAdditionalPercentage();
$item['totalRebate'] = floatval(number_format($totalRebate, 2, '.', ''));
$item['PriceRebateInclude'] = floatval(number_format($product['pricePharmacy'] - $item['totalRebate'], 2, '.', ''));
$product['pricePharmacyWithRebate'] = $item['PriceRebateInclude'];
}
// dd($products);
return $products;
}
#[Route('/productsJson/{brand}', name: 'productsJson')]
public function allProductJson(EntityManagerInterface $manager, $brand = null): JsonResponse
{
$checkControl = $manager->getRepository(CustomerControl::class)->findOneBy(array('user' => $this->getUser(), 'finish' => false));
if (!$checkControl) {
$user = $this->getUser();
} else {
$user = $checkControl->getCustomer();
}
$information = $manager->getRepository(AdditionalCompanyInformation::class)->findOneBy(array('user' => $user));
if ($brand == null) {
$products = $manager->getRepository(Products::class)->getAllProduct($information);
} elseif ($brand == 'dlu-gooddeal') {
$products = $manager->getRepository(Products::class)->getAllProductWithGoodDeals($information);
} elseif ($brand == 'action-shock') {
$products = $manager->getRepository(Products::class)->getAllProductshockDeals($information);
} elseif ($brand == 'special-discount') {
$products = $manager->getRepository(Products::class)->getAllProductSpecialDiscount($information);
} elseif ($brand == 'action-special') {
// Combine les produits "action-shock" et "special-discount"
$productsActionShock = $manager->getRepository(Products::class)->getAllProductshockDeals($information);
$productsSpecialDiscount = $manager->getRepository(Products::class)->getAllProductSpecialDiscount($information);
// Fusionne les deux tableaux
$products = array_merge($productsActionShock, $productsSpecialDiscount);
// Élimine les doublons éventuels en se basant sur l'ID du produit
$uniqueProducts = [];
$uniqueIds = [];
foreach ($products as $product) {
if (!in_array($product['id'], $uniqueIds)) {
$uniqueIds[] = $product['id'];
$uniqueProducts[] = $product;
}
}
$products = $uniqueProducts;
} elseif ($brand == 'tools') {
$products = $manager->getRepository(Products::class)->getAllProductTools($information);
} else {
if ($brand == "Jacob") {
$products = $manager->getRepository(Products::class)->getAllProductWithBrandJacob($brand, $information);
} else {
$products = $manager->getRepository(Products::class)->getAllProductWithBrand($brand, $information);
}
}
// Liège === stock de la boutique physique
// online === stock boutique en ligne si produit pas mercator
$excludedDepots = LotNumber::EXCLUDED_DEPOTS;
foreach ($products as $index => $product) {
$quantity = 0;
if ($products[$index]['lotNumber']) {
// keep stock only for valid warehouses
$products[$index]['lotNumber'] = array_values(array_filter($products[$index]['lotNumber'], function ($lot) use ($excludedDepots) {
return $lot['depot'] !== LotNumber::PHYSICAL_DEPOT && $lot['stock'] > 0;
}));
if (count($products[$index]['lotNumber']) === 1 && $products[$index]['lotNumber'][0]['depot'] === LotNumber::ONLINE_DEPOT) {
$quantity = $products[$index]['lotNumber'][0]['stock'];
} else {
array_walk($products[$index]['lotNumber'], function ($lot) use (&$quantity, $excludedDepots) {
if (!in_array($lot['depot'], $excludedDepots)) {
$quantity += $lot['stock'];
}
});
}
}
$products[$index]['quantity'] = $quantity;
}
return new JsonResponse($products, headers: ['Content-Type' => 'application/json;charset=UTF-8']);
}
}