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

4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. return $this->_filterOrderStatus($query, OrderStatus::$statusAliasAsValid) ;
  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. private function _filterOrderStatus($query, $statusArray) {
  68. $query->leftJoin('e.orderStatus', 'os');
  69. $query->andWhere('os.alias IN (:alias)');
  70. $query->setParameter('alias',$statusArray);
  71. return $query ;
  72. }
  73. public function findCartCurrent($params)
  74. {
  75. $query = $this->findByMerchantQuery();
  76. if (isset($params['user'])) {
  77. $query->andWhere('e.user = :user')->setParameter('user', $params['user']);
  78. }
  79. if (isset($params['visitor'])) {
  80. $query->andWhere('e.visitor = :visitor')->setParameter('visitor', $params['visitor']);
  81. }
  82. $query = $this->filterOrderCart($query);
  83. $query->leftJoin('e.orderReductionCarts', 'orderReductionCarts')
  84. ->addSelect('orderReductionCarts');
  85. $results = $query->getQuery()->getResult();
  86. if ($results) {
  87. return $results[0];
  88. }
  89. return null;
  90. }
  91. public function findAllBy($params = [])
  92. {
  93. $query = $this->findByMerchantQuery();
  94. if (isset($params['count']) && $params['count']) {
  95. $query->select('count(e.id)');
  96. }
  97. if (isset($params['select'])) {
  98. $query->select( $params['select']);
  99. }
  100. if (isset($params['dateStart']) || isset($params['dateEnd'])) {
  101. $params['dateField'] = isset($params['dateField']) ? $params['dateField'] : 'validationDate';
  102. }
  103. if (isset($params['dateStart'])) {
  104. $query->andWhere('e.' . $params['dateField'] . ' >= :dateStart')->setParameter('dateStart', $params['dateStart']);
  105. }
  106. if (isset($params['dateEnd'])) {
  107. $query->andWhere('e.' . $params['dateField'] . ' <= :dateEnd')->setParameter('dateEnd', $params['dateEnd']);
  108. }
  109. if (isset($params['weekNumber'])) {
  110. $query->andWhere('e.weekNumber = :weekNumber')->setParameter('weekNumber', $params['weekNumber']);
  111. }
  112. if (isset($params['isCart'])) {
  113. $query = $this->filterOrderCart($query);
  114. }
  115. if (isset($params['isValid'])) {
  116. $query = $this->filterOrderValid($query);
  117. }
  118. if (isset($params['isWaitingDelivery'])) {
  119. $query = $this->filterOrderWaitingDelivery($query);
  120. }
  121. if (isset($params['orderStatus'])) {
  122. $query->leftJoin('e.orderStatus', 'os');
  123. $query->andWhere('os.alias LIKE :alias');
  124. $query->setParameter('alias', $params['orderStatus']);
  125. }
  126. if (isset($params['user'])) {
  127. $query->andWhere('e.user = :user')->setParameter('user', $params['user']);
  128. }
  129. if(isset($params['address'])) {
  130. $query->andWhere('e.deliveryAddress = :address OR e.invoiceAddress = :address')->setParameter('address', $params['address']);
  131. }
  132. if(isset($params['weekDeliveryTruck'])) {
  133. $query->andWhere('e.weekDeliveryTruck = :weekDeliveryTruck')->setParameter('weekDeliveryTruck', $params['weekDeliveryTruck']);
  134. }
  135. if(isset($params['deliveryDate'])) {
  136. $query->andWhere('e.deliveryDate >= :deliveryDateStart');
  137. $query->andWhere('e.deliveryDate < :deliveryDateEnd');
  138. $query->setParameter('deliveryDateStart', $params['deliveryDate']->format('Y-m-d 00:00:00'));
  139. $query->setParameter('deliveryDateEnd', $params['deliveryDate']->modify('+1 day')->format('Y-m-d 00:00:00'));
  140. }
  141. if (isset($params['mergeComplementaryOrderShops'])) {
  142. $query->andWhere('e.mainOrderShop IS NULL');
  143. $query->leftJoin('e.complementaryOrderShops', 'complementaryOrderShops');
  144. $query->leftJoin('complementaryOrderShops.orderStatus', 'complementaryOrderStatus');
  145. $query->andWhere('complementaryOrderShops IS NULL OR complementaryOrderStatus.alias IN (:statusAliasValid)');
  146. $query->setParameter('statusAliasValid', OrderStatus::$statusAliasAsValid);
  147. }
  148. if (isset($params['excludeComplementaryOrderShops'])) {
  149. $query->andWhere('e.mainOrderShop IS NULL');
  150. }
  151. $setParameterHorsTournee = false ;
  152. if (isset($params['isCircuit'])) {
  153. $query->leftJoin('e.deliveryPointSale', 'pointSale');
  154. $query->andWhere('e.deliveryPointSale IS NULL OR (pointSale.isDepository = 0 AND (pointSale.devAlias IS NULL OR pointSale.devAlias != :devAliasHorsTournee))');
  155. $setParameterHorsTournee = true ;
  156. }
  157. if (isset($params['isDepository'])) {
  158. $query->innerJoin('e.deliveryPointSale', 'pointSale');
  159. $query->andWhere('pointSale IS NOT NULL AND pointSale.isDepository = 1 AND (pointSale.devAlias IS NULL OR pointSale.devAlias != :devAliasHorsTournee)');
  160. $setParameterHorsTournee = true ;
  161. }
  162. if (isset($params['isOffCircuit'])) {
  163. $query->innerJoin('e.deliveryPointSale', 'pointSale');
  164. $query->andWhere('pointSale IS NOT NULL AND pointSale.devAlias = :devAliasHorsTournee');
  165. $setParameterHorsTournee = true ;
  166. }
  167. if($setParameterHorsTournee) {
  168. $query->setParameter('devAliasHorsTournee', 'horstournee') ;
  169. }
  170. if (isset($params['deliveryAvailability'])) {
  171. $deliveryAvailability = $params['deliveryAvailability'] ;
  172. $deliveryAvailabilityZone = ($deliveryAvailability instanceof DeliveryAvailabilityZone) ? $deliveryAvailability : false;
  173. $deliveryAvailabilityPointSale = ($deliveryAvailability instanceof DeliveryAvailabilityPointSale) ? $deliveryAvailability : false;
  174. if($deliveryAvailabilityZone){
  175. $query->andWhere('e.deliveryAvailabilityZone = :deliveryAvailabilityZone');
  176. $query->setParameter('deliveryAvailabilityZone', $deliveryAvailabilityZone);
  177. }
  178. if($deliveryAvailabilityPointSale) {
  179. $query->andWhere('e.deliveryAvailabilityPointSale = :deliveryAvailabilityPointSale');
  180. $query->setParameter('deliveryAvailabilityPointSale', $deliveryAvailabilityPointSale);
  181. }
  182. }
  183. else{
  184. $query->leftJoin('e.deliveryAvailabilityZone', 'deliveryAvailabilityZone');
  185. $query->leftJoin('deliveryAvailabilityZone.deliverySlot', 'deliverySlotZone');
  186. $query->leftJoin('e.deliveryAvailabilityPointSale', 'deliveryAvailabilityPointSale');
  187. $query->leftJoin('deliveryAvailabilityPointSale.deliverySlot', 'deliverySlotPointSale');
  188. }
  189. if (isset($params['orderBy'])) {
  190. $query->orderBy('e.' . $params['orderBy'], isset($params['orderByDirection']) ? $params['orderByDirection'] : 'DESC');
  191. } else {
  192. $query->orderBy('e.id', 'DESC');
  193. }
  194. if (isset($params['groupBy'])) {
  195. $query->groupBy( $params['groupBy']);
  196. }
  197. if (isset($params['count']) && $params['count']) {
  198. return $query->getQuery()->getSingleScalarResult();
  199. } else {
  200. return $query->getQuery()->getResult();
  201. }
  202. }
  203. public function findLastOrderValidOfWeek($weekNumber)
  204. {
  205. $query = $this->findByMerchantQuery();
  206. $query = $this->filterOrderValid($query);
  207. $query->andWhere('e.weekNumber = :weekNumber');
  208. $query->andWhere('e.mainOrderShop = false OR e.mainOrderShop IS NULL');
  209. $query->setParameter('weekNumber', $weekNumber);
  210. $query->orderBy('e.weekId', 'DESC');
  211. $query->setMaxResults(1);
  212. return $query->getQuery()->getOneOrNullResult();
  213. }
  214. public function findLastOrderValid()
  215. {
  216. $query = $this->findByMerchantQuery();
  217. $query = $this->filterOrderValid($query);
  218. $query->andWhere('e.mainOrderShop = false OR e.mainOrderShop IS NULL');
  219. $query->orderBy('e.idValidOrder', 'DESC');
  220. $query->setMaxResults(1);
  221. return $query->getQuery()->getOneOrNullResult();
  222. }
  223. public function countValidOrderProductsOfWeekByProduct($weekNumber, $productId)
  224. {
  225. $query = $this->findByMerchantQuery();
  226. $query = $this->filterOrderValid($query);
  227. $query->andWhere('e.weekNumber = :weekNumber');
  228. $query->setParameter('weekNumber', $weekNumber);
  229. $query->leftJoin('e.orderProducts', 'orderProduct');
  230. $query->andWhere('orderProduct.product = :product');
  231. $query->setParameter('product', $productId);
  232. $query->select('SUM(orderProduct.quantityOrder) as quantity');
  233. $result = $query->getQuery()->getOneOrNullResult();
  234. return $result['quantity'];
  235. }
  236. }