Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

194 lines
6.3KB

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