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.

320 lines
14KB

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