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.

339 lines
13KB

  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. {
  73. $query->leftJoin('e.orderStatus', 'os');
  74. $query->andWhere('os.alias IN (:alias)');
  75. $query->setParameter('alias', $statusArray);
  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. if (isset($params['merchant'])) {
  99. $query = $this->findByMerchantQuery($params['merchant']);
  100. } else {
  101. $query = $this->findByMerchantQuery();
  102. }
  103. if (isset($params['count']) && $params['count']) {
  104. $query->select('count(e.id)');
  105. }
  106. if (isset($params['select'])) {
  107. $query->select($params['select']);
  108. }
  109. if (isset($params['section'])) {
  110. $query = $query->andWhere('e.section = :section')->setParameter('section', $params['section']);
  111. }
  112. if (isset($params['dateStart']) || isset($params['dateEnd'])) {
  113. $params['dateField'] = isset($params['dateField']) ? $params['dateField'] : 'validationDate';
  114. }
  115. if (isset($params['dateStart'])) {
  116. $query->andWhere('e.'.$params['dateField'].' >= :dateStart')->setParameter(
  117. 'dateStart',
  118. $params['dateStart']
  119. );
  120. }
  121. if (isset($params['dateEnd'])) {
  122. $query->andWhere('e.'.$params['dateField'].' <= :dateEnd')->setParameter('dateEnd', $params['dateEnd']);
  123. }
  124. if (isset($params['weekNumber'])) {
  125. $query->andWhere('e.weekNumber = :weekNumber')->setParameter('weekNumber', $params['weekNumber']);
  126. $currentYear = new \DateTime();
  127. $currentYear->setDate($currentYear->format('Y'), 1, 1);
  128. $query->andWhere('e.validationDate >= :now')->setParameter('now', $currentYear);
  129. }
  130. if (isset($params['isCart'])) {
  131. $query = $this->filterOrderCart($query);
  132. }
  133. if (isset($params['isValid'])) {
  134. $query = $this->filterOrderValid($query);
  135. }
  136. if (isset($params['isWaitingDelivery'])) {
  137. $query = $this->filterOrderWaitingDelivery($query);
  138. }
  139. if (isset($params['orderStatus'])) {
  140. $query->leftJoin('e.orderStatus', 'os');
  141. $query->andWhere('os.alias LIKE :alias');
  142. $query->setParameter('alias', $params['orderStatus']);
  143. }
  144. if (isset($params['user'])) {
  145. $query->andWhere('e.user = :user')->setParameter('user', $params['user']);
  146. }
  147. if (isset($params['address'])) {
  148. $query->andWhere('e.deliveryAddress = :address OR e.invoiceAddress = :address')->setParameter(
  149. 'address',
  150. $params['address']
  151. );
  152. }
  153. if (isset($params['weekDeliveryTrucks'])) {
  154. $query->andWhere('e.weekDeliveryTruck IN (:weekDeliveryTrucks)')->setParameter(
  155. 'weekDeliveryTrucks',
  156. $params['weekDeliveryTrucks']
  157. );
  158. }
  159. if (isset($params['estimatedDeliveryDateTime'])) {
  160. $query->andWhere('e.estimatedDeliveryDateTime >= :deliveryDateStart');
  161. $query->andWhere('e.estimatedDeliveryDateTime < :deliveryDateEnd');
  162. $date = clone $params['estimatedDeliveryDateTime'];
  163. $query->setParameter('deliveryDateStart', $date->format('Y-m-d 00:00:00'));
  164. $query->setParameter('deliveryDateEnd', $date->modify('+1 day')->format('Y-m-d 00:00:00'));
  165. }
  166. if (isset($params['deliveryDate'])) {
  167. $query->andWhere('e.deliveryDate >= :deliveryDateStart');
  168. $query->andWhere('e.deliveryDate < :deliveryDateEnd');
  169. $date = clone $params['deliveryDate'];
  170. $query->setParameter('deliveryDateStart', $date->format('Y-m-d 00:00:00'));
  171. $query->setParameter('deliveryDateEnd', $date->modify('+1 day')->format('Y-m-d 00:00:00'));
  172. }
  173. if (isset($params['mergeComplementaryOrderShops'])) {
  174. $query->andWhere('e.mainOrderShop IS NULL');
  175. $query->leftJoin('e.complementaryOrderShops', 'complementaryOrderShops');
  176. }
  177. if (isset($params['excludeComplementaryOrderShops'])) {
  178. $query->andWhere('e.mainOrderShop IS NULL');
  179. }
  180. $setParameterHorsTournee = false;
  181. $setParameterGiftVoucher = false;
  182. if (isset($params['isCircuit'])) {
  183. $query->leftJoin('e.deliveryPointSale', 'pointSale');
  184. $query->andWhere(
  185. 'e.deliveryPointSale IS NULL OR (pointSale.isDepository = 0 AND (pointSale.devAlias IS NULL OR (pointSale.devAlias != :devAliasHorsTournee AND pointSale.devAlias != :devAliasGiftVoucher)))'
  186. );
  187. $setParameterHorsTournee = true;
  188. $setParameterGiftVoucher = true;
  189. }
  190. if (isset($params['isDepository'])) {
  191. $query->innerJoin('e.deliveryPointSale', 'pointSale');
  192. $query->andWhere(
  193. 'pointSale IS NOT NULL AND pointSale.isDepository = 1 AND (pointSale.devAlias IS NULL OR (pointSale.devAlias != :devAliasHorsTournee AND pointSale.devAlias != :devAliasGiftVoucher))'
  194. );
  195. $setParameterHorsTournee = true;
  196. $setParameterGiftVoucher = true;
  197. }
  198. if (isset($params['isOffCircuit'])) {
  199. $query->innerJoin('e.deliveryPointSale', 'pointSale');
  200. $query->andWhere('pointSale IS NOT NULL AND pointSale.devAlias = :devAliasHorsTournee');
  201. $setParameterHorsTournee = true;
  202. }
  203. if (isset($params['isGiftVoucher'])) {
  204. $query->innerJoin('e.deliveryPointSale', 'pointSale');
  205. $query->andWhere('pointSale IS NOT NULL AND pointSale.devAlias = :devAliasGiftVoucher');
  206. $setParameterGiftVoucher = true;
  207. }
  208. if ($setParameterHorsTournee) {
  209. $query->setParameter('devAliasHorsTournee', PointSale::DEV_ALIAS_OFF_CIRCUIT);
  210. }
  211. if ($setParameterGiftVoucher) {
  212. $query->setParameter('devAliasGiftVoucher', PointSale::DEV_ALIAS_GIFT_VOUCHER);
  213. }
  214. if (isset($params['deliveryAvailability'])) {
  215. $deliveryAvailability = $params['deliveryAvailability'];
  216. $deliveryAvailabilityZone = ($deliveryAvailability instanceof DeliveryAvailabilityZone) ? $deliveryAvailability : false;
  217. $deliveryAvailabilityPointSale = ($deliveryAvailability instanceof DeliveryAvailabilityPointSale) ? $deliveryAvailability : false;
  218. if ($deliveryAvailabilityZone) {
  219. $query->andWhere('e.deliveryAvailabilityZone = :deliveryAvailabilityZone');
  220. $query->setParameter('deliveryAvailabilityZone', $deliveryAvailabilityZone);
  221. }
  222. if ($deliveryAvailabilityPointSale) {
  223. $query->andWhere('e.deliveryAvailabilityPointSale = :deliveryAvailabilityPointSale');
  224. $query->setParameter('deliveryAvailabilityPointSale', $deliveryAvailabilityPointSale);
  225. }
  226. } else {
  227. $query->leftJoin('e.deliveryAvailabilityZone', 'deliveryAvailabilityZone');
  228. $query->leftJoin('deliveryAvailabilityZone.deliverySlot', 'deliverySlotZone');
  229. $query->leftJoin('e.deliveryAvailabilityPointSale', 'deliveryAvailabilityPointSale');
  230. $query->leftJoin('deliveryAvailabilityPointSale.deliverySlot', 'deliverySlotPointSale');
  231. }
  232. if (isset($params['orderBy'])) {
  233. $query->orderBy(
  234. 'e.'.$params['orderBy'],
  235. isset($params['orderByDirection']) ? $params['orderByDirection'] : 'DESC'
  236. );
  237. } else {
  238. $query->orderBy('e.id', 'DESC');
  239. }
  240. if (isset($params['groupBy'])) {
  241. $query->groupBy($params['groupBy']);
  242. }
  243. if (isset($params['count']) && $params['count']) {
  244. return $query->getQuery()->getSingleScalarResult();
  245. } else {
  246. return $query->getQuery()->getResult();
  247. }
  248. }
  249. public function findLastOrderValidOfWeek($weekNumber)
  250. {
  251. $query = $this->findByMerchantQuery();
  252. $query = $this->filterOrderValid($query);
  253. $query->andWhere('e.weekNumber = :weekNumber');
  254. $currentYear = new \DateTime();
  255. $currentYear->setDate($currentYear->format('Y'), 1, 1);
  256. $query->andWhere('e.validationDate >= :now')->setParameter('now', $currentYear);
  257. $query->andWhere('e.mainOrderShop = false OR e.mainOrderShop IS NULL');
  258. $query->setParameter('weekNumber', $weekNumber);
  259. $query->orderBy('e.weekId', 'DESC');
  260. $query->setMaxResults(1);
  261. return $query->getQuery()->getOneOrNullResult();
  262. }
  263. public function findLastOrderValid()
  264. {
  265. $query = $this->findByMerchantQuery();
  266. $query = $this->filterOrderValid($query);
  267. $query->andWhere('e.mainOrderShop = false OR e.mainOrderShop IS NULL');
  268. $query->orderBy('e.idValidOrder', 'DESC');
  269. $query->setMaxResults(1);
  270. return $query->getQuery()->getOneOrNullResult();
  271. }
  272. }