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.

107 satır
3.8KB

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