OrderShopRepository.php 8.9KB

4 年之前
4 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. $query = $this->findByMerchantQuery();
  23. $query = $this->filterOrderValid($query);
  24. $query->select('count(e.id)');
  25. $query->andWhere('e.user = :user');
  26. $query->innerJoin('e.orderReductionCredits', 'orc');
  27. $query->andWhere('orc.reductionCredit = :reductionCredit');
  28. $query->setParameter('reductionCredit', $reductionCredit);
  29. $query->setParameter('user', $user);
  30. return $query->getQuery()->getSingleScalarResult();
  31. }
  32. public function countValidOrderWithReductionCart($reductionCart){
  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. $query = $this->findByMerchantQuery();
  43. $query = $this->filterOrderValid($query);
  44. $query->select('count(e.id)');
  45. $query->andWhere('e.user = :user');
  46. $query->join('e.orderReductionCarts', 'orc');
  47. $query->andWhere('orc.reductionCart = :reductionCart');
  48. $query->setParameter('reductionCart', $reductionCart);
  49. $query->setParameter('user', $user);
  50. return $query->getQuery()->getSingleScalarResult();
  51. }
  52. public function filterOrderValid(?QueryBuilder $query) :QueryBuilder {
  53. $query->leftJoin('e.orderStatus', 'os');
  54. $query->andWhere('os.alias IN (:alias)');
  55. $query->setParameter('alias', $this->statusAliasAsValid);
  56. return $query;
  57. }
  58. public function filterOrderCart($query)
  59. {
  60. $query->leftJoin('e.orderStatus', 'os');
  61. $query->andWhere('os.alias IN (:alias)');
  62. $query->setParameter('alias', $this->statusAliasAsCart);
  63. return $query;
  64. }
  65. public function filterIsOffCircuit($query, $isOffCircuit)
  66. {
  67. if($isOffCircuit) {
  68. $query->andWhere('e.isOffCircuit = 1') ;
  69. }
  70. else {
  71. $query->andWhere('e.isOffCircuit IS NULL OR e.isOffCircuit = 0') ;
  72. }
  73. return $query ;
  74. }
  75. public function findCartCurrent($params)
  76. {
  77. $query = $this->findByMerchantQuery() ;
  78. if(isset($params['user'])) {
  79. $query->andWhere('e.user = :user')->setParameter('user', $params['user']) ;
  80. }
  81. if(isset($params['visitor'])) {
  82. $query->andWhere('e.visitor = :visitor')->setParameter('visitor', $params['visitor']) ;
  83. }
  84. $query = $this->filterOrderCart($query);
  85. $query->leftJoin('e.orderReductionCarts', 'orderReductionCarts')
  86. ->addSelect('orderReductionCarts');
  87. $results = $query->getQuery()->getResult() ;
  88. if($results) {
  89. return $results[0] ;
  90. }
  91. return null ;
  92. }
  93. public function findAllBy($params = [])
  94. {
  95. $query = $this->findByMerchantQuery() ;
  96. if(isset($params['count']) && $params['count']) {
  97. $query->select('count(e.id)') ;
  98. }
  99. if(isset($params['dateStart']) || isset($params['dateEnd'])) {
  100. $params['dateField'] = isset($params['dateField']) ? $params['dateField'] : 'validationDate' ;
  101. }
  102. if(isset($params['dateStart'])) {
  103. $query->andWhere('e.'.$params['dateField'].' >= :dateStart')->setParameter('dateStart', $params['dateStart']) ;
  104. }
  105. if(isset($params['dateEnd'])) {
  106. $query->andWhere('e.'.$params['dateField'].' <= :dateEnd')->setParameter('dateEnd', $params['dateEnd']) ;
  107. }
  108. if(isset($params['isCart'])) {
  109. $query = $this->filterOrderCart($query) ;
  110. }
  111. if(isset($params['isValid'])) {
  112. $query = $this->filterOrderValid($query) ;
  113. }
  114. if(isset($params['user'])) {
  115. $query->andWhere('e.user = :user')->setParameter('user', $params['user']) ;
  116. }
  117. if(isset($params['mergeComplementaryOrderShops'])) {
  118. $query->andWhere('e.mainOrderShop IS NULL') ;
  119. $query->leftJoin('e.complementaryOrderShops', 'complementaryOrderShops') ;
  120. $query->leftJoin('complementaryOrderShops.orderStatus', 'complementaryOrderStatus');
  121. $query->andWhere('complementaryOrderShops IS NULL OR complementaryOrderStatus.alias IN (:statusAliasValid)');
  122. $query->setParameter('statusAliasValid', $this->statusAliasAsValid);
  123. }
  124. if(isset($params['excludeComplementaryOrderShops'])) {
  125. $query->andWhere('e.mainOrderShop IS NULL') ;
  126. }
  127. if(isset($params['isOffCircuit'])) {
  128. $query = $this->filterIsOffCircuit($query, true) ;
  129. }
  130. if(isset($params['isCircuit'])) {
  131. $query = $this->filterIsOffCircuit($query, false) ;
  132. $query->leftJoin('e.deliveryPointSale', 'pointSale') ;
  133. $query->andWhere('e.deliveryPointSale IS NULL OR pointSale.isDepository = 0') ;
  134. }
  135. if(isset($params['isDepository'])) {
  136. $query = $this->filterIsOffCircuit($query, false) ;
  137. $query->innerJoin('e.deliveryPointSale', 'pointSale') ;
  138. $query->andWhere('pointSale.isDepository = 1') ;
  139. }
  140. if(isset($params['orderBy'])) {
  141. $query->orderBy('e.'.$params['orderBy'], isset($params['orderByDirection']) ? $params['orderByDirection'] : 'DESC') ;
  142. }
  143. else {
  144. $query->orderBy('e.id', 'DESC') ;
  145. }
  146. $query->leftJoin('e.deliveryAvailabilityZone', 'deliveryAvailabilityZone') ;
  147. $query->leftJoin('deliveryAvailabilityZone.deliverySlot', 'deliverySlotZone') ;
  148. $query->leftJoin('e.deliveryAvailabilityPointSale', 'deliveryAvailabilityPointSale') ;
  149. $query->leftJoin('deliveryAvailabilityPointSale.deliverySlot', 'deliverySlotPointSale') ;
  150. if(isset($params['count']) && $params['count']) {
  151. return $query->getQuery()->getSingleScalarResult();
  152. }
  153. else {
  154. return $query->getQuery()->getResult() ;
  155. }
  156. }
  157. public function findLastOrderValidOfWeek($weekNumber){
  158. $query = $this->findByMerchantQuery();
  159. $query = $this->filterOrderValid($query);
  160. $query->andWhere('e.weekNumber = :weekNumber');
  161. $query->setParameter('weekNumber', $weekNumber);
  162. $query->orderBy('e.validationDate','DESC');
  163. $query->setMaxResults(1);
  164. return $query->getQuery()->getOneOrNullResult();
  165. }
  166. public function countValidOrderProductsOfWeekByProduct(){
  167. }
  168. }