選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

124 行
4.4KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Resolver;
  3. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  4. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  5. use Lc\CaracoleBundle\Model\Product\ProductFamilyModel;
  6. use Lc\CaracoleBundle\Solver\Order\OrderShopSolver;
  7. use Lc\CaracoleBundle\Solver\Price\PriceSolver;
  8. use Lc\CaracoleBundle\Solver\Product\ProductFamilySolver;
  9. class ProductFamilyResolver
  10. {
  11. protected PriceSolver $priceSolver;
  12. protected ProductFamilySolver $productFamilySolver;
  13. protected OrderShopSolver $orderShopSolver;
  14. public function __construct(PriceSolver $priceSolver, ProductFamilySolver $productFamilySolver, OrderShopSolver $orderShopSolver)
  15. {
  16. $this->priceSolver = $priceSolver;
  17. $this->productFamilySolver = $productFamilySolver;
  18. $this->orderShopSolver = $orderShopSolver;
  19. }
  20. public function getMultiplyingFactor(ProductFamilyInterface $productFamily)
  21. {
  22. if ($productFamily->getBehaviorPrice() == ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE) {
  23. if ($productFamily->getBuyingPrice() > 0) {
  24. return number_format(
  25. $this->priceSolver->getPriceWithTax($productFamily) / $productFamily->getBuyingPrice(),
  26. 3
  27. );
  28. }
  29. } elseif ($productFamily->getBehaviorPrice() == ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT) {
  30. if ($productFamily->getBuyingPriceByRefUnit() > 0) {
  31. return number_format(
  32. $this->priceSolver->getPriceByRefUnitWithTax(
  33. $productFamily
  34. ) / $productFamily->getBuyingPriceByRefUnit(),
  35. 3
  36. );
  37. }
  38. }
  39. }
  40. public function getCheapestProduct(ProductFamilyInterface $productFamily, OrderShopInterface $orderShop)
  41. {
  42. $priceSolver = $this->priceSolver;
  43. return $this->getCheapestOrMostExpensiveProduct(
  44. $productFamily,
  45. function ($a, $b) use ($priceSolver) {
  46. return $priceSolver->getPriceWithTaxAndReduction(
  47. $a
  48. ) > $priceSolver->getPriceWithTaxAndReduction($b);
  49. },
  50. true,
  51. $orderShop
  52. );
  53. }
  54. public function getCheapestProductByRefUnit(ProductFamilyInterface $productFamily, OrderShopInterface $orderShop)
  55. {
  56. $priceSolver = $this->priceSolver;
  57. return $this->getCheapestOrMostExpensiveProduct(
  58. $productFamily,
  59. function ($a, $b) use ($priceSolver) {
  60. return $priceSolver->getPriceByRefUnitWithTaxAndReduction(
  61. $a
  62. ) > $priceSolver->getPriceByRefUnitWithTaxAndReduction($b);
  63. },
  64. false,
  65. $orderShop
  66. );
  67. }
  68. public function getMostExpensiveProductByRefUnit(ProductFamilyInterface $productFamily, OrderShopInterface $orderShop)
  69. {
  70. $priceSolver = $this->priceSolver;
  71. return $this->getCheapestOrMostExpensiveProduct(
  72. $productFamily,
  73. function ($a, $b) use ($priceSolver) {
  74. return $priceSolver->getPriceByRefUnitWithTaxAndReduction(
  75. $a
  76. ) < $priceSolver->getPriceByRefUnitWithTaxAndReduction($b);
  77. },
  78. false,
  79. $orderShop
  80. );
  81. }
  82. private function getCheapestOrMostExpensiveProduct(
  83. ProductFamilyInterface $productFamily,
  84. $comparisonFunction,
  85. $returnSelfIfNotActiveProducts,
  86. OrderShopInterface $orderShop
  87. )
  88. {
  89. if ($productFamily->getActiveProducts()) {
  90. $products = $this->productFamilySolver->getProductsOnline($productFamily)->getValues();
  91. if (count($products) > 0) {
  92. usort($products, $comparisonFunction);
  93. foreach ($products as $product) {
  94. if ($this->orderShopSolver->isProductAvailable($orderShop->getSection(), $orderShop, $product, 1, true)) {
  95. return $product;
  96. }
  97. }
  98. return $products[0];
  99. }
  100. } else {
  101. return $this->productFamilySolver->getOriginProduct($productFamily);
  102. }
  103. if ($returnSelfIfNotActiveProducts) {
  104. return $productFamily;
  105. } else {
  106. return false;
  107. }
  108. }
  109. }