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.

231 lines
9.4KB

  1. <?php
  2. namespace Lc\ShopBundle\Repository;
  3. use Doctrine\ORM\QueryBuilder;
  4. use Lc\ShopBundle\Context\DefaultRepositoryInterface;
  5. use Lc\ShopBundle\Context\OrderShopInterface;
  6. use Lc\ShopBundle\Model\OrderStatus;
  7. /**
  8. * @method OrderShopInterface|null find($id, $lockMode = null, $lockVersion = null)
  9. * @method OrderShopInterface|null findOneBy(array $criteria, array $orderBy = null)
  10. * @method OrderShopInterface[] findAll()
  11. * @method OrderShopInterface[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  12. */
  13. class OrderShopRepository extends BaseRepository implements DefaultRepositoryInterface
  14. {
  15. public function getInterfaceClass()
  16. {
  17. return OrderShopInterface::class;
  18. }
  19. public function countValidOrderWithReductionCredit($reductionCredit, $user)
  20. {
  21. $query = $this->findByMerchantQuery();
  22. $query = $this->filterOrderValid($query);
  23. $query->select('count(e.id)');
  24. $query->andWhere('e.user = :user');
  25. $query->innerJoin('e.orderReductionCredits', 'orc');
  26. $query->andWhere('orc.reductionCredit = :reductionCredit');
  27. $query->setParameter('reductionCredit', $reductionCredit);
  28. $query->setParameter('user', $user);
  29. return $query->getQuery()->getSingleScalarResult();
  30. }
  31. public function countValidOrderWithReductionCart($reductionCart)
  32. {
  33. $query = $this->findByMerchantQuery();
  34. $query = $this->filterOrderValid($query);
  35. $query->select('count(e.id)');
  36. $query->join('e.orderReductionCarts', 'orc');
  37. $query->andWhere('orc.reductionCart = :reductionCart');
  38. $query->setParameter('reductionCart', $reductionCart);
  39. return $query->getQuery()->getSingleScalarResult();
  40. }
  41. public function countValidOrderWithReductionCartPerUser($reductionCart, $user)
  42. {
  43. $query = $this->findByMerchantQuery();
  44. $query = $this->filterOrderValid($query);
  45. $query->select('count(e.id)');
  46. $query->andWhere('e.user = :user');
  47. $query->join('e.orderReductionCarts', 'orc');
  48. $query->andWhere('orc.reductionCart = :reductionCart');
  49. $query->setParameter('reductionCart', $reductionCart);
  50. $query->setParameter('user', $user);
  51. return $query->getQuery()->getSingleScalarResult();
  52. }
  53. public function filterOrderValid(?QueryBuilder $query): QueryBuilder
  54. {
  55. $query->leftJoin('e.orderStatus', 'os');
  56. $query->andWhere('os.alias IN (:alias)');
  57. $query->setParameter('alias', OrderStatus::$statusAliasAsValid);
  58. return $query;
  59. }
  60. public function filterOrderCart($query)
  61. {
  62. $query->leftJoin('e.orderStatus', 'os');
  63. $query->andWhere('os.alias IN (:alias)');
  64. $query->setParameter('alias', OrderStatus::$statusAliasAsCart);
  65. return $query;
  66. }
  67. public function filterIsOffCircuit($query, $isOffCircuit)
  68. {
  69. if ($isOffCircuit) {
  70. $query->andWhere('e.isOffCircuit = 1');
  71. } else {
  72. $query->andWhere('e.isOffCircuit IS NULL OR e.isOffCircuit = 0');
  73. }
  74. return $query;
  75. }
  76. public function findCartCurrent($params)
  77. {
  78. $query = $this->findByMerchantQuery();
  79. if (isset($params['user'])) {
  80. $query->andWhere('e.user = :user')->setParameter('user', $params['user']);
  81. }
  82. if (isset($params['visitor'])) {
  83. $query->andWhere('e.visitor = :visitor')->setParameter('visitor', $params['visitor']);
  84. }
  85. $query = $this->filterOrderCart($query);
  86. $query->leftJoin('e.orderReductionCarts', 'orderReductionCarts')
  87. ->addSelect('orderReductionCarts');
  88. $results = $query->getQuery()->getResult();
  89. if ($results) {
  90. return $results[0];
  91. }
  92. return null;
  93. }
  94. public function findAllBy($params = [])
  95. {
  96. $query = $this->findByMerchantQuery();
  97. if (isset($params['count']) && $params['count']) {
  98. $query->select('count(e.id)');
  99. }
  100. if (isset($params['dateStart']) || isset($params['dateEnd'])) {
  101. $params['dateField'] = isset($params['dateField']) ? $params['dateField'] : 'validationDate';
  102. }
  103. if (isset($params['dateStart'])) {
  104. $query->andWhere('e.' . $params['dateField'] . ' >= :dateStart')->setParameter('dateStart', $params['dateStart']);
  105. }
  106. if (isset($params['dateEnd'])) {
  107. $query->andWhere('e.' . $params['dateField'] . ' <= :dateEnd')->setParameter('dateEnd', $params['dateEnd']);
  108. }
  109. if (isset($params['weekNumber'])) {
  110. $query->andWhere('e.weekNumber = :weekNumber')->setParameter('weekNumber', $params['weekNumber']);
  111. }
  112. if (isset($params['isCart'])) {
  113. $query = $this->filterOrderCart($query);
  114. }
  115. if (isset($params['isValid'])) {
  116. $query = $this->filterOrderValid($query);
  117. }
  118. if (isset($params['user'])) {
  119. $query->andWhere('e.user = :user')->setParameter('user', $params['user']);
  120. }
  121. if (isset($params['mergeComplementaryOrderShops'])) {
  122. $query->andWhere('e.mainOrderShop IS NULL');
  123. $query->leftJoin('e.complementaryOrderShops', 'complementaryOrderShops');
  124. $query->leftJoin('complementaryOrderShops.orderStatus', 'complementaryOrderStatus');
  125. $query->andWhere('complementaryOrderShops IS NULL OR complementaryOrderStatus.alias IN (:statusAliasValid)');
  126. $query->setParameter('statusAliasValid', OrderStatus::$statusAliasAsValid);
  127. }
  128. if (isset($params['excludeComplementaryOrderShops'])) {
  129. $query->andWhere('e.mainOrderShop IS NULL');
  130. }
  131. if (isset($params['isOffCircuit'])) {
  132. $query = $this->filterIsOffCircuit($query, true);
  133. }
  134. if (isset($params['isCircuit'])) {
  135. $query = $this->filterIsOffCircuit($query, false);
  136. $query->leftJoin('e.deliveryPointSale', 'pointSale');
  137. $query->andWhere('e.deliveryPointSale IS NULL OR pointSale.isDepository = 0');
  138. }
  139. if (isset($params['isDepository'])) {
  140. $query = $this->filterIsOffCircuit($query, false);
  141. $query->innerJoin('e.deliveryPointSale', 'pointSale');
  142. $query->andWhere('pointSale.isDepository = 1');
  143. }
  144. if (isset($params['orderBy'])) {
  145. $query->orderBy('e.' . $params['orderBy'], isset($params['orderByDirection']) ? $params['orderByDirection'] : 'DESC');
  146. } else {
  147. $query->orderBy('e.id', 'DESC');
  148. }
  149. $query->leftJoin('e.deliveryAvailabilityZone', 'deliveryAvailabilityZone');
  150. $query->leftJoin('deliveryAvailabilityZone.deliverySlot', 'deliverySlotZone');
  151. $query->leftJoin('e.deliveryAvailabilityPointSale', 'deliveryAvailabilityPointSale');
  152. $query->leftJoin('deliveryAvailabilityPointSale.deliverySlot', 'deliverySlotPointSale');
  153. if (isset($params['count']) && $params['count']) {
  154. return $query->getQuery()->getSingleScalarResult();
  155. } else {
  156. return $query->getQuery()->getResult();
  157. }
  158. }
  159. public function findLastOrderValidOfWeek($weekNumber)
  160. {
  161. $query = $this->findByMerchantQuery();
  162. $query = $this->filterOrderValid($query);
  163. $query->andWhere('e.weekNumber = :weekNumber');
  164. $query->setParameter('weekNumber', $weekNumber);
  165. $query->orderBy('e.validationDate', 'DESC');
  166. $query->setMaxResults(1);
  167. return $query->getQuery()->getOneOrNullResult();
  168. }
  169. public function countValidOrderProductsOfWeekByProduct($weekNumber, $productId)
  170. {
  171. $query = $this->findByMerchantQuery();
  172. $query = $this->filterOrderValid($query);
  173. $query->andWhere('e.weekNumber = :weekNumber');
  174. $query->setParameter('weekNumber', $weekNumber);
  175. $query->leftJoin('e.orderProducts', 'orderProduct');
  176. $query->andWhere('orderProduct.product = :product');
  177. $query->setParameter('product', $productId);
  178. $query->select('SUM(orderProduct.quantityOrder) as quantity');
  179. return $query->getQuery()->getOneOrNullResult();
  180. }
  181. }