Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

246 rindas
11KB

  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['user'] = $order->getUser()->getSummary();
  163. $data['deliveryAddress'] = $order->getDeliveryAddress()->getSummary();
  164. $data['invoiceAddress'] = $order->getInvoiceAddress()->getSummary();
  165. $data['total'] = $this->priceUtils->getTotal($order);
  166. $data['totalWithTax'] = $this->priceUtils->getTotalWithTax($order);
  167. $data['totalWithTaxAndReduction'] = $this->priceUtils->getTotalWithTax($order);
  168. $i=0;
  169. foreach ($this->getOrderProductsByParentCategory($order) as $labelCategory => $orderProducts) {
  170. foreach ($orderProducts as $orderProduct) {
  171. $data['orderProducts'][$i]['id'] = $orderProduct->getId();
  172. $data['orderProducts'][$i]['product'] = $orderProduct->getProduct()->getId();
  173. $data['orderProducts'][$i]['quantityOrder'] = $orderProduct->getQuantityOrder();
  174. $data['orderProducts'][$i]['labelCategory'] = $labelCategory;
  175. $data['orderProducts'][$i]['title'] = $orderProduct->getTitle();
  176. $data['orderProducts'][$i]['price'] = $this->priceUtils->getPrice($orderProduct);
  177. $data['orderProducts'][$i]['priceWithTax'] = $this->priceUtils->getPriceWithTax($orderProduct);
  178. $data['orderProducts'][$i]['priceWithTaxAndReduction'] = $this->priceUtils->getPriceWithTaxAndReduction($orderProduct);
  179. $data['orderProducts'][$i]['quantity'] = $orderProduct->getQuantityOrder();
  180. $data['orderProducts'][$i]['totalWithTaxAndReduction'] = $this->priceUtils->getTotalOrderProductsWithTaxAndReduction(array($orderProduct));
  181. $i++;
  182. }
  183. }
  184. return $data;
  185. }
  186. public function getSummaryOrderProductReductionCatalog($orderProductReductionCatalog)
  187. {
  188. $text = '' ;
  189. if($orderProductReductionCatalog) {
  190. if($orderProductReductionCatalog->getUnit() == 'amount') {
  191. $text .= '- '.$orderProductReductionCatalog->getValue().'&nbsp;€' ;
  192. }
  193. if($orderProductReductionCatalog->getUnit() == 'percent') {
  194. $text .= '- '.$orderProductReductionCatalog->getValue().'&nbsp;%' ;
  195. }
  196. }
  197. return $text ;
  198. }
  199. }