<?php
declare(strict_types=1);
namespace App\Controller;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sylius\Bundle\CustomerBundle\Form\Type\CustomerType;
use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
use App\Entity\SiteShop;
use App\Entity\Customer\Customer;
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Sylius\Component\Taxation\Calculator\CalculatorInterface;
use Sylius\Component\Order\Context\CartContextInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use App\Entity\SiteSacZipAccessories;
use App\Entity\Product\ProductVariant;
use App\Entity\SiteSacZipSelection;
class SiteConfigurateurController extends AbstractController
{
// protected $container;
// public function __construct(
// ContainerInterface $container,
// ){
// // parent::__construct();
// $this->container = $container;
// }
public function indexAction(
Breadcrumbs $breadcrumbs,
ProductVariantRepositoryInterface $productVariantRepository
): Response
{
$em = $this->get('doctrine')->getManager();
$breadcrumbs->addRouteItem("sylius.ui.home", "sylius_shop_homepage");
$breadcrumbs->addItem("Configurateur Sac & Zip");
// Selectionne les produits de base (en dur)
// Grande besace : BASE.BES.CAR.G
// Petite besace : BASE.BES.P
// Sac compagnon : BASE.SAC.COM
// --
// Rabat grande besace : RABA.BES.CAR.G.MILL.POI
// Rabat petite besace : RABA.BES.P
// Rabat besace petit nuage : RABA.BES.PET.
// Rabat sac compagnon : RABA.SAC.COM
// --
// Petite Lanière : LANI.P
// Grande lanière : LANI.G
// FIXME leftJoin translation variant
$variants = $productVariantRepository->createQueryBuilder('v')
->addSelect('product')
->leftJoin('v.product', 'product')
->andWhere('v.enabled = 1')
->andWhere('v.onHand >= 5')
->getQuery()
->getResult();
// Besaces
$grandeBesaces = array_values(array_filter($variants, function ($row) {
return $row->getProduct()->getCode() == ProductVariant::SACZIP_FORMES['base']['grand_besace'] /*&& $row->getBadge() == 'newest'*/;
}));
usort($grandeBesaces, function($a, $b) {
if($a->getBadge() == 'newest'){
return -1;
}else{
return $a->getOnHand() < $b->getOnHand();
}
});
foreach($grandeBesaces as $key => $grandeBesace){
foreach ($grandeBesace->getOptionValues() as $optionValue) {
$grandeBesace->collection = $optionValue->getTranslation()->getValue();
}
}
$petiteBesaces = array_values(array_filter($variants, function ($row) {
return $row->getProduct()->getCode() == ProductVariant::SACZIP_FORMES['base']['petite_besace'];
}));
usort($petiteBesaces, function($a, $b) {
if($a->getBadge() == 'newest'){
return -1;
}else{
return $a->getOnHand() < $b->getOnHand();
}
});
foreach($petiteBesaces as $key => $petiteBesace){
foreach ($petiteBesace->getOptionValues() as $optionValue) {
$petiteBesace->collection = $optionValue->getTranslation()->getValue();
}
}
$sacCompagnons = array_values(array_filter($variants, function ($row) {
return $row->getProduct()->getCode() == ProductVariant::SACZIP_FORMES['base']['sac_compagnon'];
}));
usort($sacCompagnons, function($a, $b) {
if($a->getBadge() == 'newest'){
return -1;
}else{
return $a->getOnHand() < $b->getOnHand();
}
});
foreach($sacCompagnons as $key => $sacCompagnon){
foreach ($sacCompagnon->getOptionValues() as $optionValue) {
$sacCompagnon->collection = $optionValue->getTranslation()->getValue();
}
}
// Rabats
$rabatGrandeBesaces = array_values(array_filter($variants, function ($row) {
return $row->getProduct()->getCode() == ProductVariant::SACZIP_FORMES['rabat']['rabat_grande_besace'];
}));
usort($rabatGrandeBesaces, function($a, $b) {
if($a->getBadge() == 'newest'){
return -1;
}else{
return $a->getOnHand() < $b->getOnHand();
}
});
foreach($rabatGrandeBesaces as $key => $rabatGrandeBesace){
foreach ($rabatGrandeBesace->getOptionValues() as $optionValue) {
$rabatGrandeBesace->collection = $optionValue->getTranslation()->getValue();
}
}
$rabatPetiteBesaces = array_values(array_filter($variants, function ($row) {
return $row->getProduct()->getCode() == ProductVariant::SACZIP_FORMES['rabat']['rabat_petite_besace'];
}));
usort($rabatPetiteBesaces, function($a, $b) {
if($a->getBadge() == 'newest'){
return -1;
}else{
return $a->getOnHand() < $b->getOnHand();
}
});
foreach($rabatPetiteBesaces as $key => $rabatPetiteBesace){
foreach ($rabatPetiteBesace->getOptionValues() as $optionValue) {
$rabatPetiteBesace->collection = $optionValue->getTranslation()->getValue();
}
}
$rabatBesacePetitNuages = array_values(array_filter($variants, function ($row) {
return $row->getProduct()->getCode() == ProductVariant::SACZIP_FORMES['rabat']['rabat_besace_petite_nuage'];
}));
usort($rabatBesacePetitNuages, function($a, $b) {
if($a->getBadge() == 'newest'){
return -1;
}else{
return $a->getOnHand() < $b->getOnHand();
}
});
foreach($rabatBesacePetitNuages as $key => $rabatBesacePetitNuage){
foreach ($rabatBesacePetitNuage->getOptionValues() as $optionValue) {
$rabatBesacePetitNuage->collection = $optionValue->getTranslation()->getValue();
}
}
$rabatSacCompagnons = array_values(array_filter($variants, function ($row) {
return $row->getProduct()->getCode() == ProductVariant::SACZIP_FORMES['rabat']['rabat_sac_compagnon'];
}));
usort($rabatSacCompagnons, function($a, $b) {
if($a->getBadge() == 'newest'){
return -1;
}else{
return $a->getOnHand() < $b->getOnHand();
}
});
foreach($rabatSacCompagnons as $key => $rabatSacCompagnon){
foreach ($rabatSacCompagnon->getOptionValues() as $optionValue) {
$rabatSacCompagnon->collection = $optionValue->getTranslation()->getValue();
}
}
// Lanières
$petiteLanieres = array_values(array_filter($variants, function ($row) {
return $row->getProduct()->getCode() == ProductVariant::SACZIP_FORMES['laniere']['petite_laniere'];
}));
usort($petiteLanieres, function($a, $b) {
if($a->getBadge() == 'newest'){
return -1;
}else{
return $a->getOnHand() < $b->getOnHand();
}
});
foreach($petiteLanieres as $key => $petiteLaniere){
foreach ($petiteLaniere->getOptionValues() as $optionValue) {
$petiteLaniere->collection = $optionValue->getTranslation()->getValue();
}
}
$grandeLanieres = array_values(array_filter($variants, function ($row) {
return $row->getProduct()->getCode() == ProductVariant::SACZIP_FORMES['laniere']['grande_laniere'];
}));
usort($grandeLanieres, function($a, $b) {
if($a->getBadge() == 'newest'){
return -1;
}else{
return $a->getOnHand() < $b->getOnHand();
}
});
foreach($grandeLanieres as $key => $grandeLaniere){
foreach ($grandeLaniere->getOptionValues() as $optionValue) {
$grandeLaniere->collection = $optionValue->getTranslation()->getValue();
}
}
// Accessoires (administrable)
$accessoriesRepository = $em->getRepository(SiteSacZipAccessories::class);
$accessoriesShapes = $accessoriesRepository->findBy(array(), ['position' => 'ASC']);
$accessories = $productVariantRepository->createQueryBuilder('v')
->addSelect('product')
->leftJoin('v.product', 'product')
->andWhere('v.enabled = 1')
->andWhere('v.onHand >= 5')
->andWhere('product.id IN (:ids)')
->setParameter('ids', array_map(function ($row) {
return $row->getProduct()->getId();
}, $accessoriesShapes))
->getQuery()
->getResult();
usort($accessories, function($a, $b) {
if($a->getBadge() == 'newest'){
return -1;
}else{
return $a->getOnHand() < $b->getOnHand();
}
});
foreach($accessories as $key => $accessorie){
foreach ($accessorie->getOptionValues() as $optionValue) {
$accessorie->collection = $optionValue->getTranslation()->getValue();
}
}
return $this->render('site_configurateur_index.html.twig', [
'grandeBesaces' => $grandeBesaces,
'petiteBesaces' => $petiteBesaces,
'sacCompagnons' => $sacCompagnons,
'rabatGrandeBesaces' => $rabatGrandeBesaces,
'rabatPetiteBesaces' => $rabatPetiteBesaces,
'rabatBesacePetitNuages' => $rabatBesacePetitNuages,
'rabatSacCompagnons' => $rabatSacCompagnons,
'petiteLanieres' => $petiteLanieres,
'grandeLanieres' => $grandeLanieres,
'accessoriesFormes' => $accessoriesShapes,
'accessories' => $accessories
]);
}
public function refreshAction(
Request $request,
ChannelContextInterface $channelContext,
ProductVariantRepositoryInterface $productVariantRepository
): JsonResponse
{
$productsIds = (array) @json_decode($request->request->get('data'), true);
$channel = $channelContext->getChannel();
$variants = [];
if (count($productsIds) > 0) {
$variants = $productVariantRepository->findBy([
'id' => $productsIds
]);
}
$total = 0;
foreach ($variants as $variant) {
$total += $variant->getChannelPricingForChannel($channel)->getPrice();
}
return new JsonResponse([
'products' => $this->render('site_configurateur_produits.html.twig', [
'products' => $variants,
])->getContent(),
'total' => $this->render('site_configurateur_addtocart.html.twig', [
'total' => $total,
])->getContent(),
]);
}
public function addToCartAction(
Request $request,
CartContextInterface $cartContext,
ProductVariantRepositoryInterface $productVariantRepository,
ContainerInterface $container,
): JsonResponse
{
$cart = $cartContext->getCart();
$productsIds = (array) @json_decode($request->request->get('data'), true);
if (count($productsIds) > 0) {
$variants = $productVariantRepository->findBy([
'id' => $productsIds
]);
foreach ($variants as $variant) {
// Ajout au panier
$orderItem = $container->get('sylius.factory.order_item')->createNew();
$orderItem->setVariant($variant);
$container->get('sylius.order_item_quantity_modifier')->modify($orderItem, 1);
$cart->addItem($orderItem);
}
}
// Recalcul des promotions
$container->get('sylius.order_processing.order_processor')->process($cart);
$cartManager = $container->get('sylius.manager.order');
$cartManager->persist($cart);
$cartManager->flush();
$amount = 0;
$total = 0;
$diff = 0;
$progress = 0;
$cdts = $container->get('sylius.repository.promotion')->findOneByCode('FREE_DELIVERY')->getRules();
foreach($cdts as $cdt){
if( $cdt->getType() == 'item_total'){
$amount = $cdt->getConfiguration()['ppmc']['amount'];
$total = $cart->getTotal();
$diff = $amount-$total;
$progress = $total / $amount * 100;
}
}
return new JsonResponse([
'produits' => $this->render('@SyliusShop/Webapic/panier/panier-produits.html.twig', [
'cart' => $cart,
]),
'total' => $this->render('@SyliusShop/Webapic/panier/panier-total.html.twig', [
'cart' => $cart,
]),
'top' => $this->render('@SyliusShop/Webapic/panier/panier-top.html.twig', [
'cart' => $cart,
]),
]);
}
public function randomAction(Request $request, EntityManagerInterface $em): JsonResponse
{
// Selectionne une combinaison aléatoirement
// avec prise en compte des stocks
$combinaison = $em->getRepository(SiteSacZipSelection::class)
->createQueryBuilder('s')
->leftJoin('s.productVariantBase', 'productVariantBase')
->leftJoin('s.productVariantFlap', 'productVariantFlap')
->leftJoin('s.productVariantAccessory', 'productVariantAccessory')
->andWhere('productVariantBase.onHand >= 5')
->andWhere('productVariantFlap.onHand >= 5')
->andWhere('productVariantAccessory.onHand >= 5')
->andWhere('productVariantBase.enabled = 1')
->andWhere('productVariantFlap.enabled = 1')
->andWhere('productVariantAccessory.enabled = 1')
->andWhere('s.enabled = 1')
->orderBy('RAND()')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
// Pour éviter d'avoir 0 résultat, on prend même ceux qui ne sont pas en stock
if (empty($combinaison)) {
$combinaison = $em->getRepository(SiteSacZipSelection::class)
->createQueryBuilder('s')
->andWhere('s.enabled = 1')
->orderBy('RAND()')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
}
// Renvoyer la bonne combinaison en fonction de la forme de base selectionnée
// forme 1 = Base grande besace = BASE.BES.CAR.G
// forme 2 = Base petite besace = BASE.BES.P
// forme 3 = Base compagnon = BASE.SAC.COM
$data = [];
if ($combinaison->getProductVariantBase()->getProduct()->getCode() == 'BASE.BES.CAR.G') {
$data = [
'forme' => 1,
'colori-base1' => $combinaison->getProductVariantBase()->getId(),
'rabat1' => $combinaison->getProductVariantFlap()->getId(),
'accessoire' => $combinaison->getProductVariantAccessory()->getId()
];
} else if ($combinaison->getProductVariantBase()->getProduct()->getCode() == 'BASE.BES.P') {
$data = [
'forme' => 2,
'colori-base2' => $combinaison->getProductVariantBase()->getId(),
'rabat2' => $combinaison->getProductVariantFlap()->getId(),
'accessoire' => $combinaison->getProductVariantAccessory()->getId()
];
} else {
$laniereId = 0;
if (empty($combinaison->getProductVariantStrap()) == false) {
$laniereId = $combinaison->getProductVariantStrap()->getId();
}
$data = [
'forme' => 3,
'colori-base3' => $combinaison->getProductVariantBase()->getId(),
'rabat3' => $combinaison->getProductVariantFlap()->getId(),
'laniere3' => $laniereId,
'accessoire' => $combinaison->getProductVariantAccessory()->getId()
];
}
return new JsonResponse([
'random' => $data
]);
}
}