您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

400 行
20KB

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