Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

302 lines
14KB

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