Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

OrderUtils.php 20KB

il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. }