175 rindas
6.5KB

  1. <?php
  2. namespace Lc\ShopBundle\Services ;
  3. use App\Entity\OrderShop;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  6. use Symfony\Component\Security\Core\Security;
  7. class OrderUtils
  8. {
  9. protected $em;
  10. protected $security;
  11. protected $userUtils;
  12. protected $merchantUtils;
  13. private $orderShopRepo;
  14. public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils, MerchantUtilsInterface $merchantUtils)
  15. {
  16. $this->em = $em;
  17. $this->security = $security;
  18. $this->userUtils = $userUtils;
  19. $this->merchantUtils = $merchantUtils;
  20. $this->orderShopRepo = $this->em->getRepository(OrderShop::class);
  21. }
  22. public function getOrderShopCurrent()
  23. {
  24. $paramsSearchOrderShop = [];
  25. $user = $this->security->getUser();
  26. $visitor = $this->userUtils->getVisitorCurrent();
  27. if ($user) {
  28. $paramsSearchOrderShop['user'] = $user;
  29. }
  30. if ($visitor) {
  31. $paramsSearchOrderShop['visitor'] = $visitor;
  32. }
  33. $orderShop = $this->orderShopRepo->findOneBy($paramsSearchOrderShop);
  34. if (!$orderShop) {
  35. $orderShop = $this->createOrderShop([
  36. 'user' => $user,
  37. 'visitor' => $visitor,
  38. 'merchant' => $this->merchantUtils->getMerchantCurrent()
  39. ]);
  40. }
  41. return $orderShop;
  42. }
  43. public function createOrderShop($params)
  44. {
  45. $orderShop = new OrderShop();
  46. $orderShopBelongTo = false;
  47. if (isset($params['user']) && $params['user']) {
  48. $orderShopBelongTo = true;
  49. $orderShop->setUser($params['user']);
  50. }
  51. if (isset($params['visitor']) && $params['visitor']) {
  52. $orderShopBelongTo = true;
  53. $orderShop->setVisitor($params['visitor']);
  54. }
  55. if (!$orderShopBelongTo) {
  56. throw new \ErrorException('La commande doit être liée à un utilisateur ou à un visiteur.');
  57. }
  58. if (isset($params['merchant']) && $params['merchant']) {
  59. $orderShop->setMerchant($params['merchant']);
  60. } else {
  61. throw new \ErrorException('La commande doit être liée à un merchant.');
  62. }
  63. $this->em->persist($orderShop);
  64. $this->em->flush();
  65. return $orderShop;
  66. }
  67. public function addOrderProduct($orderShop, $orderProductAdd)
  68. {
  69. if ($orderProductAdd->getQuantity() > 0) {
  70. $updated = false;
  71. $orderProductAdd->setTitle($orderProductAdd->getTitleOrderShop());
  72. $orderProductAdd->setPrice($orderProductAdd->getProduct()->getPriceInherited());
  73. $orderProductAdd->setUnit($orderProductAdd->getProduct()->getUnitInherited());
  74. $orderProductAdd->setTaxRate($orderProductAdd->getProduct()->getTaxRateInherited());
  75. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  76. if ($orderProduct->getProduct()->getId() == $orderProductAdd->getProduct()->getId()) {
  77. $updated = true;
  78. $orderProduct->setQuantity($orderProduct->getQuantity() + $orderProductAdd->getQuantity());
  79. $this->em->persist($orderProduct);
  80. }
  81. }
  82. if (!$updated) {
  83. $orderShop->addOrderProduct($orderProductAdd);
  84. $this->em->persist($orderProductAdd);
  85. $this->em->persist($orderShop);
  86. }
  87. $this->em->flush();
  88. }
  89. }
  90. public function countQuantities($orderShop)
  91. {
  92. return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
  93. }
  94. public function countQuantitiesByOrderProducts($orderProducts = [])
  95. {
  96. $count = 0;
  97. foreach ($orderProducts as $orderProduct) {
  98. $count += $orderProduct->getQuantity();
  99. }
  100. return $count;
  101. }
  102. public function calculateTotalWithTax($orderShop)
  103. {
  104. return $this->calculateTotalWithTaxByOrderProducts($orderShop->getOrderProducts());
  105. }
  106. public function calculateTotalWithTaxByOrderProducts($orderProducts = [])
  107. {
  108. $totalWithTax = 0;
  109. foreach ($orderProducts as $orderProduct) {
  110. $totalWithTax += $orderProduct->getPriceWithTax() * $orderProduct->getQuantity();
  111. }
  112. return $totalWithTax;
  113. }
  114. public function orderProductsByParentCategory($orderShop = null)
  115. {
  116. $categoriesArray = [];
  117. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  118. $productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
  119. $category = $productCategories[0]->getParentCategory();
  120. $labelCategory = $category->getTitle() ;
  121. if (!isset($categoriesArray[$labelCategory])) {
  122. $categoriesArray[$labelCategory] = [] ;
  123. }
  124. $categoriesArray[$labelCategory][] = $orderProduct;
  125. }
  126. return $categoriesArray;
  127. }
  128. public function getDatasSummary($orderShop = null)
  129. {
  130. if(!$orderShop) {
  131. $orderShop = $this->getOrderShopCurrent() ;
  132. }
  133. $data = [] ;
  134. $data['count'] = $this->countQuantities($orderShop) ;
  135. $data['total_with_tax'] = $this->calculateTotalWithTax($orderShop) ;
  136. $data['categories'] = $this->orderProductsByParentCategory($orderShop) ;
  137. return $data ;
  138. }
  139. }