|
|
@@ -6,9 +6,11 @@ use App\Entity\OrderProductReductionCatalog; |
|
|
|
use App\Entity\OrderShop; |
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
use Lc\ShopBundle\Context\MerchantUtilsInterface; |
|
|
|
use Lc\ShopBundle\Context\OrderProductInterface; |
|
|
|
use Lc\ShopBundle\Context\OrderShopInterface; |
|
|
|
use Lc\ShopBundle\Context\ProductFamilyUtilsInterface; |
|
|
|
use Lc\ShopBundle\Context\UserInterface; |
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
|
|
|
use Symfony\Component\Security\Core\Security; |
|
|
|
|
|
|
|
class OrderUtils |
|
|
@@ -20,9 +22,11 @@ class OrderUtils |
|
|
|
protected $orderShopRepo; |
|
|
|
protected $priceUtils ; |
|
|
|
protected $productFamilyUtils ; |
|
|
|
protected $session ; |
|
|
|
|
|
|
|
public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils, |
|
|
|
MerchantUtilsInterface $merchantUtils, PriceUtils $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils) |
|
|
|
MerchantUtilsInterface $merchantUtils, PriceUtils $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils, |
|
|
|
SessionInterface $session) |
|
|
|
{ |
|
|
|
$this->em = $em; |
|
|
|
$this->security = $security; |
|
|
@@ -31,6 +35,7 @@ class OrderUtils |
|
|
|
$this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName()); |
|
|
|
$this->priceUtils = $priceUtils ; |
|
|
|
$this->productFamilyUtils = $productFamilyUtils ; |
|
|
|
$this->session = $session ; |
|
|
|
} |
|
|
|
|
|
|
|
public function getCartCurrent() |
|
|
@@ -40,22 +45,44 @@ class OrderUtils |
|
|
|
$user = $this->security->getUser(); |
|
|
|
$visitor = $this->userUtils->getVisitorCurrent(); |
|
|
|
|
|
|
|
$orderShop = null ; |
|
|
|
$orderShopUser = null ; |
|
|
|
$orderShopVisitor = null ; |
|
|
|
|
|
|
|
if ($user) { |
|
|
|
$paramsSearchOrderShop['user'] = $user; |
|
|
|
$orderShopUser = $this->orderShopRepo->findCartCurrent([ |
|
|
|
'user' => $user |
|
|
|
]); |
|
|
|
} |
|
|
|
|
|
|
|
if ($visitor) { |
|
|
|
$paramsSearchOrderShop['visitor'] = $visitor; |
|
|
|
$orderShopVisitor = $this->orderShopRepo->findCartCurrent([ |
|
|
|
'visitor' => $visitor |
|
|
|
]); |
|
|
|
} |
|
|
|
|
|
|
|
$orderShop = $this->orderShopRepo->findCartCurrent($paramsSearchOrderShop); |
|
|
|
if($orderShopUser || $orderShopVisitor) { |
|
|
|
if($orderShopUser && $orderShopVisitor && $orderShopUser != $orderShopVisitor && $orderShopVisitor->getOrderProducts() && count($orderShopVisitor->getOrderProducts())) { |
|
|
|
$orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor) ; |
|
|
|
$this->session->getFlashBag()->add('success', "Votre panier visiteur vient d'être fusionné avec votre panier client.") ; |
|
|
|
} |
|
|
|
else { |
|
|
|
$orderShop = ($orderShopUser) ? $orderShopUser : $orderShopVisitor ; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (!$orderShop) { |
|
|
|
$orderShop = $this->createOrderShop([ |
|
|
|
'user' => $user, |
|
|
|
'visitor' => $visitor, |
|
|
|
'merchant' => $this->merchantUtils->getMerchantCurrent() |
|
|
|
]); |
|
|
|
if(!$user && !$visitor) |
|
|
|
{ |
|
|
|
$this->session->getFlashBag()->add('error', 'Vous devez accepter les cookies ou vous connecter pour créer un panier.') ; |
|
|
|
} |
|
|
|
else { |
|
|
|
if (!$orderShop) { |
|
|
|
$orderShop = $this->createOrderShop([ |
|
|
|
'user' => $user, |
|
|
|
'visitor' => $visitor, |
|
|
|
'merchant' => $this->merchantUtils->getMerchantCurrent() |
|
|
|
]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return $orderShop; |
|
|
@@ -103,7 +130,7 @@ class OrderUtils |
|
|
|
return $orderShop; |
|
|
|
} |
|
|
|
|
|
|
|
public function addOrderProduct($orderShop, $orderProductAdd) |
|
|
|
public function addOrderProduct($orderShop, $orderProductAdd, $persist = true) |
|
|
|
{ |
|
|
|
if ($orderProductAdd->getQuantityOrder() > 0) { |
|
|
|
$updated = false; |
|
|
@@ -128,37 +155,49 @@ class OrderUtils |
|
|
|
|
|
|
|
foreach($orderShop->getOrderProducts() as $orderProduct) { |
|
|
|
if ($orderProduct->getProduct()->getId() == $orderProductAdd->getProduct()->getId() |
|
|
|
&& $this->priceUtils->getPrice($orderProduct) == $this->priceUtils->getPrice($orderProductAdd) |
|
|
|
&& (string) $this->priceUtils->getPrice($orderProduct) == (string) $this->priceUtils->getPrice($orderProductAdd) |
|
|
|
&& $this->compareOrderProductReductionCatalog($orderProduct->getOrderProductReductionCatalog(), $orderProductAdd->getOrderProductReductionCatalog())) { |
|
|
|
|
|
|
|
$updated = true; |
|
|
|
$orderProduct->setQuantityOrder($orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder()); |
|
|
|
$this->em->persist($orderProduct); |
|
|
|
|
|
|
|
if($persist) { |
|
|
|
$this->em->persist($orderProduct); |
|
|
|
} |
|
|
|
|
|
|
|
$updated = 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); |
|
|
|
} |
|
|
|
|
|
|
|
$this->em->persist($orderProductAdd); |
|
|
|
$this->em->persist($orderShop); |
|
|
|
} |
|
|
|
|
|
|
|
$this->em->flush(); |
|
|
|
if($persist) { |
|
|
|
$this->em->flush(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public function compareOrderProductReductionCatalog($orderProductReductionCatalog1, $orderProductReductionCatalog2) |
|
|
|
{ |
|
|
|
return $orderProductReductionCatalog1 && $orderProductReductionCatalog2 |
|
|
|
&& $orderProductReductionCatalog1->getUnit() == $orderProductReductionCatalog2->getUnit() |
|
|
|
&& $orderProductReductionCatalog1->getValue() == $orderProductReductionCatalog2->getValue() |
|
|
|
&& $orderProductReductionCatalog1->getBehaviorTaxRate() == $orderProductReductionCatalog2->getBehaviorTaxRate() ; |
|
|
|
return (!$orderProductReductionCatalog1 && !$orderProductReductionCatalog2) |
|
|
|
|
|
|
|
|| ($orderProductReductionCatalog1 |
|
|
|
&& $orderProductReductionCatalog2 |
|
|
|
&& $orderProductReductionCatalog1->getUnit() == $orderProductReductionCatalog2->getUnit() |
|
|
|
&& (string) $orderProductReductionCatalog1->getValue() == (string) $orderProductReductionCatalog2->getValue() |
|
|
|
&& $orderProductReductionCatalog1->getBehaviorTaxRate() == $orderProductReductionCatalog2->getBehaviorTaxRate()) ; |
|
|
|
} |
|
|
|
|
|
|
|
public function countQuantities($orderShop) |
|
|
@@ -195,15 +234,17 @@ class OrderUtils |
|
|
|
|
|
|
|
public function getOrderDatas($order = null) |
|
|
|
{ |
|
|
|
$data = [] ; |
|
|
|
if(!$order) { |
|
|
|
$order = $this->getCartCurrent() ; |
|
|
|
} |
|
|
|
|
|
|
|
$data = [] ; |
|
|
|
$data['order'] = $order ; |
|
|
|
$data['count'] = $this->countQuantities($order) ; |
|
|
|
$data['total_with_tax'] = $this->priceUtils->getTotalWithTaxAndReduction($order) ; |
|
|
|
$data['order_products_by_category'] = $this->getOrderProductsByParentCategory($order) ; |
|
|
|
|
|
|
|
if($order) { |
|
|
|
$data['count'] = $this->countQuantities($order) ; |
|
|
|
$data['total_with_tax'] = $this->priceUtils->getTotalWithTaxAndReduction($order) ; |
|
|
|
$data['order_products_by_category'] = $this->getOrderProductsByParentCategory($order) ; |
|
|
|
} |
|
|
|
|
|
|
|
return $data ; |
|
|
|
} |
|
|
@@ -256,4 +297,21 @@ class OrderUtils |
|
|
|
return $text ; |
|
|
|
} |
|
|
|
|
|
|
|
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 ; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |