You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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