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 17KB

4 år sedan
4 år sedan
4 år sedan
4 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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\OrderPaymentInterface;
  9. use Lc\ShopBundle\Context\OrderReductionCartInterface;
  10. use Lc\ShopBundle\Context\OrderProductInterface;
  11. use Lc\ShopBundle\Context\OrderReductionCreditInterface;
  12. use Lc\ShopBundle\Context\OrderShopInterface;
  13. use Lc\ShopBundle\Context\OrderStatusHistoryInterface;
  14. use Lc\ShopBundle\Context\OrderStatusInterface;
  15. use Lc\ShopBundle\Context\PriceUtilsInterface;
  16. use Lc\ShopBundle\Context\ProductFamilyUtilsInterface;
  17. use Lc\ShopBundle\Context\ReductionCartInterface;
  18. use Lc\ShopBundle\Context\ReductionCreditInterface;
  19. use Lc\ShopBundle\Context\UserInterface;
  20. use Lc\ShopBundle\Model\Document;
  21. use Lc\ShopBundle\Form\Backend\Order\OrderReductionCreditType;
  22. use Lc\ShopBundle\Model\Product;
  23. use Lc\ShopBundle\Model\ProductFamily;
  24. use Lc\ShopBundle\Services\CreditUtils;
  25. use Lc\ShopBundle\Services\DocumentUtils;
  26. use Lc\ShopBundle\Services\Price\OrderShopPriceUtils;
  27. use Lc\ShopBundle\Services\UserUtils;
  28. use Lc\ShopBundle\Services\Utils;
  29. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  30. use Symfony\Component\Security\Core\Security;
  31. class OrderUtils
  32. {
  33. use OrderUtilsReductionTrait;
  34. use OrderUtilsStockTrait;
  35. use OrderUtilsPaymentTrait;
  36. use OrderUtilsDocumentTrait;
  37. use OrderUtilsCartTrait;
  38. protected $em;
  39. protected $security;
  40. protected $userUtils;
  41. protected $merchantUtils;
  42. protected $orderShopRepo;
  43. protected $reductionCreditRepo ;
  44. protected $orderReductionCreditRepo ;
  45. protected $priceUtils;
  46. protected $productFamilyUtils;
  47. protected $documentUtils;
  48. protected $utils;
  49. protected $creditUtils;
  50. public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils,
  51. MerchantUtilsInterface $merchantUtils, PriceUtilsInterface $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils,
  52. DocumentUtils $documentUtils, Utils $utils, CreditUtils $creditUtils)
  53. {
  54. $this->em = $em;
  55. $this->security = $security;
  56. $this->userUtils = $userUtils;
  57. $this->merchantUtils = $merchantUtils;
  58. $this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName());
  59. $this->reductionCreditRepo = $this->em->getRepository($this->em->getClassMetadata(ReductionCreditInterface::class)->getName());
  60. $this->orderReductionCreditRepo = $this->em->getRepository($this->em->getClassMetadata(OrderReductionCreditInterface::class)->getName());
  61. $this->priceUtils = $priceUtils;
  62. $this->productFamilyUtils = $productFamilyUtils;
  63. $this->documentUtils = $documentUtils;
  64. $this->utils = $utils;
  65. $this->creditUtils = $creditUtils;
  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']) {
  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. $orderShop = $this->changeOrderStatus('cart', $orderShop);
  89. return $orderShop;
  90. }
  91. public function addOrderProduct($orderShop, $orderProductAdd, $persist = true)
  92. {
  93. $return = false;
  94. $user = $this->security->getUser();
  95. $visitor = $this->userUtils->getVisitorCurrent();
  96. if (!$orderShop) {
  97. $orderShop = $this->createOrderShop([
  98. 'user' => $user,
  99. 'visitor' => $visitor,
  100. 'merchant' => $this->merchantUtils->getMerchantCurrent()
  101. ]);
  102. }
  103. if($this->isOrderProductAvailableAddCart($orderProductAdd, $orderShop)) {
  104. if ($orderProductAdd->getQuantityOrder() > 0) {
  105. $updated = false;
  106. $orderProductAdd->setTitle($orderProductAdd->getTitleOrderShop());
  107. $orderProductAdd->setPrice($this->priceUtils->getPrice($orderProductAdd->getProduct()));
  108. $orderProductAdd->setBuyingPrice($this->priceUtils->getBuyingPrice($orderProductAdd->getProduct()));
  109. $orderProductAdd->setUnit($orderProductAdd->getProduct()->getUnitInherited());
  110. $orderProductAdd->setTaxRate($orderProductAdd->getProduct()->getTaxRateInherited());
  111. $orderProductAdd->setQuantityProduct($orderProductAdd->getProduct()->getQuantityInherited());
  112. $productFamily = $this->productFamilyUtils->getProductFamilyBySlug($orderProductAdd->getProduct()->getProductFamily()->getSlug());
  113. $reductionCatalog = $productFamily->getReductionCatalog();
  114. if ($reductionCatalog) {
  115. $orderProductReductionCatalog = new OrderProductReductionCatalog();
  116. $orderProductReductionCatalog->setTitle($reductionCatalog->getTitle());
  117. $orderProductReductionCatalog->setValue($reductionCatalog->getValue());
  118. $orderProductReductionCatalog->setUnit($reductionCatalog->getUnit());
  119. $orderProductReductionCatalog->setBehaviorTaxRate($reductionCatalog->getBehaviorTaxRate());
  120. $orderProductAdd->setOrderProductReductionCatalog($orderProductReductionCatalog);
  121. }
  122. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  123. if ($orderProduct->getProduct()->getId() == $orderProductAdd->getProduct()->getId()
  124. && $orderProduct->getRedelivery() == $orderProductAdd->getRedelivery()
  125. && (string) $this->priceUtils->getPrice($orderProduct) == (string) $this->priceUtils->getPrice($orderProductAdd)
  126. && $this->compareOrderProductReductionCatalog($orderProduct->getOrderProductReductionCatalog(), $orderProductAdd->getOrderProductReductionCatalog())) {
  127. $orderProduct->setQuantityOrder($orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder());
  128. if ($persist) {
  129. $this->em->persist($orderProduct);
  130. }
  131. $updated = true;
  132. $return = true;
  133. break;
  134. }
  135. }
  136. if (!$updated) {
  137. $orderShop->addOrderProduct($orderProductAdd);
  138. if (isset($orderProductReductionCatalog)) {
  139. $this->em->persist($orderProductReductionCatalog);
  140. if ($persist) {
  141. if (isset($orderProductReductionCatalog)) {
  142. $this->em->persist($orderProductReductionCatalog);
  143. }
  144. $this->em->persist($orderProductAdd);
  145. $this->em->persist($orderShop);
  146. }
  147. }
  148. $return = true;
  149. }
  150. if ($persist) {
  151. $this->em->flush();
  152. }
  153. }
  154. }
  155. else {
  156. $availableQuantity = $orderProductAdd->getProduct()->getAvailableQuantityInherited() ;
  157. $textError = "Le produit <strong>".$orderProductAdd->getTitleOrderShop()."</strong> n'est pas disponible" ;
  158. if($availableQuantity !== false && $availableQuantity > 0) {
  159. $unit = '' ;
  160. if($orderProductAdd->getProduct()->getProductFamily()->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  161. $unit = $orderProductAdd->getProduct()->getUnitInherited()->getUnit() ;
  162. }
  163. $textError .= ' dans cette quantité ' ;
  164. $textError .= '<br />'.$availableQuantity.$unit.' disponible(s) dont '.$this->getQuantityOrderByProduct($orderShop, $orderProductAdd->getProduct()).$unit.' déjà dans votre panier.' ;
  165. }
  166. $this->utils->addFlash('error', $textError);
  167. }
  168. return $return ;
  169. }
  170. public function countQuantities($orderShop)
  171. {
  172. return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
  173. }
  174. public function countQuantitiesByOrderProducts($orderProducts = [])
  175. {
  176. $count = 0;
  177. foreach ($orderProducts as $orderProduct) {
  178. $count += $orderProduct->getQuantityOrder();
  179. }
  180. return $count;
  181. }
  182. public function getOrderProductsByParentCategory($orderShop = null)
  183. {
  184. $categoriesArray = [];
  185. if ($orderShop) {
  186. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  187. $productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
  188. $category = $productCategories[0]->getParentCategory();
  189. $labelCategory = $category->getTitle();
  190. if (!isset($categoriesArray[$labelCategory])) {
  191. $categoriesArray[$labelCategory] = [];
  192. }
  193. $categoriesArray[$labelCategory][] = $orderProduct;
  194. }
  195. }
  196. return $categoriesArray;
  197. }
  198. public function getOrderDatas($order = null)
  199. {
  200. $data = [];
  201. if (!$order) {
  202. $order = $this->getCartCurrent();
  203. }
  204. $data['order'] = $order;
  205. if ($order) {
  206. $data['count'] = $this->countQuantities($order);
  207. $data['total_with_tax'] = $this->priceUtils->getTotalWithTax($order);
  208. $data['order_products_by_category'] = $this->getOrderProductsByParentCategory($order);
  209. }
  210. return $data;
  211. }
  212. public function getOrderAsJsonObject(OrderShopInterface $order)
  213. {
  214. $data['id'] = $order->getId();
  215. $data['user'] = $order->getUser()->getSummary();
  216. $data['orderStatus'] = $order->getOrderStatus()->__tosString();
  217. $data['deliveryAddress'] = $order->getDeliveryAddress()->getSummary();
  218. $data['invoiceAddress'] = $order->getInvoiceAddress()->getSummary();
  219. $data['total'] = $this->priceUtils->getTotal($order);
  220. $data['totalWithTax'] = $this->priceUtils->getTotalWithTax($order);
  221. $data['totalWithTaxAndReduction'] = $this->priceUtils->getTotalWithTax($order);
  222. $i = 0;
  223. foreach ($this->getOrderProductsByParentCategory($order) as $labelCategory => $orderProducts) {
  224. foreach ($orderProducts as $orderProduct) {
  225. $data['orderProducts'][$i]['id'] = $orderProduct->getId();
  226. $data['orderProducts'][$i]['product'] = $orderProduct->getProduct()->getId();
  227. $data['orderProducts'][$i]['quantityOrder'] = $orderProduct->getQuantityOrder();
  228. $data['orderProducts'][$i]['labelCategory'] = $labelCategory;
  229. $data['orderProducts'][$i]['title'] = $orderProduct->getTitle();
  230. $data['orderProducts'][$i]['price'] = $this->priceUtils->getPrice($orderProduct);
  231. $data['orderProducts'][$i]['priceWithTax'] = $this->priceUtils->getPriceWithTax($orderProduct);
  232. $data['orderProducts'][$i]['priceWithTaxAndReduction'] = $this->priceUtils->getPriceWithTaxAndReduction($orderProduct);
  233. $data['orderProducts'][$i]['quantity'] = $orderProduct->getQuantityOrder();
  234. $data['orderProducts'][$i]['totalWithTaxAndReduction'] = $this->priceUtils->getTotalOrderProductsWithTaxAndReduction(array($orderProduct));
  235. $i++;
  236. }
  237. }
  238. return $data;
  239. }
  240. public function newOrderStatusHistory($order, $status, $origin = 'user')
  241. {
  242. $orderStatusHistoryClass = $this->em->getClassMetadata(OrderStatusHistoryInterface::class);
  243. $orderStatusHistory = new $orderStatusHistoryClass->name;
  244. $orderStatusHistory->setOrderShop($order);
  245. $orderStatusHistory->setOrderStatus($status);
  246. $orderStatusHistory->setOrigin($origin);
  247. $this->em->persist($orderStatusHistory);
  248. }
  249. public function mergeOrderShops($orderShop1, $orderShop2, $persist = true)
  250. {
  251. if ($orderShop1 && $orderShop2) {
  252. foreach ($orderShop2->getOrderProducts() as $orderProduct) {
  253. $this->addOrderProduct($orderShop1, $orderProduct);
  254. if($persist) {
  255. $this->em->remove($orderProduct);
  256. }
  257. }
  258. if($persist) {
  259. $this->em->remove($orderShop2);
  260. $this->em->persist($orderShop1);
  261. $this->em->flush();
  262. }
  263. return $orderShop1;
  264. }
  265. }
  266. public function groupOrderProductsByProductFamily($orderProducts)
  267. {
  268. $orderProductsByProductFamily = [];
  269. foreach ($orderProducts as $orderProduct) {
  270. if ($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {
  271. $productFamily = $orderProduct->getProduct()->getProductFamily();
  272. if (!isset($orderProductsByProductFamily[$productFamily->getId()])) {
  273. $orderProductsByProductFamily[$productFamily->getId()] = [
  274. 'order_products' => [],
  275. 'total_quantity_weight' => 0,
  276. ];
  277. }
  278. $orderProductsByProductFamily[$productFamily->getId()]['order_products'][] = $orderProduct;
  279. $orderProductsByProductFamily[$productFamily->getId()]['total_quantity_weight'] += ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient()) * $orderProduct->getQuantityOrder();
  280. }
  281. }
  282. return $orderProductsByProductFamily;
  283. }
  284. }