<?phpnamespace Shopping\Service\Order;use App\Entity\Shop;use Core\Entity\ErrorCode;use Core\Service\Session\Session;use Doctrine\Common\Persistence\ManagerRegistry;use Shopping\Entity\CodePromo;use Shopping\Entity\CodePromoType;use Shopping\Entity\DeliveryMethod;use Shopping\Entity\OrderStatus;use Shopping\Entity\PaymentMethod;use Shopping\Service\Tools;use Symfony\Component\HttpFoundation\Session\SessionInterface;/** * OrderManager - Gestion de la commande et des prix appliqués */class OrderManager{ private $order; private $order_session; private $entityManager; /** * Constructeur */ public function __construct(Session $session, ManagerRegistry $entityManager) { $this->entityManager = $entityManager; // Création de la seession de commande $this->order_session = new OrderSession($session); // On récupère la commande depuis la session $this->order = $this->order_session->getOrderFromSession(); } /* * Section générale de la commande */ /** * Définition de l'adresse e-mail associée à la commande */ public function setEmail($params) { $this->order->setEmail($params); // Modifie l'adresse de livraison $this->update(); // Modifie la commande en session } /** * Définition du commentaire de livraison */ public function setDeliveryComment($params) { $this->order->setDeliveryComment($params); $this->update(); // Modifie la commande en session } /* * Section livraison */ /** * Définition de la méthode de livraison */ public function setDeliveryMethod($params) { $this->order->setDeliveryMethod($params); // Modifie le moyen de livraison de la commande $this->update(); // Modifie la commande en session } public function getDeliveryMethod() { return $this->order->getdeliveryMethod(); } /** * Définition du magasin de livraison */ public function setDeliveryShop($params) { $this->order->setDeliveryShop($params); // Modifie le moyen de livraison de la commande $this->update(); // Modifie la commande en session } /** * Définition du magasin de livraison */ public function setAddressRelay($params) { $this->order->setAddressRelay($params); // Modifie le moyen de livraison de la commande $this->update(); // Modifie la commande en session } /** * Définition de l'adresse de livraison */ public function setAddressDelivery($params) { $this->order->setAddressDelivery($params); // Modifie l'adresse de livraison $this->update(); // Modifie la commande en session } public function getAddressDelivery() { return $this->order->getAddressDelivery(); } /** * Définition de l'adresse de facturation */ public function setAddressInvoice($params) { $this->order->setAddressInvoice($params); // Modifie l'adresse de facturation $this->update(); // Modifie la commande en session } public function getAddressInvoice() { return $this->order->getAddressInvoice(); } /** * Définition de la tva ht de livraison */ public function setDeliveryHt($price_ht) { $this->order->setDeliveryHt($price_ht); $this->update(); } public function getDeliveryHt() { return $this->order->getDeliveryHt(); } /** * Définition de la tva de livraison */ public function setDeliveryTva($chargePrice) { $this->order->setDeliveryTva($chargePrice); $this->update(); } public function getDeliveryTva() { return $this->order->getDeliveryTva(); } /** * */ public function setDeliveryTaxName($delivery_tax_name) { $this->order->setDeliveryTaxName($delivery_tax_name); $this->update(); } /** * */ public function setDeliveryTaxRate($delivery_tax_rate) { $this->order->setDeliveryTaxRate($delivery_tax_rate); $this->update(); } /** * Définition du prix de livraison TTC */ public function setDeliveryPrice($chargePrice) { $this->order->setDeliveryPrice($chargePrice); $this->update(); } public function getDeliveryPrice() { return $this->order->getDeliveryPrice(); } /** * */ public function setDeliveryFree($delivery_free) { $this->order->setDeliveryFree($delivery_free); } /* * Section paiements */ /** * Définition de la méthode de paiement */ public function setPaymentMethod($params) { $this->order->setPaymentMethod($params); // Modifie le moyen de paiement de la commande $this->update(); // Modifie la commande en session } public function getPaymentMethod() { return $this->order->getPaymentMethod(); } /** * Définition du status du paiement - Paiement validée */ public function setValidatedPayment() { $order = $this->order->getOrder(); $order->setStatus(OrderStatus::VALIDATED_PAYMENT); $this->entityManager->flush(); } /* * Prix des produits de la commande */ /** * Définition du prix HT des produits */ public function setProductsTotalHt($subTotalHT) { $this->order->setSubTotalHt($subTotalHT); $this->order_session->persist($this->order); // Modifie la commande en session } /** * Définition du montant TVA des produits */ public function setProductsTotalTva($subTotalTva) { $this->order->setSubTotalTva($subTotalTva); $this->update(); } /** * Définition du montant TTC des produits */ public function setProductsTotalTtc($amount) { $this->order->setAmountTotalTTC($amount); $this->update(); } /* * Caractéristiques globales de la commandes */ /** * Définition du poids total de la commande */ public function setOrderTotalWeight($weight) { $this->order->setOrderTotalWeight($weight); $this->update(); } /** * Définition du nombre de produit total de la commande */ public function setOrderTotalCount($count) { $this->order->setOrderTotalCount($count); $this->update(); } /* * Prix globales de la commandes */ /** * Retourne le prix globale total hors taxe de la commande */ public function getOrderTotalHt() { $order_total_ht = $this->updateOrderTotalHT(); $this->update(); return Tools::formatPrice($order_total_ht); } /** * Retourne la tva globale total de la commande */ public function getOrderTotalTva() { $order_total_tva = $this->updateOrderTotalTVA(); $this->update(); return Tools::formatPrice($order_total_tva); } /** * Retourne le prix globale total de la commande */ public function getOrderTotalTtc() { $order_total_ttc = $this->updateOrderTotalTTC(); $this->update(); return Tools::formatPrice($order_total_ttc); } /* * Définition des variables fixes à sauvegarder après le passage de la commande pour la sauvegarde */ /** * Calcule et mise à jours du montant globale HT de la commande */ public function updateOrderTotalHT() { $order = $this->order->getOrder(); // Définitions $products_total_ht = $order->getSubTotalHt(); // Montant total HT des produits $delivery_ht = $order->getDeliveryHt(); // Montant total HT de la lirvaison if ($products_total_ht === null || $delivery_ht === null) { return null; } // Calcul $order_total_ht = $products_total_ht + $delivery_ht; // Sauvegarde $order->setOrderTotalHt($order_total_ht); return $order_total_ht; } /** * Calcule et mise à jours du montant globale TVA de la commande */ public function updateOrderTotalTVA() { $order = $this->order->getOrder(); // Définitions $products_total_tva = $order->getSubTotalTva(); // Montant total de la TVA des produits $delivery_tva = $order->getDeliveryTva(); // Montant total de la TVA de la livraison $products_discount_tva = 0; $delivery_discounts_tva = 0; if ($products_total_tva === null || $delivery_tva === null) { return null; } // Calcul $global_total_tva = ($products_total_tva - $products_discount_tva) + ($delivery_tva - $delivery_discounts_tva); // Sauvegarde $order->setOrderTotalTva($global_total_tva); return $global_total_tva; } /** * Calcule et mise à jours du montant globale TTC de la commande */ public function updateOrderTotalTTC() { $order = $this->order->getOrder(); // Définitions $products_total_ttc = $order->getAmountTotalTtc(); // Montant total TTC des produits $delivery_ttc = $order->getDeliveryPrice(); // Montant total TTC de la livraison $discount_ttc = 0; // Montant total TTC des réductions if ($products_total_ttc === null || $delivery_ttc === null) { return null; } // Calcul $global_total_ttc = $products_total_ttc + $delivery_ttc - $discount_ttc; // Evite les réduction supérieur au prix du produit if ($global_total_ttc < 0) { $global_total_ttc = 0; } // Sauvegarde $order->setOrderTotalTtc($global_total_ttc); return $global_total_ttc; } /* * Récupération de la commande sous divers formats */ /** * Retourne la commande sous le format object */ public function getObjectOrder() { $order = $this->order->getOrder(); if ($order->getPaymentMethod() != null) { $payment = $this->entityManager->getRepository(PaymentMethod::class)->find($order->getPaymentMethod()); $order->setPaymentMethod($payment); } if ($order->getDeliveryMethod() != null) { $delivery = $this->entityManager->getRepository(DeliveryMethod::class)->find($order->getDeliveryMethod()); $order->setDeliveryMethod($delivery); } if ($order->getDeliveryShop() != null) { $shop = $this->entityManager->getRepository(Shop::class)->find($order->getDeliveryShop()); $order->setDeliveryShop($shop); } return $order; } /** * Retourne la commande sous le format sérialisée */ public function getOrder() { return $this->order->serialize(); } /* * Modification de la commande en session */ /** * Mise à jours de la commande en session */ public function update() { $this->order_session->persist($this->order); // Modification en session de la commande en cours } /** * Réinitialisation de la commande en session */ public function clear() { $this->order = new \Shopping\Entity\Order(); // Réinitialise la commannde $this->order_session->clear(); // Réinitialize la commande en session }}