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.

OrderUtils.php 19KB

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