Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

310 lines
14KB

  1. <?php
  2. namespace Lc\ShopBundle\Repository;
  3. use App\Entity\DeliveryAvailabilityPointSale;
  4. use App\Entity\DeliveryAvailabilityZone;
  5. use Doctrine\ORM\QueryBuilder;
  6. use Lc\ShopBundle\Context\DefaultRepositoryInterface;
  7. use Lc\ShopBundle\Context\OrderShopInterface;
  8. use Lc\ShopBundle\Model\OrderStatus;
  9. /**
  10. * @method OrderShopInterface|null find($id, $lockMode = null, $lockVersion = null)
  11. * @method OrderShopInterface|null findOneBy(array $criteria, array $orderBy = null)
  12. * @method OrderShopInterface[] findAll()
  13. * @method OrderShopInterface[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  14. */
  15. class OrderShopRepository extends BaseRepository implements DefaultRepositoryInterface
  16. {
  17. public function getInterfaceClass()
  18. {
  19. return OrderShopInterface::class;
  20. }
  21. public function countValidOrderWithReductionCredit($reductionCredit, $user)
  22. {
  23. $query = $this->findByMerchantQuery();
  24. $query = $this->filterOrderValid($query);
  25. $query->select('count(e.id)');
  26. $query->andWhere('e.user = :user');
  27. $query->innerJoin('e.orderReductionCredits', 'orc');
  28. $query->andWhere('orc.reductionCredit = :reductionCredit');
  29. $query->setParameter('reductionCredit', $reductionCredit);
  30. $query->setParameter('user', $user);
  31. return $query->getQuery()->getSingleScalarResult();
  32. }
  33. public function countValidOrderWithReductionCart($reductionCart)
  34. {
  35. $query = $this->findByMerchantQuery();
  36. $query = $this->filterOrderValid($query);
  37. $query->select('count(e.id)');
  38. $query->join('e.orderReductionCarts', 'orc');
  39. $query->andWhere('orc.reductionCart = :reductionCart');
  40. $query->setParameter('reductionCart', $reductionCart);
  41. return $query->getQuery()->getSingleScalarResult();
  42. }
  43. public function countValidOrderWithReductionCartPerUser($reductionCart, $user)
  44. {
  45. $query = $this->findByMerchantQuery();
  46. $query = $this->filterOrderValid($query);
  47. $query->select('count(e.id)');
  48. $query->andWhere('e.user = :user');
  49. $query->join('e.orderReductionCarts', 'orc');
  50. $query->andWhere('orc.reductionCart = :reductionCart');
  51. $query->setParameter('reductionCart', $reductionCart);
  52. $query->setParameter('user', $user);
  53. return $query->getQuery()->getSingleScalarResult();
  54. }
  55. public function filterOrderValid(?QueryBuilder $query): QueryBuilder
  56. {
  57. return $this->_filterOrderStatus($query, OrderStatus::$statusAliasAsValid) ;
  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. private function _filterOrderStatus($query, $statusArray) {
  68. $query->leftJoin('e.orderStatus', 'os');
  69. $query->andWhere('os.alias IN (:alias)');
  70. $query->setParameter('alias',$statusArray);
  71. return $query ;
  72. }
  73. public function findCartCurrent($params)
  74. {
  75. $query = $this->findByMerchantQuery();
  76. if (isset($params['user'])) {
  77. $query->andWhere('e.user = :user')->setParameter('user', $params['user']);
  78. }
  79. if (isset($params['visitor'])) {
  80. $query->andWhere('e.visitor = :visitor')->setParameter('visitor', $params['visitor']);
  81. }
  82. $query = $this->filterOrderCart($query);
  83. $query->leftJoin('e.orderReductionCarts', 'orderReductionCarts')
  84. ->addSelect('orderReductionCarts');
  85. $results = $query->getQuery()->getResult();
  86. if ($results) {
  87. return $results[0];
  88. }
  89. return null;
  90. }
  91. public function findAllBy($params = [])
  92. {
  93. $query = $this->findByMerchantQuery();
  94. if (isset($params['count']) && $params['count']) {
  95. $query->select('count(e.id)');
  96. }
  97. if (isset($params['select'])) {
  98. $query->select( $params['select']);
  99. }
  100. if (isset($params['dateStart']) || isset($params['dateEnd'])) {
  101. $params['dateField'] = isset($params['dateField']) ? $params['dateField'] : 'validationDate';
  102. }
  103. if (isset($params['dateStart'])) {
  104. $query->andWhere('e.' . $params['dateField'] . ' >= :dateStart')->setParameter('dateStart', $params['dateStart']);
  105. }
  106. if (isset($params['dateEnd'])) {
  107. $query->andWhere('e.' . $params['dateField'] . ' <= :dateEnd')->setParameter('dateEnd', $params['dateEnd']);
  108. }
  109. if (isset($params['weekNumber'])) {
  110. $query->andWhere('e.weekNumber = :weekNumber')->setParameter('weekNumber', $params['weekNumber']);
  111. }
  112. if (isset($params['isCart'])) {
  113. $query = $this->filterOrderCart($query);
  114. }
  115. if (isset($params['isValid'])) {
  116. $query = $this->filterOrderValid($query);
  117. }
  118. if (isset($params['isWaitingDelivery'])) {
  119. $query = $this->filterOrderWaitingDelivery($query);
  120. }
  121. if (isset($params['orderStatus'])) {
  122. $query->leftJoin('e.orderStatus', 'os');
  123. $query->andWhere('os.alias LIKE :alias');
  124. $query->setParameter('alias', $params['orderStatus']);
  125. }
  126. if (isset($params['user'])) {
  127. $query->andWhere('e.user = :user')->setParameter('user', $params['user']);
  128. }
  129. if(isset($params['address'])) {
  130. $query->andWhere('e.deliveryAddress = :address OR e.invoiceAddress = :address')->setParameter('address', $params['address']);
  131. }
  132. if(isset($params['weekDeliveryTrucks'])) {
  133. $query->andWhere('e.weekDeliveryTruck IN (:weekDeliveryTrucks)')->setParameter('weekDeliveryTrucks', $params['weekDeliveryTrucks']);
  134. }
  135. if(isset($params['estimatedDeliveryDateTime'])) {
  136. $query->andWhere('e.estimatedDeliveryDateTime >= :deliveryDateStart');
  137. $query->andWhere('e.estimatedDeliveryDateTime < :deliveryDateEnd');
  138. $date = clone $params['estimatedDeliveryDateTime'];
  139. $query->setParameter('deliveryDateStart', $date->format('Y-m-d 00:00:00'));
  140. $query->setParameter('deliveryDateEnd', $date->modify('+1 day')->format('Y-m-d 00:00:00'));
  141. }
  142. if(isset($params['deliveryDate'])) {
  143. $query->andWhere('e.deliveryDate >= :deliveryDateStart');
  144. $query->andWhere('e.deliveryDate < :deliveryDateEnd');
  145. $date = clone $params['deliveryDate'];
  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['mergeComplementaryOrderShops'])) {
  150. $query->andWhere('e.mainOrderShop IS NULL');
  151. $query->leftJoin('e.complementaryOrderShops', 'complementaryOrderShops');
  152. $query->leftJoin('complementaryOrderShops.orderStatus', 'complementaryOrderStatus');
  153. $query->andWhere('complementaryOrderShops IS NULL OR complementaryOrderStatus.alias IN (:statusAliasValid)');
  154. $query->setParameter('statusAliasValid', OrderStatus::$statusAliasAsValid);
  155. }
  156. if (isset($params['excludeComplementaryOrderShops'])) {
  157. $query->andWhere('e.mainOrderShop IS NULL');
  158. }
  159. $setParameterHorsTournee = false ;
  160. if (isset($params['isCircuit'])) {
  161. $query->leftJoin('e.deliveryPointSale', 'pointSale');
  162. $query->andWhere('e.deliveryPointSale IS NULL OR (pointSale.isDepository = 0 AND (pointSale.devAlias IS NULL OR pointSale.devAlias != :devAliasHorsTournee))');
  163. $setParameterHorsTournee = 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)');
  168. $setParameterHorsTournee = true ;
  169. }
  170. if (isset($params['isOffCircuit'])) {
  171. $query->innerJoin('e.deliveryPointSale', 'pointSale');
  172. $query->andWhere('pointSale IS NOT NULL AND pointSale.devAlias = :devAliasHorsTournee');
  173. $setParameterHorsTournee = true ;
  174. }
  175. if($setParameterHorsTournee) {
  176. $query->setParameter('devAliasHorsTournee', 'horstournee') ;
  177. }
  178. if (isset($params['deliveryAvailability'])) {
  179. $deliveryAvailability = $params['deliveryAvailability'] ;
  180. $deliveryAvailabilityZone = ($deliveryAvailability instanceof DeliveryAvailabilityZone) ? $deliveryAvailability : false;
  181. $deliveryAvailabilityPointSale = ($deliveryAvailability instanceof DeliveryAvailabilityPointSale) ? $deliveryAvailability : false;
  182. if($deliveryAvailabilityZone){
  183. $query->andWhere('e.deliveryAvailabilityZone = :deliveryAvailabilityZone');
  184. $query->setParameter('deliveryAvailabilityZone', $deliveryAvailabilityZone);
  185. }
  186. if($deliveryAvailabilityPointSale) {
  187. $query->andWhere('e.deliveryAvailabilityPointSale = :deliveryAvailabilityPointSale');
  188. $query->setParameter('deliveryAvailabilityPointSale', $deliveryAvailabilityPointSale);
  189. }
  190. }
  191. else{
  192. $query->leftJoin('e.deliveryAvailabilityZone', 'deliveryAvailabilityZone');
  193. $query->leftJoin('deliveryAvailabilityZone.deliverySlot', 'deliverySlotZone');
  194. $query->leftJoin('e.deliveryAvailabilityPointSale', 'deliveryAvailabilityPointSale');
  195. $query->leftJoin('deliveryAvailabilityPointSale.deliverySlot', 'deliverySlotPointSale');
  196. }
  197. if (isset($params['orderBy'])) {
  198. $query->orderBy('e.' . $params['orderBy'], isset($params['orderByDirection']) ? $params['orderByDirection'] : 'DESC');
  199. } else {
  200. $query->orderBy('e.id', 'DESC');
  201. }
  202. if (isset($params['groupBy'])) {
  203. $query->groupBy( $params['groupBy']);
  204. }
  205. if (isset($params['count']) && $params['count']) {
  206. return $query->getQuery()->getSingleScalarResult();
  207. } else {
  208. return $query->getQuery()->getResult();
  209. }
  210. }
  211. public function findLastOrderValidOfWeek($weekNumber)
  212. {
  213. $query = $this->findByMerchantQuery();
  214. $query = $this->filterOrderValid($query);
  215. $query->andWhere('e.weekNumber = :weekNumber');
  216. $query->andWhere('e.mainOrderShop = false OR e.mainOrderShop IS NULL');
  217. $query->setParameter('weekNumber', $weekNumber);
  218. $query->orderBy('e.weekId', 'DESC');
  219. $query->setMaxResults(1);
  220. return $query->getQuery()->getOneOrNullResult();
  221. }
  222. public function findLastOrderValid()
  223. {
  224. $query = $this->findByMerchantQuery();
  225. $query = $this->filterOrderValid($query);
  226. $query->andWhere('e.mainOrderShop = false OR e.mainOrderShop IS NULL');
  227. $query->orderBy('e.idValidOrder', 'DESC');
  228. $query->setMaxResults(1);
  229. return $query->getQuery()->getOneOrNullResult();
  230. }
  231. public function countValidOrderProductsOfWeekByProduct($weekNumber, $productId)
  232. {
  233. $query = $this->findByMerchantQuery();
  234. $query = $this->filterOrderValid($query);
  235. $query->andWhere('e.weekNumber = :weekNumber');
  236. $query->setParameter('weekNumber', $weekNumber);
  237. $query->leftJoin('e.orderProducts', 'orderProduct');
  238. $query->andWhere('orderProduct.product = :product');
  239. $query->setParameter('product', $productId);
  240. $query->select('SUM(orderProduct.quantityOrder) as quantity');
  241. $result = $query->getQuery()->getOneOrNullResult();
  242. return $result['quantity'];
  243. }
  244. }