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