Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

308 lines
13KB

  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 filterOrderValid(?QueryBuilder $query): QueryBuilder
  57. {
  58. return $this->_filterOrderStatus($query, OrderStatus::$statusAliasAsValid) ;
  59. }
  60. public function filterOrderWaitingDelivery($query)
  61. {
  62. return $this->_filterOrderStatus($query, OrderStatus::$statusAliasWaitingDelivery) ;
  63. }
  64. public function filterOrderCart($query)
  65. {
  66. return $this->_filterOrderStatus($query, OrderStatus::$statusAliasAsCart) ;
  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. if (isset($params['isCircuit'])) {
  159. $query->leftJoin('e.deliveryPointSale', 'pointSale');
  160. $query->andWhere('e.deliveryPointSale IS NULL OR (pointSale.isDepository = 0 AND (pointSale.devAlias IS NULL OR pointSale.devAlias != :devAliasHorsTournee))');
  161. $setParameterHorsTournee = true ;
  162. }
  163. if (isset($params['isDepository'])) {
  164. $query->innerJoin('e.deliveryPointSale', 'pointSale');
  165. $query->andWhere('pointSale IS NOT NULL AND pointSale.isDepository = 1 AND (pointSale.devAlias IS NULL OR pointSale.devAlias != :devAliasHorsTournee)');
  166. $setParameterHorsTournee = true ;
  167. }
  168. if (isset($params['isOffCircuit'])) {
  169. $query->innerJoin('e.deliveryPointSale', 'pointSale');
  170. $query->andWhere('pointSale IS NOT NULL AND pointSale.devAlias = :devAliasHorsTournee');
  171. $setParameterHorsTournee = true ;
  172. }
  173. if($setParameterHorsTournee) {
  174. $query->setParameter('devAliasHorsTournee', 'horstournee') ;
  175. }
  176. if (isset($params['deliveryAvailability'])) {
  177. $deliveryAvailability = $params['deliveryAvailability'] ;
  178. $deliveryAvailabilityZone = ($deliveryAvailability instanceof DeliveryAvailabilityZone) ? $deliveryAvailability : false;
  179. $deliveryAvailabilityPointSale = ($deliveryAvailability instanceof DeliveryAvailabilityPointSale) ? $deliveryAvailability : false;
  180. if($deliveryAvailabilityZone){
  181. $query->andWhere('e.deliveryAvailabilityZone = :deliveryAvailabilityZone');
  182. $query->setParameter('deliveryAvailabilityZone', $deliveryAvailabilityZone);
  183. }
  184. if($deliveryAvailabilityPointSale) {
  185. $query->andWhere('e.deliveryAvailabilityPointSale = :deliveryAvailabilityPointSale');
  186. $query->setParameter('deliveryAvailabilityPointSale', $deliveryAvailabilityPointSale);
  187. }
  188. }
  189. else{
  190. $query->leftJoin('e.deliveryAvailabilityZone', 'deliveryAvailabilityZone');
  191. $query->leftJoin('deliveryAvailabilityZone.deliverySlot', 'deliverySlotZone');
  192. $query->leftJoin('e.deliveryAvailabilityPointSale', 'deliveryAvailabilityPointSale');
  193. $query->leftJoin('deliveryAvailabilityPointSale.deliverySlot', 'deliverySlotPointSale');
  194. }
  195. if (isset($params['orderBy'])) {
  196. $query->orderBy('e.' . $params['orderBy'], isset($params['orderByDirection']) ? $params['orderByDirection'] : 'DESC');
  197. } else {
  198. $query->orderBy('e.id', 'DESC');
  199. }
  200. if (isset($params['groupBy'])) {
  201. $query->groupBy( $params['groupBy']);
  202. }
  203. if (isset($params['count']) && $params['count']) {
  204. return $query->getQuery()->getSingleScalarResult();
  205. } else {
  206. return $query->getQuery()->getResult();
  207. }
  208. }
  209. public function findLastOrderValidOfWeek($weekNumber)
  210. {
  211. $query = $this->findByMerchantQuery();
  212. $query = $this->filterOrderValid($query);
  213. $query->andWhere('e.weekNumber = :weekNumber');
  214. $query->andWhere('e.mainOrderShop = false OR e.mainOrderShop IS NULL');
  215. $query->setParameter('weekNumber', $weekNumber);
  216. $query->orderBy('e.weekId', 'DESC');
  217. $query->setMaxResults(1);
  218. return $query->getQuery()->getOneOrNullResult();
  219. }
  220. public function findLastOrderValid()
  221. {
  222. $query = $this->findByMerchantQuery();
  223. $query = $this->filterOrderValid($query);
  224. $query->andWhere('e.mainOrderShop = false OR e.mainOrderShop IS NULL');
  225. $query->orderBy('e.idValidOrder', 'DESC');
  226. $query->setMaxResults(1);
  227. return $query->getQuery()->getOneOrNullResult();
  228. }
  229. public function countValidOrderProductsOfWeekByProduct($weekNumber, $productId)
  230. {
  231. $query = $this->findByMerchantQuery();
  232. $query = $this->filterOrderValid($query);
  233. $query->andWhere('e.weekNumber = :weekNumber');
  234. $query->setParameter('weekNumber', $weekNumber);
  235. $query->leftJoin('e.orderProducts', 'orderProduct');
  236. $query->andWhere('orderProduct.product = :product');
  237. $query->setParameter('product', $productId);
  238. $query->select('SUM(orderProduct.quantityOrder) as quantity');
  239. $result = $query->getQuery()->getOneOrNullResult();
  240. return $result['quantity'];
  241. }
  242. }