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.

162 lines
6.5KB

  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.orderReductionCart', '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.orderReductionCart', '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 findCartCurrent($params)
  66. {
  67. $query = $this->findByMerchantQuery() ;
  68. if(isset($params['user'])) {
  69. $query->andWhere('e.user = :user')->setParameter('user', $params['user']) ;
  70. }
  71. if(isset($params['visitor'])) {
  72. $query->andWhere('e.visitor = :visitor')->setParameter('visitor', $params['visitor']) ;
  73. }
  74. $query = $this->filterOrderCart($query);
  75. $query->leftJoin('e.orderReductionCarts', 'orderReductionCarts')
  76. ->addSelect('orderReductionCarts');
  77. $results = $query->getQuery()->getResult() ;
  78. if($results) {
  79. return $results[0] ;
  80. }
  81. return null ;
  82. }
  83. public function findAllBy($params = [])
  84. {
  85. $query = $this->findByMerchantQuery() ;
  86. if(isset($params['count']) && $params['count']) {
  87. $query->select('count(e.id)') ;
  88. }
  89. if(isset($params['dateStart']) || isset($params['dateEnd'])) {
  90. $params['dateField'] = isset($params['dateField']) ? $params['dateField'] : 'validationDate' ;
  91. }
  92. if(isset($params['dateStart'])) {
  93. $query->andWhere('e.'.$params['dateField'].' >= :dateStart')->setParameter('dateStart', $params['dateStart']) ;
  94. }
  95. if(isset($params['dateEnd'])) {
  96. $query->andWhere('e.'.$params['dateField'].' <= :dateEnd')->setParameter('dateEnd', $params['dateEnd']) ;
  97. }
  98. if(isset($params['isCart'])) {
  99. $query = $this->filterOrderCart($query) ;
  100. }
  101. if(isset($params['isValid'])) {
  102. $query = $this->filterOrderValid($query) ;
  103. }
  104. if(isset($params['user'])) {
  105. $query->andWhere('e.user = :user')->setParameter('user', $params['user']) ;
  106. }
  107. $query->orderBy('e.id', 'DESC') ;
  108. $query->leftJoin('e.deliveryAvailabilityZone', 'deliveryAvailabilityZone') ;
  109. $query->leftJoin('deliveryAvailabilityZone.deliverySlot', 'deliverySlotZone') ;
  110. $query->addOrderBy('deliverySlotZone.day', 'ASC') ;
  111. $query->addOrderBy('deliverySlotZone.timeStart', 'ASC') ;
  112. $query->leftJoin('e.deliveryAvailabilityPointSale', 'deliveryAvailabilityPointSale') ;
  113. $query->leftJoin('deliveryAvailabilityPointSale.deliverySlot', 'deliverySlotPointSale') ;
  114. $query->addOrderBy('deliverySlotPointSale.day', 'ASC') ;
  115. $query->addOrderBy('deliverySlotPointSale.timeStart', 'ASC') ;
  116. if(isset($params['count']) && $params['count']) {
  117. return $query->getQuery()->getSingleScalarResult();
  118. }
  119. else {
  120. return $query->getQuery()->getResult() ;
  121. }
  122. }
  123. }