No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

175 líneas
5.0KB

  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. }