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.

205 line
6.8KB

  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 resetRelationsJoin(): void
  24. {
  25. $this->isJoinSections = false;
  26. $this->isJoinProductCategories = false;
  27. $this->isJoinProductFamilySectionProperties = false;
  28. $this->isJoinProducts = false;
  29. $this->isJoinQualityLabels = false;
  30. }
  31. public function joinProductFamilySectionProperties(bool $addSelect = true): self
  32. {
  33. if (!$this->isJoinProductFamilySectionProperties) {
  34. $this->isJoinProductFamilySectionProperties = true;
  35. $this->leftJoin('.productFamilySectionProperties', 'productFamilySectionProperties');
  36. if ($addSelect) {
  37. //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)
  38. //$this->addSelect('productFamilySectionProperties');
  39. }
  40. }
  41. return $this;
  42. }
  43. public function joinQualityLabels(bool $addSelect = true): self
  44. {
  45. if (!$this->isJoinQualityLabels) {
  46. $this->isJoinQualityLabels = true;
  47. $this->leftJoin('.qualityLabels', 'qualityLabels');
  48. if ($addSelect) {
  49. //$this->addSelect('qualityLabels');
  50. }
  51. }
  52. return $this;
  53. }
  54. public function joinSections(bool $addSelect = true): self
  55. {
  56. if (!$this->isJoinSections) {
  57. $this->isJoinSections = true;
  58. $this->leftJoin('productFamilySectionProperties.section', 'section');
  59. if ($addSelect) {
  60. $this->addSelect('section');
  61. }
  62. }
  63. return $this;
  64. }
  65. public function filterBySection(SectionInterface $section, bool $addSelectProductFamilySectionProperties = true)
  66. {
  67. $this->joinProductFamilySectionProperties($addSelectProductFamilySectionProperties);
  68. $this->andWhereSection('productFamilySectionProperties', $section);
  69. $this->andWhere('productFamilySectionProperties.status = 1');
  70. }
  71. public function filterByMerchantViaSection(MerchantInterface $merchant)
  72. {
  73. $this->joinProductFamilySectionProperties(false);
  74. $this->joinSections(false);
  75. $this->andWhereMerchant('section', $merchant);
  76. //$this->andWhere('productFamilySectionProperties.status = 1');
  77. }
  78. public function selectCount(): self
  79. {
  80. return $this->select('count(productFamily.id)');
  81. }
  82. public function selectProductCategories(): self
  83. {
  84. $this->joinProductCategories();
  85. return $this->addSelect('pCategories');
  86. }
  87. public function filterLikeBehaviorStockCycle(string $behaviorStockCycle): self
  88. {
  89. return $this
  90. ->andWhere('.behaviorStockCycle LIKE :behaviorStockCycle')
  91. ->setParameter('behaviorStockCycle', $behaviorStockCycle);
  92. }
  93. public function filterByProductCategory(ProductCategoryInterface $category): self
  94. {
  95. $this->joinProductCategories();
  96. return $this
  97. ->andWhere(':category MEMBER OF .productCategories')
  98. ->setParameter('category', $category->getId());
  99. }
  100. public function filterByProductCategoryArray(array $categoryArray): self
  101. {
  102. $this->joinProductCategories();
  103. return $this
  104. ->andWhere(':categoryArray MEMBER OF .productCategories')
  105. ->setParameter('categoryArray', $categoryArray);
  106. }
  107. public function filterByPropertyNoveltyExpirationDate($dateTime = null): self
  108. {
  109. if (is_null($dateTime)) {
  110. $dateTime = new \DateTime();
  111. }
  112. return $this->andWhere(':now <= .propertyNoveltyExpirationDate')
  113. ->setParameter('now', $dateTime);
  114. }
  115. public function filterIsOrganicLabel(): self
  116. {
  117. return $this->innerJoin('.qualityLabels', 'qualityLabel')
  118. ->andWhere('qualityLabel.devAlias IN (:organicLabels)')
  119. ->setParameter(':organicLabels', ProductFamilyModel::$organicLabels);
  120. }
  121. public function filterIsNovelty()
  122. {
  123. return $this->andWhere(':now <= .propertyNoveltyExpirationDate')
  124. ->setParameter('now', new \DateTime());
  125. }
  126. public function filterByTerms($terms): self
  127. {
  128. $this->andWhere('.title LIKE :terms OR productCategories.title LIKE :terms');
  129. $this->setParameter(':terms', '%' . $terms . '%');
  130. return $this;
  131. }
  132. public function filterBySupplier($supplier): self
  133. {
  134. return $this->andWhereEqual('supplier', $supplier);
  135. }
  136. public function joinProductCategories(bool $addSelect = true): self
  137. {
  138. if (!$this->isJoinProductCategories) {
  139. $this->isJoinProductCategories = true;
  140. $this->leftJoin('.productCategories', 'productCategories');
  141. if ($addSelect) {
  142. //Commenté sinon doctrine n'hydrate pas correctement les catégories liés au ProductFamily (exemple : un seul sur trois)
  143. //$this->addSelect('productCategories');
  144. }
  145. return $this;
  146. }
  147. return $this;
  148. }
  149. public function selectProducts(): self
  150. {
  151. return $this->joinProducts(true);
  152. }
  153. public function joinProducts(bool $addSelect = true): self
  154. {
  155. if (!$this->isJoinProducts) {
  156. $this->isJoinProducts = true;
  157. $this->innerJoin('.products', 'products');
  158. if ($addSelect) {
  159. // Décommenté sinon doctrine n'hydrate pas correctement les produits liés au ProductFamily (exemple : un seul sur deux)
  160. // $this->addSelect('products');
  161. }
  162. }
  163. return $this;
  164. }
  165. public function orderByPosition(): self
  166. {
  167. $this->orderBy('e.position', 'ASC');
  168. return $this;
  169. }
  170. }