src/ShoppingBundle/Service/Cart/CartManager.php line 14

Open in your IDE?
  1. <?php
  2. namespace Shopping\Service\Cart;
  3. use Core\Service\Session\Session;
  4. use Doctrine\Common\Persistence\ManagerRegistry;
  5. use Shopping\Entity\Attribute;
  6. use Shopping\Entity\AttributeGroup;
  7. use Shopping\Service\Tools;
  8. /**
  9.  * CartManager - Gestion du panier et des prix appliqués
  10.  */
  11. class CartManager
  12. {
  13.     private $cart;
  14.     private $cart_session;
  15.     private $entityManager;
  16.     private $user;
  17.     /**
  18.      * Constructeur
  19.      */
  20.     public function __construct(Session $sessionManagerRegistry $entityManager$user null)
  21.     {
  22.         $this->entityManager $entityManager;
  23.         $this->user $user;
  24.         // Création de la session de panier
  25.         $this->cart_session = new CartSession($session);
  26.         // Récupère le panier depuis la session
  27.         $this->cart $this->cart_session->getCartFromSession();
  28.     }
  29.     /**
  30.      * Ajoute $quantity de $product au panier
  31.      */
  32.     public function add($product$quantity$options)
  33.     {
  34.         $declinaison $product->getDeclinaison();
  35.         $attributes = [];
  36.         if ($declinaison != null) {
  37.             $attributes $declinaison->serialize();
  38.         }
  39.         if ($this->checkProductConformity($product$quantity)) {
  40.             // Ajoute le produit au panier
  41.             $this->cart->add($product->getProduct()->getId(), $quantity$attributesnull$options);
  42.             // Modifie le panier en session
  43.             $this->cart_session->persist($this->cart);
  44.             return true;
  45.         } else {
  46.             return false;
  47.         }
  48.     }
  49.     /**
  50.      * Mise à jours de $quantity de $product au panier
  51.      */
  52.     public function update($product$quantity$reference)
  53.     {
  54.         $declinaison $product->getDeclinaison();
  55.         $attributes = [];
  56.         if ($declinaison != null) {
  57.             $attributes $declinaison->serialize();
  58.         }
  59.         if ($this->checkProductConformity($product$quantity)) {
  60.             // Ajoute le produit au panier
  61.             $this->cart->update($product->getProduct()->getId(), $quantity$attributes$reference);
  62.             // Modifie le panier en session
  63.             $this->cart_session->persist($this->cart);
  64.             return true;
  65.         } else {
  66.             return false;
  67.         }
  68.     }
  69.     /**
  70.      * Supprime $quantity de $product au panier
  71.      */
  72.     public function remove($product$quantity null$product_cart_reference null)
  73.     {
  74.         // Supprime le produit du panier
  75.         $this->cart->remove($product->getId(), $quantity$product_cart_reference);
  76.         // Modifie le panier en session
  77.         $this->cart_session->persist($this->cart);
  78.     }
  79.     /**
  80.      * Supprime le panier
  81.      */
  82.     public function clear()
  83.     {
  84.         // Réinitialise le panier
  85.         $this->cart = new Cart();
  86.         // Réinitialize le panier en session
  87.         $this->cart_session->clear();
  88.     }
  89.     /*
  90.      * Fonctions sur une "ligne" de produits
  91.      */
  92.     /**
  93.      * Prix HT pour les produits de $row
  94.      */
  95.     public function getProductRowTotalHT($row)
  96.     {
  97.         $product = new \Shopping\Service\Product\Product($this->entityManager$this->user);
  98.         $product->createProduct($row->getProduct(), $row->getAttributes());
  99.         $totalHT = (float)($product->getPriceHT() * $row->getQuantity());
  100.         if ($totalHT 0) {
  101.             throw new \Exception('Une erreur est survenue lors du calcule HT des produits'500);
  102.         }
  103.         return $totalHT;
  104.     }
  105.     public function getProductRowTotalHTWhitoutPercentage($row)
  106.     {
  107.         $product = new \Shopping\Service\Product\Product($this->entityManager$this->user);
  108.         $product->createProduct($row->getProduct(), $row->getAttributes());
  109.         $totalHT = (float)($product->getPriceHT(false) * $row->getQuantity());
  110.         if ($totalHT 0) {
  111.             throw new \Exception('Une erreur est survenue lors du calcule HT des produits'500);
  112.         }
  113.         return $totalHT;
  114.     }
  115.     /**
  116.      * TVA pour les produits de $row
  117.      */
  118.     public function getProductRowTotalTVA($row)
  119.     {
  120.         $product = new \Shopping\Service\Product\Product($this->entityManager$this->user);
  121.         $product->createProduct($row->getProduct(), $row->getAttributes());
  122. //        $totalHT = (float)($product->getPriceTva() * $row->getQuantity());
  123.         $totalHT = (float)($product->getPriceHT() * $row->getQuantity());
  124.         $totalHT Tools::calculateProductTVA($totalHT$product->getProduct()->getTax()->getRate());
  125.         if ($totalHT 0) {
  126.             throw new \Exception('Une erreur est survenue lors du calcule HT des produits'500);
  127.         }
  128.         return $totalHT;
  129.     }
  130.     /**
  131.      * Prix TTC pour les produits de $row
  132.      */
  133.     public function getProductRowTotalTTC($row)
  134.     {
  135.         $product = new \Shopping\Service\Product\Product($this->entityManager$this->user);
  136.         $product->createProduct($row->getProduct(), $row->getAttributes());
  137.         $totalHT = (float)($product->getPriceHT() * $row->getQuantity());
  138.         $totalTVA Tools::calculateProductTVA($totalHT$product->getProduct()->getTax()->getRate());
  139.         $totalTTC Tools::calculateProductTTC($totalHT$totalTVA);
  140.         $totalTTC Tools::formatPriceRound($totalTTC);
  141.         if ($totalTTC 0) {
  142.             throw new \Exception('Une erreur est survenue lors du calcule TTC des produits'500);
  143.         }
  144.         return $totalTTC;
  145.     }
  146.     /**
  147.      * Poids pour les produits de $row
  148.      */
  149.     public function getProductRowTotalWeight($row)
  150.     {
  151.         $product = new \Shopping\Service\Product\Product($this->entityManager$this->user);
  152.         $product->createProduct($row->getProduct(), $row->getAttributes());
  153.         $totalWeight = (float)($product->getWeight() * $row->getQuantity());
  154.         if ($totalWeight 0) {
  155.             throw new \Exception('Une erreur est survenue lors du calcule du poids des produits'500);
  156.         }
  157.         return number_format($totalWeight2);
  158.     }
  159.     /**
  160.      * Nombre de produits de $row
  161.      */
  162.     public function getProductRowTotalCount($row)
  163.     {
  164.         $totalCount $row->getQuantity();
  165.         if ($totalCount 0) {
  166.             throw new \Exception('Une erreur est survenue lors du calcule du nombre de produits'500);
  167.         }
  168.         return $totalCount;
  169.     }
  170.     /*
  171.      * Fonctions sur un produit spécifique
  172.      */
  173.     /**
  174.      * Prix HT pour les produits avec son $product_id
  175.      */
  176.     public function getProductRowTotalHTByProductId($product_id)
  177.     {
  178.         foreach ($this->cart->getProducts() as $row) {
  179.             if ($row->getProduct() == $product_id) {
  180.                 $product = new \Shopping\Service\Product\Product($this->entityManager$this->user);
  181.                 $product->createProduct($row->getProduct(), $row->getAttributes());
  182.                 // Vérifie la conformité et la disponibilité du produit
  183.                 if ($this->checkProductConformity($product)) {
  184.                     return $this->getProductRowTotalHT($row);
  185.                 }
  186.             }
  187.         }
  188.         return null;
  189.     }
  190.     /**
  191.      * TVA pour les produits avec son $product_id
  192.      */
  193.     public function getProductRowTotalTVAByProductId($product_id)
  194.     {
  195.         foreach ($this->cart->getProducts() as $row) {
  196.             if ($row->getProduct() == $product_id) {
  197.                 $product = new \Shopping\Service\Product\Product($this->entityManager$this->user);
  198.                 $product->createProduct($row->getProduct(), $row->getAttributes());
  199.                 // Vérifie la conformité et la disponibilité du produit
  200.                 if ($this->checkProductConformity($product)) {
  201.                     return $this->getProductRowTotalTVA($row);
  202.                 }
  203.             }
  204.         }
  205.         return null;
  206.     }
  207.     /**
  208.      * Prix TTC pour les produits avec son $product_id
  209.      */
  210.     public function getProductRowTotalTTCByProductId($product_id)
  211.     {
  212.         foreach ($this->cart->getProducts() as $row) {
  213.             if ($row->getProduct() == $product_id) {
  214.                 $product = new \Shopping\Service\Product\Product($this->entityManager$this->user);
  215.                 $product->createProduct($row->getProduct(), $row->getAttributes());
  216.                 // Vérifie la conformité et la disponibilité du produit
  217.                 if ($this->checkProductConformity($product)) {
  218.                     return $this->getProductRowTotalTTC($row);
  219.                 }
  220.             }
  221.         }
  222.         return null;
  223.     }
  224.     /**
  225.      * Poids pour les produits avec son $product_id
  226.      */
  227.     public function getProductRowTotalWeightByProductId($product_id)
  228.     {
  229.         foreach ($this->cart->getProducts() as $row) {
  230.             if ($row->getProduct() == $product_id) {
  231.                 $product = new \Shopping\Service\Product\Product($this->entityManager$this->user);
  232.                 $product->createProduct($row->getProduct(), $row->getAttributes());
  233.                 // Vérifie la conformité et la disponibilité du produit
  234.                 if ($this->checkProductConformity($product)) {
  235.                     return $this->getProductRowTotalWeight($row);
  236.                 }
  237.             }
  238.         }
  239.         return null;
  240.     }
  241.     /**
  242.      * Nombre de produits avec $product_id
  243.      */
  244.     public function getProductRowTotalCountByProductId($product_id)
  245.     {
  246.         foreach ($this->cart->getProducts() as $row) {
  247.             if ($row->getProduct() == $product_id) {
  248.                 $product = new \Shopping\Service\Product\Product($this->entityManager$this->user);
  249.                 $product->createProduct($row->getProduct(), $row->getAttributes());
  250.                 // Vérifie la conformité et la disponibilité du produit
  251.                 if ($this->checkProductConformity($product)) {
  252.                     return $this->getProductRowTotalCount($row);
  253.                 }
  254.             }
  255.         }
  256.         return null;
  257.     }
  258.     /*
  259.      * Fonctions sur le panier
  260.      */
  261.     /**
  262.      * Prix HT de tous les produits du panier
  263.      */
  264.     public function getCartTotalHT()
  265.     {
  266.         $totalHT 0;
  267.         foreach ($this->cart->getProducts() as $row) {
  268.             $product = new \Shopping\Service\Product\Product($this->entityManager$this->user);
  269.             $product->createProduct($row->getProduct(), $row->getAttributes());
  270.             if ($this->checkProductConformity($product)) {
  271.                 $totalHT += $this->getProductRowTotalHT($row);
  272.             }
  273.         }
  274.         return $totalHT;
  275.     }
  276.     public function getCartTotalHTWhitoutPercentage()
  277.     {
  278.         $totalHT 0;
  279.         foreach ($this->cart->getProducts() as $row) {
  280.             $product = new \Shopping\Service\Product\Product($this->entityManager$this->user);
  281.             $product->createProduct($row->getProduct(), $row->getAttributes());
  282.             if ($this->checkProductConformity($product)) {
  283.                 $totalHT += $this->getProductRowTotalHTWhitoutPercentage($row);
  284.             }
  285.         }
  286.         return $totalHT;
  287.     }
  288.     /**
  289.      * Prix TTC de tous les produits du panier
  290.      */
  291.     public function getCartTotalTTC()
  292.     {
  293.         $totalTTC 0;
  294.         foreach ($this->cart->getProducts() as $row) {
  295.             $product = new \Shopping\Service\Product\Product($this->entityManager$this->user);
  296.             $product->createProduct($row->getProduct(), $row->getAttributes());
  297.             if ($this->checkProductConformity($product)) {
  298.                 $totalTTC += $this->getProductRowTotalTTC($row);
  299.             }
  300.         }
  301.         return $totalTTC;
  302.     }
  303.     /**
  304.      * Prix TVA de tous les produits du panier
  305.      */
  306.     public function getCartTotalTVA()
  307.     {
  308.         $totalTVA 0;
  309.         foreach ($this->cart->getProducts() as $row) {
  310.             $product = new \Shopping\Service\Product\Product($this->entityManager$this->user);
  311.             $product->createProduct($row->getProduct(), $row->getAttributes());
  312.             if ($this->checkProductConformity($product)) {
  313.                 $totalTVA += $this->getProductRowTotalTVA($row);
  314.             }
  315.         }
  316.         return $totalTVA;
  317.     }
  318.     /**
  319.      * Poids de tous les produits du panier
  320.      */
  321.     public function getCartTotalWeight()
  322.     {
  323.         $totalWeight 0;
  324.         foreach ($this->cart->getProducts() as $row) {
  325.             $product = new \Shopping\Service\Product\Product($this->entityManager$this->user);
  326.             $product->createProduct($row->getProduct(), $row->getAttributes());
  327.             if ($this->checkProductConformity($product)) {
  328.                 $totalWeight += $this->getProductRowTotalWeight($row);
  329.             }
  330.         }
  331.         return $totalWeight;
  332.     }
  333.     /**
  334.      * Nombre de produits du panier
  335.      */
  336.     public function getCartTotalProductsCount()
  337.     {
  338.         $totalCount 0;
  339.         foreach ($this->cart->getProducts() as $row) {
  340.             $product = new \Shopping\Service\Product\Product($this->entityManager$this->user);
  341.             $product->createProduct($row->getProduct(), $row->getAttributes());
  342.             if ($this->checkProductConformity($product)) {
  343.                 $totalCount += $this->getProductRowTotalCount($row);
  344.             }
  345.         }
  346.         return $totalCount;
  347.     }
  348.     /**
  349.      * Retourne le panier
  350.      */
  351.     public function getCart()
  352.     {
  353.         $cart = [];
  354.         $cart['products'] = [];
  355.         // Parcours des CartProduct du panier
  356.         foreach ($this->cart->getProducts() as $row) {
  357.             // Création du produit
  358.             $product = new \Shopping\Service\Product\Product($this->entityManager$this->user);
  359.             $product->createProduct($row->getProduct(), $row->getAttributes());
  360.             if ($this->checkProductConformity($product)) {
  361.                 // Informations sur les produits
  362.                 $cart['products'][] = [
  363.                     'product_manager' => $product,
  364.                     'product_cart_reference' => $row->getReference(),
  365.                     'product' => $product->getProduct(),
  366.                     'quantity' => $row->getQuantity(),
  367.                     'options' => $row->getOptions(),
  368.                     'productTotalWeight' => $this->getProductRowTotalWeight($row),
  369.                     'productTotalHT' => $this->getProductRowTotalHT($row),
  370.                     'productTotalTVA' => $this->getProductRowTotalTVA($row),
  371.                     'productTotalTTC' => $this->getProductRowTotalTTC($row),
  372.                 ];
  373.             }
  374.         }
  375.         // Informations sur le panier
  376.         $cart['cartTotalWeight'] = Tools::formatPrice($this->getCartTotalWeight());
  377.         $cart['cartTotalHT'] = Tools::formatPrice($this->getCartTotalHT());
  378.         $cart['cartTotalTVA'] = Tools::formatPrice($this->getCartTotalTVA());
  379.         $cart['cartTotalTTC'] = Tools::formatPrice($this->getCartTotalTTC());
  380.         $cart['cartTotalProductsCount'] = $this->getCartTotalProductsCount();
  381.         return $cart;
  382.     }
  383.     /*
  384.      * Fonctions privées
  385.      */
  386.     /**
  387.      * Vérification de la validité du produit pour le mettre dans le panier
  388.      */
  389.     private function checkProductConformity($object$quantity null)
  390.     {
  391.         $productValide true;
  392.         $product $object->getproduct();
  393.         // Produit introuvable
  394.         if ($product == null) {
  395.             return false;
  396.         }
  397.         // Produit en ligne
  398.         if (!$product->getIsEnabled()) {
  399.             $productValide false;
  400.         }
  401.         // Disponibilité à la commande
  402. //        if ($product->getAvailableForOrder() != 1) {
  403. //            $productValide = false;
  404. //        }
  405.         // Date de la disponibilité
  406. //        $date = new \DateTime('NOW');
  407. //        $disponibility = $product->getAvailableDate();
  408. //        if ($disponibility != null && $disponibility != '' && $date < new \DateTime($disponibility)) {
  409. //            $productValide = false;
  410. //        }
  411.         // Lieu de Disponibilité du produit
  412. //        if ($product->getOnlineOnly() != 0 and $product->getOnlineOnly() != 1) {
  413. //            $productValide = false;
  414. //        }
  415.         // Stock disponible
  416. //        if ($product->getUseStock() && ($product->getStock() < $product->getMinimalQuantity())) {
  417. //            $productValide = false;
  418. //        }
  419.         // Redirection du produit
  420. //        if ($product->getRedirect() != 0) {
  421. //            $productValide = false;
  422. //        }
  423.         // Vérification de la quantité restante
  424. //        if ($product->getUseStock() && ($quantity != null && ($product->getStock() - $quantity) < 0)) {
  425. //            $productValide = false;
  426. //        }
  427.         // Produit devenu invalide
  428.         if (!$productValide && $quantity == null) {
  429.             // Suppression du produit du panier
  430.             $this->remove($product);
  431.         }
  432.         return $productValide;
  433.     }
  434. }