Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

189 lines
5.9KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Repository\Product;
  3. use Knp\Component\Pager\PaginatorInterface;
  4. use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
  5. use Lc\CaracoleBundle\Model\Product\ProductCategoryInterface;
  6. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  7. use Lc\CaracoleBundle\Repository\SectionRepositoryQueryTrait;
  8. use Lc\SovBundle\Repository\AbstractRepositoryQuery;
  9. class ProductFamilyRepositoryQuery extends AbstractRepositoryQuery
  10. {
  11. protected bool $isJoinSections = false;
  12. protected bool $isJoinProductCategories = false;
  13. protected bool $isJoinProductFamilySectionProperties = false;
  14. protected bool $isJoinProducts = false;
  15. protected bool $isJoinQualityLabels = false;
  16. public function __construct(ProductFamilyRepository $repository, PaginatorInterface $paginator)
  17. {
  18. parent::__construct($repository, 'productFamily', $paginator);
  19. }
  20. public function joinProductFamilySectionProperties(bool $addSelect = true): self
  21. {
  22. if (!$this->isJoinProductFamilySectionProperties) {
  23. $this->isJoinProductFamilySectionProperties = true;
  24. $this->leftJoin('.productFamilySectionProperties', 'productFamilySectionProperties');
  25. if ($addSelect) {
  26. //NB : Ici le select est en commentaire car si il est actif doctrine n'hydrate pas correectement ProductFamilySectionProperties (si filtre sur section les ProductFamilySectionProperties des autres sections ne sont pas chargé et ça peut être problématique pr la gestion des stocks)
  27. //$this->addSelect('productFamilySectionProperties');
  28. }
  29. }
  30. return $this;
  31. }
  32. public function joinQualityLabels(bool $addSelect = true): self
  33. {
  34. if (!$this->isJoinQualityLabels) {
  35. $this->isJoinQualityLabels = true;
  36. $this->leftJoin('.qualityLabels', 'qualityLabels');
  37. if ($addSelect) {
  38. $this->addSelect('qualityLabels');
  39. }
  40. }
  41. return $this;
  42. }
  43. public function joinSections(bool $addSelect = true): self
  44. {
  45. if (!$this->isJoinSections) {
  46. $this->isJoinSections = true;
  47. $this->leftJoin('productFamilySectionProperties.section', 'section');
  48. if ($addSelect) {
  49. $this->addSelect('section');
  50. }
  51. }
  52. return $this;
  53. }
  54. public function filterBySection(SectionInterface $section, bool $addSelectProductFamilySectionProperties = true)
  55. {
  56. $this->joinProductFamilySectionProperties($addSelectProductFamilySectionProperties);
  57. $this->andWhereSection('productFamilySectionProperties', $section);
  58. $this->andWhere('productFamilySectionProperties.status = 1');
  59. }
  60. public function filterByMerchantViaSection(MerchantInterface $merchant)
  61. {
  62. $this->joinProductFamilySectionProperties(false);
  63. $this->joinSections(false);
  64. $this->andWhereMerchant('section', $merchant);
  65. $this->andWhere('productFamilySectionProperties.status = 1');
  66. }
  67. public function selectCount(): self
  68. {
  69. return $this->select('count(productFamily.id)');
  70. }
  71. public function selectProductCategories(): self
  72. {
  73. $this->joinProductCategories();
  74. return $this->addSelect('pCategories');
  75. }
  76. public function filterLikeBehaviorStockCycle(string $behaviorStockCycle): self
  77. {
  78. return $this
  79. ->andWhere('.behaviorStockCycle LIKE :behaviorStockCycle')
  80. ->setParameter('behaviorStockCycle', $behaviorStockCycle);
  81. }
  82. public function filterByProductCategory(ProductCategoryInterface $category): self
  83. {
  84. $this->joinProductCategories();
  85. return $this
  86. ->andWhere(':category MEMBER OF .productCategories')
  87. ->setParameter('category', $category->getId());
  88. }
  89. public function filterByProductCategoryArray(Array $categoryArray): self
  90. {
  91. $this->joinProductCategories();
  92. return $this
  93. ->andWhere(':categoryArray MEMBER OF .productCategories')
  94. ->setParameter('categoryArray', $categoryArray);
  95. }
  96. public function filterByPropertyNoveltyExpirationDate($dateTime = null): self
  97. {
  98. if (is_null($dateTime)) {
  99. $dateTime = new \DateTime();
  100. }
  101. return $this->andWhere(':now <= .propertyNoveltyExpirationDate')
  102. ->setParameter('now', $dateTime);
  103. }
  104. public function filterIsOrganicLabel(): self
  105. {
  106. return $this->andWhere('.propertyOrganicLabel IS NOT NULL');
  107. }
  108. public function filterIsNovelty()
  109. {
  110. return $this->andWhere(':now <= .propertyNoveltyExpirationDate')
  111. ->setParameter('now', new \DateTime());
  112. }
  113. public function filterByTerms($terms): self
  114. {
  115. $this->andWhere('.title LIKE :terms OR productCategories.title LIKE :terms');
  116. $this->setParameter(':terms', '%' . $terms . '%');
  117. return $this;
  118. }
  119. public function filterBySupplier($supplier): self
  120. {
  121. return $this->andWhereEqual('supplier', $supplier);
  122. }
  123. public function joinProductCategories(bool $addSelect = false): self
  124. {
  125. if (!$this->isJoinProductCategories) {
  126. $this->isJoinProductCategories = true;
  127. $this->leftJoin('.productCategories', 'productCategories');
  128. if ($addSelect) {
  129. $this->addSelect('productCategories');
  130. }
  131. return $this;
  132. }
  133. return $this;
  134. }
  135. public function selectProducts(): self
  136. {
  137. return $this->joinProducts(true);
  138. }
  139. public function joinProducts(bool $addSelect = true): self
  140. {
  141. if (!$this->isJoinProducts) {
  142. $this->isJoinProducts = true;
  143. $this->innerJoin('.products', 'products');
  144. if ($addSelect) {
  145. $this->addSelect('products');
  146. }
  147. }
  148. return $this;
  149. }
  150. public function orderByPosition(): self
  151. {
  152. $this->orderBy('e.position', 'ASC');
  153. return $this;
  154. }
  155. }