Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

OrderUtils.php 19KB

4 år sedan
4 år sedan
4 år sedan
4 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. namespace Lc\ShopBundle\Services\Order;
  3. use App\Entity\OrderProductReductionCatalog;
  4. use App\Entity\OrderShop;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Lc\ShopBundle\Context\DocumentInterface;
  7. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  8. use Lc\ShopBundle\Context\OrderReductionCreditInterface;
  9. use Lc\ShopBundle\Context\OrderShopInterface;
  10. use Lc\ShopBundle\Context\OrderStatusHistoryInterface;
  11. use Lc\ShopBundle\Context\PriceUtilsInterface;
  12. use Lc\ShopBundle\Context\ProductFamilyUtilsInterface;
  13. use Lc\ShopBundle\Context\ReductionCreditInterface;
  14. use Lc\ShopBundle\Context\UserUtilsInterface;
  15. use Lc\ShopBundle\Model\ProductFamily;
  16. use Lc\ShopBundle\Services\CreditUtils;
  17. use Lc\ShopBundle\Services\DocumentUtils;
  18. use Lc\ShopBundle\Services\UserUtils;
  19. use Lc\ShopBundle\Services\Utils;
  20. use Symfony\Component\Routing\RouterInterface;
  21. use Symfony\Component\Security\Core\Security;
  22. class OrderUtils
  23. {
  24. use OrderUtilsReductionTrait;
  25. use OrderUtilsStockTrait;
  26. use OrderUtilsPaymentTrait;
  27. use OrderUtilsDocumentTrait;
  28. use OrderUtilsCartTrait;
  29. protected $em;
  30. protected $security;
  31. protected $userUtils;
  32. protected $merchantUtils;
  33. protected $orderShopRepo;
  34. protected $reductionCreditRepo ;
  35. protected $orderReductionCreditRepo ;
  36. protected $documentRepo ;
  37. protected $priceUtils;
  38. protected $productFamilyUtils;
  39. protected $documentUtils;
  40. protected $utils;
  41. protected $creditUtils;
  42. protected $router;
  43. public function __construct(EntityManagerInterface $em, Security $security, RouterInterface $router, UserUtilsInterface $userUtils,
  44. MerchantUtilsInterface $merchantUtils, PriceUtilsInterface $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils,
  45. DocumentUtils $documentUtils, Utils $utils, CreditUtils $creditUtils)
  46. {
  47. $this->em = $em;
  48. $this->security = $security;
  49. $this->userUtils = $userUtils;
  50. $this->merchantUtils = $merchantUtils;
  51. $this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName());
  52. $this->reductionCreditRepo = $this->em->getRepository($this->em->getClassMetadata(ReductionCreditInterface::class)->getName());
  53. $this->orderReductionCreditRepo = $this->em->getRepository($this->em->getClassMetadata(OrderReductionCreditInterface::class)->getName());
  54. $this->documentRepo = $this->em->getRepository($this->em->getClassMetadata(DocumentInterface::class)->getName());
  55. $this->priceUtils = $priceUtils;
  56. $this->productFamilyUtils = $productFamilyUtils;
  57. $this->documentUtils = $documentUtils;
  58. $this->utils = $utils;
  59. $this->creditUtils = $creditUtils;
  60. $this->router = $router;
  61. }
  62. public function createOrderShop($params)
  63. {
  64. //TODO vérifier que l'utilisateur n'a pas déjà une commande en cours
  65. $orderShop = new OrderShop();
  66. $orderShopBelongTo = false;
  67. if (isset($params['user']) && $params['user']) {
  68. $orderShopBelongTo = true;
  69. $orderShop->setUser($params['user']);
  70. }
  71. if (isset($params['visitor']) && $params['visitor'] && !$orderShop->getUser()) {
  72. $orderShopBelongTo = true;
  73. $orderShop->setVisitor($params['visitor']);
  74. }
  75. if (!$orderShopBelongTo) {
  76. throw new \ErrorException('La commande doit être liée à un utilisateur ou à un visiteur.');
  77. }
  78. if (isset($params['merchant']) && $params['merchant']) {
  79. $orderShop->setMerchant($params['merchant']);
  80. } else {
  81. throw new \ErrorException('La commande doit être liée à un merchant.');
  82. }
  83. $orderShop = $this->changeOrderStatus('cart', $orderShop);
  84. return $orderShop;
  85. }
  86. public function addOrderProduct($orderShop, $orderProductAdd, $persist = true)
  87. {
  88. $return = false;
  89. if (!$orderShop) {
  90. $user = $this->security->getUser();
  91. $visitor = $this->userUtils->getVisitorCurrent();
  92. $orderShop = $this->createOrderShop([
  93. 'user' => $user,
  94. 'visitor' => $visitor,
  95. 'merchant' => $this->merchantUtils->getMerchantCurrent()
  96. ]);
  97. }
  98. if($this->isOrderProductAvailableAddCart($orderProductAdd, $orderShop)) {
  99. if ($orderProductAdd->getQuantityOrder() > 0) {
  100. $updated = false;
  101. $orderProductAdd->setTitle($orderProductAdd->getTitleOrderShop());
  102. $orderProductAdd->setPrice($this->priceUtils->getPrice($orderProductAdd->getProduct()));
  103. $orderProductAdd->setBuyingPrice($this->priceUtils->getBuyingPrice($orderProductAdd->getProduct()));
  104. $orderProductAdd->setUnit($orderProductAdd->getProduct()->getUnitInherited());
  105. $orderProductAdd->setTaxRate($orderProductAdd->getProduct()->getTaxRateInherited());
  106. $orderProductAdd->setQuantityProduct($orderProductAdd->getProduct()->getQuantityInherited());
  107. $productFamily = $this->productFamilyUtils->getProductFamilyBySlug($orderProductAdd->getProduct()->getProductFamily()->getSlug());
  108. if($productFamily) {
  109. $reductionCatalog = $productFamily->getReductionCatalog();
  110. if ($reductionCatalog) {
  111. $orderProductReductionCatalog = new OrderProductReductionCatalog();
  112. $orderProductReductionCatalog->setTitle($reductionCatalog->getTitle());
  113. $orderProductReductionCatalog->setValue($reductionCatalog->getValue());
  114. $orderProductReductionCatalog->setUnit($reductionCatalog->getUnit());
  115. $orderProductReductionCatalog->setBehaviorTaxRate($reductionCatalog->getBehaviorTaxRate());
  116. $orderProductAdd->setOrderProductReductionCatalog($orderProductReductionCatalog);
  117. }
  118. }
  119. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  120. if ($orderProduct->getProduct()->getId() == $orderProductAdd->getProduct()->getId()
  121. && $orderProduct->getRedelivery() == $orderProductAdd->getRedelivery()
  122. && (string) $this->priceUtils->getPrice($orderProduct) == (string) $this->priceUtils->getPrice($orderProductAdd)
  123. && $this->compareOrderProductReductionCatalog($orderProduct->getOrderProductReductionCatalog(), $orderProductAdd->getOrderProductReductionCatalog())) {
  124. $orderProduct->setQuantityOrder($orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder());
  125. if ($persist) {
  126. $this->em->persist($orderProduct);
  127. }
  128. $updated = true;
  129. $return = true;
  130. break;
  131. }
  132. }
  133. if (!$updated) {
  134. $orderShop->addOrderProduct($orderProductAdd);
  135. if ($persist) {
  136. if (isset($orderProductReductionCatalog)) {
  137. $this->em->persist($orderProductReductionCatalog);
  138. }
  139. $this->em->persist($orderProductAdd);
  140. $this->em->persist($orderShop);
  141. }
  142. $return = true;
  143. }
  144. if ($persist) {
  145. $this->em->flush();
  146. }
  147. $this->eventCartChange($orderShop) ;
  148. }
  149. }
  150. else {
  151. $availableQuantity = $orderProductAdd->getProduct()->getAvailableQuantityInherited() ;
  152. $textError = "Le produit <strong>".$orderProductAdd->getTitleOrderShop()."</strong> n'est pas disponible" ;
  153. if($availableQuantity !== false && $availableQuantity > 0) {
  154. $unit = '' ;
  155. if($orderProductAdd->getProduct()->getProductFamily()->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  156. $unit = $orderProductAdd->getProduct()->getUnitInherited()->getUnitReference()->getUnit() ;
  157. }
  158. $textError .= ' dans cette quantité ' ;
  159. $user = $this->security->getUser() ;
  160. if($user && $user->hasRole('ROLE_USER')) {
  161. $textError .= '<br />'.$availableQuantity.$unit.' disponible(s) dont '.$this->getQuantityOrderByProduct($orderShop, $orderProductAdd->getProduct()).$unit.' déjà dans votre panier.' ;
  162. }
  163. }
  164. $this->utils->addFlash('error', $textError);
  165. }
  166. return $return ;
  167. }
  168. public function countQuantities($orderShop)
  169. {
  170. return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
  171. }
  172. public function countQuantitiesByOrderProducts($orderProducts = [])
  173. {
  174. $count = 0;
  175. foreach ($orderProducts as $orderProduct) {
  176. $count += $orderProduct->getQuantityOrder();
  177. }
  178. return $count;
  179. }
  180. public function getOrderProductsByParentCategory($orderShop = null)
  181. {
  182. $categoriesArray = [];
  183. if ($orderShop) {
  184. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  185. $productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
  186. $category = $productCategories[0]->getParentCategory();
  187. $labelCategory = $category->getTitle();
  188. if (!isset($categoriesArray[$labelCategory])) {
  189. $categoriesArray[$labelCategory] = [];
  190. }
  191. $categoriesArray[$labelCategory][] = $orderProduct;
  192. }
  193. }
  194. return $categoriesArray;
  195. }
  196. public function getOrderDatas($order = null)
  197. {
  198. $data = [];
  199. if (!$order) {
  200. $order = $this->getCartCurrent();
  201. }
  202. $data['order'] = $order;
  203. if ($order) {
  204. $data['count'] = $this->countQuantities($order);
  205. $data['total_with_tax'] = $this->priceUtils->getTotalWithTax($order);
  206. $data['order_products_by_category'] = $this->getOrderProductsByParentCategory($order);
  207. }
  208. return $data;
  209. }
  210. public function getOrderAsJsonObject(OrderShopInterface $order)
  211. {
  212. $data['id'] = $order->getId();
  213. $data['user'] = $order->getUser()->getSummary();
  214. $data['orderStatus'] = $order->getOrderStatus()->__toString();
  215. $data['deliveryAddress'] = $order->getDeliveryAddress()->getSummary();
  216. $data['invoiceAddress'] = $order->getInvoiceAddress()->getSummary();
  217. $data['total'] = $this->priceUtils->getTotal($order);
  218. $data['totalWithTax'] = $this->priceUtils->getTotalWithTax($order);
  219. $data['totalWithTaxAndReduction'] = $this->priceUtils->getTotalWithTax($order);
  220. $i = 0;
  221. foreach ($this->getOrderProductsByParentCategory($order) as $labelCategory => $orderProducts) {
  222. foreach ($orderProducts as $orderProduct) {
  223. $data['orderProducts'][$i]['id'] = $orderProduct->getId();
  224. $data['orderProducts'][$i]['product'] = $orderProduct->getProduct()->getId();
  225. $data['orderProducts'][$i]['quantityOrder'] = $orderProduct->getQuantityOrder();
  226. $data['orderProducts'][$i]['labelCategory'] = $labelCategory;
  227. $data['orderProducts'][$i]['title'] = $orderProduct->getTitle();
  228. $data['orderProducts'][$i]['price'] = $this->priceUtils->getPrice($orderProduct);
  229. $data['orderProducts'][$i]['priceWithTax'] = $this->priceUtils->getPriceWithTax($orderProduct);
  230. $data['orderProducts'][$i]['priceWithTaxAndReduction'] = $this->priceUtils->getPriceWithTaxAndReduction($orderProduct);
  231. $data['orderProducts'][$i]['quantity'] = $orderProduct->getQuantityOrder();
  232. $data['orderProducts'][$i]['totalWithTaxAndReduction'] = $this->priceUtils->getTotalOrderProductsWithTaxAndReduction(array($orderProduct));
  233. $i++;
  234. }
  235. }
  236. return $data;
  237. }
  238. public function newOrderStatusHistory($order, $status, $origin = 'user')
  239. {
  240. $orderStatusHistoryClass = $this->em->getClassMetadata(OrderStatusHistoryInterface::class);
  241. $orderStatusHistory = new $orderStatusHistoryClass->name;
  242. $orderStatusHistory->setOrderShop($order);
  243. $orderStatusHistory->setOrderStatus($status);
  244. $orderStatusHistory->setOrigin($origin);
  245. $this->em->persist($orderStatusHistory);
  246. }
  247. public function mergeOrderShops($orderShop1, $orderShop2, $persist = true)
  248. {
  249. if ($orderShop1 && $orderShop2) {
  250. foreach ($orderShop2->getOrderProducts() as $orderProduct) {
  251. $orderProductAlreadyInCart = $this->hasOrderProductAlreadyInCart($orderShop1, $orderProduct) ;
  252. if($orderProductAlreadyInCart) {
  253. if($orderProduct->getQuantityOrder() > $orderProductAlreadyInCart->getQuantityOrder()) {
  254. $orderShop1->removeOrderProduct($orderProductAlreadyInCart) ;
  255. $this->addOrderProduct($orderShop1, $orderProduct);
  256. }
  257. }
  258. else {
  259. $this->addOrderProduct($orderShop1, $orderProduct);
  260. }
  261. if($persist) {
  262. $this->em->remove($orderProduct);
  263. }
  264. }
  265. if($persist) {
  266. $this->em->remove($orderShop2);
  267. $this->em->persist($orderShop1);
  268. $this->em->flush();
  269. }
  270. return $orderShop1;
  271. }
  272. }
  273. public function hasOrderProductAlreadyInCart($orderShop, $orderProductTest)
  274. {
  275. foreach($orderShop->getOrderProducts() as $orderProduct) {
  276. if($orderProduct->getProduct() == $orderProductTest->getProduct()) {
  277. return $orderProduct ;
  278. }
  279. }
  280. return false ;
  281. }
  282. public function getOrderProductsByProductFamilyInCart($productFamily)
  283. {
  284. $arrayOrderProducts = [] ;
  285. $orderShop = $this->getCartCurrent() ;
  286. if($orderShop) {
  287. foreach($orderShop->getOrderProducts() as $orderProduct) {
  288. if($orderProduct->getProduct()->getProductFamily() == $productFamily) {
  289. $arrayOrderProducts[] = $orderProduct ;
  290. }
  291. }
  292. }
  293. return $arrayOrderProducts ;
  294. }
  295. public function groupOrderProductsByProductFamily($orderProducts)
  296. {
  297. $orderProductsByProductFamily = [];
  298. foreach ($orderProducts as $orderProduct) {
  299. if ($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {
  300. $productFamily = $orderProduct->getProduct()->getProductFamily();
  301. if (!isset($orderProductsByProductFamily[$productFamily->getId()])) {
  302. $orderProductsByProductFamily[$productFamily->getId()] = [
  303. 'order_products' => [],
  304. 'total_quantity_weight' => 0,
  305. ];
  306. }
  307. $orderProductsByProductFamily[$productFamily->getId()]['order_products'][] = $orderProduct;
  308. $orderProductsByProductFamily[$productFamily->getId()]['total_quantity_weight'] += ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient()) * $orderProduct->getQuantityOrder();
  309. }
  310. }
  311. return $orderProductsByProductFamily;
  312. }
  313. public function isOrderShopPositiveAmount(OrderShopInterface $orderShop)
  314. {
  315. return $this->priceUtils->getTotalWithTax($orderShop) >= 0 ;
  316. }
  317. public function eventCartChange(OrderShopInterface $orderShop)
  318. {
  319. }
  320. }