|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
-
- namespace Lc\ShopBundle\Controller\Frontend ;
-
- use App\Entity\ReductionCredit;
- use App\Form\Frontend\OrderProductsType;
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\ShopBundle\Context\MerchantUtilsInterface;
- use Lc\ShopBundle\Context\OrderProductInterface;
- use Lc\ShopBundle\Context\OrderReductionCartInterface;
- use Lc\ShopBundle\Context\OrderReductionCreditInterface;
- use Lc\ShopBundle\Context\OrderUtilsInterface;
- use Lc\ShopBundle\Context\ProductFamilyInterface;
- use Lc\ShopBundle\Context\ReductionCartInterface;
- use Lc\ShopBundle\Model\OrderReductionCart;
- use Lc\ShopBundle\Model\ProductFamily;
- use Lc\ShopBundle\Services\UserUtils;
- use Lc\ShopBundle\Services\UtilsManager;
- use Symfony\Component\HttpFoundation\JsonResponse;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\Routing\Generator\UrlGenerator;
- use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
- use Symfony\Component\Security\Core\Security;
-
- class CartController extends BaseController
- {
- protected $orderUtils ;
- protected $userUtils ;
- protected $priceUtils ;
- protected $router ;
- protected $productFamilyRepository ;
- protected $orderProductRepository ;
-
- protected $productFamily ;
- protected $orderProducts = [] ;
-
-
- public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils,
- UrlGeneratorInterface $router, UtilsManager $utilsManager)
- {
- parent::__construct($em, $security, $merchantUtils);
- $this->orderUtils = $utilsManager->getOrderUtils() ;
- $this->userUtils = $utilsManager->getUserUtils() ;
- $this->priceUtils = $utilsManager->getPriceUtils() ;
- $this->router = $router ;
- $this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetaData(ProductFamilyInterface::class)->getName()) ;
- $this->orderProductRepository = $this->em->getRepository($this->em->getClassMetaData(OrderProductInterface::class)->getName()) ;
- }
-
- public function addProductFamily(Request $request)
- {
- $user = $this->security->getUser() ;
- $visitor = $this->userUtils->getVisitorCurrent() ;
-
- $return = [] ;
- $data = $request->request->all() ;
-
- if(isset($data['order_products']['id_product_family'])) {
- $idProductFamily = $data['order_products']['id_product_family'] ;
- $this->productFamily = $this->productFamilyRepository->find($idProductFamily) ;
-
-
- if (!$user && !$visitor) {
- $this->addFlash('error', 'Vous devez <a href="'.$this->router->generate('frontend_page', ['devAlias' => 'politique-de-confidentialite']).'">accepter les cookies</a> ou vous <a href="'.$this->router->generate('fos_user_security_login').'">connecter</a> pour ajouter un produit.');
- return false ;
- }
-
- if($this->productFamily) {
- $form = $this->createForm(OrderProductsType::class, ['id_product_family' => $this->productFamily->getId()]);
- $form->handleRequest($request);
-
- if ($form->isSubmitted() && $form->isValid()) {
- $orderShop = $this->orderUtils->getCartCurrent(true) ;
- $data = $form->getData() ;
- foreach($data as $orderProduct) {
- if($orderProduct instanceof OrderProductInterface) {
- if($orderProduct->getQuantityOrder() > 0) {
- $addOrderProduct = $this->orderUtils->addOrderProduct($orderShop, $orderProduct) ;
- }
- if(isset($addOrderProduct) && $addOrderProduct && $orderProduct->getQuantityOrder() > 0) {
- $this->orderProducts[] = $orderProduct ;
- }
- }
- }
- }
- }
- }
-
- return new JsonResponse($return) ;
- }
-
- public function redirectToReferer($request) {
- $referer = $request->headers->get('referer');
-
- if($referer) {
- return $this->redirect($referer);
- }
- else {
- return $this->redirectToRoute('frontend_order_cart');
- }
- }
-
- public function deleteReductionCart(Request $request)
- {
- $id = $request->get('id') ;
-
- $orderReductionCartRepository = $this->em->getRepository($this->em->getClassMetadata(OrderReductionCartInterface::class)->getName()) ;
- $orderReductionCart = $orderReductionCartRepository->findOneById((int) $id) ;
-
- $orderShop = $this->orderUtils->getCartCurrent() ;
-
- if($orderReductionCart && $orderShop->getOrderReductionCarts() && $orderShop->getOrderReductionCarts()->contains($orderReductionCart)) {
- $this->em->remove($orderReductionCart) ;
- $this->em->flush() ;
-
- $this->addFlash('success', 'La réduction a bien été supprimée de votre panier.') ;
- }
- else {
- $this->addFlash('error', 'Une erreur est survenue lors de la suppression de la réduction. ') ;
- }
-
- return $this->redirectToReferer($request) ;
- }
-
- public function addReductionCredit(Request $request)
- {
- $id = $request->get('id') ;
- $orderShop = $this->orderUtils->getCartCurrent() ;
- $user = $this->security->getUser() ;
-
- $reductionCreditRepository = $this->em->getRepository(ReductionCredit::class) ;
- $reductionCredit = $reductionCreditRepository->find($id) ;
-
- if($orderShop && $user && $reductionCredit
- && $this->orderUtils->isReductionCreditAllowAddToOrder($orderShop, $reductionCredit)
- && !$this->orderUtils->isReductionCreditAddedToOrder($orderShop, $reductionCredit)) {
-
- $return = $this->orderUtils->createOrderReductionCredit($orderShop, $reductionCredit) ;
-
- if($return) {
- $this->addFlash('success', 'Votre avoir a bien été ajouté à votre panier.') ;
- }
- else {
- $this->addFlash('error', 'Vous ne pouvez pas effectuer cette action. Le montant de la commande est insuffisant.') ;
- }
- }
- else {
- $this->addFlash('error', "Impossible d'effectuer cette action");
- }
-
- return $this->redirectToReferer($request) ;
- }
-
- public function deleteReductionCredit(Request $request)
- {
- $id = $request->get('id') ;
-
- $orderReductionCreditRepository = $this->em->getRepository($this->em->getClassMetadata(OrderReductionCreditInterface::class)->getName()) ;
- $orderReductionCredit = $orderReductionCreditRepository->findOneById((int) $id) ;
-
- $orderShop = $this->orderUtils->getCartCurrent() ;
-
- if($orderReductionCredit && $orderShop->getOrderReductionCredits() && $orderShop->getOrderReductionCredits()->contains($orderReductionCredit)) {
- $this->em->remove($orderReductionCredit) ;
- $this->em->flush() ;
-
- $this->addFlash('success', 'Votre avoir a bien été supprimé de votre panier.') ;
- }
- else {
- $this->addFlash('error', 'Une erreur est survenue lors de la suppression de votre avoir. ') ;
- }
-
- $referer = $request->headers->get('referer');
-
- if($referer) {
- return $this->redirect($referer);
- }
- else {
- return $this->redirectToRoute('frontend_order_cart');
- }
- }
-
- }
|