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.

367 lines
18KB

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