|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Lc\ShopBundle\Services ; |
|
|
namespace Lc\ShopBundle\Services ; |
|
|
|
|
|
|
|
|
|
|
|
use App\Entity\OrderShop; |
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
|
|
use Lc\ShopBundle\Context\MerchantUtilsInterface; |
|
|
|
|
|
use Symfony\Component\Security\Core\Security; |
|
|
|
|
|
|
|
|
class OrderUtils |
|
|
class OrderUtils |
|
|
{ |
|
|
{ |
|
|
|
|
|
protected $em; |
|
|
|
|
|
protected $security; |
|
|
|
|
|
protected $userUtils; |
|
|
|
|
|
protected $merchantUtils; |
|
|
|
|
|
private $orderShopRepo; |
|
|
|
|
|
|
|
|
|
|
|
public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils, MerchantUtilsInterface $merchantUtils) |
|
|
|
|
|
{ |
|
|
|
|
|
$this->em = $em; |
|
|
|
|
|
$this->security = $security; |
|
|
|
|
|
$this->userUtils = $userUtils; |
|
|
|
|
|
$this->merchantUtils = $merchantUtils; |
|
|
|
|
|
$this->orderShopRepo = $this->em->getRepository(OrderShop::class); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function getOrderShopCurrent() |
|
|
|
|
|
{ |
|
|
|
|
|
$paramsSearchOrderShop = []; |
|
|
|
|
|
|
|
|
|
|
|
$user = $this->security->getUser(); |
|
|
|
|
|
$visitor = $this->userUtils->getVisitorCurrent(); |
|
|
|
|
|
|
|
|
|
|
|
if ($user) { |
|
|
|
|
|
$paramsSearchOrderShop['user'] = $user; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ($visitor) { |
|
|
|
|
|
$paramsSearchOrderShop['visitor'] = $visitor; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$orderShop = $this->orderShopRepo->findOneBy($paramsSearchOrderShop); |
|
|
|
|
|
|
|
|
|
|
|
if (!$orderShop) { |
|
|
|
|
|
$orderShop = $this->createOrderShop([ |
|
|
|
|
|
'user' => $user, |
|
|
|
|
|
'visitor' => $visitor, |
|
|
|
|
|
'merchant' => $this->merchantUtils->getMerchantCurrent() |
|
|
|
|
|
]); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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.'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$this->em->persist($orderShop); |
|
|
|
|
|
$this->em->flush(); |
|
|
|
|
|
|
|
|
|
|
|
return $orderShop; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function addOrderProduct($orderShop, $orderProductAdd) |
|
|
|
|
|
{ |
|
|
|
|
|
if ($orderProductAdd->getQuantity() > 0) { |
|
|
|
|
|
$updated = false; |
|
|
|
|
|
|
|
|
|
|
|
$orderProductAdd->setTitle($orderProductAdd->getTitleOrderShop()); |
|
|
|
|
|
$orderProductAdd->setPrice($orderProductAdd->getProduct()->getPriceInherited()); |
|
|
|
|
|
$orderProductAdd->setUnit($orderProductAdd->getProduct()->getUnitInherited()); |
|
|
|
|
|
$orderProductAdd->setTaxRate($orderProductAdd->getProduct()->getTaxRateInherited()); |
|
|
|
|
|
|
|
|
|
|
|
foreach ($orderShop->getOrderProducts() as $orderProduct) { |
|
|
|
|
|
if ($orderProduct->getProduct()->getId() == $orderProductAdd->getProduct()->getId()) { |
|
|
|
|
|
$updated = true; |
|
|
|
|
|
$orderProduct->setQuantity($orderProduct->getQuantity() + $orderProductAdd->getQuantity()); |
|
|
|
|
|
$this->em->persist($orderProduct); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!$updated) { |
|
|
|
|
|
$orderShop->addOrderProduct($orderProductAdd); |
|
|
|
|
|
$this->em->persist($orderProductAdd); |
|
|
|
|
|
$this->em->persist($orderShop); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$this->em->flush(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function countQuantities($orderShop) |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function countQuantitiesByOrderProducts($orderProducts = []) |
|
|
|
|
|
{ |
|
|
|
|
|
$count = 0; |
|
|
|
|
|
|
|
|
|
|
|
foreach ($orderProducts as $orderProduct) { |
|
|
|
|
|
$count += $orderProduct->getQuantity(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return $count; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function calculateTotalWithTax($orderShop) |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->calculateTotalWithTaxByOrderProducts($orderShop->getOrderProducts()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function calculateTotalWithTaxByOrderProducts($orderProducts = []) |
|
|
|
|
|
{ |
|
|
|
|
|
$totalWithTax = 0; |
|
|
|
|
|
|
|
|
|
|
|
foreach ($orderProducts as $orderProduct) { |
|
|
|
|
|
$totalWithTax += $orderProduct->getPriceWithTax() * $orderProduct->getQuantity(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return $totalWithTax; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function orderProductsByParentCategory($orderShop = null) |
|
|
|
|
|
{ |
|
|
|
|
|
$categoriesArray = []; |
|
|
|
|
|
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 getDatasSummary($orderShop = null) |
|
|
|
|
|
{ |
|
|
|
|
|
if(!$orderShop) { |
|
|
|
|
|
$orderShop = $this->getOrderShopCurrent() ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$data = [] ; |
|
|
|
|
|
$data['count'] = $this->countQuantities($orderShop) ; |
|
|
|
|
|
$data['total_with_tax'] = $this->calculateTotalWithTax($orderShop) ; |
|
|
|
|
|
$data['categories'] = $this->orderProductsByParentCategory($orderShop) ; |
|
|
|
|
|
|
|
|
|
|
|
return $data ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |