Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

OrderUtils.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 Symfony\Component\Security\Core\Security;
  10. class OrderUtils
  11. {
  12. protected $em;
  13. protected $security;
  14. protected $userUtils;
  15. protected $merchantUtils;
  16. protected $orderShopRepo;
  17. protected $priceUtils ;
  18. protected $productFamilyUtils ;
  19. public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils,
  20. MerchantUtilsInterface $merchantUtils, PriceUtils $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils)
  21. {
  22. $this->em = $em;
  23. $this->security = $security;
  24. $this->userUtils = $userUtils;
  25. $this->merchantUtils = $merchantUtils;
  26. $this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName());
  27. $this->priceUtils = $priceUtils ;
  28. $this->productFamilyUtils = $productFamilyUtils ;
  29. }
  30. public function getCartCurrent()
  31. {
  32. $paramsSearchOrderShop = [];
  33. $user = $this->security->getUser();
  34. $visitor = $this->userUtils->getVisitorCurrent();
  35. if ($user) {
  36. $paramsSearchOrderShop['user'] = $user;
  37. }
  38. if ($visitor) {
  39. $paramsSearchOrderShop['visitor'] = $visitor;
  40. }
  41. $orderShop = $this->orderShopRepo->findCartCurrent($paramsSearchOrderShop);
  42. if (!$orderShop) {
  43. $orderShop = $this->createOrderShop([
  44. 'user' => $user,
  45. 'visitor' => $visitor,
  46. 'merchant' => $this->merchantUtils->getMerchantCurrent()
  47. ]);
  48. }
  49. return $orderShop;
  50. }
  51. public function createOrderShop($params)
  52. {
  53. $orderShop = new OrderShop();
  54. $orderShopBelongTo = false;
  55. if (isset($params['user']) && $params['user']) {
  56. $orderShopBelongTo = true;
  57. $orderShop->setUser($params['user']);
  58. }
  59. if (isset($params['visitor']) && $params['visitor']) {
  60. $orderShopBelongTo = true;
  61. $orderShop->setVisitor($params['visitor']);
  62. }
  63. if (!$orderShopBelongTo) {
  64. throw new \ErrorException('La commande doit être liée à un utilisateur ou à un visiteur.');
  65. }
  66. if (isset($params['merchant']) && $params['merchant']) {
  67. $orderShop->setMerchant($params['merchant']);
  68. } else {
  69. throw new \ErrorException('La commande doit être liée à un merchant.');
  70. }
  71. $this->em->persist($orderShop);
  72. $this->em->flush();
  73. return $orderShop;
  74. }
  75. public function addOrderProduct($orderShop, $orderProductAdd)
  76. {
  77. if ($orderProductAdd->getQuantityOrder() > 0) {
  78. $updated = false;
  79. $orderProductAdd->setTitle($orderProductAdd->getTitleOrderShop());
  80. $orderProductAdd->setPrice($this->priceUtils->getPrice($orderProductAdd->getProduct()));
  81. $orderProductAdd->setUnit($orderProductAdd->getProduct()->getUnitInherited());
  82. $orderProductAdd->setTaxRate($orderProductAdd->getProduct()->getTaxRateInherited());
  83. $orderProductAdd->setQuantityProduct($orderProductAdd->getProduct()->getQuantityInherited());
  84. $productFamily = $this->productFamilyUtils->getProductFamilyBySlug($orderProductAdd->getProduct()->getProductFamily()->getSlug()) ;
  85. $reductionCatalog = $productFamily->getReductionCatalog() ;
  86. if($reductionCatalog) {
  87. $orderProductReductionCatalog = new OrderProductReductionCatalog() ;
  88. $orderProductReductionCatalog->setTitle($reductionCatalog->getTitle()) ;
  89. $orderProductReductionCatalog->setValue($reductionCatalog->getValue()) ;
  90. $orderProductReductionCatalog->setUnit($reductionCatalog->getUnit()) ;
  91. $orderProductReductionCatalog->setBehaviorTaxRate($reductionCatalog->getBehaviorTaxRate()) ;
  92. $orderProductAdd->setOrderProductReductionCatalog($orderProductReductionCatalog);
  93. }
  94. foreach($orderShop->getOrderProducts() as $orderProduct) {
  95. if ($orderProduct->getProduct()->getId() == $orderProductAdd->getProduct()->getId()
  96. && $this->priceUtils->getPrice($orderProduct) == $this->priceUtils->getPrice($orderProductAdd)
  97. && $this->compareOrderProductReductionCatalog($orderProduct->getOrderProductReductionCatalog(), $orderProductAdd->getOrderProductReductionCatalog())) {
  98. $updated = true;
  99. $orderProduct->setQuantityOrder($orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder());
  100. $this->em->persist($orderProduct);
  101. }
  102. }
  103. if (!$updated) {
  104. $orderShop->addOrderProduct($orderProductAdd);
  105. if(isset($orderProductReductionCatalog)) {
  106. $this->em->persist($orderProductReductionCatalog);
  107. }
  108. $this->em->persist($orderProductAdd);
  109. $this->em->persist($orderShop);
  110. }
  111. $this->em->flush();
  112. }
  113. }
  114. public function compareOrderProductReductionCatalog($orderProductReductionCatalog1, $orderProductReductionCatalog2)
  115. {
  116. return $orderProductReductionCatalog1 && $orderProductReductionCatalog2
  117. && $orderProductReductionCatalog1->getUnit() == $orderProductReductionCatalog2->getUnit()
  118. && $orderProductReductionCatalog1->getValue() == $orderProductReductionCatalog2->getValue()
  119. && $orderProductReductionCatalog1->getBehaviorTaxRate() == $orderProductReductionCatalog2->getBehaviorTaxRate() ;
  120. }
  121. public function countQuantities($orderShop)
  122. {
  123. return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
  124. }
  125. public function countQuantitiesByOrderProducts($orderProducts = [])
  126. {
  127. $count = 0;
  128. foreach ($orderProducts as $orderProduct) {
  129. $count += $orderProduct->getQuantityOrder();
  130. }
  131. return $count;
  132. }
  133. public function getOrderProductsByParentCategory($orderShop = null)
  134. {
  135. $categoriesArray = [];
  136. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  137. $productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
  138. $category = $productCategories[0]->getParentCategory();
  139. $labelCategory = $category->getTitle() ;
  140. if (!isset($categoriesArray[$labelCategory])) {
  141. $categoriesArray[$labelCategory] = [] ;
  142. }
  143. $categoriesArray[$labelCategory][] = $orderProduct;
  144. }
  145. return $categoriesArray;
  146. }
  147. public function getOrderDatas($order = null)
  148. {
  149. if(!$order) {
  150. $order = $this->getCartCurrent() ;
  151. }
  152. $data = [] ;
  153. $data['order'] = $order ;
  154. $data['count'] = $this->countQuantities($order) ;
  155. $data['total_with_tax'] = $this->priceUtils->getTotalWithTaxAndReduction($order) ;
  156. $data['order_products_by_category'] = $this->getOrderProductsByParentCategory($order) ;
  157. return $data ;
  158. }
  159. public function getOrderAsJsonObject(OrderShopInterface $order)
  160. {
  161. $data['id'] = $order->getId();
  162. $data['total'] = $this->priceUtils->getTotal($order);
  163. $data['totalWithTax'] = $this->priceUtils->getTotalWithTax($order);
  164. $data['totalWithTaxAndReduction'] = $this->priceUtils->getTotalWithTax($order);
  165. $data['deliveryAddress'] = $order->getDeliveryAddress($order);
  166. $i=0;
  167. foreach ($this->getOrderProductsByParentCategory($order) as $labelCategory => $orderProducts) {
  168. foreach ($orderProducts as $orderProduct) {
  169. $data['orderProducts'][$i]['id'] = $orderProduct->getId();
  170. $data['orderProducts'][$i]['product'] = $orderProduct->getProduct()->getId();
  171. $data['orderProducts'][$i]['quantityOrder'] = $orderProduct->getQuantityOrder();
  172. $data['orderProducts'][$i]['labelCategory'] = $labelCategory;
  173. $data['orderProducts'][$i]['title'] = $orderProduct->getTitle();
  174. $data['orderProducts'][$i]['price'] = $this->priceUtils->getPrice($orderProduct);
  175. $data['orderProducts'][$i]['priceWithTax'] = $this->priceUtils->getPriceWithTax($orderProduct);
  176. $data['orderProducts'][$i]['priceWithTaxAndReduction'] = $this->priceUtils->getPriceWithTaxAndReduction($orderProduct);
  177. $data['orderProducts'][$i]['quantity'] = $orderProduct->getQuantityOrder();
  178. $data['orderProducts'][$i]['totalWithTaxAndReduction'] = $this->priceUtils->getTotalOrderProductsWithTaxAndReduction(array($orderProduct));
  179. $i++;
  180. }
  181. }
  182. return $data;
  183. }
  184. public function getSummaryOrderProductReductionCatalog($orderProductReductionCatalog)
  185. {
  186. $text = '' ;
  187. if($orderProductReductionCatalog) {
  188. if($orderProductReductionCatalog->getUnit() == 'amount') {
  189. $text .= '- '.$orderProductReductionCatalog->getValue().'&nbsp;€' ;
  190. }
  191. if($orderProductReductionCatalog->getUnit() == 'percent') {
  192. $text .= '- '.$orderProductReductionCatalog->getValue().'&nbsp;%' ;
  193. }
  194. }
  195. return $text ;
  196. }
  197. }