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 14KB

4 年之前
4 年之前
4 年之前
4 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. namespace Lc\ShopBundle\Repository;
  3. use App\Entity\DeliveryAvailabilityPointSale;
  4. use App\Entity\DeliveryAvailabilityZone;
  5. use Doctrine\ORM\Query\Expr\Join;
  6. use Doctrine\ORM\QueryBuilder;
  7. use Lc\ShopBundle\Context\DefaultRepositoryInterface;
  8. use Lc\ShopBundle\Context\OrderShopInterface;
  9. use Lc\ShopBundle\Model\OrderStatus;
  10. /**
  11. * @method OrderShopInterface|null find($id, $lockMode = null, $lockVersion = null)
  12. * @method OrderShopInterface|null findOneBy(array $criteria, array $orderBy = null)
  13. * @method OrderShopInterface[] findAll()
  14. * @method OrderShopInterface[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  15. */
  16. class OrderShopRepository extends BaseRepository implements DefaultRepositoryInterface
  17. {
  18. public function getInterfaceClass()
  19. {
  20. return OrderShopInterface::class;
  21. }
  22. public function countValidOrderWithReductionCredit($reductionCredit, $user=null)
  23. {
  24. $query = $this->findByMerchantQuery();
  25. $query = $this->filterOrderValid($query);
  26. $query->select('count(e.id)');
  27. if($user) {
  28. $query->andWhere('e.user = :user');
  29. $query->setParameter('user', $user);
  30. }
  31. $query->innerJoin('e.orderReductionCredits', 'orc');
  32. $query->andWhere('orc.reductionCredit = :reductionCredit');
  33. $query->setParameter('reductionCredit', $reductionCredit);
  34. return $query->getQuery()->getSingleScalarResult();
  35. }
  36. public function countValidOrderWithReductionCart($reductionCart)
  37. {
  38. $query = $this->findByMerchantQuery();
  39. $query = $this->filterOrderValid($query);
  40. $query->select('count(e.id)');
  41. $query->join('e.orderReductionCarts', 'orc');
  42. $query->andWhere('orc.reductionCart = :reductionCart');
  43. $query->setParameter('reductionCart', $reductionCart);
  44. return $query->getQuery()->getSingleScalarResult();
  45. }
  46. public function countValidOrderWithReductionCartPerUser($reductionCart, $user)
  47. {
  48. $query = $this->findByMerchantQuery();
  49. $query = $this->filterOrderValid($query);
  50. $query->select('count(e.id)');
  51. $query->andWhere('e.user = :user');
  52. $query->join('e.orderReductionCarts', 'orc');
  53. $query->andWhere('orc.reductionCart = :reductionCart');
  54. $query->setParameter('reductionCart', $reductionCart);
  55. $query->setParameter('user', $user);
  56. return $query->getQuery()->getSingleScalarResult();
  57. }
  58. public function filterOrderWaitingDelivery($query)
  59. {
  60. return $this->_filterOrderStatus($query, OrderStatus::$statusAliasWaitingDelivery) ;
  61. }
  62. public function filterOrderCart($query)
  63. {
  64. return $this->_filterOrderStatus($query, OrderStatus::$statusAliasAsCart) ;
  65. }
  66. public function filterOrderValid(?QueryBuilder $query): QueryBuilder
  67. {
  68. return $this->_filterOrderStatus($query, OrderStatus::$statusAliasAsValid) ;
  69. }
  70. private function _filterOrderStatus($query, $statusArray) {
  71. $query->leftJoin('e.orderStatus', 'os');
  72. $query->andWhere('os.alias IN (:alias)');
  73. $query->setParameter('alias',$statusArray);
  74. return $query ;
  75. }
  76. public function findCartCurrent($params)
  77. {
  78. $query = $this->findByMerchantQuery();
  79. if (isset($params['user'])) {
  80. $query->andWhere('e.user = :user')->setParameter('user', $params['user']);
  81. }
  82. if (isset($params['visitor'])) {
  83. $query->andWhere('e.visitor = :visitor')->setParameter('visitor', $params['visitor']);
  84. }
  85. $query = $this->filterOrderCart($query);
  86. $query->leftJoin('e.orderReductionCarts', 'orderReductionCarts')
  87. ->addSelect('orderReductionCarts');
  88. $results = $query->getQuery()->getResult();
  89. if ($results) {
  90. return $results[0];
  91. }
  92. return null;
  93. }
  94. public function findAllBy($params = [])
  95. {
  96. $query = $this->findByMerchantQuery();
  97. if (isset($params['count']) && $params['count']) {
  98. $query->select('count(e.id)');
  99. }
  100. if (isset($params['select'])) {
  101. $query->select( $params['select']);
  102. }
  103. if (isset($params['dateStart']) || isset($params['dateEnd'])) {
  104. $params['dateField'] = isset($params['dateField']) ? $params['dateField'] : 'validationDate';
  105. }
  106. if (isset($params['dateStart'])) {
  107. $query->andWhere('e.' . $params['dateField'] . ' >= :dateStart')->setParameter('dateStart', $params['dateStart']);
  108. }
  109. if (isset($params['dateEnd'])) {
  110. $query->andWhere('e.' . $params['dateField'] . ' <= :dateEnd')->setParameter('dateEnd', $params['dateEnd']);
  111. }
  112. if (isset($params['weekNumber'])) {
  113. $query->andWhere('e.weekNumber = :weekNumber')->setParameter('weekNumber', $params['weekNumber']);
  114. }
  115. if (isset($params['isCart'])) {
  116. $query = $this->filterOrderCart($query);
  117. }
  118. if (isset($params['isValid'])) {
  119. $query = $this->filterOrderValid($query);
  120. }
  121. if (isset($params['isWaitingDelivery'])) {
  122. $query = $this->filterOrderWaitingDelivery($query);
  123. }
  124. if (isset($params['orderStatus'])) {
  125. $query->leftJoin('e.orderStatus', 'os');
  126. $query->andWhere('os.alias LIKE :alias');
  127. $query->setParameter('alias', $params['orderStatus']);
  128. }
  129. if (isset($params['user'])) {
  130. $query->andWhere('e.user = :user')->setParameter('user', $params['user']);
  131. }
  132. if(isset($params['address'])) {
  133. $query->andWhere('e.deliveryAddress = :address OR e.invoiceAddress = :address')->setParameter('address', $params['address']);
  134. }
  135. if(isset($params['weekDeliveryTrucks'])) {
  136. $query->andWhere('e.weekDeliveryTruck IN (:weekDeliveryTrucks)')->setParameter('weekDeliveryTrucks', $params['weekDeliveryTrucks']);
  137. }
  138. if(isset($params['estimatedDeliveryDateTime'])) {
  139. $query->andWhere('e.estimatedDeliveryDateTime >= :deliveryDateStart');
  140. $query->andWhere('e.estimatedDeliveryDateTime < :deliveryDateEnd');
  141. $date = clone $params['estimatedDeliveryDateTime'];
  142. $query->setParameter('deliveryDateStart', $date->format('Y-m-d 00:00:00'));
  143. $query->setParameter('deliveryDateEnd', $date->modify('+1 day')->format('Y-m-d 00:00:00'));
  144. }
  145. if(isset($params['deliveryDate'])) {
  146. $query->andWhere('e.deliveryDate >= :deliveryDateStart');
  147. $query->andWhere('e.deliveryDate < :deliveryDateEnd');
  148. $date = clone $params['deliveryDate'];
  149. $query->setParameter('deliveryDateStart', $date->format('Y-m-d 00:00:00'));
  150. $query->setParameter('deliveryDateEnd', $date->modify('+1 day')->format('Y-m-d 00:00:00'));
  151. }
  152. if (isset($params['mergeComplementaryOrderShops'])) {
  153. $query->andWhere('e.mainOrderShop IS NULL');
  154. $query->leftJoin('e.complementaryOrderShops', 'complementaryOrderShops');
  155. }
  156. if (isset($params['excludeComplementaryOrderShops'])) {
  157. $query->andWhere('e.mainOrderShop IS NULL');
  158. }
  159. $setParameterHorsTournee = false ;
  160. $setParameterGiftVoucher = false ;
  161. if(isset($params['isCircuit'])) {
  162. $query->leftJoin('e.deliveryPointSale', 'pointSale');
  163. $query->andWhere('e.deliveryPointSale IS NULL OR (pointSale.isDepository = 0 AND (pointSale.devAlias IS NULL OR (pointSale.devAlias != :devAliasHorsTournee AND pointSale.devAlias != :devAliasGiftVoucher)))');
  164. $setParameterHorsTournee = true ;
  165. $setParameterGiftVoucher = true ;
  166. }
  167. if(isset($params['isDepository'])) {
  168. $query->innerJoin('e.deliveryPointSale', 'pointSale');
  169. $query->andWhere('pointSale IS NOT NULL AND pointSale.isDepository = 1 AND (pointSale.devAlias IS NULL OR (pointSale.devAlias != :devAliasHorsTournee AND pointSale.devAlias != :devAliasGiftVoucher))');
  170. $setParameterHorsTournee = true ;
  171. $setParameterGiftVoucher = true ;
  172. }
  173. if(isset($params['isOffCircuit'])) {
  174. $query->innerJoin('e.deliveryPointSale', 'pointSale');
  175. $query->andWhere('pointSale IS NOT NULL AND pointSale.devAlias = :devAliasHorsTournee');
  176. $setParameterHorsTournee = true ;
  177. }
  178. if(isset($params['isGiftVoucher'])) {
  179. $query->innerJoin('e.deliveryPointSale', 'pointSale');
  180. $query->andWhere('pointSale IS NOT NULL AND pointSale.devAlias = :devAliasGiftVoucher');
  181. $setParameterGiftVoucher = true ;
  182. }
  183. if($setParameterHorsTournee) {
  184. $query->setParameter('devAliasHorsTournee', 'horstournee') ;
  185. }
  186. if($setParameterGiftVoucher) {
  187. $query->setParameter('devAliasGiftVoucher', 'giftvoucher') ;
  188. }
  189. if (isset($params['deliveryAvailability'])) {
  190. $deliveryAvailability = $params['deliveryAvailability'] ;
  191. $deliveryAvailabilityZone = ($deliveryAvailability instanceof DeliveryAvailabilityZone) ? $deliveryAvailability : false;
  192. $deliveryAvailabilityPointSale = ($deliveryAvailability instanceof DeliveryAvailabilityPointSale) ? $deliveryAvailability : false;
  193. if($deliveryAvailabilityZone){
  194. $query->andWhere('e.deliveryAvailabilityZone = :deliveryAvailabilityZone');
  195. $query->setParameter('deliveryAvailabilityZone', $deliveryAvailabilityZone);
  196. }
  197. if($deliveryAvailabilityPointSale) {
  198. $query->andWhere('e.deliveryAvailabilityPointSale = :deliveryAvailabilityPointSale');
  199. $query->setParameter('deliveryAvailabilityPointSale', $deliveryAvailabilityPointSale);
  200. }
  201. }
  202. else{
  203. $query->leftJoin('e.deliveryAvailabilityZone', 'deliveryAvailabilityZone');
  204. $query->leftJoin('deliveryAvailabilityZone.deliverySlot', 'deliverySlotZone');
  205. $query->leftJoin('e.deliveryAvailabilityPointSale', 'deliveryAvailabilityPointSale');
  206. $query->leftJoin('deliveryAvailabilityPointSale.deliverySlot', 'deliverySlotPointSale');
  207. }
  208. if (isset($params['orderBy'])) {
  209. $query->orderBy('e.' . $params['orderBy'], isset($params['orderByDirection']) ? $params['orderByDirection'] : 'DESC');
  210. } else {
  211. $query->orderBy('e.id', 'DESC');
  212. }
  213. if (isset($params['groupBy'])) {
  214. $query->groupBy( $params['groupBy']);
  215. }
  216. if (isset($params['count']) && $params['count']) {
  217. return $query->getQuery()->getSingleScalarResult();
  218. } else {
  219. return $query->getQuery()->getResult();
  220. }
  221. }
  222. public function findLastOrderValidOfWeek($weekNumber)
  223. {
  224. $query = $this->findByMerchantQuery();
  225. $query = $this->filterOrderValid($query);
  226. $query->andWhere('e.weekNumber = :weekNumber');
  227. $query->andWhere('e.mainOrderShop = false OR e.mainOrderShop IS NULL');
  228. $query->setParameter('weekNumber', $weekNumber);
  229. $query->orderBy('e.weekId', 'DESC');
  230. $query->setMaxResults(1);
  231. return $query->getQuery()->getOneOrNullResult();
  232. }
  233. public function findLastOrderValid()
  234. {
  235. $query = $this->findByMerchantQuery();
  236. $query = $this->filterOrderValid($query);
  237. $query->andWhere('e.mainOrderShop = false OR e.mainOrderShop IS NULL');
  238. $query->orderBy('e.idValidOrder', 'DESC');
  239. $query->setMaxResults(1);
  240. return $query->getQuery()->getOneOrNullResult();
  241. }
  242. public function countValidOrderProductsOfWeekByProduct($weekNumber, $productId)
  243. {
  244. $query = $this->findByMerchantQuery();
  245. $query = $this->filterOrderValid($query);
  246. $query->andWhere('e.weekNumber = :weekNumber');
  247. $query->setParameter('weekNumber', $weekNumber);
  248. $query->leftJoin('e.orderProducts', 'orderProduct');
  249. $query->andWhere('orderProduct.product = :product');
  250. $query->setParameter('product', $productId);
  251. $query->select('SUM(orderProduct.quantityOrder) as quantity');
  252. $result = $query->getQuery()->getOneOrNullResult();
  253. return $result['quantity'];
  254. }
  255. }