Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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