|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
-
- namespace Lc\ShopBundle\Services ;
-
- use App\Entity\OrderProductReductionCatalog;
- use App\Entity\OrderShop;
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\ShopBundle\Context\MerchantUtilsInterface;
- use Lc\ShopBundle\Context\ProductFamilyUtilsInterface;
- use Symfony\Component\Security\Core\Security;
-
- class OrderUtils
- {
- protected $em;
- protected $security;
- protected $userUtils;
- protected $merchantUtils;
- private $orderShopRepo;
- protected $priceUtils ;
- protected $productFamilyUtils ;
-
- public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils,
- MerchantUtilsInterface $merchantUtils, PriceUtils $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils)
- {
- $this->em = $em;
- $this->security = $security;
- $this->userUtils = $userUtils;
- $this->merchantUtils = $merchantUtils;
- $this->orderShopRepo = $this->em->getRepository(OrderShop::class);
- $this->priceUtils = $priceUtils ;
- $this->productFamilyUtils = $productFamilyUtils ;
- }
-
- 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->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()
- && $this->priceUtils->getPrice($orderProduct) == $this->priceUtils->getPrice($orderProductAdd)
- && $this->compareOrderProductReductionCatalog($orderProduct->getOrderProductReductionCatalog(), $orderProductAdd->getOrderProductReductionCatalog())) {
-
- $updated = true;
- $orderProduct->setQuantityOrder($orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder());
- $this->em->persist($orderProduct);
- }
- }
-
- if (!$updated) {
- $orderShop->addOrderProduct($orderProductAdd);
-
- if(isset($orderProductReductionCatalog)) {
- $this->em->persist($orderProductReductionCatalog);
- }
-
- $this->em->persist($orderProductAdd);
- $this->em->persist($orderShop);
- }
-
- $this->em->flush();
- }
- }
-
- public function compareOrderProductReductionCatalog($orderProductReductionCatalog1, $orderProductReductionCatalog2)
- {
- return $orderProductReductionCatalog1 && $orderProductReductionCatalog2
- && $orderProductReductionCatalog1->getUnit() == $orderProductReductionCatalog2->getUnit()
- && $orderProductReductionCatalog1->getValue() == $orderProductReductionCatalog2->getValue()
- && $orderProductReductionCatalog1->getBehaviorTaxRate() == $orderProductReductionCatalog2->getBehaviorTaxRate() ;
- }
-
- 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 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->priceUtils->getTotalWithTaxAndReduction($orderShop) ;
- $data['categories'] = $this->orderProductsByParentCategory($orderShop) ;
-
- return $data ;
- }
-
- public function getSummaryOrderProductReductionCatalog($orderProductReductionCatalog)
- {
- $text = '' ;
-
- if($orderProductReductionCatalog) {
- if($orderProductReductionCatalog->getUnit() == 'amount') {
- $text .= '- '.$orderProductReductionCatalog->getValue().' €' ;
- }
-
- if($orderProductReductionCatalog->getUnit() == 'percent') {
- $text .= '- '.$orderProductReductionCatalog->getValue().' %' ;
- }
- }
-
- return $text ;
- }
-
- }
|