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.

OrderProductSolver.php 7.0KB

10 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace Lc\CaracoleBundle\Solver\Order;
  3. use Lc\CaracoleBundle\Model\Order\OrderProductInterface;
  4. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  5. use Lc\CaracoleBundle\Solver\Price\PriceSolver;
  6. use Lc\CaracoleBundle\Solver\Product\ProductFamilySolver;
  7. use Lc\CaracoleBundle\Solver\Product\ProductSolver;
  8. class OrderProductSolver
  9. {
  10. protected ProductSolver $productSolver;
  11. protected ProductFamilySolver $productFamilySolver;
  12. protected OrderProductReductionCatalogSolver $orderProductReductionCatalogSolver;
  13. protected PriceSolver $priceSolver;
  14. public function __construct(
  15. ProductSolver $productSolver,
  16. ProductFamilySolver $productFamilySolver,
  17. OrderProductReductionCatalogSolver $orderProductReductionCatalogSolver,
  18. PriceSolver $priceSolver
  19. ) {
  20. $this->productSolver = $productSolver;
  21. $this->productFamilySolver = $productFamilySolver;
  22. $this->orderProductReductionCatalogSolver = $orderProductReductionCatalogSolver;
  23. $this->priceSolver = $priceSolver;
  24. }
  25. public function compare(OrderProductInterface $orderProduct1, OrderProductInterface $orderProduct2)
  26. {
  27. return $orderProduct1->getProduct()->getId() == $orderProduct2->getProduct()->getId()
  28. && $orderProduct1->getRedelivery() == $orderProduct2->getRedelivery()
  29. && (string)$this->priceSolver->getPrice($orderProduct1)
  30. == (string)$this->priceSolver->getPrice($orderProduct2)
  31. && $this->orderProductReductionCatalogSolver->compare(
  32. $orderProduct1->getOrderProductReductionCatalog(),
  33. $orderProduct2->getOrderProductReductionCatalog()
  34. );
  35. }
  36. // groupOrderProductsByProductFamily
  37. public function getOrderProductsByProductFamily(array $orderProducts): array
  38. {
  39. $orderProductsByProductFamily = [];
  40. foreach ($orderProducts as $orderProduct) {
  41. if ($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {
  42. $productFamily = $orderProduct->getProduct()->getProductFamily();
  43. if (!isset($orderProductsByProductFamily[$productFamily->getId()])) {
  44. $orderProductsByProductFamily[$productFamily->getId()] = [
  45. 'order_products' => [],
  46. 'total_quantity_weight' => 0,
  47. ];
  48. }
  49. $orderProductsByProductFamily[$productFamily->getId()]['order_products'][] = $orderProduct;
  50. $orderProductsByProductFamily[$productFamily->getId(
  51. )]['total_quantity_weight'] += ($orderProduct->getQuantityProduct() / $orderProduct->getUnit(
  52. )->getCoefficient()) * $orderProduct->getQuantityOrder();
  53. }
  54. }
  55. return $orderProductsByProductFamily;
  56. }
  57. // getOrderProductsFormOrderShopsGroupByStorageOrder
  58. public function getOrderProductsByOrderShopsGroupByStorageOrder(array $orderShops): array
  59. {
  60. $orderProductsByStorageOrder = [];
  61. foreach ($orderShops as $orderShop) {
  62. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  63. if ($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {
  64. $productFamily = $orderProduct->getProduct()->getProductFamily();
  65. $storageOrder = 0;
  66. if ($productFamily->getStorageOrder()) {
  67. $storageOrder = $productFamily->getStorageOrder();
  68. }
  69. if (!isset($orderProductsByStorageOrder[$storageOrder])) {
  70. $orderProductsByStorageOrder[$productFamily->getId()] = [
  71. 'order_products' => [],
  72. 'total_quantity_weight' => 0,
  73. ];
  74. }
  75. $orderProductsByStorageOrder[$storageOrder]['order_products'][] = $orderProduct;
  76. $orderProductsByStorageOrder[$storageOrder]['total_quantity_weight'] += ($orderProduct->getQuantityProduct(
  77. ) / $orderProduct->getUnit()->getCoefficient()) * $orderProduct->getQuantityOrder();
  78. }
  79. }
  80. }
  81. return $orderProductsByStorageOrder;
  82. }
  83. public function getTitleOrderShop(OrderProductInterface $orderProduct)
  84. {
  85. $product = $orderProduct->getProduct();
  86. $productFamily = $product->getProductFamily();
  87. $titleProduct = $product->getTitle();
  88. $titleProductFamily = $productFamily->getTitle();
  89. if (strlen($titleProduct) > 0 && strlen($titleProductFamily) > 0) {
  90. $title = $titleProductFamily . ' - ' . $titleProduct;
  91. } else {
  92. $title = strlen($titleProduct) ? $titleProduct : $titleProductFamily;
  93. }
  94. if ($this->productFamilySolver->hasProductsWithVariousWeight($productFamily)) {
  95. $title .= ' - ' . $this->productSolver->getQuantityLabelInherited($product);
  96. }
  97. return $title;
  98. }
  99. public function getTitleProduct(OrderProductInterface $orderProduct): string
  100. {
  101. $product = $orderProduct->getProduct();
  102. $productFamily = $product->getProductFamily();
  103. $title = ($product->getTitle() && strlen($product->getTitle()) > 0) ? $product->getTitle() : '';
  104. if ($this->productFamilySolver->hasProductsWithVariousWeight($productFamily)) {
  105. if(strlen($title) > 0) {
  106. $title .= ' - ';
  107. }
  108. $title .= $this->productSolver->getQuantityLabelInherited($product);
  109. }
  110. return $title;
  111. }
  112. public function getTitleSummaryAfterAddCart(OrderProductInterface $orderProduct)
  113. {
  114. $title = '';
  115. $product = $orderProduct->getProduct();
  116. $productFamily = $product->getProductFamily();
  117. $titleProduct = $this->productSolver->getTitleInherited($product);
  118. $titleProductFamily = $productFamily->getTitle();
  119. // multiple
  120. if ($productFamily->getActiveProducts() && $productFamily->getBehaviorAddToCart() == 'multiple') {
  121. $title .= $titleProduct;
  122. if ($this->productFamilySolver->hasProductsWithVariousWeight($productFamily)) {
  123. $title .= ' - ' . $this->productSolver->getQuantityLabelInherited($product);
  124. }
  125. }
  126. // simple
  127. if ($productFamily->getBehaviorAddToCart() == 'simple') {
  128. if($orderProduct->getOrderPackProduct()) {
  129. $title .= $this->productFamilySolver->getTitleProduct($product);
  130. }
  131. else {
  132. if ($productFamily->getActiveProducts()) {
  133. $title .= $titleProduct;
  134. if ($this->productFamilySolver->hasProductsWithVariousWeight($productFamily)) {
  135. $title .= ' - ' . $this->productSolver->getQuantityLabelInherited($product);
  136. }
  137. }
  138. }
  139. }
  140. return $title;
  141. }
  142. }