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.

328 lines
14KB

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