Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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