選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

OrderUtils.php 19KB

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