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.

237 lines
9.7KB

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