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.

ReductionCatalogRepositoryQuery.php 5.0KB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace Lc\CaracoleBundle\Repository\Reduction;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Knp\Component\Pager\PaginatorInterface;
  5. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  6. use Lc\CaracoleBundle\Repository\MerchantRepositoryQueryTrait;
  7. use Lc\SovBundle\Model\User\UserInterface;
  8. use Lc\SovBundle\Repository\AbstractRepositoryQuery;
  9. class ReductionCatalogRepositoryQuery extends AbstractRepositoryQuery
  10. {
  11. protected bool $isJoinProductFamilies = false;
  12. protected bool $isJoinProductFamily = false;
  13. protected bool $isJoinProductCategories = false;
  14. protected bool $isJoinUsers = false;
  15. protected bool $isJoinGroupUsers = false;
  16. use MerchantRepositoryQueryTrait;
  17. public function __construct(ReductionCatalogRepository $repository, PaginatorInterface $paginator)
  18. {
  19. parent::__construct($repository, 'r', $paginator);
  20. }
  21. public function joinUsers()
  22. {
  23. if (!$this->isJoinUsers) {
  24. $this->isJoinUsers = true;
  25. return $this
  26. ->leftJoin('.users', 'pf_users')
  27. ->addSelect('pf_users');
  28. }
  29. return $this;
  30. }
  31. public function joinGroupUsers()
  32. {
  33. if (!$this->isJoinGroupUsers) {
  34. $this->isJoinGroupUsers = true;
  35. return $this
  36. ->leftJoin('.groupUsers', 'groupUsers')
  37. ->addSelect('groupUsers');
  38. }
  39. return $this;
  40. }
  41. public function joinProductFamilies()
  42. {
  43. if (!$this->isJoinProductFamilies) {
  44. $this->isJoinProductFamilies = true;
  45. return $this
  46. ->leftJoin('.productFamilies', 'productFamilies')
  47. ->addSelect('productFamilies');
  48. }
  49. return $this;
  50. }
  51. public function joinProductFamily()
  52. {
  53. if (!$this->isJoinProductFamily) {
  54. $this->isJoinProductFamily = true;
  55. return $this
  56. ->leftJoin('.productFamily', 'productFamily')
  57. ->addSelect('productFamily');
  58. }
  59. return $this;
  60. }
  61. public function joinProductCategories()
  62. {
  63. if (!$this->isJoinProductCategories) {
  64. $this->isJoinProductCategories = true;
  65. return $this
  66. ->leftJoin('.productCategories', 'productCategories')
  67. ->addSelect('productCategories');
  68. }
  69. return $this;
  70. }
  71. public function filterProductFamily(ProductFamilyInterface $productFamily)
  72. {
  73. return $this
  74. ->andWhere('.productFamily = :productFamily')
  75. ->setParameter(':productFamily', $productFamily);
  76. }
  77. public function filterConditionDate()
  78. {
  79. return $this
  80. ->andWhere('.permanent = 1 OR ( .dateStart <= :now AND .dateEnd >= :now)')
  81. ->setParameter(':now', new \DateTime());
  82. }
  83. public function filterConditionUser(UserInterface $user = null)
  84. {
  85. $this->joinUsers();
  86. if ($user) {
  87. return $this
  88. ->andWhere(':user MEMBER OF .users OR .users is empty')
  89. ->setParameter('user', $user);
  90. } else {
  91. return $this
  92. ->andWhere('.users is empty');
  93. }
  94. }
  95. public function filterByUser(UserInterface $user = null)
  96. {
  97. return $this
  98. ->andWhere(':user MEMBER OF .users')
  99. ->setParameter('user', $user);
  100. }
  101. public function filterConditionGroupUser(UserInterface $user = null)
  102. {
  103. $this->joinGroupUsers();
  104. if ($user) {
  105. return $this
  106. ->andWhere(':groupUser MEMBER OF .groupUsers OR .groupUsers is empty')
  107. ->setParameter('groupUser', $user->getGroupUsers());
  108. } else {
  109. return $this
  110. ->andWhere('.groupUsers is empty');
  111. }
  112. }
  113. public function filterConditionProductFamilies(array $productFamiliesIds, array $productFamilies)
  114. {
  115. $this->joinProductFamilies();
  116. $this->joinProductFamily();
  117. return $this
  118. ->andWhere(':productFamiliesIds MEMBER OF .productFamilies OR .productFamilies is empty')
  119. ->andWhere('.productFamily IN (:productFamilies) OR .productFamily is null')
  120. ->setParameter('productFamiliesIds', $productFamiliesIds)
  121. ->setParameter('productFamilies', $productFamilies);
  122. }
  123. public function filterConditionProductFamily(ProductFamilyInterface $productFamily)
  124. {
  125. $this->joinProductFamily();
  126. return $this
  127. ->andWhere(':productFamily MEMBER OF .productFamily OR .productFamily is empty')
  128. ->setParameter('productFamily', $productFamily);
  129. }
  130. public function filterConditionProductCategories(array $productCategories)
  131. {
  132. $this->joinProductCategories();
  133. return $this
  134. ->andWhere(':productCategory MEMBER OF .productCategories OR .productCategories is empty')
  135. ->setParameter('productCategory', $productCategories);
  136. }
  137. public function filterIsNotLinkToProductFamily()
  138. {
  139. return $this->andWhere('.productFamily is null');
  140. }
  141. }