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.

259 line
12KB

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