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.

104 linhas
3.5KB

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