Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

340 linhas
17KB

  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->setUnit($orderProductAdd->getProduct()->getUnitInherited());
  109. $orderProductAdd->setTaxRate($orderProductAdd->getProduct()->getTaxRateInherited());
  110. $orderProductAdd->setQuantityProduct($orderProductAdd->getProduct()->getQuantityInherited());
  111. $productFamily = $this->productFamilyUtils->getProductFamilyBySlug($orderProductAdd->getProduct()->getProductFamily()->getSlug());
  112. $reductionCatalog = $productFamily->getReductionCatalog();
  113. if ($reductionCatalog) {
  114. $orderProductReductionCatalog = new OrderProductReductionCatalog();
  115. $orderProductReductionCatalog->setTitle($reductionCatalog->getTitle());
  116. $orderProductReductionCatalog->setValue($reductionCatalog->getValue());
  117. $orderProductReductionCatalog->setUnit($reductionCatalog->getUnit());
  118. $orderProductReductionCatalog->setBehaviorTaxRate($reductionCatalog->getBehaviorTaxRate());
  119. $orderProductAdd->setOrderProductReductionCatalog($orderProductReductionCatalog);
  120. }
  121. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  122. if ($orderProduct->getProduct()->getId() == $orderProductAdd->getProduct()->getId()
  123. && $orderProduct->getRedelivery() == $orderProductAdd->getRedelivery()
  124. && (string) $this->priceUtils->getPrice($orderProduct) == (string) $this->priceUtils->getPrice($orderProductAdd)
  125. && $this->compareOrderProductReductionCatalog($orderProduct->getOrderProductReductionCatalog(), $orderProductAdd->getOrderProductReductionCatalog())) {
  126. $orderProduct->setQuantityOrder($orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder());
  127. if ($persist) {
  128. $this->em->persist($orderProduct);
  129. }
  130. $updated = true;
  131. $return = true;
  132. break;
  133. }
  134. }
  135. if (!$updated) {
  136. $orderShop->addOrderProduct($orderProductAdd);
  137. if (isset($orderProductReductionCatalog)) {
  138. $this->em->persist($orderProductReductionCatalog);
  139. if ($persist) {
  140. if (isset($orderProductReductionCatalog)) {
  141. $this->em->persist($orderProductReductionCatalog);
  142. }
  143. $this->em->persist($orderProductAdd);
  144. $this->em->persist($orderShop);
  145. }
  146. }
  147. $return = true;
  148. }
  149. if ($persist) {
  150. $this->em->flush();
  151. }
  152. }
  153. }
  154. else {
  155. $availableQuantity = $orderProductAdd->getProduct()->getAvailableQuantityInherited() ;
  156. $textError = "Le produit <strong>".$orderProductAdd->getTitleOrderShop()."</strong> n'est pas disponible" ;
  157. if($availableQuantity !== false && $availableQuantity > 0) {
  158. $unit = '' ;
  159. if($orderProductAdd->getProduct()->getProductFamily()->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  160. $unit = $orderProductAdd->getProduct()->getUnitInherited()->getUnit() ;
  161. }
  162. $textError .= ' dans cette quantité ' ;
  163. $textError .= '<br />'.$availableQuantity.$unit.' disponible(s) dont '.$this->getQuantityOrderByProduct($orderShop, $orderProductAdd->getProduct()).$unit.' déjà dans votre panier.' ;
  164. }
  165. $this->utils->addFlash('error', $textError);
  166. }
  167. return $return ;
  168. }
  169. public function countQuantities($orderShop)
  170. {
  171. return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
  172. }
  173. public function countQuantitiesByOrderProducts($orderProducts = [])
  174. {
  175. $count = 0;
  176. foreach ($orderProducts as $orderProduct) {
  177. $count += $orderProduct->getQuantityOrder();
  178. }
  179. return $count;
  180. }
  181. public function getOrderProductsByParentCategory($orderShop = null)
  182. {
  183. $categoriesArray = [];
  184. if ($orderShop) {
  185. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  186. $productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
  187. $category = $productCategories[0]->getParentCategory();
  188. $labelCategory = $category->getTitle();
  189. if (!isset($categoriesArray[$labelCategory])) {
  190. $categoriesArray[$labelCategory] = [];
  191. }
  192. $categoriesArray[$labelCategory][] = $orderProduct;
  193. }
  194. }
  195. return $categoriesArray;
  196. }
  197. public function getOrderDatas($order = null)
  198. {
  199. $data = [];
  200. if (!$order) {
  201. $order = $this->getCartCurrent();
  202. }
  203. $data['order'] = $order;
  204. if ($order) {
  205. $data['count'] = $this->countQuantities($order);
  206. $data['total_with_tax'] = $this->priceUtils->getTotalWithTax($order);
  207. $data['order_products_by_category'] = $this->getOrderProductsByParentCategory($order);
  208. }
  209. return $data;
  210. }
  211. public function getOrderAsJsonObject(OrderShopInterface $order)
  212. {
  213. $data['id'] = $order->getId();
  214. $data['user'] = $order->getUser()->getSummary();
  215. $data['orderStatus'] = $order->getOrderStatus()->__tosString();
  216. $data['deliveryAddress'] = $order->getDeliveryAddress()->getSummary();
  217. $data['invoiceAddress'] = $order->getInvoiceAddress()->getSummary();
  218. $data['total'] = $this->priceUtils->getTotal($order);
  219. $data['totalWithTax'] = $this->priceUtils->getTotalWithTax($order);
  220. $data['totalWithTaxAndReduction'] = $this->priceUtils->getTotalWithTax($order);
  221. $i = 0;
  222. foreach ($this->getOrderProductsByParentCategory($order) as $labelCategory => $orderProducts) {
  223. foreach ($orderProducts as $orderProduct) {
  224. $data['orderProducts'][$i]['id'] = $orderProduct->getId();
  225. $data['orderProducts'][$i]['product'] = $orderProduct->getProduct()->getId();
  226. $data['orderProducts'][$i]['quantityOrder'] = $orderProduct->getQuantityOrder();
  227. $data['orderProducts'][$i]['labelCategory'] = $labelCategory;
  228. $data['orderProducts'][$i]['title'] = $orderProduct->getTitle();
  229. $data['orderProducts'][$i]['price'] = $this->priceUtils->getPrice($orderProduct);
  230. $data['orderProducts'][$i]['priceWithTax'] = $this->priceUtils->getPriceWithTax($orderProduct);
  231. $data['orderProducts'][$i]['priceWithTaxAndReduction'] = $this->priceUtils->getPriceWithTaxAndReduction($orderProduct);
  232. $data['orderProducts'][$i]['quantity'] = $orderProduct->getQuantityOrder();
  233. $data['orderProducts'][$i]['totalWithTaxAndReduction'] = $this->priceUtils->getTotalOrderProductsWithTaxAndReduction(array($orderProduct));
  234. $i++;
  235. }
  236. }
  237. return $data;
  238. }
  239. public function newOrderStatusHistory($order, $status, $origin = 'user')
  240. {
  241. $orderStatusHistoryClass = $this->em->getClassMetadata(OrderStatusHistoryInterface::class);
  242. $orderStatusHistory = new $orderStatusHistoryClass->name;
  243. $orderStatusHistory->setOrderShop($order);
  244. $orderStatusHistory->setOrderStatus($status);
  245. $orderStatusHistory->setOrigin($origin);
  246. $this->em->persist($orderStatusHistory);
  247. }
  248. public function mergeOrderShops($orderShop1, $orderShop2, $persist = true)
  249. {
  250. if ($orderShop1 && $orderShop2) {
  251. foreach ($orderShop2->getOrderProducts() as $orderProduct) {
  252. $this->addOrderProduct($orderShop1, $orderProduct);
  253. if($persist) {
  254. $this->em->remove($orderProduct);
  255. }
  256. }
  257. if($persist) {
  258. $this->em->remove($orderShop2);
  259. $this->em->persist($orderShop1);
  260. $this->em->flush();
  261. }
  262. return $orderShop1;
  263. }
  264. }
  265. public function groupOrderProductsByProductFamily($orderProducts)
  266. {
  267. $orderProductsByProductFamily = [];
  268. foreach ($orderProducts as $orderProduct) {
  269. if ($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {
  270. $productFamily = $orderProduct->getProduct()->getProductFamily();
  271. if (!isset($orderProductsByProductFamily[$productFamily->getId()])) {
  272. $orderProductsByProductFamily[$productFamily->getId()] = [
  273. 'order_products' => [],
  274. 'total_quantity_weight' => 0,
  275. ];
  276. }
  277. $orderProductsByProductFamily[$productFamily->getId()]['order_products'][] = $orderProduct;
  278. $orderProductsByProductFamily[$productFamily->getId()]['total_quantity_weight'] += ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient()) * $orderProduct->getQuantityOrder();
  279. }
  280. }
  281. return $orderProductsByProductFamily;
  282. }
  283. }