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.

124 lines
3.4KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Repository\Product;
  3. use Lc\CaracoleBundle\Model\Product\ProductCategoryInterface;
  4. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  5. use Lc\CaracoleBundle\Model\Reduction\ReductionCatalogInterface;
  6. use Lc\CaracoleBundle\Repository\SectionStoreTrait;
  7. use Lc\CaracoleBundle\Solver\Price\PriceSolver;
  8. use Lc\SovBundle\Repository\AbstractStore;
  9. use Lc\SovBundle\Repository\RepositoryQueryInterface;
  10. class ProductFamilyStore extends AbstractStore
  11. {
  12. use SectionStoreTrait;
  13. protected ProductFamilyRepositoryQuery $query;
  14. protected PriceSolver $priceSolver;
  15. public function __construct(ProductFamilyRepositoryQuery $query, PriceSolver $priceSolver)
  16. {
  17. $this->query = $query;
  18. $this->priceSolver = $priceSolver;
  19. }
  20. public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
  21. {
  22. $query->orderBy('position');
  23. }
  24. public function filtersDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
  25. {
  26. $query->filterBySection($this->section);
  27. return $query;
  28. }
  29. public function relationsDefault($query): RepositoryQueryInterface
  30. {
  31. $query->joinProductCategories();
  32. $query->joinProducts();
  33. return $query;
  34. }
  35. //getProductFamiliesByCategory
  36. public function getByCategory(ProductCategoryInterface $productCategory, $query = null)
  37. {
  38. $query = $this->createDefaultQuery($query);
  39. $query
  40. ->filterIsOnline()
  41. ->filterByProductCategory($productCategory);
  42. return $query->find();
  43. }
  44. //getProductFamiliesNovelties
  45. public function getByNovelties($query = null)
  46. {
  47. $query = $this->createDefaultQuery($query);
  48. $query
  49. ->filterByPropertyNoveltyExpirationDate()
  50. ->filterIsOnline();
  51. return $query->find();
  52. }
  53. //getProductFamiliesOrganics
  54. public function getOrganics($query = null)
  55. {
  56. $query = $this->createDefaultQuery($query);
  57. $query
  58. ->filterPropertyOrganicLabel()
  59. ->filterIsOnline();
  60. return $query->find();
  61. }
  62. //findByTerms
  63. public function getByTerms($terms, $maxResults = false, $query = null)
  64. {
  65. $query = $this->createDefaultQuery($query);
  66. $query->filterIsOnline();
  67. $query->groupBy('id');
  68. if($maxResults) {
  69. $query->limit($maxResults);
  70. }
  71. return $query->find();
  72. }
  73. public function getBestReductionCatalog(
  74. ProductFamilyInterface $productFamily,
  75. ReductionCatalogInterface $reductionCatalog1,
  76. ReductionCatalogInterface $reductionCatalog2
  77. ) {
  78. $price1 = $this->priceSolver->applyReductionCatalog(
  79. $productFamily,
  80. $this->priceSolver->getPrice($productFamily),
  81. $this->priceSolver->getPriceWithTax($productFamily),
  82. 1,
  83. $reductionCatalog1
  84. );
  85. $price2 = $this->priceSolver->applyReductionCatalog(
  86. $productFamily,
  87. $this->priceSolver->getPrice($productFamily),
  88. $this->priceSolver->getPriceWithTax($productFamily),
  89. 1,
  90. $reductionCatalog2
  91. );
  92. if ($price1 > $price2) {
  93. return $reductionCatalog2;
  94. } else {
  95. return $reductionCatalog1;
  96. }
  97. }
  98. }