src/ShoppingBundle/Service/Order/OrderManager.php line 20

Open in your IDE?
  1. <?php
  2. namespace Shopping\Service\Order;
  3. use App\Entity\Shop;
  4. use Core\Entity\ErrorCode;
  5. use Core\Service\Session\Session;
  6. use Doctrine\Common\Persistence\ManagerRegistry;
  7. use Shopping\Entity\CodePromo;
  8. use Shopping\Entity\CodePromoType;
  9. use Shopping\Entity\DeliveryMethod;
  10. use Shopping\Entity\OrderStatus;
  11. use Shopping\Entity\PaymentMethod;
  12. use Shopping\Service\Tools;
  13. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  14. /**
  15.  * OrderManager - Gestion de la commande et des prix appliqués
  16.  */
  17. class OrderManager
  18. {
  19.     private $order;
  20.     private $order_session;
  21.     private $entityManager;
  22.     /**
  23.      * Constructeur
  24.      */
  25.     public function __construct(Session $sessionManagerRegistry $entityManager)
  26.     {
  27.         $this->entityManager $entityManager;
  28.         // Création de la seession de commande
  29.         $this->order_session = new OrderSession($session);
  30.         // On récupère la commande depuis la session
  31.         $this->order $this->order_session->getOrderFromSession();
  32.     }
  33.     /*
  34.      * Section générale de la commande
  35.      */
  36.     /**
  37.      * Définition de l'adresse e-mail associée à la commande
  38.      */
  39.     public function setEmail($params)
  40.     {
  41.         $this->order->setEmail($params); // Modifie l'adresse de livraison
  42.         $this->update(); // Modifie la commande en session
  43.     }
  44.     /**
  45.      * Définition du commentaire de livraison
  46.      */
  47.     public function setDeliveryComment($params)
  48.     {
  49.         $this->order->setDeliveryComment($params);
  50.         $this->update(); // Modifie la commande en session
  51.     }
  52.     /*
  53.      * Section livraison
  54.      */
  55.     /**
  56.      * Définition de la méthode de livraison
  57.      */
  58.     public function setDeliveryMethod($params)
  59.     {
  60.         $this->order->setDeliveryMethod($params); // Modifie le moyen de livraison de la commande
  61.         $this->update(); // Modifie la commande en session
  62.     }
  63.     public function getDeliveryMethod()
  64.     {
  65.         return $this->order->getdeliveryMethod();
  66.     }
  67.     /**
  68.      * Définition du magasin de livraison
  69.      */
  70.     public function setDeliveryShop($params)
  71.     {
  72.         $this->order->setDeliveryShop($params); // Modifie le moyen de livraison de la commande
  73.         $this->update(); // Modifie la commande en session
  74.     }
  75.     /**
  76.      * Définition du magasin de livraison
  77.      */
  78.     public function setAddressRelay($params)
  79.     {
  80.         $this->order->setAddressRelay($params); // Modifie le moyen de livraison de la commande
  81.         $this->update(); // Modifie la commande en session
  82.     }
  83.     /**
  84.      * Définition de l'adresse de livraison
  85.      */
  86.     public function setAddressDelivery($params)
  87.     {
  88.         $this->order->setAddressDelivery($params); // Modifie l'adresse de livraison
  89.         $this->update(); // Modifie la commande en session
  90.     }
  91.     public function getAddressDelivery()
  92.     {
  93.         return $this->order->getAddressDelivery();
  94.     }
  95.     /**
  96.      * Définition de l'adresse de facturation
  97.      */
  98.     public function setAddressInvoice($params)
  99.     {
  100.         $this->order->setAddressInvoice($params); // Modifie l'adresse de facturation
  101.         $this->update(); // Modifie la commande en session
  102.     }
  103.     public function getAddressInvoice()
  104.     {
  105.         return $this->order->getAddressInvoice();
  106.     }
  107.     /**
  108.      * Définition de la tva ht de livraison
  109.      */
  110.     public function setDeliveryHt($price_ht)
  111.     {
  112.         $this->order->setDeliveryHt($price_ht);
  113.         $this->update();
  114.     }
  115.     public function getDeliveryHt()
  116.     {
  117.         return $this->order->getDeliveryHt();
  118.     }
  119.     /**
  120.      * Définition de la tva de livraison
  121.      */
  122.     public function setDeliveryTva($chargePrice)
  123.     {
  124.         $this->order->setDeliveryTva($chargePrice);
  125.         $this->update();
  126.     }
  127.     public function getDeliveryTva()
  128.     {
  129.         return $this->order->getDeliveryTva();
  130.     }
  131.     /**
  132.      *
  133.      */
  134.     public function setDeliveryTaxName($delivery_tax_name)
  135.     {
  136.         $this->order->setDeliveryTaxName($delivery_tax_name);
  137.         $this->update();
  138.     }
  139.     /**
  140.      *
  141.      */
  142.     public function setDeliveryTaxRate($delivery_tax_rate)
  143.     {
  144.         $this->order->setDeliveryTaxRate($delivery_tax_rate);
  145.         $this->update();
  146.     }
  147.     /**
  148.      * Définition du prix de livraison TTC
  149.      */
  150.     public function setDeliveryPrice($chargePrice)
  151.     {
  152.         $this->order->setDeliveryPrice($chargePrice);
  153.         $this->update();
  154.     }
  155.     public function getDeliveryPrice()
  156.     {
  157.         return $this->order->getDeliveryPrice();
  158.     }
  159.     /**
  160.      *
  161.      */
  162.     public function setDeliveryFree($delivery_free)
  163.     {
  164.         $this->order->setDeliveryFree($delivery_free);
  165.     }
  166.     /*
  167.      * Section paiements
  168.      */
  169.     /**
  170.      * Définition de la méthode de paiement
  171.      */
  172.     public function setPaymentMethod($params)
  173.     {
  174.         $this->order->setPaymentMethod($params); // Modifie le moyen de paiement de la commande
  175.         $this->update(); // Modifie la commande en session
  176.     }
  177.     public function getPaymentMethod()
  178.     {
  179.         return $this->order->getPaymentMethod();
  180.     }
  181.     /**
  182.      * Définition du status du paiement - Paiement validée
  183.      */
  184.     public function setValidatedPayment()
  185.     {
  186.         $order $this->order->getOrder();
  187.         $order->setStatus(OrderStatus::VALIDATED_PAYMENT);
  188.         $this->entityManager->flush();
  189.     }
  190.     /*
  191.      * Prix des produits de la commande
  192.      */
  193.     /**
  194.      * Définition du prix HT des produits
  195.      */
  196.     public function setProductsTotalHt($subTotalHT)
  197.     {
  198.         $this->order->setSubTotalHt($subTotalHT);
  199.         $this->order_session->persist($this->order); // Modifie la commande en session
  200.     }
  201.     /**
  202.      * Définition du montant TVA des produits
  203.      */
  204.     public function setProductsTotalTva($subTotalTva)
  205.     {
  206.         $this->order->setSubTotalTva($subTotalTva);
  207.         $this->update();
  208.     }
  209.     /**
  210.      * Définition du montant TTC des produits
  211.      */
  212.     public function setProductsTotalTtc($amount)
  213.     {
  214.         $this->order->setAmountTotalTTC($amount);
  215.         $this->update();
  216.     }
  217.     /*
  218.      * Caractéristiques globales de la commandes
  219.      */
  220.     /**
  221.      * Définition du poids total de la commande
  222.      */
  223.     public function setOrderTotalWeight($weight)
  224.     {
  225.         $this->order->setOrderTotalWeight($weight);
  226.         $this->update();
  227.     }
  228.     /**
  229.      * Définition du nombre de produit total de la commande
  230.      */
  231.     public function setOrderTotalCount($count)
  232.     {
  233.         $this->order->setOrderTotalCount($count);
  234.         $this->update();
  235.     }
  236.     /*
  237.      * Prix globales de la commandes
  238.      */
  239.     /**
  240.      * Retourne le prix globale total hors taxe de la commande
  241.      */
  242.     public function getOrderTotalHt()
  243.     {
  244.         $order_total_ht $this->updateOrderTotalHT();
  245.         $this->update();
  246.         return Tools::formatPrice($order_total_ht);
  247.     }
  248.     /**
  249.      * Retourne la tva globale total de la commande
  250.      */
  251.     public function getOrderTotalTva()
  252.     {
  253.         $order_total_tva $this->updateOrderTotalTVA();
  254.         $this->update();
  255.         return Tools::formatPrice($order_total_tva);
  256.     }
  257.     /**
  258.      * Retourne le prix globale total de la commande
  259.      */
  260.     public function getOrderTotalTtc()
  261.     {
  262.         $order_total_ttc $this->updateOrderTotalTTC();
  263.         $this->update();
  264.         return Tools::formatPrice($order_total_ttc);
  265.     }
  266.     /*
  267.     *  Définition des variables fixes à sauvegarder après le passage de la commande pour la sauvegarde
  268.     */
  269.     /**
  270.      * Calcule et mise à jours du montant globale HT de la commande
  271.      */
  272.     public function updateOrderTotalHT()
  273.     {
  274.         $order $this->order->getOrder();
  275.         // Définitions
  276.         $products_total_ht $order->getSubTotalHt();                   // Montant total HT des produits
  277.         $delivery_ht $order->getDeliveryHt();                         // Montant total HT de la lirvaison
  278.         if ($products_total_ht === null || $delivery_ht === null) {
  279.             return null;
  280.         }
  281.         // Calcul
  282.         $order_total_ht $products_total_ht $delivery_ht;
  283.         // Sauvegarde
  284.         $order->setOrderTotalHt($order_total_ht);
  285.         return $order_total_ht;
  286.     }
  287.     /**
  288.      * Calcule et mise à jours du montant globale TVA de la commande
  289.      */
  290.     public function updateOrderTotalTVA()
  291.     {
  292.         $order $this->order->getOrder();
  293.         // Définitions
  294.         $products_total_tva $order->getSubTotalTva();                  // Montant total de la TVA des produits
  295.         $delivery_tva $order->getDeliveryTva();                        // Montant total de la TVA de la livraison
  296.         $products_discount_tva 0;
  297.         $delivery_discounts_tva 0;
  298.         if ($products_total_tva === null || $delivery_tva === null) {
  299.             return null;
  300.         }
  301.         // Calcul
  302.         $global_total_tva = ($products_total_tva $products_discount_tva) + ($delivery_tva $delivery_discounts_tva);
  303.         // Sauvegarde
  304.         $order->setOrderTotalTva($global_total_tva);
  305.         return $global_total_tva;
  306.     }
  307.     /**
  308.      * Calcule et mise à jours du montant globale TTC de la commande
  309.      */
  310.     public function updateOrderTotalTTC()
  311.     {
  312.         $order $this->order->getOrder();
  313.         // Définitions
  314.         $products_total_ttc $order->getAmountTotalTtc();              // Montant total TTC des produits
  315.         $delivery_ttc $order->getDeliveryPrice();                     // Montant total TTC de la livraison
  316.         $discount_ttc 0;                     // Montant total TTC des réductions
  317.         if ($products_total_ttc === null || $delivery_ttc === null) {
  318.             return null;
  319.         }
  320.         // Calcul
  321.         $global_total_ttc $products_total_ttc $delivery_ttc $discount_ttc;
  322.         // Evite les réduction supérieur au prix du produit
  323.         if ($global_total_ttc 0) {
  324.             $global_total_ttc 0;
  325.         }
  326.         // Sauvegarde
  327.         $order->setOrderTotalTtc($global_total_ttc);
  328.         return $global_total_ttc;
  329.     }
  330.     /*
  331.      * Récupération de la commande sous divers formats
  332.      */
  333.     /**
  334.      * Retourne la commande sous le format object
  335.      */
  336.     public function getObjectOrder()
  337.     {
  338.         $order $this->order->getOrder();
  339.         if ($order->getPaymentMethod() != null) {
  340.             $payment $this->entityManager->getRepository(PaymentMethod::class)->find($order->getPaymentMethod());
  341.             $order->setPaymentMethod($payment);
  342.         }
  343.         if ($order->getDeliveryMethod() != null) {
  344.             $delivery $this->entityManager->getRepository(DeliveryMethod::class)->find($order->getDeliveryMethod());
  345.             $order->setDeliveryMethod($delivery);
  346.         }
  347.         if ($order->getDeliveryShop() != null) {
  348.             $shop $this->entityManager->getRepository(Shop::class)->find($order->getDeliveryShop());
  349.             $order->setDeliveryShop($shop);
  350.         }
  351.         return $order;
  352.     }
  353.     /**
  354.      * Retourne la commande sous le format sérialisée
  355.      */
  356.     public function getOrder()
  357.     {
  358.         return $this->order->serialize();
  359.     }
  360.     /*
  361.      * Modification de la commande en session
  362.      */
  363.     /**
  364.      * Mise à jours de la commande en session
  365.      */
  366.     public function update()
  367.     {
  368.         $this->order_session->persist($this->order); // Modification en session de la commande en cours
  369.     }
  370.     /**
  371.      * Réinitialisation de la commande en session
  372.      */
  373.     public function clear()
  374.     {
  375.         $this->order = new \Shopping\Entity\Order(); // Réinitialise la commannde
  376.         $this->order_session->clear(); // Réinitialize la commande en session
  377.     }
  378. }