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.

203 lines
8.1KB

  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['isOffCircuit'])) {
  118. $query = $this->filterIsOffCircuit($query, true) ;
  119. }
  120. if(isset($params['isCircuit'])) {
  121. $query = $this->filterIsOffCircuit($query, false) ;
  122. $query->leftJoin('e.deliveryPointSale', 'pointSale') ;
  123. $query->andWhere('e.deliveryPointSale IS NULL OR pointSale.isDepository = 0') ;
  124. }
  125. if(isset($params['isDepository'])) {
  126. $query = $this->filterIsOffCircuit($query, false) ;
  127. $query->innerJoin('e.deliveryPointSale', 'pointSale') ;
  128. $query->andWhere('pointSale.isDepository = 1') ;
  129. }
  130. if(isset($params['orderBy'])) {
  131. $query->orderBy('e.'.$params['orderBy'], isset($params['orderByDirection']) ? $params['orderByDirection'] : 'DESC') ;
  132. }
  133. else {
  134. $query->orderBy('e.id', 'DESC') ;
  135. }
  136. $query->leftJoin('e.deliveryAvailabilityZone', 'deliveryAvailabilityZone') ;
  137. $query->leftJoin('deliveryAvailabilityZone.deliverySlot', 'deliverySlotZone') ;
  138. $query->leftJoin('e.deliveryAvailabilityPointSale', 'deliveryAvailabilityPointSale') ;
  139. $query->leftJoin('deliveryAvailabilityPointSale.deliverySlot', 'deliverySlotPointSale') ;
  140. if(isset($params['count']) && $params['count']) {
  141. return $query->getQuery()->getSingleScalarResult();
  142. }
  143. else {
  144. return $query->getQuery()->getResult() ;
  145. }
  146. }
  147. public function findLastOrderValidOfWeek($weekNumber){
  148. $query = $this->findByMerchantQuery();
  149. $query = $this->filterOrderValid($query);
  150. $query->andWhere('e.weekNumber = :weekNumber');
  151. $query->setParameter('weekNumber', $weekNumber);
  152. $query->orderBy('e.validationDate','DESC');
  153. $query->setMaxResults(1);
  154. return $query->getQuery()->getOneOrNullResult();
  155. }
  156. }