You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

317 lines
15KB

  1. <?php
  2. namespace Lc\ShopBundle\Services ;
  3. use App\Entity\OrderProductReductionCatalog;
  4. use App\Entity\OrderShop;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  7. use Lc\ShopBundle\Context\OrderProductInterface;
  8. use Lc\ShopBundle\Context\OrderShopInterface;
  9. use Lc\ShopBundle\Context\ProductFamilyUtilsInterface;
  10. use Lc\ShopBundle\Context\UserInterface;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. use Symfony\Component\Security\Core\Security;
  13. class OrderUtils
  14. {
  15. protected $em;
  16. protected $security;
  17. protected $userUtils;
  18. protected $merchantUtils;
  19. protected $orderShopRepo;
  20. protected $priceUtils ;
  21. protected $productFamilyUtils ;
  22. protected $session ;
  23. public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils,
  24. MerchantUtilsInterface $merchantUtils, PriceUtils $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils,
  25. SessionInterface $session)
  26. {
  27. $this->em = $em;
  28. $this->security = $security;
  29. $this->userUtils = $userUtils;
  30. $this->merchantUtils = $merchantUtils;
  31. $this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName());
  32. $this->priceUtils = $priceUtils ;
  33. $this->productFamilyUtils = $productFamilyUtils ;
  34. $this->session = $session ;
  35. }
  36. public function getCartCurrent()
  37. {
  38. $paramsSearchOrderShop = [];
  39. $user = $this->security->getUser();
  40. $visitor = $this->userUtils->getVisitorCurrent();
  41. $orderShop = null ;
  42. $orderShopUser = null ;
  43. $orderShopVisitor = null ;
  44. if ($user) {
  45. $orderShopUser = $this->orderShopRepo->findCartCurrent([
  46. 'user' => $user
  47. ]);
  48. }
  49. if ($visitor) {
  50. $orderShopVisitor = $this->orderShopRepo->findCartCurrent([
  51. 'visitor' => $visitor
  52. ]);
  53. }
  54. if($orderShopUser || $orderShopVisitor) {
  55. if($orderShopUser && $orderShopVisitor && $orderShopUser != $orderShopVisitor && $orderShopVisitor->getOrderProducts() && count($orderShopVisitor->getOrderProducts())) {
  56. $orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor) ;
  57. $this->session->getFlashBag()->add('success', "Votre panier visiteur vient d'être fusionné avec votre panier client.") ;
  58. }
  59. else {
  60. $orderShop = ($orderShopUser) ? $orderShopUser : $orderShopVisitor ;
  61. }
  62. }
  63. if(!$user && !$visitor)
  64. {
  65. $this->session->getFlashBag()->add('error', 'Vous devez accepter les cookies ou vous connecter pour créer un panier.') ;
  66. }
  67. else {
  68. if (!$orderShop) {
  69. $orderShop = $this->createOrderShop([
  70. 'user' => $user,
  71. 'visitor' => $visitor,
  72. 'merchant' => $this->merchantUtils->getMerchantCurrent()
  73. ]);
  74. }
  75. }
  76. return $orderShop;
  77. }
  78. public function createOrderShop($params)
  79. {
  80. $orderShop = new OrderShop();
  81. $orderShopBelongTo = false;
  82. if (isset($params['user']) && $params['user']) {
  83. $orderShopBelongTo = true;
  84. $orderShop->setUser($params['user']);
  85. }
  86. if (isset($params['visitor']) && $params['visitor']) {
  87. $orderShopBelongTo = true;
  88. $orderShop->setVisitor($params['visitor']);
  89. }
  90. if (!$orderShopBelongTo) {
  91. throw new \ErrorException('La commande doit être liée à un utilisateur ou à un visiteur.');
  92. }
  93. if (isset($params['merchant']) && $params['merchant']) {
  94. $orderShop->setMerchant($params['merchant']);
  95. } else {
  96. throw new \ErrorException('La commande doit être liée à un merchant.');
  97. }
  98. if($this->security->getUser()) {
  99. $orderShop->setCreatedBy($this->security->getUser()) ;
  100. $orderShop->setUpdatedBy($this->security->getUser()) ;
  101. }
  102. else {
  103. // createdBy doit pouvoir être NULL pour OrderShop, en attendant qu'on en discute, j'assigne ça au premier de la base
  104. $userRepository = $this->em->getRepository($this->em->getClassMetadata(UserInterface::class)->getName()) ;
  105. $user = $userRepository->find(1) ;
  106. $orderShop->setCreatedBy($user) ;
  107. $orderShop->setUpdatedBy($user) ;
  108. }
  109. $this->em->persist($orderShop);
  110. $this->em->flush();
  111. return $orderShop;
  112. }
  113. public function addOrderProduct($orderShop, $orderProductAdd, $persist = true)
  114. {
  115. if ($orderProductAdd->getQuantityOrder() > 0) {
  116. $updated = false;
  117. $orderProductAdd->setTitle($orderProductAdd->getTitleOrderShop());
  118. $orderProductAdd->setPrice($this->priceUtils->getPrice($orderProductAdd->getProduct()));
  119. $orderProductAdd->setUnit($orderProductAdd->getProduct()->getUnitInherited());
  120. $orderProductAdd->setTaxRate($orderProductAdd->getProduct()->getTaxRateInherited());
  121. $orderProductAdd->setQuantityProduct($orderProductAdd->getProduct()->getQuantityInherited());
  122. $productFamily = $this->productFamilyUtils->getProductFamilyBySlug($orderProductAdd->getProduct()->getProductFamily()->getSlug()) ;
  123. $reductionCatalog = $productFamily->getReductionCatalog() ;
  124. if($reductionCatalog) {
  125. $orderProductReductionCatalog = new OrderProductReductionCatalog() ;
  126. $orderProductReductionCatalog->setTitle($reductionCatalog->getTitle()) ;
  127. $orderProductReductionCatalog->setValue($reductionCatalog->getValue()) ;
  128. $orderProductReductionCatalog->setUnit($reductionCatalog->getUnit()) ;
  129. $orderProductReductionCatalog->setBehaviorTaxRate($reductionCatalog->getBehaviorTaxRate()) ;
  130. $orderProductAdd->setOrderProductReductionCatalog($orderProductReductionCatalog);
  131. }
  132. foreach($orderShop->getOrderProducts() as $orderProduct) {
  133. if ($orderProduct->getProduct()->getId() == $orderProductAdd->getProduct()->getId()
  134. && (string) $this->priceUtils->getPrice($orderProduct) == (string) $this->priceUtils->getPrice($orderProductAdd)
  135. && $this->compareOrderProductReductionCatalog($orderProduct->getOrderProductReductionCatalog(), $orderProductAdd->getOrderProductReductionCatalog())) {
  136. $orderProduct->setQuantityOrder($orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder());
  137. if($persist) {
  138. $this->em->persist($orderProduct);
  139. }
  140. $updated = true;
  141. break ;
  142. }
  143. }
  144. if (!$updated) {
  145. $orderShop->addOrderProduct($orderProductAdd);
  146. if($persist) {
  147. if(isset($orderProductReductionCatalog)) {
  148. $this->em->persist($orderProductReductionCatalog);
  149. }
  150. $this->em->persist($orderProductAdd);
  151. $this->em->persist($orderShop);
  152. }
  153. }
  154. if($persist) {
  155. $this->em->flush();
  156. }
  157. }
  158. }
  159. public function compareOrderProductReductionCatalog($orderProductReductionCatalog1, $orderProductReductionCatalog2)
  160. {
  161. return (!$orderProductReductionCatalog1 && !$orderProductReductionCatalog2)
  162. || ($orderProductReductionCatalog1
  163. && $orderProductReductionCatalog2
  164. && $orderProductReductionCatalog1->getUnit() == $orderProductReductionCatalog2->getUnit()
  165. && (string) $orderProductReductionCatalog1->getValue() == (string) $orderProductReductionCatalog2->getValue()
  166. && $orderProductReductionCatalog1->getBehaviorTaxRate() == $orderProductReductionCatalog2->getBehaviorTaxRate()) ;
  167. }
  168. public function countQuantities($orderShop)
  169. {
  170. return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
  171. }
  172. public function countQuantitiesByOrderProducts($orderProducts = [])
  173. {
  174. $count = 0;
  175. foreach ($orderProducts as $orderProduct) {
  176. $count += $orderProduct->getQuantityOrder();
  177. }
  178. return $count;
  179. }
  180. public function getOrderProductsByParentCategory($orderShop = null)
  181. {
  182. $categoriesArray = [];
  183. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  184. $productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
  185. $category = $productCategories[0]->getParentCategory();
  186. $labelCategory = $category->getTitle() ;
  187. if (!isset($categoriesArray[$labelCategory])) {
  188. $categoriesArray[$labelCategory] = [] ;
  189. }
  190. $categoriesArray[$labelCategory][] = $orderProduct;
  191. }
  192. return $categoriesArray;
  193. }
  194. public function getOrderDatas($order = null)
  195. {
  196. $data = [] ;
  197. if(!$order) {
  198. $order = $this->getCartCurrent() ;
  199. }
  200. $data['order'] = $order ;
  201. if($order) {
  202. $data['count'] = $this->countQuantities($order) ;
  203. $data['total_with_tax'] = $this->priceUtils->getTotalWithTaxAndReduction($order) ;
  204. $data['order_products_by_category'] = $this->getOrderProductsByParentCategory($order) ;
  205. }
  206. return $data ;
  207. }
  208. public function getOrderAsJsonObject(OrderShopInterface $order)
  209. {
  210. $data['id'] = $order->getId();
  211. $data['user'] = $order->getUser()->getSummary();
  212. $data['deliveryAddress'] = $order->getDeliveryAddress()->getSummary();
  213. $data['invoiceAddress'] = $order->getInvoiceAddress()->getSummary();
  214. $data['total'] = $this->priceUtils->getTotal($order);
  215. $data['totalWithTax'] = $this->priceUtils->getTotalWithTax($order);
  216. $data['totalWithTaxAndReduction'] = $this->priceUtils->getTotalWithTax($order);
  217. $i=0;
  218. foreach ($this->getOrderProductsByParentCategory($order) as $labelCategory => $orderProducts) {
  219. foreach ($orderProducts as $orderProduct) {
  220. $data['orderProducts'][$i]['id'] = $orderProduct->getId();
  221. $data['orderProducts'][$i]['product'] = $orderProduct->getProduct()->getId();
  222. $data['orderProducts'][$i]['quantityOrder'] = $orderProduct->getQuantityOrder();
  223. $data['orderProducts'][$i]['labelCategory'] = $labelCategory;
  224. $data['orderProducts'][$i]['title'] = $orderProduct->getTitle();
  225. $data['orderProducts'][$i]['price'] = $this->priceUtils->getPrice($orderProduct);
  226. $data['orderProducts'][$i]['priceWithTax'] = $this->priceUtils->getPriceWithTax($orderProduct);
  227. $data['orderProducts'][$i]['priceWithTaxAndReduction'] = $this->priceUtils->getPriceWithTaxAndReduction($orderProduct);
  228. $data['orderProducts'][$i]['quantity'] = $orderProduct->getQuantityOrder();
  229. $data['orderProducts'][$i]['totalWithTaxAndReduction'] = $this->priceUtils->getTotalOrderProductsWithTaxAndReduction(array($orderProduct));
  230. $i++;
  231. }
  232. }
  233. return $data;
  234. }
  235. public function getSummaryOrderProductReductionCatalog($orderProductReductionCatalog)
  236. {
  237. $text = '' ;
  238. if($orderProductReductionCatalog) {
  239. if($orderProductReductionCatalog->getUnit() == 'amount') {
  240. $text .= '- '.$orderProductReductionCatalog->getValue().'&nbsp;€' ;
  241. }
  242. if($orderProductReductionCatalog->getUnit() == 'percent') {
  243. $text .= '- '.$orderProductReductionCatalog->getValue().'&nbsp;%' ;
  244. }
  245. }
  246. return $text ;
  247. }
  248. public function mergeOrderShops($orderShop1, $orderShop2)
  249. {
  250. if($orderShop1 && $orderShop2) {
  251. foreach($orderShop2->getOrderProducts() as $orderProduct) {
  252. $this->addOrderProduct($orderShop1, $orderProduct);
  253. $this->em->remove($orderProduct) ;
  254. }
  255. $this->em->remove($orderShop2) ;
  256. $this->em->persist($orderShop1);
  257. $this->em->flush() ;
  258. return $orderShop1 ;
  259. }
  260. }
  261. }