Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

OrderUtils.php 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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\OrderReductionCreditInterface;
  11. use Lc\ShopBundle\Context\OrderShopInterface;
  12. use Lc\ShopBundle\Context\OrderStatusHistoryInterface;
  13. use Lc\ShopBundle\Context\OrderStatusInterface;
  14. use Lc\ShopBundle\Context\ProductFamilyUtilsInterface;
  15. use Lc\ShopBundle\Context\ReductionCartInterface;
  16. use Lc\ShopBundle\Context\ReductionCreditInterface;
  17. use Lc\ShopBundle\Context\UserInterface;
  18. use Lc\ShopBundle\Model\Document;
  19. use Lc\ShopBundle\Form\Backend\Order\OrderReductionCreditType;
  20. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  21. use Symfony\Component\Security\Core\Security;
  22. class OrderUtils
  23. {
  24. protected $em;
  25. protected $security;
  26. protected $userUtils;
  27. protected $merchantUtils;
  28. protected $orderShopRepo;
  29. protected $priceUtils;
  30. protected $productFamilyUtils;
  31. protected $documentUtils ;
  32. public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils,
  33. MerchantUtilsInterface $merchantUtils, PriceUtils $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils,
  34. DocumentUtils $documentUtils)
  35. {
  36. $this->em = $em;
  37. $this->security = $security;
  38. $this->userUtils = $userUtils;
  39. $this->merchantUtils = $merchantUtils;
  40. $this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName());
  41. $this->priceUtils = $priceUtils;
  42. $this->productFamilyUtils = $productFamilyUtils;
  43. $this->documentUtils = $documentUtils ;
  44. }
  45. public function getCartCurrent()
  46. {
  47. $paramsSearchOrderShop = [];
  48. $user = $this->security->getUser();
  49. $visitor = $this->userUtils->getVisitorCurrent();
  50. $orderShop = null;
  51. $orderShopUser = null;
  52. $orderShopVisitor = null;
  53. if ($user) {
  54. $orderShopUser = $this->orderShopRepo->findCartCurrent([
  55. 'user' => $user
  56. ]);
  57. }
  58. if ($visitor) {
  59. $orderShopVisitor = $this->orderShopRepo->findCartCurrent([
  60. 'visitor' => $visitor
  61. ]);
  62. }
  63. if ($orderShopUser || $orderShopVisitor) {
  64. if ($orderShopUser && $orderShopVisitor && $orderShopUser != $orderShopVisitor && $orderShopVisitor->getOrderProducts() && count($orderShopVisitor->getOrderProducts())) {
  65. $orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor);
  66. $this->session->getFlashBag()->add('success', "Votre panier visiteur vient d'être fusionné avec votre panier client.");
  67. } else {
  68. $orderShop = ($orderShopUser) ? $orderShopUser : $orderShopVisitor;
  69. }
  70. }
  71. if (!$user && !$visitor) {
  72. $this->session->getFlashBag()->add('error', 'Vous devez accepter les cookies ou vous connecter pour créer un panier.');
  73. } else {
  74. if (!$orderShop) {
  75. $orderShop = $this->createOrderShop([
  76. 'user' => $user,
  77. 'visitor' => $visitor,
  78. 'merchant' => $this->merchantUtils->getMerchantCurrent()
  79. ]);
  80. }
  81. }
  82. return $orderShop;
  83. }
  84. public function createOrderShop($params)
  85. {
  86. $orderShop = new OrderShop();
  87. $orderShopBelongTo = false;
  88. if (isset($params['user']) && $params['user']) {
  89. $orderShopBelongTo = true;
  90. $orderShop->setUser($params['user']);
  91. }
  92. if (isset($params['visitor']) && $params['visitor']) {
  93. $orderShopBelongTo = true;
  94. $orderShop->setVisitor($params['visitor']);
  95. }
  96. if (!$orderShopBelongTo) {
  97. throw new \ErrorException('La commande doit être liée à un utilisateur ou à un visiteur.');
  98. }
  99. if (isset($params['merchant']) && $params['merchant']) {
  100. $orderShop->setMerchant($params['merchant']);
  101. } else {
  102. throw new \ErrorException('La commande doit être liée à un merchant.');
  103. }
  104. $orderShop = $this->setOrderStatus('cart', $orderShop);
  105. $this->em->persist($orderShop);
  106. $this->em->flush();
  107. return $orderShop;
  108. }
  109. public function addOrderProduct($orderShop, $orderProductAdd, $persist = true)
  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. && (string)$this->priceUtils->getPrice($orderProduct) == (string)$this->priceUtils->getPrice($orderProductAdd)
  131. && $this->compareOrderProductReductionCatalog($orderProduct->getOrderProductReductionCatalog(), $orderProductAdd->getOrderProductReductionCatalog())) {
  132. $orderProduct->setQuantityOrder($orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder());
  133. if ($persist) {
  134. $this->em->persist($orderProduct);
  135. }
  136. $updated = true;
  137. break;
  138. }
  139. }
  140. if (!$updated) {
  141. $orderShop->addOrderProduct($orderProductAdd);
  142. if (isset($orderProductReductionCatalog)) {
  143. $this->em->persist($orderProductReductionCatalog);
  144. if ($persist) {
  145. if (isset($orderProductReductionCatalog)) {
  146. $this->em->persist($orderProductReductionCatalog);
  147. }
  148. $this->em->persist($orderProductAdd);
  149. $this->em->persist($orderShop);
  150. }
  151. }
  152. if ($persist) {
  153. $this->em->flush();
  154. }
  155. }
  156. }
  157. }
  158. public function compareOrderProductReductionCatalog($orderProductReductionCatalog1, $orderProductReductionCatalog2)
  159. {
  160. return (!$orderProductReductionCatalog1 && !$orderProductReductionCatalog2)
  161. || ($orderProductReductionCatalog1
  162. && $orderProductReductionCatalog2
  163. && $orderProductReductionCatalog1->getUnit() == $orderProductReductionCatalog2->getUnit()
  164. && (string) $orderProductReductionCatalog1->getValue() == (string) $orderProductReductionCatalog2->getValue()
  165. && $orderProductReductionCatalog1->getBehaviorTaxRate() == $orderProductReductionCatalog2->getBehaviorTaxRate()) ;
  166. }
  167. public function countQuantities($orderShop)
  168. {
  169. return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
  170. }
  171. public function countQuantitiesByOrderProducts($orderProducts = [])
  172. {
  173. $count = 0;
  174. foreach ($orderProducts as $orderProduct) {
  175. $count += $orderProduct->getQuantityOrder();
  176. }
  177. return $count;
  178. }
  179. public function getOrderProductsByParentCategory($orderShop = null)
  180. {
  181. $categoriesArray = [];
  182. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  183. $productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
  184. $category = $productCategories[0]->getParentCategory();
  185. $labelCategory = $category->getTitle();
  186. if (!isset($categoriesArray[$labelCategory])) {
  187. $categoriesArray[$labelCategory] = [];
  188. }
  189. $categoriesArray[$labelCategory][] = $orderProduct;
  190. }
  191. return $categoriesArray;
  192. }
  193. public function getOrderDatas($order = null)
  194. {
  195. $data = [];
  196. if (!$order) {
  197. $order = $this->getCartCurrent();
  198. }
  199. $data['order'] = $order;
  200. if ($order) {
  201. $data['count'] = $this->countQuantities($order);
  202. $data['total_with_tax'] = $this->priceUtils->getTotalWithTaxAndReduction($order);
  203. $data['order_products_by_category'] = $this->getOrderProductsByParentCategory($order);
  204. }
  205. return $data;
  206. }
  207. public function getOrderAsJsonObject(OrderShopInterface $order)
  208. {
  209. $data['id'] = $order->getId();
  210. $data['user'] = $order->getUser()->getSummary();
  211. $data['orderStatus'] = $order->getOrderStatus()->__tosString();
  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 newOrderStatusHistory($order, $status, $origin = 'user')
  249. {
  250. $orderStatusHistoryClass = $this->em->getClassMetadata(OrderStatusHistoryInterface::class);
  251. $orderStatusHistory = new $orderStatusHistoryClass->name;
  252. $orderStatusHistory->setOrderShop($order);
  253. $orderStatusHistory->setOrderStatus($status);
  254. $orderStatusHistory->setOrigin($origin);
  255. $this->em->persist($orderStatusHistory);
  256. }
  257. public function createOrderReductionCart(OrderShopInterface $orderShop, ReductionCartInterface $reductionCart)
  258. {
  259. $orderReductionCartClass = $this->em->getClassMetadata(OrderReductionCartInterface::class);
  260. $orderReductionCart = new $orderReductionCartClass->name;
  261. $orderReductionCart->setOrderShop($orderShop);
  262. $orderReductionCart->setReductionCart($reductionCart);
  263. $orderReductionCart->setTitle($reductionCart->getTitle());
  264. $orderReductionCart->setValue($reductionCart->getValue());
  265. $orderReductionCart->setUnit($reductionCart->getUnit());
  266. $orderReductionCart->setBehaviorTaxRate($reductionCart->getBehaviorTaxRate());
  267. $orderReductionCart->setFreeShipping($reductionCart->getFreeShipping());
  268. $orderReductionCart->setAppliedTo($reductionCart->getAppliedTo());
  269. $orderReductionCart->setType($reductionCart->getType());
  270. $this->em->persist($orderReductionCart) ;
  271. $this->em->flush() ;
  272. return $orderReductionCart;
  273. }
  274. public function isReductionCreditAllowToBeAddToOrder($orderShop, $reductionCredit){
  275. if($this->orderShopRepo->findValidOrderWithReductionCredit($reductionCredit, $orderShop->getUser())>0){
  276. return false;
  277. }else{
  278. return true;
  279. }
  280. }
  281. public function createOrderReductionCredit(OrderShopInterface $orderShop, ReductionCreditInterface $reductionCredit)
  282. {
  283. $orderReductionCreditClass = $this->em->getClassMetadata(OrderReductionCreditInterface::class);
  284. $orderReductionCredit = new $orderReductionCreditClass->name;
  285. $orderReductionCredit->setOrderShop($orderShop);
  286. $orderReductionCredit->setReductionCredit($reductionCredit);
  287. $orderReductionCredit->setTitle($reductionCredit->getTitle());
  288. $orderReductionCredit->setValue($reductionCredit->getValue());
  289. $orderReductionCredit->setUnit($reductionCredit->getUnit());
  290. $orderReductionCredit->setBehaviorTaxRate($reductionCredit->getBehaviorTaxRate());
  291. return $orderReductionCredit;
  292. }
  293. /*public function getReductionCreditsAvailable($order)
  294. {
  295. $reductionCreditRepo = $this->em->getRepository(ReductionCreditInterface::class);
  296. $reductionCredits = $reductionCreditRepo->getReductionCreditByUser($order->getUser());
  297. foreach ($reductionCredits as $reductionCredit){
  298. }
  299. }*/
  300. public function mergeOrderShops($orderShop1, $orderShop2)
  301. {
  302. if ($orderShop1 && $orderShop2) {
  303. foreach ($orderShop2->getOrderProducts() as $orderProduct) {
  304. $this->addOrderProduct($orderShop1, $orderProduct);
  305. $this->em->remove($orderProduct);
  306. }
  307. $this->em->remove($orderShop2);
  308. $this->em->persist($orderShop1);
  309. $this->em->flush();
  310. return $orderShop1;
  311. }
  312. }
  313. public function createDocumentInvoice(OrderShopInterface $orderShop)
  314. {
  315. $merchantAddress = $orderShop->getMerchant()->getAddress() ;
  316. $buyerAddress = $orderShop->getBillingAddress() ;
  317. $document = $this->documentUtils->createDocument([
  318. 'type' => Document::TYPE_INVOICE,
  319. 'title' => '',
  320. 'status' => 1,
  321. 'order_shops' => [$orderShop],
  322. 'merchant_address' => $merchantAddress,
  323. 'buyer_address' => $buyerAddress,
  324. 'created_by' => $orderShop->getUser()
  325. ]) ;
  326. return $document ;
  327. }
  328. public function groupOrderProductsByProductFamily($orderProducts)
  329. {
  330. $orderProductsByProductFamily = [] ;
  331. foreach($orderProducts as $orderProduct) {
  332. if($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {
  333. $productFamily = $orderProduct->getProduct()->getProductFamily() ;
  334. if(!isset($orderProductsByProductFamily[$productFamily->getId()])) {
  335. $orderProductsByProductFamily[$productFamily->getId()] = [
  336. 'order_products' => [],
  337. 'total_quantity_weight' => 0,
  338. ] ;
  339. }
  340. $orderProductsByProductFamily[$productFamily->getId()]['order_products'][] = $orderProduct ;
  341. $orderProductsByProductFamily[$productFamily->getId()]['total_quantity_weight'] += ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient()) * $orderProduct->getQuantityOrder() ;
  342. }
  343. }
  344. return $orderProductsByProductFamily ;
  345. }
  346. }