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.

117 lines
4.2KB

  1. <?php
  2. namespace Lc\ShopBundle\Repository;
  3. use Lc\ShopBundle\Context\DefaultRepositoryInterface;
  4. use Lc\ShopBundle\Context\OrderShopInterface;
  5. /**
  6. * @method OrderShopInterface|null find($id, $lockMode = null, $lockVersion = null)
  7. * @method OrderShopInterface|null findOneBy(array $criteria, array $orderBy = null)
  8. * @method OrderShopInterface[] findAll()
  9. * @method OrderShopInterface[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  10. */
  11. class OrderShopRepository extends BaseRepository implements DefaultRepositoryInterface
  12. {
  13. public function getInterfaceClass()
  14. {
  15. return OrderShopInterface::class;
  16. }
  17. protected $statusAliasAsValid = array('paid-online', 'paid-by-credit', 'waiting-delivery', 'waiting-delivery-with-payment', 'delivered-without-payment', 'done');
  18. public function getValidOrderWithReductionCredit($reductionCredit, $user){
  19. $query = $this->findValidOrderQuery();
  20. $query->andWhere('e.user = :user');
  21. $query->leftJoin('e.orderReductionCredits', 'orc');
  22. $query->andWhere('orc.reductionCredit = :reductionCredit');
  23. $query->setParameter('reductionCredit', $reductionCredit);
  24. $query->setParameter('user', $user);
  25. return $query->getQuery()->getResult();
  26. }
  27. public function findValidOrderQuery(){
  28. $query = $this->findByMerchantQuery();
  29. $query->leftJoin('e.orderStatus', 'os');
  30. $query->andWhere('os.alias IN (:alias)');
  31. $query->setParameter('alias', $this->statusAliasAsValid);
  32. return $query;
  33. }
  34. public function findCartCurrent($params)
  35. {
  36. $query = $this->findByMerchantQuery() ;
  37. if(isset($params['user'])) {
  38. $query->andWhere('e.user = :user')->setParameter('user', $params['user']) ;
  39. }
  40. if(isset($params['visitor'])) {
  41. $query->andWhere('e.visitor = :visitor')->setParameter('visitor', $params['visitor']) ;
  42. }
  43. $query->leftJoin('e.orderStatusHistories', 'orderStatusHistories')
  44. ->andWhere('SIZE(e.orderStatusHistories) = 0') ;
  45. $results = $query->getQuery()->getResult() ;
  46. if($results) {
  47. return $results[0] ;
  48. }
  49. return null ;
  50. }
  51. public function findAllBy($params = [])
  52. {
  53. $query = $this->findByMerchantQuery() ;
  54. if(isset($params['dateStart'])) {
  55. $query->andWhere('e.createdAt >= :dateStart')->setParameter('dateStart', $params['dateStart']) ;
  56. }
  57. if(isset($params['dateEnd'])) {
  58. $query->andWhere('e.createdAt <= :dateEnd')->setParameter('dateEnd', $params['dateEnd']) ;
  59. }
  60. if(!isset($params['isCart'])) {
  61. $query = $this->filterOrderCart($query) ;
  62. }
  63. if(isset($params['user'])) {
  64. $query->andWhere('e.user = :user')->setParameter('user', $params['user']) ;
  65. }
  66. $query->orderBy('e.id', 'DESC') ;
  67. return $query->getQuery()->getResult() ;
  68. }
  69. public function findAllByDateStartEnd($dateStart, $dateEnd)
  70. {
  71. $query = $this->findByMerchantQuery()
  72. ->andWhere('e.createdAt >= :dateStart')
  73. ->setParameter('dateStart', $dateStart)
  74. ->andWhere('e.createdAt <= :dateEnd')
  75. ->setParameter('dateEnd', $dateEnd);
  76. $this->filterOrderCart($query) ;
  77. return $query->getQuery()->getResult() ;
  78. }
  79. public function filterOrderCart($query, $isCart = false)
  80. {
  81. $operator = $isCart ? '=' : '>' ;
  82. return $query->leftJoin('e.orderStatusHistories', 'orderStatusHistories')
  83. ->andWhere('SIZE(e.orderStatusHistories) '.$operator.' 0') ;
  84. }
  85. //getValidOrders()
  86. }