No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

178 líneas
6.8KB

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