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

153 行
6.4KB

  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 getTitleSummaryAfterAddCart(OrderProductInterface $orderProduct)
  100. {
  101. $title = '';
  102. $product = $orderProduct->getProduct();
  103. $productFamily = $product->getProductFamily();
  104. $titleProduct = $this->productSolver->getTitleInherited($product);
  105. $titleProductFamily = $productFamily->getTitle();
  106. // multiple
  107. if ($productFamily->getActiveProducts() && $productFamily->getBehaviorAddToCart() == 'multiple') {
  108. $title .= $titleProduct;
  109. if ($this->productFamilySolver->hasProductsWithVariousWeight($productFamily)) {
  110. $title .= ' - ' . $this->productSolver->getQuantityLabelInherited($product);
  111. }
  112. }
  113. // simple
  114. if ($productFamily->getBehaviorAddToCart() == 'simple') {
  115. if($orderProduct->getOrderPackProduct()) {
  116. $title .= $this->productFamilySolver->getTitleProduct($product);
  117. }
  118. else {
  119. if ($productFamily->getActiveProducts()) {
  120. $title .= $titleProduct;
  121. if ($this->productFamilySolver->hasProductsWithVariousWeight($productFamily)) {
  122. $title .= ' - ' . $this->productSolver->getQuantityLabelInherited($product);
  123. }
  124. }
  125. }
  126. }
  127. return $title;
  128. }
  129. }