|
- <?php
-
- namespace Lc\ShopBundle\Services\Order;
-
- use App\Entity\OrderProductReductionCatalog;
- use App\Entity\OrderShop;
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\ShopBundle\Context\DocumentInterface;
- use Lc\ShopBundle\Context\MerchantUtilsInterface;
- use Lc\ShopBundle\Context\OrderPaymentInterface;
- use Lc\ShopBundle\Context\OrderReductionCartInterface;
- use Lc\ShopBundle\Context\OrderProductInterface;
- use Lc\ShopBundle\Context\OrderReductionCreditInterface;
- use Lc\ShopBundle\Context\OrderShopInterface;
- use Lc\ShopBundle\Context\OrderStatusHistoryInterface;
- use Lc\ShopBundle\Context\OrderStatusInterface;
- use Lc\ShopBundle\Context\PriceUtilsInterface;
- use Lc\ShopBundle\Context\ProductFamilyUtilsInterface;
- use Lc\ShopBundle\Context\ReductionCartInterface;
- use Lc\ShopBundle\Context\ReductionCreditInterface;
- use Lc\ShopBundle\Context\UserInterface;
- use Lc\ShopBundle\Model\Document;
- use Lc\ShopBundle\Form\Backend\Order\OrderReductionCreditType;
- use Lc\ShopBundle\Model\Product;
- use Lc\ShopBundle\Model\ProductFamily;
- use Lc\ShopBundle\Services\CreditUtils;
- use Lc\ShopBundle\Services\DocumentUtils;
- use Lc\ShopBundle\Services\Price\OrderShopPriceUtils;
- use Lc\ShopBundle\Services\UserUtils;
- use Lc\ShopBundle\Services\Utils;
- use Symfony\Component\HttpFoundation\Session\SessionInterface;
- use Symfony\Component\Security\Core\Security;
-
- class OrderUtils
- {
- use OrderUtilsReductionTrait;
-
- protected $em;
- protected $security;
- protected $userUtils;
- protected $merchantUtils;
- protected $orderShopRepo;
- protected $reductionCreditRepo ;
- protected $orderReductionCreditRepo ;
- protected $priceUtils;
- protected $productFamilyUtils;
- protected $documentUtils;
- protected $utils;
- protected $creditUtils;
-
- public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils,
- MerchantUtilsInterface $merchantUtils, PriceUtilsInterface $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils,
- DocumentUtils $documentUtils, Utils $utils, CreditUtils $creditUtils)
- {
- $this->em = $em;
- $this->security = $security;
- $this->userUtils = $userUtils;
- $this->merchantUtils = $merchantUtils;
- $this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName());
- $this->reductionCreditRepo = $this->em->getRepository($this->em->getClassMetadata(ReductionCreditInterface::class)->getName());
- $this->orderReductionCreditRepo = $this->em->getRepository($this->em->getClassMetadata(OrderReductionCreditInterface::class)->getName());
- $this->priceUtils = $priceUtils;
- $this->productFamilyUtils = $productFamilyUtils;
- $this->documentUtils = $documentUtils;
- $this->utils = $utils;
- $this->creditUtils = $creditUtils;
- }
-
-
- public function getCartCurrent()
- {
- $paramsSearchOrderShop = [];
-
- $user = $this->security->getUser();
- $visitor = $this->userUtils->getVisitorCurrent();
-
- $orderShop = null;
- $orderShopUser = null;
- $orderShopVisitor = null;
-
- if ($user) {
- $orderShopUser = $this->orderShopRepo->findCartCurrent([
- 'user' => $user
- ]);
- }
-
- if ($visitor) {
- $orderShopVisitor = $this->orderShopRepo->findCartCurrent([
- 'visitor' => $visitor
- ]);
- }
-
- if ($orderShopUser || $orderShopVisitor) {
-
- if ($orderShopUser && $orderShopVisitor && $orderShopUser != $orderShopVisitor
- && $orderShopVisitor->getOrderProducts() && count($orderShopVisitor->getOrderProducts())) {
- $orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor);
- $this->utils->addFlash('success', "Votre panier visiteur vient d'être fusionné avec votre panier client.");
- } else {
- $orderShop = ($orderShopUser) ? $orderShopUser : $orderShopVisitor;
- }
-
- if ($orderShop && $user && !$orderShop->getUser()) {
- $orderShop->setUser($user);
- $this->em->persist($orderShop);
- $this->em->flush();
- }
- }
-
- return $orderShop;
- }
-
- public function createOrderShop($params)
- {
- $orderShop = new OrderShop();
-
- $orderShopBelongTo = false;
- if (isset($params['user']) && $params['user']) {
- $orderShopBelongTo = true;
- $orderShop->setUser($params['user']);
- }
- if (isset($params['visitor']) && $params['visitor']) {
- $orderShopBelongTo = true;
- $orderShop->setVisitor($params['visitor']);
- }
-
- if (!$orderShopBelongTo) {
- throw new \ErrorException('La commande doit être liée à un utilisateur ou à un visiteur.');
- }
-
- if (isset($params['merchant']) && $params['merchant']) {
- $orderShop->setMerchant($params['merchant']);
- } else {
- throw new \ErrorException('La commande doit être liée à un merchant.');
- }
-
- $orderShop = $this->changeOrderStatus('cart', $orderShop);
-
- return $orderShop;
- }
-
- public function addOrderProduct($orderShop, $orderProductAdd, $persist = true)
- {
- $return = false;
-
- $user = $this->security->getUser();
- $visitor = $this->userUtils->getVisitorCurrent();
-
- if (!$orderShop) {
- $orderShop = $this->createOrderShop([
- 'user' => $user,
- 'visitor' => $visitor,
- 'merchant' => $this->merchantUtils->getMerchantCurrent()
- ]);
- }
-
- if($this->isOrderProductAvailableAddCart($orderProductAdd, $orderShop)) {
- if ($orderProductAdd->getQuantityOrder() > 0) {
- $updated = false;
-
- $orderProductAdd->setTitle($orderProductAdd->getTitleOrderShop());
- $orderProductAdd->setPrice($this->priceUtils->getPrice($orderProductAdd->getProduct()));
- $orderProductAdd->setUnit($orderProductAdd->getProduct()->getUnitInherited());
- $orderProductAdd->setTaxRate($orderProductAdd->getProduct()->getTaxRateInherited());
- $orderProductAdd->setQuantityProduct($orderProductAdd->getProduct()->getQuantityInherited());
-
- $productFamily = $this->productFamilyUtils->getProductFamilyBySlug($orderProductAdd->getProduct()->getProductFamily()->getSlug());
- $reductionCatalog = $productFamily->getReductionCatalog();
- if ($reductionCatalog) {
- $orderProductReductionCatalog = new OrderProductReductionCatalog();
- $orderProductReductionCatalog->setTitle($reductionCatalog->getTitle());
- $orderProductReductionCatalog->setValue($reductionCatalog->getValue());
- $orderProductReductionCatalog->setUnit($reductionCatalog->getUnit());
- $orderProductReductionCatalog->setBehaviorTaxRate($reductionCatalog->getBehaviorTaxRate());
-
- $orderProductAdd->setOrderProductReductionCatalog($orderProductReductionCatalog);
- }
-
- foreach ($orderShop->getOrderProducts() as $orderProduct) {
- if ($orderProduct->getProduct()->getId() == $orderProductAdd->getProduct()->getId()
- && (string)$this->priceUtils->getPrice($orderProduct) == (string)$this->priceUtils->getPrice($orderProductAdd)
- && $this->compareOrderProductReductionCatalog($orderProduct->getOrderProductReductionCatalog(), $orderProductAdd->getOrderProductReductionCatalog())) {
-
- $orderProduct->setQuantityOrder($orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder());
-
- if ($persist) {
- $this->em->persist($orderProduct);
- }
-
- $updated = true;
- $return = true;
-
- break;
- }
- }
-
- if (!$updated) {
- $orderShop->addOrderProduct($orderProductAdd);
-
- if (isset($orderProductReductionCatalog)) {
- $this->em->persist($orderProductReductionCatalog);
-
- if ($persist) {
- if (isset($orderProductReductionCatalog)) {
- $this->em->persist($orderProductReductionCatalog);
- }
- $this->em->persist($orderProductAdd);
- $this->em->persist($orderShop);
- }
- }
-
- $return = true;
- }
-
- if ($persist) {
- $this->em->flush();
- }
- }
- }
- else {
- $availableQuantity = $orderProductAdd->getProduct()->getAvailableQuantityInherited() ;
- $textError = "Le produit <strong>".$orderProductAdd->getTitleOrderShop()."</strong> n'est pas disponible" ;
- if($availableQuantity !== false && $availableQuantity > 0) {
- $unit = '' ;
- if($orderProductAdd->getProduct()->getProductFamily()->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
- $unit = $orderProductAdd->getProduct()->getUnitInherited()->getUnit() ;
- }
- $textError .= ' dans cette quantité ' ;
- $textError .= '<br />'.$availableQuantity.$unit.' disponible(s) dont '.$this->getQuantityOrderByProduct($orderShop, $orderProductAdd->getProduct()).$unit.' déjà dans votre panier.' ;
- }
- $this->utils->addFlash('error', $textError);
- }
-
- return $return ;
- }
-
- public function countQuantities($orderShop)
- {
- return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
- }
-
- public function countQuantitiesByOrderProducts($orderProducts = [])
- {
- $count = 0;
-
- foreach ($orderProducts as $orderProduct) {
- $count += $orderProduct->getQuantityOrder();
- }
-
- return $count;
- }
-
- public function getOrderProductsByParentCategory($orderShop = null)
- {
- $categoriesArray = [];
- if ($orderShop) {
- foreach ($orderShop->getOrderProducts() as $orderProduct) {
- $productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
- $category = $productCategories[0]->getParentCategory();
- $labelCategory = $category->getTitle();
- if (!isset($categoriesArray[$labelCategory])) {
- $categoriesArray[$labelCategory] = [];
- }
- $categoriesArray[$labelCategory][] = $orderProduct;
- }
- }
-
- return $categoriesArray;
- }
-
- public function getOrderDatas($order = null)
- {
- $data = [];
- if (!$order) {
- $order = $this->getCartCurrent();
- }
- $data['order'] = $order;
-
- if ($order) {
- $data['count'] = $this->countQuantities($order);
- $data['total_with_tax'] = $this->priceUtils->getTotalWithTax($order);
- $data['order_products_by_category'] = $this->getOrderProductsByParentCategory($order);
- }
- return $data;
- }
-
-
- public function getOrderAsJsonObject(OrderShopInterface $order)
- {
- $data['id'] = $order->getId();
- $data['user'] = $order->getUser()->getSummary();
- $data['orderStatus'] = $order->getOrderStatus()->__tosString();
- $data['deliveryAddress'] = $order->getDeliveryAddress()->getSummary();
- $data['invoiceAddress'] = $order->getInvoiceAddress()->getSummary();
- $data['total'] = $this->priceUtils->getTotal($order);
- $data['totalWithTax'] = $this->priceUtils->getTotalWithTax($order);
- $data['totalWithTaxAndReduction'] = $this->priceUtils->getTotalWithTax($order);
- $i = 0;
- foreach ($this->getOrderProductsByParentCategory($order) as $labelCategory => $orderProducts) {
-
- foreach ($orderProducts as $orderProduct) {
- $data['orderProducts'][$i]['id'] = $orderProduct->getId();
- $data['orderProducts'][$i]['product'] = $orderProduct->getProduct()->getId();
- $data['orderProducts'][$i]['quantityOrder'] = $orderProduct->getQuantityOrder();
- $data['orderProducts'][$i]['labelCategory'] = $labelCategory;
- $data['orderProducts'][$i]['title'] = $orderProduct->getTitle();
- $data['orderProducts'][$i]['price'] = $this->priceUtils->getPrice($orderProduct);
- $data['orderProducts'][$i]['priceWithTax'] = $this->priceUtils->getPriceWithTax($orderProduct);
- $data['orderProducts'][$i]['priceWithTaxAndReduction'] = $this->priceUtils->getPriceWithTaxAndReduction($orderProduct);
- $data['orderProducts'][$i]['quantity'] = $orderProduct->getQuantityOrder();
- $data['orderProducts'][$i]['totalWithTaxAndReduction'] = $this->priceUtils->getTotalOrderProductsWithTaxAndReduction(array($orderProduct));
- $i++;
- }
- }
-
- return $data;
- }
-
-
- public function newOrderStatusHistory($order, $status, $origin = 'user')
- {
- $orderStatusHistoryClass = $this->em->getClassMetadata(OrderStatusHistoryInterface::class);
- $orderStatusHistory = new $orderStatusHistoryClass->name;
- $orderStatusHistory->setOrderShop($order);
- $orderStatusHistory->setOrderStatus($status);
- $orderStatusHistory->setOrigin($origin);
- $this->em->persist($orderStatusHistory);
- }
-
-
- public function mergeOrderShops($orderShop1, $orderShop2)
- {
- if ($orderShop1 && $orderShop2) {
-
- foreach ($orderShop2->getOrderProducts() as $orderProduct) {
- $this->addOrderProduct($orderShop1, $orderProduct);
- $this->em->remove($orderProduct);
- }
-
- $this->em->remove($orderShop2);
- $this->em->persist($orderShop1);
- $this->em->flush();
-
- return $orderShop1;
- }
- }
-
- public function createDocumentInvoice(OrderShopInterface $orderShop)
- {
- $merchantAddress = $orderShop->getMerchant()->getAddress();
- $buyerAddress = $orderShop->getInvoiceAddress();
-
- $document = $this->documentUtils->createDocument([
- 'type' => Document::TYPE_INVOICE,
- 'title' => '',
- 'status' => 1,
- 'order_shops' => [$orderShop],
- 'merchant_address' => $merchantAddress,
- 'buyer_address' => $buyerAddress,
- 'created_by' => $orderShop->getUser()
- ]);
-
- return $document;
- }
-
- public function groupOrderProductsByProductFamily($orderProducts)
- {
- $orderProductsByProductFamily = [];
- foreach ($orderProducts as $orderProduct) {
- if ($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {
- $productFamily = $orderProduct->getProduct()->getProductFamily();
- if (!isset($orderProductsByProductFamily[$productFamily->getId()])) {
- $orderProductsByProductFamily[$productFamily->getId()] = [
- 'order_products' => [],
- 'total_quantity_weight' => 0,
- ];
- }
- $orderProductsByProductFamily[$productFamily->getId()]['order_products'][] = $orderProduct;
- $orderProductsByProductFamily[$productFamily->getId()]['total_quantity_weight'] += ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient()) * $orderProduct->getQuantityOrder();
- }
- }
-
- return $orderProductsByProductFamily;
- }
-
- public function createOrderPayment($orderShop, $meanPayment, $amount, $reference = null, $comment = null, $paidAt = null)
- {
- $classOrderPayment = $this->em->getClassMetadata(OrderPaymentInterface::class)->getName();
- $orderPayment = new $classOrderPayment;
-
- $orderPayment->setOrderShop($orderShop);
- $orderPayment->setMeanPayment($meanPayment);
- $orderPayment->setAmount($amount);
- $orderPayment->setReference($reference);
- $orderPayment->setComment($comment);
- $orderPayment->setEditable(false);
-
- if ($paidAt) {
- $orderPayment->setPaidAt($paidAt);
- } else {
- $orderPayment->setPaidAt(new \DateTime('now'));
- }
-
- $this->em->persist($orderPayment);
- $this->em->flush();
-
- return $orderPayment ;
- }
-
- public function isOrderPaid($order)
- {
- if ($this->getTotalOrderPayments($order) >= $this->priceUtils->getTotalWithTax($order)) {
- return true;
- } else {
- return false;
- }
- }
-
- public function getTotalOrderPayments($order): float
- {
- $totalAmount = floatval(0);
- foreach ($order->getOrderPayments() as $orderPayment) {
- $totalAmount = $orderPayment->getAmount() + $totalAmount;
- }
- return $totalAmount;
- }
-
- public function deductAvailabilityProduct(\Lc\ShopBundle\Model\OrderShop $orderShop)
- {
- foreach ($orderShop->getOrderProducts() as $orderProduct) {
- switch ($orderProduct->getProduct()->getProductFamily()->getBehaviorCountStock()) {
- case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE :
-
-
- $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
- $newAvailability = $oldAvailability - ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient());
- $orderProduct->getProduct()->getProductFamily()->setAvailableQuantity($newAvailability);
-
- $this->em->persist($orderProduct->getProduct()->getProductFamily());
-
- break;
- case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY :
-
- $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
- $newAvailability = $oldAvailability - $orderProduct->getQuantityOrder();
- $orderProduct->getProduct()->getProductFamily()->setAvailableQuantity($newAvailability);
-
- $this->em->persist($orderProduct->getProduct()->getProductFamily());
-
- break;
- case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT :
- $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
- $newAvailability = $oldAvailability - $orderProduct->getQuantityOrder();
- $orderProduct->getProduct()->setAvailableQuantity($newAvailability);
-
- $this->em->persist($orderProduct->getProduct());
-
- break;
- }
-
- $this->em->flush();
- }
- }
-
- public function isCartAllowToBeOrder($order){
- return true;
- }
-
- public function getReductionCreditsAvailableByUser($user)
- {
- $reductionCredits = $this->reductionCreditRepo->findReductionCreditsByUser($user) ;
-
- $reductionCreditsArray = [] ;
- foreach($reductionCredits as $reductionCredit) {
- if(!$this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $user)) {
- $reductionCreditsArray[] = $reductionCredit ;
- }
- }
-
- return $reductionCreditsArray ;
- }
-
- public function isReductionCreditAddedToOrder($orderShop, $reductionCredit)
- {
- foreach($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
- if($orderReductionCredit->getReductionCredit() == $reductionCredit) {
- return true ;
- }
- }
-
- return false ;
- }
-
- public function isProductAvailable(Product $product, $quantityOrder = 0, $checkCart = false, $orderShop = null)
- {
- if(!$orderShop) {
- $orderShop = $this->getCartCurrent() ;
- }
- $productFamily = $product->getProductFamily() ;
- $quantityAsked = $quantityOrder;
-
- if($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
- if(!$quantityOrder) {
- $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product, true) ;
- }
- else {
- $quantityAsked = ($product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient()) * $quantityOrder;
- }
-
- if($checkCart) {
- $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product, true) ;
- }
- }
-
- if(($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY
- || $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT)) {
-
- if(!$quantityOrder) {
- $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product) ;
- }
-
- if($checkCart) {
- $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product) ;
- }
- }
-
- if ($product->getAvailableQuantityInherited() >= $quantityAsked
- || $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_UNLIMITED) {
- return true;
- }
- else {
- return false;
- }
- }
-
- public function isOneProductAvailableAddCart($products): bool
- {
- $orderShop = $this->getCartCurrent() ;
-
- foreach($products as $product) {
- if($this->isProductAvailable($product, 1, true)) {
- return true ;
- }
- }
-
- return false ;
- }
-
- public function isOrderProductAvailable(OrderProductInterface $orderProduct)
- {
- return $this->isProductAvailable($orderProduct->getProduct(), $orderProduct->getQuantityOrder()) ;
- }
-
- public function isOrderProductAvailableAddCart(OrderProductInterface $orderProduct, $orderShop = null)
- {
- $product = $orderProduct->getProduct() ;
- return $this->isProductAvailable($product, $orderProduct->getQuantityOrder(), true, $orderShop);
- }
-
- public function getQuantityOrderByProduct($orderShop, $product, $byWeight = false)
- {
- $quantity = 0 ;
- $productFamily = $product->getProductFamily() ;
- $behaviorCountStock = $productFamily->getBehaviorCountStock() ;
-
- if($orderShop) {
- foreach($orderShop->getOrderProducts() as $orderProduct) {
- if($orderProduct->getProduct()->getId() == $product->getId()
- || ( ($behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY || $behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE)
- && $orderProduct->getProduct()->getProductFamily()->getId() == $productFamily->getId())) {
-
- if($byWeight) {
- $quantity += $orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct() / $product->getUnitInherited()->getCoefficient()) ;
- }
- else {
- $quantity += $orderProduct->getQuantityOrder() ;
- }
- }
- }
- }
-
- return $quantity ;
- }
-
- public function getProductQuantityMaxAddCart($product)
- {
- $orderShop = $this->getCartCurrent() ;
- $productFamily = $product->getProductFamily() ;
-
- $byWeight = false ;
- if($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
- $byWeight = true ;
- }
-
- return $product->getAvailableQuantityInherited() - $this->getQuantityOrderByProduct($orderShop, $product, $byWeight) ;
- }
- }
|