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.

OrderShopRepository.php 11KB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace Lc\ShopBundle\Repository;
  3. use App\Entity\DeliveryAvailabilityPointSale;
  4. use App\Entity\DeliveryAvailabilityZone;
  5. use Doctrine\ORM\QueryBuilder;
  6. use Lc\ShopBundle\Context\DefaultRepositoryInterface;
  7. use Lc\ShopBundle\Context\OrderShopInterface;
  8. use Lc\ShopBundle\Model\OrderStatus;
  9. /**
  10. * @method OrderShopInterface|null find($id, $lockMode = null, $lockVersion = null)
  11. * @method OrderShopInterface|null findOneBy(array $criteria, array $orderBy = null)
  12. * @method OrderShopInterface[] findAll()
  13. * @method OrderShopInterface[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  14. */
  15. class OrderShopRepository extends BaseRepository implements DefaultRepositoryInterface
  16. {
  17. public function getInterfaceClass()
  18. {
  19. return OrderShopInterface::class;
  20. }
  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', OrderStatus::$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', OrderStatus::$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', OrderStatus::$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['deliveryAvailability'])) {
  147. $deliveryAvailability = $params['deliveryAvailability'] ;
  148. $deliveryAvailabilityZone = ($deliveryAvailability instanceof DeliveryAvailabilityZone) ? $deliveryAvailability : false;
  149. $deliveryAvailabilityPointSale = ($deliveryAvailability instanceof DeliveryAvailabilityPointSale) ? $deliveryAvailability : false;
  150. if($deliveryAvailabilityZone){
  151. $query->andWhere('e.deliveryAvailabilityZone = :deliveryAvailabilityZone');
  152. $query->setParameter('deliveryAvailabilityZone', $deliveryAvailabilityZone);
  153. }
  154. if($deliveryAvailabilityPointSale) {
  155. $query->andWhere('e.deliveryAvailabilityPointSale = :deliveryAvailabilityPointSale');
  156. $query->setParameter('deliveryAvailabilityPointSale', $deliveryAvailabilityPointSale);
  157. }
  158. }
  159. else{
  160. $query->leftJoin('e.deliveryAvailabilityZone', 'deliveryAvailabilityZone');
  161. $query->leftJoin('deliveryAvailabilityZone.deliverySlot', 'deliverySlotZone');
  162. $query->leftJoin('e.deliveryAvailabilityPointSale', 'deliveryAvailabilityPointSale');
  163. $query->leftJoin('deliveryAvailabilityPointSale.deliverySlot', 'deliverySlotPointSale');
  164. }
  165. if (isset($params['orderBy'])) {
  166. $query->orderBy('e.' . $params['orderBy'], isset($params['orderByDirection']) ? $params['orderByDirection'] : 'DESC');
  167. } else {
  168. $query->orderBy('e.id', 'DESC');
  169. }
  170. if (isset($params['count']) && $params['count']) {
  171. return $query->getQuery()->getSingleScalarResult();
  172. } else {
  173. return $query->getQuery()->getResult();
  174. }
  175. }
  176. public function findLastOrderValidOfWeek($weekNumber)
  177. {
  178. $query = $this->findByMerchantQuery();
  179. $query = $this->filterOrderValid($query);
  180. $query->andWhere('e.weekNumber = :weekNumber');
  181. $query->setParameter('weekNumber', $weekNumber);
  182. $query->orderBy('e.validationDate', 'DESC');
  183. $query->setMaxResults(1);
  184. return $query->getQuery()->getOneOrNullResult();
  185. }
  186. public function countValidOrderProductsOfWeekByProduct($weekNumber, $productId)
  187. {
  188. $query = $this->findByMerchantQuery();
  189. $query = $this->filterOrderValid($query);
  190. $query->andWhere('e.weekNumber = :weekNumber');
  191. $query->setParameter('weekNumber', $weekNumber);
  192. $query->leftJoin('e.orderProducts', 'orderProduct');
  193. $query->andWhere('orderProduct.product = :product');
  194. $query->setParameter('product', $productId);
  195. $query->select('SUM(orderProduct.quantityOrder) as quantity');
  196. return $query->getQuery()->getOneOrNullResult();
  197. }
  198. }